This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
abstract_ascoltatore.js
214 lines (185 loc) · 6.27 KB
/
abstract_ascoltatore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";
var EventEmitter = require('events').EventEmitter;
/**
* An `AbstractAscoltatore` is a class that inherits from `EventEmitter`.
* It is also the base class of `ascoltatori`. It is not meant to be used alone,
* but it defines the interface for every ascoltatore.
*
* Every ascoltatore emits the following events:
* - `ready`, when the ascolatore is ready for subscribing and/or
* publishing messages;
* - `closed`, when the ascoltatore has closed all the connections
* and therefore it cannot accept new messages;
* - `error`, if something goes wrong.
*
* @api public
*/
function AbstractAscoltatore(settings, nativeSettings) {
EventEmitter.call(this);
settings = settings || {};
nativeSettings = nativeSettings || {};
this._separator = settings.separator || '/',
this._wildcardOne = settings.wildcardOne || '+',
this._wildcardSome = settings.wildcardSome || '*';
this._nativeSettings = nativeSettings;
if (nativeSettings.separator &&
(this._separator !== nativeSettings.separator)) {
this._reInSeparator = new RegExp('\\' + this._separator, 'g');
this._reOutSeparator = new RegExp('\\' + nativeSettings.separator, 'g');
}
if (nativeSettings.wildcardOne &&
(this._wildcardOne !== nativeSettings.wildcardOne)) {
this._reInWildcardOne = new RegExp('\\' + this._wildcardOne, 'g');
}
if (nativeSettings.wildcardSome &&
(this._wildcardSome !== nativeSettings.wildcardSome)) {
this._reInWildcardSome = new RegExp('\\' + this._wildcardSome, 'g');
}
this._setPublish();
this._ready = false;
this._closed = false;
var that = this;
this.on("ready", function() {
that._ready = true;
});
this.on("closed", function() {
that._closed = true;
});
this.on("newListener", function(event, listener) {
if (event === "ready" && that._ready) {
listener();
}
});
this.setMaxListeners(0);
}
AbstractAscoltatore.prototype = Object.create(EventEmitter.prototype);
AbstractAscoltatore.prototype._raiseIfClosed = function raiseIfClosed() {
if (this._closed) {
throw new Error("This ascoltatore is closed");
}
};
/**
* This method provides a way for users to subscribe for messages.
*
* The messages are published on topics, that is just a "path", e.g.
* `/this/is/a/topic`.
* The topic are organized in a hierarchy, and `subscribe` support the usage
* of wildcards, e.g. you can subscribe to `*` and it will
* match all the topics
*
* Example:
* ascoltatore.subscribe("*", function () {
* // this will print { '0': "hello/42", '1': "a message" }
* console.log(arguments);
* });
*
* @param {String} topic the topic to subscribe to
* @param {Function} callback the callback that will be called when a new message is published.
* @param {Function} done the callback that will be called when the subscribe is completed
* @api public
*/
AbstractAscoltatore.prototype.subscribe = function(topic, callback, done) {
throw new Error("Subclass to implement");
};
/**
* This method allow publishing of messages to topics.
*
* Example:
* ascoltatore.publish("hello/42", "a message", function () {
* console.log("message published");
* });
*
*
* @param {String} topic the topic to publish to
* @param {Object} payload the callback that will be called when a new message is published.
* @param {Object} options (optional) Metadata associated with the message (e.g. qos, messageId). If you only specify 3 parameters to your method then you won't be passed this parameter.
* @param {Function} done the callback that will be called after the message has been published.
* @api public
*/
AbstractAscoltatore.prototype.publish = function(topic, payload, options, done) {
throw new Error("Subclass to implement");
};
AbstractAscoltatore.prototype._setPublish = function() {
var proto = Object.getPrototypeOf(this),
f = proto.publish,
publish,
newPublish;
if (!f) { return; }
if (f.length === 4) {
publish = this.publish;
} else {
publish = function (topic, payload, options, done) {
return f.call(this, topic, payload, done);
};
}
newPublish = function (topic, payload, options, done) {
if ((typeof options === 'function') ||
(done && (typeof done !== 'function'))) {
done = options;
options = {};
} else if ((options === undefined) || (options === null)) {
options = {};
}
return publish.call(this, topic, payload, options, done);
};
this.publish = newPublish;
if (proto.pub === proto.publish) {
this.pub = newPublish;
}
};
/**
* This method provides the inverse of subscribe.
*
* @param {String} topic the topic from which to unsubscribe
* @param {Function} callback the callback that will be unsubscribed
* @param {Function} done the callback that will be called when the unsubscribe is completed
* @api public
*/
AbstractAscoltatore.prototype.unsubscribe = function(topic, callback, done) {
throw new Error("Subclass to implement");
};
/**
* This method closes the Ascoltatore.
* After this method is called every call to subscribe or publish will raise
* an exception
*
* @param {Function} done the callback that will be called when Ascoltatore is closed
* @api public
*/
AbstractAscoltatore.prototype.close = function(done) {
throw new Error("Subclass to implement");
};
AbstractAscoltatore.prototype._subTopic = function(topic) {
if (this._reInSeparator) {
topic = topic.replace(this._reInSeparator,
this._nativeSettings.separator);
}
if (this._reInWildcardSome) {
topic = topic.replace(this._reInWildcardSome,
this._nativeSettings.wildcardSome);
}
if (this._reInWildcardOne) {
topic = topic.replace(this._reInWildcardOne,
this._nativeSettings.wildcardOne);
}
return topic;
};
AbstractAscoltatore.prototype._recvTopic = function(topic) {
if (this._reOutSeparator) {
topic = topic.replace(this._reOutSeparator, this._separator);
}
return topic;
};
AbstractAscoltatore.prototype._pubTopic = function(topic) {
if (this._reInSeparator) {
topic = topic.replace(this._reInSeparator,
this._nativeSettings.separator);
}
return topic;
};
/**
* Exports the AbstractAscoltatore;
*
* @api public
*/
module.exports = AbstractAscoltatore;