1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var define = require('./define');
|
8 | var utils = require('./utils');
|
9 | var Protocol = require('./protocol');
|
10 | var Connection = require('./connection');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function CloudsBase (options) {
|
22 | options = options || {};
|
23 | options.type = options.type || 'base';
|
24 |
|
25 | var ns = this._ns = utils.createNamespace(options);
|
26 | var id = this.id = options.id = options.id || utils.uniqueId(options.type);
|
27 | var debug = this._debug = utils.debug(options.type + ':' + id);
|
28 | var me = this;
|
29 |
|
30 | this._protocol = new Protocol(this.id);
|
31 |
|
32 | this._handlers = {};
|
33 | this._setHandler('message.m', function (msg) {
|
34 | this._debug('on message: @%s => %s', msg.sender, msg.data);
|
35 | this.emit('message', msg.sender, msg.data);
|
36 | });
|
37 |
|
38 | this._connection = new Connection(options);
|
39 | this._connection.on('message', function (msg) {
|
40 | me._handleMessage(me._protocol.unpack(msg));
|
41 | });
|
42 | this._connection.on('listen', function () {
|
43 | me.emit('listen');
|
44 | });
|
45 | }
|
46 |
|
47 | utils.inheritsEventEmitter(CloudsBase);
|
48 |
|
49 |
|
50 | CloudsBase.prototype._key = function () {
|
51 | var list = Array.prototype.slice.call(arguments);
|
52 | return list.join(':');
|
53 | };
|
54 |
|
55 |
|
56 | CloudsBase.prototype._callback = function (fn) {
|
57 | if (typeof fn !== 'function') {
|
58 | var debug = this._debug;
|
59 | fn = function (err) {
|
60 | debug('callback: err=%s, args=%s', err, Array.prototype.slice.call(arguments));
|
61 | };
|
62 | }
|
63 | return fn;
|
64 | };
|
65 |
|
66 |
|
67 | CloudsBase.prototype._setHandler = function (name, fn) {
|
68 | this._debug('set handler: %s => %s', name, fn);
|
69 | this._handlers[name] = fn;
|
70 | };
|
71 |
|
72 |
|
73 | CloudsBase.prototype._getHandler = function (name) {
|
74 | return this._handlers[name];
|
75 | };
|
76 |
|
77 |
|
78 | CloudsBase.prototype._handleMessage = function (msg) {
|
79 | this._debug('handle message: sender=%s, type=%s, data=%s', msg.sender, msg.type, msg.data);
|
80 |
|
81 | var handler = this._getHandler('message.' + msg.type);
|
82 | if (typeof handler !== 'function') {
|
83 | this._debug('unknown message type: %s', msg.type);
|
84 | this.emit('unknown message', msg);
|
85 | }
|
86 |
|
87 | handler.call(this, msg);
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | CloudsBase.prototype.send = function (receiver, message, callback) {
|
98 | this._debug('send: @%s => %s', receiver, message);
|
99 |
|
100 | var msg = this._protocol.packMessage(message);
|
101 | this._sendMessage(receiver, msg);
|
102 | };
|
103 |
|
104 | CloudsBase.prototype._sendMessage = function (receiver, msg, callback) {
|
105 | var key = this._key(receiver);
|
106 | this._debug('send message: receiver=%s, key=%s', receiver, key);
|
107 |
|
108 | this._connection.send(key, msg.raw, this._callback(callback));
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | CloudsBase.prototype.exit = function (callback) {
|
117 | var me = this;
|
118 |
|
119 | me._debug('exit');
|
120 |
|
121 |
|
122 | me.emit('exit', me);
|
123 |
|
124 | setTimeout(function () {
|
125 |
|
126 | me._connection.exit(function () {
|
127 | me._callback(callback);
|
128 | });
|
129 | }, define.exitConnectionDelay);
|
130 | };
|
131 |
|
132 |
|
133 | module.exports = CloudsBase;
|