UNPKG

3.23 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 = utils.clone(options || {});
23 options.type = options.type || 'base';
24
25 var ns = this._ns = utils.createNamespace(options);
26 var id = this.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 return;
86 }
87
88 handler.call(this, msg);
89};
90
91/**
92 * 发送消息
93 *
94 * @param {String} receiver
95 * @param {Mixed} message
96 * @param {Function} callback
97 */
98CloudsBase.prototype.send = function (receiver, message, callback) {
99 this._debug('send: @%s => %s', receiver, message);
100
101 var msg = this._protocol.packMessage(message);
102 this._sendMessage(receiver, msg);
103};
104
105CloudsBase.prototype._sendMessage = function (receiver, msg, callback) {
106 var key = this._key(receiver);
107 this._debug('send message: receiver=%s, key=%s', receiver, key);
108
109 this._connection.send(key, msg.raw, this._callback(callback));
110};
111
112/**
113 * 退出
114 *
115 * @param {Function} callback
116 */
117CloudsBase.prototype.exit = function (callback) {
118 var me = this;
119
120 me._debug('exit');
121
122 // 触发exit事件
123 me.emit('exit', me);
124
125 setTimeout(function () {
126 // 关闭数据连接
127 me._connection.exit(function () {
128 me._callback(callback);
129 });
130 }, define.exitConnectionDelay);
131};
132
133
134module.exports = CloudsBase;