Implemented playlist, removed version from node protocol.
[blynken.git] / node / node.py
blob0c2cc43c7d25ff272fbc104e86cd8670c1f81868
1 #!/usr/bin/env python
2 import asyncore, socket
4 class Node(asyncore.dispatcher):
5 def __init__(self, host, coords):
6 self.coords = coords
7 asyncore.dispatcher.__init__(self)
8 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
9 self.socket.setblocking(1)
10 self.connect(host)
11 self.socket.setblocking(0)
13 def handle_connect(self):
14 self.send("%i:%i\r\n" % self.coords)
15 data = self.recv(4096)
16 if data != "okay\r\n":
17 raise RuntimeError
18 self.writable = lambda : False
20 def handle_read(self):
21 data = self.recv(4096)
22 if data == 'on\r\n':
23 self.process(True)
24 elif data == 'off\r\n':
25 self.process(False)
27 def handle_close(self):
28 pass
30 def process(self, val):
31 print self.coords, val
34 if __name__ == '__main__':
35 from sys import argv
37 host = ('', 10000)
38 nodes = []
40 if len(argv) > 1:
41 for i in argv[1:]:
42 x, y = i.split(':')
43 nodes.append(Node(host, (int(x), int(y))))
44 else:
45 for x in range(8):
46 for y in range(6):
47 nodes.append(Node(host, (x, y)))
49 asyncore.loop()