aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/sockets.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/sockets.js')
-rwxr-xr-xjs/helper-classes/RDGE/src/core/script/sockets.js243
1 files changed, 117 insertions, 126 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/sockets.js b/js/helper-classes/RDGE/src/core/script/sockets.js
index 28c43387..0a4e9364 100755
--- a/js/helper-classes/RDGE/src/core/script/sockets.js
+++ b/js/helper-classes/RDGE/src/core/script/sockets.js
@@ -4,78 +4,81 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7// RDGE namespaces
8var RDGE = RDGE || {};
9
7/* 10/*
8* Simple socket connection discovery and management 11* Simple socket connection discovery and management
9*/ 12*/
10 13
11g_connectionManager = null; 14RDGE.CreateSocket = function (url, listenList, connectedList, messageHandler, socketID, openHandler, closeHandler) {
15 var ws = new WebSocket(url);
12 16
13function CreateSocket(url, listenList, connectedList, messageHandler, socketID, openHandler, closeHandler) 17 ws.listenIndex = socketID;
14{
15 var ws = new WebSocket(url);
16 18
17 ws.listenIndex = socketID; 19 ws.onmessage = messageHandler;
18 20
19 ws.onmessage = messageHandler; 21 ws.connectedList = connectedList;
22 ws.listenList = listenList;
20 23
21 ws.connectedList = connectedList; 24 // make up a flag
22 ws.listenList = listenList; 25 ws.rdge_isConnected = false;
26 ws.rdge_tryDisconnect = false;
23 27
24 // make up a flag 28 if (openHandler) {
25 ws.rdge_isConnected = false; 29 ws.addEventListener("open", openHandler);
26 ws.rdge_tryDisconnect = false; 30 }
27 31 if (closeHandler) {
28 if (openHandler) 32 ws.addEventListener("close", closeHandler);
29 { 33 ws.addEventListener("error", closeHandler);
30 ws.addEventListener("open", openHandler); 34 }
31 } 35
32 if (closeHandler) 36 ws.onopen = function (evt) {
33 { 37 var websocket = evt.srcElement;
34 ws.addEventListener("close", closeHandler); 38 document.getElementById("socketConnection").innerHTML += "connected: " + websocket.URL + "</br>";
35 ws.addEventListener("error", closeHandler); 39
36 } 40 websocket.rdge_isConnected = true;
37 41
38 ws.onopen = function(evt) 42 websocket.connectedList.push(ws);
39 { 43
40 var websocket = evt.srcElement; 44 // save the connected array index of this item
41 document.getElementById("socketConnection").innerHTML += "connected: " + websocket.URL +"</br>"; 45 ws.connectedIndex = websocket.connectedList.length - 1;
42 46 };
43 websocket.rdge_isConnected = true; 47
44 48 ws.onerror = function (event) {
45 websocket.connectedList.push(ws); 49 window.console.log("error: " + event);
46 50 }
47 // save the connected array index of this item 51
48 ws.connectedIndex = websocket.connectedList.length - 1; 52 ws.onclose = function (evt) {
49 } 53 var websocket = evt.srcElement;
50 54
51 ws.onerror = function(event) 55 websocket.rdge_isConnected = false;
52 { 56
53 window.console.log("error: " + event); 57 // send disconnect message
54 } 58 websocket.send("type=srv\nmsg=2\n");
55 59
56 ws.onclose = function(evt) 60 // remove from connected sockets
57 { 61 websocket.connectedList[websocket.connectedIndex, 1];
58 var websocket = evt.srcElement; 62
59 63 // re-open port for listening
60 websocket.rdge_isConnected = false; 64 //websocket.listenList[websocket.listenIndex] = new WebSocket(websocket.URL.toString());
61 65
62 // send disconnect message 66 //document.getElementById("socketConnection").innerHTML += "disconnected: " + websocket.URL +"</br>";
63 websocket.send("type=srv\nmsg=2\n"); 67 };
64 68
65 // remove from connected sockets 69 return ws;
66 websocket.connectedList[websocket.connectedIndex, 1]; 70};
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 71
77// The DCHP range on Whitney 10.0.0.1 - 10.0.0.254 72// The DCHP range on Whitney 10.0.0.1 - 10.0.0.254
78 73
74// add the connection Pool's to this list for auto polling
75RDGE.ConnPoll = function() {
76 var len = RDGE.globals.poolList.length;
77 for (var i = 0; i < len; ++i) {
78 RDGE.globals.poolList[i].Poll();
79 }
80};
81
79/* 82/*
80* manages a connection 83* manages a connection
81* @param base: base url minus the last octet (ie "ws://192.168.1.") 84* @param base: base url minus the last octet (ie "ws://192.168.1.")
@@ -83,7 +86,7 @@ function CreateSocket(url, listenList, connectedList, messageHandler, socketID,
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) 86* @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 87* @param messageHandlerFunc: handle to onMessage function
85*/ 88*/
86function ConnectionPool(base, startAddr, endAddr, messageHandlerFunc) 89RDGE.ConnectionPool = funciton(base, startAddr, endAddr, messageHandlerFunc)
87{ 90{
88 this.start = startAddr; 91 this.start = startAddr;
89 this.end = endAddr; 92 this.end = endAddr;
@@ -98,69 +101,57 @@ function ConnectionPool(base, startAddr, endAddr, messageHandlerFunc)
98 101
99 this.interval = null; 102 this.interval = null;
100 103
101 this.Init = function(openHandler, closeHandler) 104 this.Init = function (openHandler, closeHandler) {
102 { 105 // empty out lists
103 // emtpy out lists 106 this.listenSockets = [];
104 this.listenSockets = []; 107 this.connectedSockets = [];
105 this.connectedSockets = []; 108
106 109 this.openHandler = openHandler;
107 this.openHandler = openHandler; 110 this.closeHandler = closeHandler;
108 this.closeHandler = closeHandler; 111
109 112 var len = this.end - this.start;
110 var len = this.end - this.start; 113 for (var i = 0; i < len; ++i) {
111 for (var i = 0; i < len; ++i) 114 var url = base + (this.start + i) + ":" + this.port + "/resourcePath";
112 { 115 var ws = CreateSocket(url, this.listenSockets, this.connectedSockets, this.messageHandler, i, this.openHandler, this.closeHandler);
113 var url = base + (this.start + i) + ":" + this.port + "/resourcePath"; 116
114 var ws = CreateSocket(url, this.listenSockets, this.connectedSockets, this.messageHandler, i, this.openHandler, this.closeHandler); 117 this.listenSockets.push(ws);
115 118 }
116 this.listenSockets.push(ws); 119
117 } 120 this.interval = setInterval(ConnPoll, 200);
118 121 };
119 this.interval = setInterval(ConnPoll, 200); 122
120 } 123 this.Shutdown = function () {
121 124 if (this.interval != null) {
122 this.Shutdown = function() 125 clearInterval(this.interval);
123 { 126 this.interval = null;
124 if(this.interval != null) 127 }
125 { 128 var len = this.connectedSockets.length;
126 clearInterval(this.interval); 129 for (var i = 0; i < len; ++i) {
127 this.interval = null; 130 // send disconnect message
128 } 131 this.connectedSockets[i].send("type=srv\nmsg=2\n"); ;
129 var len = this.connectedSockets.length; 132 }
130 for (var i = 0; i < len; ++i) 133 };
131 { 134
132 // send disconnect message 135 this.Close = function (socket) {
133 this.connectedSockets[i].send("type=srv\nmsg=2\n");; 136 socket.rdge_tryDisconnect = true;
134 } 137 };
135 } 138
136 139 this.Poll = function () {
137 this.Close = function(socket) 140 var len = this.end - this.start;
138 { 141 for (var i = 0; i < len; ++i) {
139 socket.rdge_tryDisconnect = true; 142 // re init the sockets to simulate broadcast
140 } 143 if (this.listenSockets[i].readyState == 3/*CLOSING*/ || this.listenSockets[i].readyState == 2/*CLOSING*/ || this.listenSockets[i].rdge_tryDisconnect == true) {
141 144 if (this.listenSockets[i].rdge_tryDisconnect) {
142 this.Poll = function() 145 document.getElementById("socketConnection").innerHTML += "graceful discon: " + this.listenSockets[i].URL + "</br>";
143 { 146 }
144 var len =this. end - this.start; 147
145