Skip to content

Commit f941d04

Browse files
committed
Added serverPush broadcast to all browser clients
1 parent 2610383 commit f941d04

File tree

8 files changed

+131
-32
lines changed

8 files changed

+131
-32
lines changed

examples/processes/sharedVariablesPython2/www

-1
This file was deleted.

examples/pythonAndJS/serverPush/__init__.py

Whitespace-only changes.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head >
5+
<script data-main="www/libs/weioLibs" src="www/libs/require.js"></script>
6+
<title>My first Web app</title>
7+
</head>
8+
9+
<body>
10+
11+
<p>Hello world!</p>
12+
13+
<script>
14+
15+
function onWeioReady() {
16+
console.log("DOM is loaded, websocket is opened");
17+
// Send event "colorled" with message 1
18+
genericMessage("tst", 22);
19+
20+
weioCallbacks["myJsCallback"] = myFunction;
21+
}
22+
23+
function myFunction(data) {
24+
console.log(data);
25+
}
26+
27+
</script>
28+
</body>
29+
</html>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from weioLib.weio import *
2+
3+
import time
4+
5+
sharedVar[0] = 1
6+
def setup():
7+
attach.process(myProcess)
8+
attach.event("tst", myEvent)
9+
10+
def myEvent(dataIn):
11+
print "EVENT"
12+
print dataIn
13+
sharedVar[0] = dataIn
14+
15+
def myProcess():
16+
print("Hello world")
17+
while True:
18+
if sharedVar[0] is not None:
19+
print sharedVar[0]
20+
time.sleep(1)
21+
data = 55
22+
serverPush("myJsCallback", data)

handlers/weioJSPYHandler.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
###
1+
###
22
#
33
# WEIO Web Of Things Platform
44
# Copyright (C) 2013 Nodesign.net, Uros PETREVSKI, Drasko DRASKOVIC
55
# All rights reserved
66
#
7-
# ## ## ######## #### #######
8-
# ## ## ## ## ## ## ##
9-
# ## ## ## ## ## ## ##
10-
# ## ## ## ###### ## ## ##
11-
# ## ## ## ## ## ## ##
12-
# ## ## ## ## ## ## ##
7+
# ## ## ######## #### #######
8+
# ## ## ## ## ## ## ##
9+
# ## ## ## ## ## ## ##
10+
# ## ## ## ###### ## ## ##
11+
# ## ## ## ## ## ## ##
12+
# ## ## ## ## ## ## ##
1313
# ### ### ######## #### #######
1414
#
15-
# Web Of Things Platform
15+
# Web Of Things Platform
1616
#
1717
# This file is part of WEIO and is published under BSD license.
1818
#
@@ -41,7 +41,7 @@
4141
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4242
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4343
#
44-
# Authors :
44+
# Authors :
4545
# Uros PETREVSKI <[email protected]>
4646
# Drasko DRASKOVIC <[email protected]>
4747
#
@@ -70,6 +70,7 @@ def on_open(self, request):
7070
# Add the connection to the connections dictionary
7171
connUuid = uuid.uuid4()
7272
weioRunnerGlobals.weioConnections[connUuid] = self
73+
weioRunnerGlobals.weioConnUuids.append(connUuid)
7374

7475
# collect client ip address and user machine info
7576
# print self.request to see all available info on user connection
@@ -129,8 +130,8 @@ def serve(self, data) :
129130
elif key == "callback":
130131
msg.callbackJS = data["callback"]
131132

132-
# Send message to launcher process
133-
weioRunnerGlobals.QOUT.put(msg)
133+
# Send message to launcher process
134+
weioRunnerGlobals.QOUT.put(msg)
134135

135136

136137
def on_close(self):
@@ -141,11 +142,12 @@ def on_close(self):
141142
for connUuid, conn in weioRunnerGlobals.weioConnections.iteritems():
142143
if (conn == self):
143144
weioRunnerGlobals.weioConnections.pop(connUuid)
145+
weioRunnerGlobals.weioConnUuids.remove(connUuid)
144146

145147

146148
###
147149
# This is used for remote apps - Tornado User opens __client__ socket
148-
# and puths everything that comes from this socket to WeioHandlerRemote()
150+
# and puths everything that comes from this socket to WeioHandlerRemote()
149151
###
150152
class WeioHandlerRemote():
151153
def __init__(self):
@@ -179,13 +181,12 @@ def serve(self, data) :
179181
elif key == "callback":
180182
msg.callbackJS = data["callback"]
181183

182-
# Send message to launcher process
183-
weioRunnerGlobals.QOUT.put(msg)
184+
# Send message to launcher process
185+
weioRunnerGlobals.QOUT.put(msg)
184186

185187
def on_close(self):
186188
self.remoteConn = None
187189

188190
# Remove client from the clients list and broadcast leave message
189191
weioRunnerGlobals.weioConnections.pop(self.connUuid)
190192
self.connUuid = None
191-

weioLib/weioRunnerGlobals.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
###
1+
###
22
#
33
# WEIO Web Of Things Platform
44
# Copyright (C) 2013 Nodesign.net, Uros PETREVSKI, Drasko DRASKOVIC
55
# All rights reserved
66
#
7-
# ## ## ######## #### #######
8-
# ## ## ## ## ## ## ##
9-
# ## ## ## ## ## ## ##
10-
# ## ## ## ###### ## ## ##
11-
# ## ## ## ## ## ## ##
12-
# ## ## ## ## ## ## ##
7+
# ## ## ######## #### #######
8+
# ## ## ## ## ## ## ##
9+
# ## ## ## ## ## ## ##
10+
# ## ## ## ###### ## ## ##
11+
# ## ## ## ## ## ## ##
12+
# ## ## ## ## ## ## ##
1313
# ### ### ######## #### #######
1414
#
15-
# Web Of Things Platform
15+
# Web Of Things Platform
1616
#
1717
# This file is part of WEIO and is published under BSD license.
1818
#
@@ -41,7 +41,7 @@
4141
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4242
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4343
#
44-
# Authors :
44+
# Authors :
4545
# Uros PETREVSKI <[email protected]>
4646
# Drasko DRASKOVIC <[email protected]>
4747
#
@@ -50,17 +50,18 @@
5050
import multiprocessing
5151
import ctypes
5252

53-
5453
# Pins declared by the user program
5554
DECLARED_PINS = None
5655

5756
# If serial connection is opened for coprocessor
5857
WEIO_SERIAL_LINKED = False
5958

6059
# Connected clients
61-
weioConnectionSet = set()
6260
weioConnections = {}
6361

62+
# Initialize with multiprocessing.Manager().list in weioRunner
63+
weioConnUuids = None
64+
6465
# Running flag
6566
running = multiprocessing.Value(ctypes.c_bool, False)
6667

weioLib/weioUserApi.py

+34
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ def __init__(self, info, connection):
145145
self.info = info
146146
self.connection = connection
147147

148+
class WeioServerMsg():
149+
def __init__(self, qout, msg):
150+
# Create userAgentMessage and send it to the launcher process
151+
self.qout = qout
152+
self.msg = msg
153+
154+
def send(self, callback, data, connUuid):
155+
self.msg.connUuid = connUuid
156+
self.msg.req = "serverPush"
157+
self.msg.res = data
158+
self.msg.callbackJS = callback
159+
160+
# Send message to launcher process
161+
self.qout.put(self.msg)
162+
163+
def broadcast(self, callback, data):
164+
self.msg.connUuid = "all"
165+
self.msg.req = "serverPush"
166+
self.msg.res = data
167+
self.msg.callbackJS = callback
168+
169+
# Send message to launcher process
170+
self.qout.put(self.msg)
171+
172+
def serverPush(callback, data):
173+
weioServerMsg.broadcast(callback, data)
174+
175+
148176
###
149177
# Global instances
150178
###
@@ -154,5 +182,11 @@ def __init__(self, info, connection):
154182
# Global shared dict
155183
sharedVar = None
156184

185+
# Global connections
186+
weioConns = None
187+
157188
# Global WeIO gpio object
158189
gpio = None
190+
191+
# serverPush variable
192+
weioServerMsg = None

weioRunner.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ def launcher(self):
277277
while (True):
278278
# Get the command from userTornado (blocking)
279279
msg = self.qIn.get()
280-
281280
#print "*** GOT THE COMMAND: ", msg.req
282-
283281
# Execute the command
284282
msg.res = None
285283
if msg.req in weioParser.weioSpells or msg.req in weioParser.weioUserSpells:
@@ -315,17 +313,25 @@ def listenerThread():
315313
#print "GOT MSG: ", msg
316314
if (msg.res is not None):
317315
#print "MESSAGE", msg.req, msg.res, msg.data, msg.connUuid
318-
if (msg.connUuid in weioRunnerGlobals.weioConnections):
316+
if (msg.connUuid in weioRunnerGlobals.weioConnections or msg.connUuid == "all"):
319317
result = {}
320318

321319
if (msg.callbackJS is not None):
322320
result["serverPush"] = msg.callbackJS
323321
result["data"] = msg.res
324322
#print "RESULT",result
325323
if (weioRunnerGlobals.remoteConnected.value == True):
326-
weioRunnerGlobals.weioConnections[msg.connUuid].write_message(json.dumps(result))
324+
if (msg.connUuid == "all"):
325+
for connUuid, conn in weioRunnerGlobals.weioConnections.iteritems():
326+
weioRunnerGlobals.weioConnections[connUuid].send(json.dumps(result))
327+
else:
328+
weioRunnerGlobals.weioConnections[msg.connUuid].write_message(json.dumps(result))
327329
else:
328-
weioRunnerGlobals.weioConnections[msg.connUuid].send(json.dumps(result))
330+
if (msg.connUuid == "all"):
331+
for connUuid, conn in weioRunnerGlobals.weioConnections.iteritems():
332+
weioRunnerGlobals.weioConnections[connUuid].send(json.dumps(result))
333+
else:
334+
weioRunnerGlobals.weioConnections[msg.connUuid].send(json.dumps(result))
329335

330336
class WeioRemote():
331337
conn = None
@@ -391,6 +397,13 @@ def close(self):
391397
manager = multiprocessing.Manager()
392398
weioUserApi.sharedVar = manager.dict()
393399

400+
# weioMsg
401+
weioUserApi.weioServerMsg = weioUserApi.WeioServerMsg(weioRunnerGlobals.QIN, weioRunnerGlobals.userAgentMessage())
402+
403+
# weioConnUuids
404+
weioRunnerGlobals.weioConnUuids = manager.list()
405+
weioUserApi.weioConns = weioRunnerGlobals.weioConnUuids
406+
394407
confFile = weioConfig.getConfiguration()
395408
# set python working directory
396409
#os.chdir("userFiles/"+sys.argv[1])

0 commit comments

Comments
 (0)