UNPKG

3.24 kBJavaScriptView Raw
1/**
2 * clouds base
3 *
4 * @author 老雷<leizongmin@gmail.com>
5 */
6
7var define = require('./define');
8var utils = require('./utils');
9var Protocol = require('./protocol');
10var Connection = require('./connection');
11
12
13/**
14 * Clouds Base
15 *
16 * @param {Object} options
17 * - {String} id
18 * - {String} type
19 * - {Object} redis {host, port, db, prefix}
20 */
21function 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
47utils.inheritsEventEmitter(CloudsBase);
48
49// 获得redis key
50CloudsBase.prototype._key = function () {
51 var list = Array.prototype.slice.call(arguments);
52 return list.join(':');
53};
54
55// 处理默认的回调函数
56CloudsBase.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// 设置消息处理程序
67CloudsBase.prototype._setHandler = function (name, fn) {
68 this._debug('set handler: %s => %s', name, fn);
69 this._handlers[name] = fn;
70};
71
72// 获取消息处理程序
73CloudsBase.prototype._getHandler = function (name) {
74 return this._handlers[name];
75};
76
77// 处理收到的消息
78CloudsBase.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 * @param {String} receiver
94 * @param {Mixed} message
95 * @param {Function} callback
96 */
97CloudsBase.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
104CloudsBase.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 * @param {Function} callback
115 */
116CloudsBase.prototype.exit = function (callback) {
117 var me = this;
118
119 me._debug('exit');
120
121 // 触发exit事件
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
133module.exports = CloudsBase;