-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbridge.py
99 lines (83 loc) · 3.48 KB
/
bridge.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json, socket, subprocess, sys, configparser, jackserver
UDP_PORT = 10000
TCP_PORT = 10001
class Bridge:
'''Bridge class is responsible for handling network communication in the app'''
def __init__(self):
self.TCP_CONN = False
self.c = None
self.addr = None
def backend(self,s,sock):
"""
Handle the network communications between the GUI and this
application.
Begins by initiating a UDP server to listen for connections to
send to a TCP server for reliable communications.
s -- UDP Socket information sent by the main process.
sock -- TCP Socket information sent by the main process.
Return the response sent to the GUI by the server.
"""
response = None
# Receive 1 byte message and send back to frontend
# If there is already a connection, don't accept another one.
if not self.TCP_CONN:
try:
data, wherefrom = s.recvfrom(1024)
if data == "1":
print("Got a request")
s.sendto(str(TCP_PORT), wherefrom)
except socket.error:
pass
#TCP socket listens for frontend TCP socket to be created
#then initiates TCP server that receieves JSON data
if not self.TCP_CONN:
sock.listen(1)
try:
self.c, self.addr = sock.accept()
if self.c is not None:
self.TCP_CONN = True
self.c.setblocking(0)
print("Accepted connection from: " + str(self.addr))
except socket.error:
return
# If there is a connection, see if there is data to be read.
if self.TCP_CONN:
try:
data = self.c.recv(1024)
print("Received data!" + data)
# There is data to be read - see what the request is.
parsed_data = configparser.parse_json_data(data)
response = self.respond_to_intent(parsed_data)
# Send back the result of the response.
self.c.send(response[1] + "\n")
except socket.error as error:
pass
except ValueError:
self.TCP_CONN = False
pass
return response
def respond_to_intent(self, parsed_data):
"""
Determine what action the GUI intends to perform.
Reads the 'intent' data from the received data (as JSON) and
performs the intended process dependent on the result of 'intent'
parsed_data -- The data entire data message sent by the GUI
Return the result of the actions performed to be sent to the GUI.
"""
intent = parsed_data.pop('intent', None)
if intent == 'EFFECT':
# The intent wants to add new effects.
configparser.update_config_file(parsed_data)
return (intent, intent)
elif intent == 'REQPORT':
# Intent wants to see what audio ports are available.
ports = {}
ports['input'] = jackserver.get_clean_inports()
ports['output'] = jackserver.get_clean_outports()
ports_str = json.dumps(ports)
return (intent, ports_str)
elif intent == 'UPDATEPORT':
# Intent wants to set new audio ports (playback/capture)
return (intent, parsed_data['in'], parsed_data['out'])
else:
return ()