aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/sockets.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/sockets.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/sockets.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/sockets.js166
1 files changed, 166 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/sockets.js b/js/helper-classes/RDGE/src/core/script/sockets.js
new file mode 100644
index 00000000..28c43387
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/sockets.js
@@ -0,0 +1,166 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/*
8* Simple socket connection discovery and management
9*/
10
11g_connectionManager = null;
12
13function CreateSocket(url, listenList, connectedList, messageHandler, socketID, openHandler, closeHandler)
14{
15 var ws = new WebSocket(url);
16
17 ws.listenIndex = socketID;
18
19 ws.onmessage = messageHandler;
20
21 ws.connectedList = connectedList;
22 ws.listenList = listenList;
23
24 // make up a flag
25 ws.rdge_isConnected = false;
26 ws.rdge_tryDisconnect = false;
27
28 if (openHandler)
29 {
30 ws.addEventListener("open", openHandler);
31 }
32 if (closeHandler)
33 {
34 ws.addEventListener("close", closeHandler);
35 ws.addEventListener("error", closeHandler);
36 }
37
38 ws.onopen = function(evt)
39 {
40 var websocket = evt.srcElement;
41 document.getElementById("socketConnection").innerHTML += "connected: " + websocket.URL +"</br>";
42
43 websocket.rdge_isConnected = true;
44
45 websocket.connectedList.push(ws);
46
47 // save the connected array index of this item
48 ws.connectedIndex = websocket.connectedList.length - 1;
49 }
50
51 ws.onerror = function(event)
52 {
53 window.console.log("error: " + event);
54 }
55
56 ws.onclose = function(evt)
57 {
58 var websocket = evt.srcElement;
59
60 websocket.rdge_isConnected = false;
61
62 // send disconnect message
63 websocket.send("type=srv\nmsg=2\n");
64
65 // remove from connected sockets
66 websocket.connectedList[websocket.connectedIndex, 1];
67
68 // re-open port for listening
69 //websocket.listenList[websocket.listenIndex] = new WebSocket(websocket.URL.toString());
70
71 //document.getElementById("socketConnection").innerHTML += "disconnected: " + websocket.URL +"</br>";
72 }
73
74 return ws;
75}
76
77// The DCHP range on Whitney 10.0.0.1 - 10.0.0.254
78
79/*
80* manages a connection
81* @param base: base url minus the last octet (ie "ws://192.168.1.")
82* @param startAddr: the last octect of the beginning address range (ie 10.0.0.x where x is the value passed in for startAddr)
83* @param endAddr: the last octect of the end address range (ie 10.0.0.x where x is the value passed in for endAddr)
84* @param messageHandlerFunc: handle to onMessage function
85*/
86function ConnectionPool(base, startAddr, endAddr, messageHandlerFunc)
87{
88 this.start = startAddr;
89 this.end = endAddr;
90 this.port = 38951;
91 this.messageHandler = messageHandlerFunc;
92
93 this.listenSockets = [];
94 this.connectedSockets = [];
95
96 this.openHandler = null;
97 this.closeHandler = null;
98
99 this.interval = null;
100
101 this.Init = function(openHandler, closeHandler)
102 {
103 // emtpy out lists
104 this.listenSockets = [];
105 this.connectedSockets = [];
106
107 this.openHandler = openHandler;
108 this.closeHandler = closeHandler;
109
110 var len = this.end - this.start;
111 for (var i = 0; i < len; ++i)
112 {
113 var url = base + (this.start + i) + ":" + this.port + "/resourcePath";
114 var ws = CreateSocket(url, this.listenSockets, this.connectedSockets, this.messageHandler, i, this.openHandler, this.closeHandler);
115
116 this.listenSockets.push(ws);
117 }
118
119 this.interval = setInterval(ConnPoll, 200);
120 }
121
122 this.Shutdown = function()
123 {
124 if(this.interval != null)
125 {
126 clearInterval(this.interval);
127 this.interval = null;
128 }
129 var len = this.connectedSockets.length;
130 for (var i = 0; i < len; ++i)
131 {
132 // send disconnect message
133 this.connectedSockets[i].send("type=srv\nmsg=2\n");;
134 }
135 }
136
137 this.Close = function(socket)
138 {
139 socket.rdge_tryDisconnect = true;
140 }
141
142 this.Poll = function()
143 {
144 var len =this. end - this.start;
145 for (var i = 0; i < len; ++i)
146 {
147 // re init the sockets to simulate broadcast
148 if(this.listenSockets[i].readyState == 3/*CLOSING*/ || this.listenSockets[i].readyState == 2/*CLOSING*/ || this.listenSockets[i].rdge_tryDisconnect == true)
149 {
150 if(this.listenSockets[i].rdge_tryDisconnect)
151 {
152 document.getElementById("socketConnection").innerHTML += "graceful discon: " + this.listenSockets[i].URL +"</br>";
153 }
154
155 this.listenSockets[i].rdge_tryDisconnect = false;
156
157 var url = base + (this.start + i) + ":" + this.port + "/resourcePath";
158 this.listenSockets[i].close();
159 this.listenSockets[i] = CreateSocket(url, this.listenSockets, this.connectedSockets, this.messageHandler, i, this.openHandler, this.closeHandler);
160 }
161 }
162
163 }
164
165}
166