Permalink
Browse files

readme and a working example

  • Loading branch information...
1 parent df849d8 commit 25ec1f64258160f8ef0d26b801cb6c37c67f405e @substack committed Jul 11, 2012
Showing with 107 additions and 3 deletions.
  1. +9 −0 example/cache.js
  2. +11 −3 index.js
  3. +87 −0 readme.markdown
View
@@ -0,0 +1,9 @@
+var seaport = require('seaport');
+var pier = require('../');
+
+var server = seaport.createServer().listen(7890);
+var cache = pier().tie(7890);
+
+setInterval(function () {
+ console.dir(cache.query('beep'));
+}, 1000);
View
@@ -1,15 +1,23 @@
var EventEmitter = require('events').EventEmitter;
var seaport = require('seaport');
-module.exports = function (ports, query) {
+module.exports = function (query) {
var server = seaport.createServer();
+ server.tie = tie.bind(null, server, query);
+ return server;
+};
+
+function tie (server, query, ports) {
+ if (typeof ports.query !== 'function') {
+ ports = seaport.connect.apply(null, [].slice.call(arguments, 2));
+ }
var matching = (function () {
+ if (query === undefined) return function () { return true };
var role = query.split('@')[0];
var version = query.split('@')[1];
return function (rec) {
- if (query === undefined) return true;
if (rec.role !== role) return false;
if (version === undefined) return true;
if (!semver.validRange(version)) {
@@ -79,4 +87,4 @@ module.exports = function (ports, query) {
});
return server;
-};
+}
View
@@ -0,0 +1,87 @@
+# pier
+
+Replicate a
+[seaport service registry](https://github.com/substack/seaport)
+for fast local in-memory access or redundancy.
+
+# example
+
+``` js
+var seaport = require('seaport');
+var pier = require('pier');
+
+var server = seaport.createServer().listen(7890);
+var cache = pier().tie(7890);
+
+setInterval(function () {
+ console.dir(cache.query('beep'));
+}, 1000);
+```
+
+***
+
+first start the server/cache program:
+
+```
+$ node example/cache.js
+[]
+[]
+[ { host: '127.0.0.1', port: 9999, role: 'beep', version: '0.0.0' } ]
+[ { host: '127.0.0.1', port: 9999, role: 'beep', version: '0.0.0' } ]
+[]
+[]
+```
+
+meanwhile, register and unregister some services:
+
+```
+$ node
+> var ports = require('seaport').connect(7890)
+> ports.assume('beep', 9999)
+> ports.free({ role : 'beep', port : 9999 })
+```
+
+# methods
+
+``` js
+var pier = require('pier')
+```
+
+## var p = pier(query)
+
+Create a new seaport server `p` with an optional semver `query` to filter the
+local cache by.
+
+## p.tie(ports), pier.tie(...)
+
+Tie a remote seaport server given either the handle itself or the parameters
+to pass to `seaport.connect(...)`.
+
+Returns the local seaport caching server `p`.
+
+# events
+
+In addition to all the usual events that seaport servers emit
+(`'assume'`, `'allocate'`, `'free'`), these extra events are emitted:
+
+## p.on('ready', function (records) { /* ... */ })
+
+Emitted when the initial recordset is ready. When the remote server is first
+bound, a `query()` is issued against it to get the initial recordset.
+
+## p.on('update', function (records) { /* ... */ })
+
+Emitted when the recordset changes. If `query` was specified, the `records`
+parameter will be filtered, otherwise `records` is the entire recordset.
+
+# install
+
+With [npm](http://npmjs.org) do:
+
+```
+npm install pier
+```
+
+# license
+
+MIT

0 comments on commit 25ec1f6

Please sign in to comment.