Coder Social home page Coder Social logo

freebird-transport's Introduction

freebird-transport

The transport for freebird REQ/RSP/IND message transmission

NPM

Travis branch npm PyPI npm

Table of Contents

  1. Overview
  2. APIs and Events
  3. TCP Server/Client Example

1. Overview

This module is the abstract class for developers to create their own transportation to communicate with freebird framework. This class inherits from EventEmitter which will emit a 'message' event when a message arrives.

  • In this document, transp will be used to denote the instance of this class.
  • In the following methods, the argument msg must be an object in the shape of { clientId, data }, where the data property is mandatory and should be a string or a buffer, and the clientId property is optional and should be a string or a number if given. One can attach an identifier, like a client id, to msg.clientId for message multiplexing.

For implementers:

To have a valid transp, two things must be done by the implementer.

  1. Must implement the transp._send(msg, callback) method on the transport instance.
  2. Must call the transp.receive(msg) method to let the transport instance know that there a message comes in. Calling transp.receive(msg) will induce a 'message' event triggered on the transport instance.

For users:

To use the transport object:

  1. Call transp.send(msg, callback) to send message out. It wil throw if _send() was not provided by the implementer.
  2. Listen to 'message' event to receive messages.

2. APIs and Events


new Transport()

Create the transport instance.

Arguments:

  1. none

Returns:

  • (Object): transp, instance of this class

Examples:

var Transport = require('freebird-transport');
var transp = new Transport();

._send(msg, callback)

The implmentation of sending out the message.

Arguments:

  1. msg (Object): The message to trasmit out is a string or a buffer attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. This err-first callback should be called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp._send = function (msg, callback) {
    var bytes;

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    // ... implemention of message sending

    // Finally, call callback
    callback(null, bytes);
};

._broadcast(msg, callback)

The implmentation of broadcasting the message. This is optional, it will use transp.send(msg, callback) internally by default if implementation is not given. This broadcasting method is used to send an indication to multiple remote clients.

Arguments:

  1. msg (Object): The message to broadcast out is a string or a buffer attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. This err-first callback should be called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp._broadcast = function (msg, callback) {
    var bytes;

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    // ... implemention of message broadcasting

    // Finally, call callback
    callback(null, bytes);
};

.send(msg, callback)

Call this method to send the message off.

Arguments:

  1. msg (Object): The message to trasmit out must be a string or a buffer which should be attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. Get called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

// assume transp._send was implemented

// call transp.send() to send message
transp.send({ data: 'Hello World' }, function (err, bytes) {
    if (err)
        console.log(err);
    else
        console.log(bytes + ' bytes were sent')
});

.broadcast(msg, callback)

Call this method to broadcast the message.

Arguments:

  1. msg (Object): The message to trasmit out must be a string or a buffer which should be attached to data property of this object.
  2. callback (Function): function (err, bytes) { ... }. Get called after message sent off. The argument bytes tells how many bytes were sent.

Returns:

  • none

Examples:

var transp = new Transport();

transp.broadcast({ data: 'Hello World' }, function (err, bytes) {
    if (err)
        console.log(err);
    else
        console.log(bytes + ' bytes were sent')
});

.receive(msg[, callback])

The Implemntor should call this method to inform the message arrival.

Arguments:

  1. msg (Object): This must be an object with a data property that holds the received message.
  2. callback (Function): function (err) { ... }. Optional. Get called after message sent off.

Returns:

  • none

Examples:

var transp = new Transport();

// Implemntor calls transp.receive() when message arrives
foo.on('data', function (data) {
    transp.receive({ data: data });
});

.on('message', function (msg) {})

The user can listen to the 'message' event to receive the message.

Arguments of the listener

  1. msg (Object): This must be an object with a data property that holds the received message of a string or a buffer.

Examples:

var transp = new Transport();

transp.on('message', function (msg) {
    console.log(msg.data.toString());
});

.on('unhandledMessage', function (msg) {})

The implementer can listen to the 'unhandledMessage' event to receive the message that is not processed by the freebird.

Arguments of the listener

  1. msg (Object): This must be an object with a data property that holds the received message of a string or a buffer.

Examples:

var transp = new Transport();

transp.on('unhandledMessage', function (msg) {
    console.log('Message not processed by freebird: ' + msg.data.toString());
});

3. TCP Server/Client Example

3.1 TCP Server

var net = require('net'),
    Transport = require('freebird-transport');

var transp, client;

transp = new Transport();

server = net.createServer(function (c) {
    client = c;

    client.on('end', function () {
        client = null;
    });

    // Message received
    client.on('data', function (data) {
        transp.receive({ data: data });
    });
});

server.listen(4321, function () {
    console.log('TCP server starts');
});

// Implementaion of _send
transp._send = function (msg, callback) {
    var bytes;

    if (typeof msg !== 'object')
        return setImmediate(callback, new TypeError('msg should be an object'));

    if (typeof msg.data === 'string')
        msg.data = new Buffer(msg.data);

    if (!Buffer.isBuffer(msg.data))
        return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

    bytes = msg.data.length;

    if (!client)
        return setImmediate(callback, new Error('No client connected'));

    client.write(msg.data);
    setImmediate(callback, null, bytes);
};

module.exports = transp;

3.2 TCP Client

var net = require('net'),
    Transport = require('freebird-transport');

var transp, client;

transp = new Transport();
client = net.createConnection(4321, 'localhost', function () {
    transp._send = function (msg, callback) {
        var bytes;

        if (typeof msg.data === 'string')
            msg.data = new Buffer(msg.data);

        if (!Buffer.isBuffer(msg.data))
            return setImmediate(callback, new TypeError('msg.data should be a string or a buffer'));

        bytes = msg.data.length;

        if (!client)
            return setImmediate(callback, new Error('No client created'));

        client.write(msg.data);
        setImmediate(callback, null, bytes);
    };
});

client.on('end', function () {
    client = null;
});

// Message received
client.on('data', function (data) {
    transp.receive({ data: data });
});

module.exports = transp;

freebird-transport's People

Contributors

amer8 avatar simenkid avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

freebird-transport's Issues

Improve test coverage

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    59.38 |       25 |     62.5 |    59.38 |                   |
 index.js |    59.38 |       25 |     62.5 |    59.38 |... 51,53,54,55,57 |
----------|----------|----------|----------|----------|-------------------|

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.