[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / red / comms.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16  
17 RED.comms = (function() {
18     
19     var errornotification = null;
20     var subscriptions = {};
21     var ws;
22     function connectWS() {
23         var path = location.hostname+":"+location.port+document.location.pathname;
24         path = path+(path.slice(-1) == "/"?"":"/")+"comms";
25         path = "ws"+(document.location.protocol=="https:"?"s":"")+"://"+path;
26         ws = new WebSocket(path);
27         ws.onopen = function() {
28             if (errornotification) {
29                 errornotification.close();
30                 errornotification = null;
31             }
32             for (var t in subscriptions) {
33                 if (subscriptions.hasOwnProperty(t)) {
34                     ws.send(JSON.stringify({subscribe:t}));
35                 }
36             }
37         }
38         ws.onmessage = function(event) {
39             var msg = JSON.parse(event.data);
40             if (msg.topic) {
41                 for (var t in subscriptions) {
42                     if (subscriptions.hasOwnProperty(t)) {
43                         var re = new RegExp("^"+t.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
44                         if (re.test(msg.topic)) {
45                             var subscribers = subscriptions[t];
46                             if (subscribers) {
47                                 for (var i=0;i<subscribers.length;i++) {
48                                     subscribers[i](msg.topic,msg.data);
49                                 }
50                             }
51                         }
52                     }
53                 }
54             }
55         };
56         ws.onclose = function() {
57             if (errornotification == null) {
58                 errornotification = RED.notify("<b>Error</b>: Lost connection to server","error",true);
59             }
60             setTimeout(connectWS,1000);
61         }
62     }
63     
64     function subscribe(topic,callback) {
65         if (subscriptions[topic] == null) {
66             subscriptions[topic] = [];
67         }
68         subscriptions[topic].push(callback);
69         if (ws && ws.readyState == 1) {
70             ws.send(JSON.stringify({subscribe:topic}));
71         }
72     }
73     
74     function unsubscribe(topic,callback) {
75         if (subscriptions.topic) {
76             for (var i=0;i<subscriptions.topic.length;i++) {
77                 if (subscriptions.topic[i] === callback) {
78                     subscriptions.topic.splice(i,1);
79                     break;
80                 }
81             }
82             if (subscriptions.topic.length === 0) {
83                 delete subscriptions.topic;
84             }
85         }
86     }
87     
88     return {
89         connect: connectWS,
90         subscribe: subscribe,
91         unsubscribe:unsubscribe
92     }
93 })();