CLIENT GUI Framework
[vnfsdk/refrepo.git] / portal-common / src / main / webapp / common / thirdparty / cometd / cometd / AckExtension.js
1 /*\r
2  * Copyright (c) 2010 the original author or authors.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 (function()\r
18 {\r
19     function bind(org_cometd)\r
20     {\r
21         /**\r
22          * This client-side extension enables the client to acknowledge to the server\r
23          * the messages that the client has received.\r
24          * For the acknowledgement to work, the server must be configured with the\r
25          * correspondent server-side ack extension. If both client and server support\r
26          * the ack extension, then the ack functionality will take place automatically.\r
27          * By enabling this extension, all messages arriving from the server will arrive\r
28          * via the long poll, so the comet communication will be slightly chattier.\r
29          * The fact that all messages will return via long poll means also that the\r
30          * messages will arrive with total order, which is not guaranteed if messages\r
31          * can arrive via both long poll and normal response.\r
32          * Messages are not acknowledged one by one, but instead a group of messages is\r
33          * acknowledged when long poll returns.\r
34          */\r
35         return org_cometd.AckExtension = function()\r
36         {\r
37             var _cometd;\r
38             var _serverSupportsAcks = false;\r
39             var _ackId = -1;\r
40 \r
41             function _debug(text, args)\r
42             {\r
43                 _cometd._debug(text, args);\r
44             }\r
45 \r
46             this.registered = function(name, cometd)\r
47             {\r
48                 _cometd = cometd;\r
49                 _debug('AckExtension: executing registration callback');\r
50             };\r
51 \r
52             this.unregistered = function()\r
53             {\r
54                 _debug('AckExtension: executing unregistration callback');\r
55                 _cometd = null;\r
56             };\r
57 \r
58             this.incoming = function(message)\r
59             {\r
60                 var channel = message.channel;\r
61                 if (channel == '/meta/handshake')\r
62                 {\r
63                     _serverSupportsAcks = message.ext && message.ext.ack;\r
64                     _debug('AckExtension: server supports acks', _serverSupportsAcks);\r
65                 }\r
66                 else if (_serverSupportsAcks && channel == '/meta/connect' && message.successful)\r
67                 {\r
68                     var ext = message.ext;\r
69                     if (ext && typeof ext.ack === 'number')\r
70                     {\r
71                         _ackId = ext.ack;\r
72                         _debug('AckExtension: server sent ack id', _ackId);\r
73                     }\r
74                 }\r
75                 return message;\r
76             };\r
77 \r
78             this.outgoing = function(message)\r
79             {\r
80                 var channel = message.channel;\r
81                 if (channel == '/meta/handshake')\r
82                 {\r
83                     if (!message.ext)\r
84                     {\r
85                         message.ext = {};\r
86                     }\r
87                     message.ext.ack = _cometd && _cometd.ackEnabled !== false;\r
88                     _ackId = -1;\r
89                 }\r
90                 else if (_serverSupportsAcks && channel == '/meta/connect')\r
91                 {\r
92                     if (!message.ext)\r
93                     {\r
94                         message.ext = {};\r
95                     }\r
96                     message.ext.ack = _ackId;\r
97                     _debug('AckExtension: client sending ack id', _ackId);\r
98                 }\r
99                 return message;\r
100             };\r
101         };\r
102     }\r
103 \r
104     if (typeof define === 'function' && define.amd)\r
105     {\r
106         define(['org/cometd'], bind);\r
107     }\r
108     else\r
109     {\r
110         bind(org.cometd);\r
111     }\r
112 })();\r