Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / transport.js
1 /**
2  * socket.io
3  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
4  * MIT Licensed
5  */
6
7 (function (exports, io) {
8
9   /**
10    * Expose constructor.
11    */
12
13   exports.Transport = Transport;
14
15   /**
16    * This is the transport template for all supported transport methods.
17    *
18    * @constructor
19    * @api public
20    */
21
22   function Transport (socket, sessid) {
23     this.socket = socket;
24     this.sessid = sessid;
25   };
26
27   /**
28    * Apply EventEmitter mixin.
29    */
30
31   io.util.mixin(Transport, io.EventEmitter);
32
33
34   /**
35    * Indicates whether heartbeats is enabled for this transport
36    *
37    * @api private
38    */
39
40   Transport.prototype.heartbeats = function () {
41     return true;
42   };
43
44   /**
45    * Handles the response from the server. When a new response is received
46    * it will automatically update the timeout, decode the message and
47    * forwards the response to the onMessage function for further processing.
48    *
49    * @param {String} data Response from the server.
50    * @api private
51    */
52
53   Transport.prototype.onData = function (data) {
54     this.clearCloseTimeout();
55
56     // If the connection in currently open (or in a reopening state) reset the close
57     // timeout since we have just received data. This check is necessary so
58     // that we don't reset the timeout on an explicitly disconnected connection.
59     if (this.socket.connected || this.socket.connecting || this.socket.reconnecting) {
60       this.setCloseTimeout();
61     }
62
63     if (data !== '') {
64       // todo: we should only do decodePayload for xhr transports
65       var msgs = io.parser.decodePayload(data);
66
67       if (msgs && msgs.length) {
68         for (var i = 0, l = msgs.length; i < l; i++) {
69           this.onPacket(msgs[i]);
70         }
71       }
72     }
73
74     return this;
75   };
76
77   /**
78    * Handles packets.
79    *
80    * @api private
81    */
82
83   Transport.prototype.onPacket = function (packet) {
84     this.socket.setHeartbeatTimeout();
85
86     if (packet.type == 'heartbeat') {
87       return this.onHeartbeat();
88     }
89
90     if (packet.type == 'connect' && packet.endpoint == '') {
91       this.onConnect();
92     }
93
94     if (packet.type == 'error' && packet.advice == 'reconnect') {
95       this.isOpen = false;
96     }
97
98     this.socket.onPacket(packet);
99
100     return this;
101   };
102
103   /**
104    * Sets close timeout
105    *
106    * @api private
107    */
108
109   Transport.prototype.setCloseTimeout = function () {
110     if (!this.closeTimeout) {
111       var self = this;
112
113       this.closeTimeout = setTimeout(function () {
114         self.onDisconnect();
115       }, this.socket.closeTimeout);
116     }
117   };
118
119   /**
120    * Called when transport disconnects.
121    *
122    * @api private
123    */
124
125   Transport.prototype.onDisconnect = function () {
126     if (this.isOpen) this.close();
127     this.clearTimeouts();
128     this.socket.onDisconnect();
129     return this;
130   };
131
132   /**
133    * Called when transport connects
134    *
135    * @api private
136    */
137
138   Transport.prototype.onConnect = function () {
139     this.socket.onConnect();
140     return this;
141   };
142
143   /**
144    * Clears close timeout
145    *
146    * @api private
147    */
148
149   Transport.prototype.clearCloseTimeout = function () {
150     if (this.closeTimeout) {
151       clearTimeout(this.closeTimeout);
152       this.closeTimeout = null;
153     }
154   };
155
156   /**
157    * Clear timeouts
158    *
159    * @api private
160    */
161
162   Transport.prototype.clearTimeouts = function () {
163     this.clearCloseTimeout();
164
165     if (this.reopenTimeout) {
166       clearTimeout(this.reopenTimeout);
167     }
168   };
169
170   /**
171    * Sends a packet
172    *
173    * @param {Object} packet object.
174    * @api private
175    */
176
177   Transport.prototype.packet = function (packet) {
178     this.send(io.parser.encodePacket(packet));
179   };
180
181   /**
182    * Send the received heartbeat message back to server. So the server
183    * knows we are still connected.
184    *
185    * @param {String} heartbeat Heartbeat response from the server.
186    * @api private
187    */
188
189   Transport.prototype.onHeartbeat = function (heartbeat) {
190     this.packet({ type: 'heartbeat' });
191   };
192
193   /**
194    * Called when the transport opens.
195    *
196    * @api private
197    */
198
199   Transport.prototype.onOpen = function () {
200     this.isOpen = true;
201     this.clearCloseTimeout();
202     this.socket.onOpen();
203   };
204
205   /**
206    * Notifies the base when the connection with the Socket.IO server
207    * has been disconnected.
208    *
209    * @api private
210    */
211
212   Transport.prototype.onClose = function () {
213     var self = this;
214
215     /* FIXME: reopen delay causing a infinit loop
216     this.reopenTimeout = setTimeout(function () {
217       self.open();
218     }, this.socket.options['reopen delay']);*/
219
220     this.isOpen = false;
221     this.socket.onClose();
222     this.onDisconnect();
223   };
224
225   /**
226    * Generates a connection url based on the Socket.IO URL Protocol.
227    * See <https://github.com/learnboost/socket.io-node/> for more details.
228    *
229    * @returns {String} Connection url
230    * @api private
231    */
232
233   Transport.prototype.prepareUrl = function () {
234     var options = this.socket.options;
235
236     return this.scheme() + '://'
237       + options.host + ':' + options.port + '/'
238       + options.resource + '/' + io.protocol
239       + '/' + this.name + '/' + this.sessid;
240   };
241
242   /**
243    * Checks if the transport is ready to start a connection.
244    *
245    * @param {Socket} socket The socket instance that needs a transport
246    * @param {Function} fn The callback
247    * @api private
248    */
249
250   Transport.prototype.ready = function (socket, fn) {
251     fn.call(this);
252   };
253 })(
254     'undefined' != typeof io ? io : module.exports
255   , 'undefined' != typeof io ? io : module.parent.exports
256 );