Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io / lib / socket.js
1
2 /*!
3  * socket.io-node
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 /**
9  * Module dependencies.
10  */
11
12 var parser = require('./parser')
13   , util = require('./util')
14   , EventEmitter = process.EventEmitter
15
16 /**
17  * Export the constructor.
18  */
19
20 exports = module.exports = Socket;
21
22 /**
23  * Default error event listener to prevent uncaught exceptions.
24  */
25
26 var defaultError = function () {};
27
28 /**
29  * Socket constructor.
30  *
31  * @param {Manager} manager instance
32  * @param {String} session id
33  * @param {Namespace} namespace the socket belongs to
34  * @param {Boolean} whether the 
35  * @api public
36  */
37
38 function Socket (manager, id, nsp, readable) {
39   this.id = id;
40   this.namespace = nsp;
41   this.manager = manager;
42   this.disconnected = false;
43   this.ackPackets = 0;
44   this.acks = {};
45   this.setFlags();
46   this.readable = readable;
47   this.store = this.manager.store.client(this.id);
48   this.on('error', defaultError);
49 };
50
51 /**
52  * Inherits from EventEmitter.
53  */
54
55 Socket.prototype.__proto__ = EventEmitter.prototype;
56
57 /**
58  * Accessor shortcut for the handshake data
59  *
60  * @api private
61  */
62
63 Socket.prototype.__defineGetter__('handshake', function () {
64   return this.manager.handshaken[this.id];
65 });
66
67 /**
68  * Accessor shortcut for the transport type
69  *
70  * @api private
71  */
72
73 Socket.prototype.__defineGetter__('transport', function () {
74   return this.manager.transports[this.id].name;
75 });
76
77 /**
78  * Accessor shortcut for the logger.
79  *
80  * @api private
81  */
82
83 Socket.prototype.__defineGetter__('log', function () {
84   return this.manager.log;
85 });
86
87 /**
88  * JSON message flag.
89  *
90  * @api public
91  */
92
93 Socket.prototype.__defineGetter__('json', function () {
94   this.flags.json = true;
95   return this;
96 });
97
98 /**
99  * Volatile message flag.
100  *
101  * @api public
102  */
103
104 Socket.prototype.__defineGetter__('volatile', function () {
105   this.flags.volatile = true;
106   return this;
107 });
108
109 /**
110  * Broadcast message flag.
111  *
112  * @api public
113  */
114
115 Socket.prototype.__defineGetter__('broadcast', function () {
116   this.flags.broadcast = true;
117   return this;
118 });
119
120 /**
121  * Overrides the room to broadcast messages to (flag)
122  *
123  * @api public
124  */
125
126 Socket.prototype.to = Socket.prototype.in = function (room) {
127   this.flags.room = room;
128   return this;
129 };
130
131 /**
132  * Resets flags
133  *
134  * @api private
135  */
136
137 Socket.prototype.setFlags = function () {
138   this.flags = {
139       endpoint: this.namespace.name
140     , room: ''
141   };
142   return this;
143 };
144
145 /**
146  * Triggered on disconnect
147  *
148  * @api private
149  */
150
151 Socket.prototype.onDisconnect = function (reason) {
152   if (!this.disconnected) {
153     this.$emit('disconnect', reason);
154     this.disconnected = true;
155   }
156 };
157
158 /**
159  * Joins a user to a room.
160  *
161  * @api public
162  */
163
164 Socket.prototype.join = function (name, fn) {
165   var nsp = this.namespace.name
166     , name = (nsp + '/') + name;
167
168   this.manager.onJoin(this.id, name);
169   this.manager.store.publish('join', this.id, name);
170
171   if (fn) {
172     this.log.warn('Client#join callback is deprecated');
173     fn();
174   }
175
176   return this;
177 };
178
179 /**
180  * Un-joins a user from a room.
181  *
182  * @api public
183  */
184
185 Socket.prototype.leave = function (name, fn) {
186   var nsp = this.namespace.name
187     , name = (nsp + '/') + name;
188
189   this.manager.onLeave(this.id, name);
190   this.manager.store.publish('leave', this.id, name);
191
192   if (fn) {
193     this.log.warn('Client#leave callback is deprecated');
194     fn();
195   }
196
197   return this;
198 };
199
200 /**
201  * Transmits a packet.
202  *
203  * @api private
204  */
205
206 Socket.prototype.packet = function (packet) {
207   if (this.flags.broadcast) {
208     this.log.debug('broadcasting packet');
209     this.namespace.in(this.flags.room).except(this.id).packet(packet);
210   } else {
211     packet.endpoint = this.flags.endpoint;
212     packet = parser.encodePacket(packet);
213
214     this.dispatch(packet, this.flags.volatile);
215   }
216
217   this.setFlags();
218
219   return this;
220 };
221
222 /**
223  * Dispatches a packet
224  *
225  * @api private
226  */
227
228 Socket.prototype.dispatch = function (packet, volatile) {
229   if (this.manager.transports[this.id] && this.manager.transports[this.id].open) {
230     this.manager.transports[this.id].onDispatch(packet, volatile);
231   } else {
232     if (!volatile) {
233       this.manager.onClientDispatch(this.id, packet, volatile);
234     }
235
236     this.manager.store.publish('dispatch:' + this.id, packet, volatile);
237   }
238 };
239
240 /**
241  * Stores data for the client.
242  *
243  * @api public
244  */
245
246 Socket.prototype.set = function (key, value, fn) {
247   this.store.set(key, value, fn);
248   return this;
249 };
250
251 /**
252  * Retrieves data for the client
253  *
254  * @api public
255  */
256
257 Socket.prototype.get = function (key, fn) {
258   this.store.get(key, fn);
259   return this;
260 };
261
262 /**
263  * Checks data for the client
264  *
265  * @api public
266  */
267
268 Socket.prototype.has = function (key, fn) {
269   this.store.has(key, fn);
270   return this;
271 };
272
273 /**
274  * Deletes data for the client
275  *
276  * @api public
277  */
278
279 Socket.prototype.del = function (key, fn) {
280   this.store.del(key, fn);
281   return this;
282 };
283
284 /**
285  * Kicks client
286  *
287  * @api public
288  */
289
290 Socket.prototype.disconnect = function () {
291   if (!this.disconnected) {
292     this.log.info('booting client');
293
294     if ('' === this.namespace.name) {
295       if (this.manager.transports[this.id] && this.manager.transports[this.id].open) {
296         this.manager.transports[this.id].onForcedDisconnect();
297       } else {
298         this.manager.onClientDisconnect(this.id);
299         this.manager.store.publish('disconnect:' + this.id);
300       }
301     } else {
302       this.packet({type: 'disconnect'});
303       this.manager.onLeave(this.id, this.namespace.name);
304       this.$emit('disconnect', 'booted');
305     }
306
307   }
308
309   return this;
310 };
311
312 /**
313  * Send a message.
314  *
315  * @api public
316  */
317
318 Socket.prototype.send = function (data, fn) {
319   var packet = {
320       type: this.flags.json ? 'json' : 'message'
321     , data: data
322   };
323
324   if (fn) {
325     packet.id = ++this.ackPackets;
326     packet.ack = true;
327     this.acks[packet.id] = fn;
328   }
329
330   return this.packet(packet);
331 };
332
333 /**
334  * Original emit function.
335  *
336  * @api private
337  */
338
339 Socket.prototype.$emit = EventEmitter.prototype.emit;
340
341 /**
342  * Emit override for custom events.
343  *
344  * @api public
345  */
346
347 Socket.prototype.emit = function (ev) {
348   if (ev == 'newListener') {
349     return this.$emit.apply(this, arguments);
350   }
351
352   var args = util.toArray(arguments).slice(1)
353     , lastArg = args[args.length - 1]
354     , packet = {
355           type: 'event'
356         , name: ev
357       };
358
359   if ('function' == typeof lastArg) {
360     packet.id = ++this.ackPackets;
361     packet.ack = lastArg.length ? 'data' : true;
362     this.acks[packet.id] = lastArg;
363     args = args.slice(0, args.length - 1);
364   }
365
366   packet.args = args;
367
368   return this.packet(packet);
369 };