Skip to content

Commit

Permalink
Documentation fixing and basic communication test
Browse files Browse the repository at this point in the history
  • Loading branch information
interpretor committed May 23, 2017
1 parent ede91ad commit 031097f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Thumbs.db
node_modules
playground
.vscode
docs
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This project is currently in development and not considered stable yet.
npm install zyre.js
```

## Documentation

A jsdoc documentation can be found [here](https://interpretor.github.io/zyre.js/)

## Examples

```js
Expand Down
9 changes: 5 additions & 4 deletions lib/zbeacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ function readDataGramBuffer(dgBuffer) {
class ZBeacon {

/**
* @param {Buffer} identity - 16 byte UUID as Buffer
* @param {string} address - Address of the zyre node
* @param {number} mailbox - Mailbox of the zyre node
* @param {ZyrePeers} zyrePeers - Global ZyrePeers object
* @param {Object} options - Options Object
* @param {Buffer} options.identity - 16 byte UUID as Buffer
* @param {string} options.address - Address of the zyre node
* @param {number} options.mailbox - Mailbox of the zyre node
* @param {ZyrePeers} options.zyrePeers - Global ZyrePeers object
*/
constructor({ identity, address, mailbox, zyrePeers }) {
this._dgBuffer = createDataGramBuffer(identity, mailbox);
Expand Down
14 changes: 12 additions & 2 deletions lib/zhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ const tcpPortUsed = require('tcp-port-used');
*/
class ZHelper {

/**
* @typedef {Object} IfaceData
* @property {string} address - IP address
* @property {string} netmask - Netmask
* @property {string} family - IPv4 or IPv6
* @property {string} mac - MAC address
* @property {boolean} internal - Internal or external
* @property {string} broadcast - Broadcast address
*/

/**
* Returns IPv4 interface data from the given interface, or searches for a public interface with
* an assigned IPv4. Also calculates the broadcast address of the interface.
*
* @param {string} [iface] - Optional interface to check
* @return {Object}
* @return {IfaceData}
*/
static getIfData(iface) {
const ifaces = os.networkInterfaces();
Expand Down Expand Up @@ -70,7 +80,7 @@ class ZHelper {
*
* @param {string} address - Address to search on
* @param {number} port - Port to start searching from
* @return {Promise}
* @return {Promise.<port>}
*/
static getFreePort(address, port) {
return new Promise((resolve) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/zre_msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ZreMsg {
/**
* @param {number} cmd - ZreMsg command as number
* @param {Object} options - Options Object
* @param {number} options.sequence - Sequence of the message
* @param {number} [options.sequence=1] - Sequence of the message
* @param {string} [options.group] - Group which the node/peer joins or leaves
* @param {string} [options.content] - Content of the message
* @param {string} [options.endpoint] - TCP address of the node/peer
Expand Down
2 changes: 1 addition & 1 deletion lib/zyre_peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ZyrePeer extends EventEmitter {
this._expiredTimeout = setTimeout(() => {
this._expiredAt = Date.now();
debug(`${this._identity}: expired at ${this._expiredAt}`);
this.disconnect();
if (this._connected) this.disconnect();
this.emit('expired');
}, PEER_EXPIRED);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/zyre_peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const ZyrePeer = require('./zyre_peer');

/**
* ZyrePeers manages all ZyrePeer objects
*
* @extends EventEmitter
*/
class ZyrePeers extends EventEmitter {

Expand Down
30 changes: 30 additions & 0 deletions test/zyre_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,34 @@ describe('Zyre', () => {
const z1 = zyre.new();
assert.instanceOf(z1, zyre);
});

it('should do basic group communication', (done) => {
const z1 = zyre.new({ name: 'z1' });
const z2 = zyre.new({ name: 'z2' });

z1.on('message', (name, message, group) => {
assert.equal(name, 'z2');
assert.equal(message, 'Hello World!');
assert.equal(group, 'CHAT');
z1.shout('CHAT', 'Hey!');
});

z2.on('message', (name, message, group) => {
assert.equal(name, 'z1');
assert.equal(message, 'Hey!');
assert.equal(group, 'CHAT');
done();
});

z1.start().then(() => {
z1.join('CHAT');
z2.start().then(() => {
z2.join('CHAT');
});
});

setTimeout(() => {
z2.shout('CHAT', 'Hello World!');
}, 1500);
});
});

0 comments on commit 031097f

Please sign in to comment.