Skip to content

Commit

Permalink
Master/Slave control working
Browse files Browse the repository at this point in the history
The threads now broadcast to the is one has take the master
Closes #20
  • Loading branch information
joewashear007 committed Feb 14, 2014
1 parent bfdf31f commit 99f7218
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ def handle_message(self, msg):
if msg_data["MASTER_REQUEST"]:
WebSocketHandler.lock_id = threading.current_thread().ident
self.send_json(dict(MASTER_STATUS=True));
print("Locking to thread: " ,WebSocketHandler.lock_id, " : ", threading.current_thread().ident)
print("Locking to thread: " ,WebSocketHandler.lock_id, " : ", self.id)
self.broadcast_all(dict(SLAVE=True))
else:
WebSocketHandler.lock_id = None
self.send_json(dict(MASTER_STATUS=False));
self.send_json(dict(MASTER_STATUS=False))
self.broadcast_all(dict(SLAVE=False))
#elif "MESSAGE" in msg_data:
# self.on_message(msg["MESSAGE"])
#else:
Expand All @@ -71,14 +73,23 @@ def send_message(self, message):
def send_json(self, data):
#sends a python dict as a json object
self.request.sendall(self._pack(json.dumps(data)))

def broadcast_all(self, data):
#send a araay converted into JSON to every thread
for t in WebSocketHandler.connections:
if t.id == self.id:
continue
t.send_json(data)

#-------------------------------------------------------------------

magic = b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
lock_id = None
connections = []

def _hasLock(self):
#there is no lock or the current thread has it
return (not WebSocketHandler.lock_id) or (WebSocketHandler.lock_id == threading.current_thread().ident)
return (not WebSocketHandler.lock_id) or (WebSocketHandler.lock_id == self.id)



Expand All @@ -87,8 +98,10 @@ def setup(self):
#Init some varibles
print("\nConnection Established", self.client_address)
self.closeHandle = False
self.id = threading.current_thread().ident
self.alive = threading.Event()
self.alive.set()
WebSocketHandler.connections.append(self)

def handle(self):
#handles the handshake with the server
Expand Down Expand Up @@ -116,6 +129,9 @@ def run(self):
for char in self.request.recv(length):
decoded += chr(char ^ masks[len(decoded) % 4])
self.handle_message(decoded)

#WebSocketHandler.broadcast_all.wait(0.01)

self.close()

def close(self, message="Cxn Closed"):
Expand Down
Binary file modified test.blend
Binary file not shown.

0 comments on commit 99f7218

Please sign in to comment.