-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
67 lines (55 loc) · 2.14 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# https://stackoverflow.com/questions/24564587/communicate-between-a-processing-sketch-and-a-python-program
import socket
import logging
log = logging.getLogger()
class Server:
def __init__(self, host='', port=5204):
self.host = host
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port))
self.socket.listen(5)
self.conn, self.addr = self.socket.accept()
log.info("Socket to communicate with processing started")
def barrier_triggered(self):
try:
self.conn.send(b"t")
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.barrier_triggered()
def new_receiver(self, address_id):
try:
self.conn.send(str(address_id).encode())
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.new_receiver(address_id)
def start(self):
try:
self.conn.send(b"s")
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.start()
def payment_received(self):
try:
self.conn.send(b"p")
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.payment_received()
def payment_missing(self):
try:
self.conn.send(b"m")
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.payment_missing()
def end(self):
try:
self.conn.close()
except BrokenPipeError as e:
log.error("Connection to Socket died with error %s" % e)
self.conn, self.addr = self.socket.accept()
self.end()