2 import asyncore
, socket
4 class Node(asyncore
.dispatcher
):
5 def __init__(self
, host
, coords
, process
=lambda self
, val
: None):
8 asyncore
.dispatcher
.__init
__(self
)
9 self
.create_socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
10 self
.socket
.setblocking(1)
12 self
.socket
.setblocking(0)
14 def handle_connect(self
):
15 self
.send("%i:%i\r\n" % self
.coords
)
16 data
= self
.recv(4096)
17 if data
!= "okay\r\n":
19 self
.writable
= lambda : False
21 def handle_read(self
):
22 data
= self
.recv(4096)
25 elif data
== 'off\r\n':
28 def handle_close(self
):
32 if __name__
== '__main__':
40 from functools
import partial
42 class MyParallel(parallel
.Parallel
):
45 def setData(self
, data
):
47 super(MyParallel
, self
).setData(data
)
49 def setPin(self
, pin
, val
):
50 self
.setData((bool(val
) << pin
) | self
._data
)
53 processor
= lambda n
: partial(port
.setPin
, n
)
55 except ImportError, OSError:
57 print sys
.exc_info()[1]
64 port
= MyParallel(self
)
66 pin
, _
, xy
= i
.partition('=')
67 x
, _
, y
= xy
.partition(':')
68 nodes
.append(Node(host
, (int(x
), int(y
)), process
=processor(pin
)))
72 nodes
.append(Node(host
, (x
, y
), process
=processor(x
+ 8 * y
)))