forked from mod-audio/mod-ui
-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocol.py
162 lines (138 loc) · 4.96 KB
/
protocol.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
# Copyright 2012-2013 AGR Audio, Industria e Comercio LTDA. <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class ProtocolError(Exception):
ERRORS = {
"-1" : "ERR_INSTANCE_INVALID",
"-2" : "ERR_INSTANCE_ALREADY_EXISTS",
"-3" : "ERR_INSTANCE_NON_EXISTS",
"-101": "ERR_LV2_INVALID_URI",
"-102": "ERR_LILV_INSTANTIATION",
"-103": "ERR_LV2_INVALID_PARAM_SYMBOL",
"-201": "ERR_JACK_CLIENT_CREATION",
"-202": "ERR_JACK_CLIENT_ACTIVATION",
"-203": "ERR_JACK_CLIENT_DEACTIVATION",
"-204": "ERR_JACK_PORT_REGISTER",
"-205": "ERR_JACK_PORT_CONNECTION",
"-206": "ERR_JACK_PORT_DISCONNECTION",
"-301": "ERR_MEMORY_ALLOCATION",
"not found": "ERR_CMD_NOT_FOUND",
"wrong arg type": "ERR_INVALID_ARGUMENTS",
"few arguments": "ERR_FEW_ARGUMENTS",
"many arguments": "ERR_MANY_ARGUMENTS",
"finish": "ERR_FINISH",
}
def __init__(self, err=""):
self.err = err
super(ProtocolError, self).__init__(self.ERRORS.get(err.replace("\0", ""), "ERR_UNKNOWN"))
def error_code(self):
try:
return "resp %d" % int(self.err)
except ValueError:
return self.err
def process_resp(resp, datatype):
if resp is None:
if datatype == 'boolean':
return False
if datatype == 'int':
return 0
if datatype == 'float_structure':
return { 'ok': False }
if datatype == 'string':
return ""
return None
if datatype == 'float_structure':
# resp is first an int representing status
# then the float
resps = resp.split()
resp = { 'ok': int(resps[0]) >= 0 }
try:
resp['value'] = float(resps[1])
except IndexError:
resp['ok'] = False
elif datatype == 'string':
# resp is a simple string, just pass it direcly
pass
else:
try:
resp = int(resp)
except ValueError:
resp = None
if datatype == 'boolean' and resp is not None:
resp = resp >= 0
return resp
class Protocol(object):
COMMANDS = {
"banks": [],
"pedalboards": [int],
"pedalboard": [int, str],
"hw_con": [int, int],
"hw_dis": [int, int],
"control_set": [int, str, float],
"control_get": [int, str],
"control_next": [int, int, int, int],
"tuner": [str],
"tuner_input": [int],
"pedalboard_save": [],
"pedalboard_reset": [],
"jack_cpu_load": [],
}
COMMANDS_FUNC = {}
RESPONSES = [
"resp", "few aguments", "many arguments", "not found"
]
@classmethod
def register_cmd_callback(cls, cmd, func):
if cmd not in cls.COMMANDS.keys():
raise ValueError("Command %s is not registered" % cmd)
cls.COMMANDS_FUNC[cmd] = func
def __init__(self, msg):
self.msg = msg.replace("\0", "").strip()
self.cmd = ""
self.args = []
self.parse()
def is_resp(self):
return any(self.msg.startswith(resp) for resp in self.RESPONSES)
def run_cmd(self, callback):
if not self.cmd:
callback("-1003") # TODO: proper error handling
return
cmd = self.COMMANDS_FUNC.get(self.cmd, None)
if cmd is None:
callback("-1003") # TODO: proper error handling
return
if len(self.args) != len(self.COMMANDS[self.cmd]):
callback("-1003") # TODO: proper error handling
return
args = self.args + [callback]
cmd(*args)
def process_resp(self, datatype):
if "resp" in self.msg:
resp = self.msg.replace("resp ", "")
return process_resp(resp, datatype)
return self.msg
def parse(self):
if self.is_resp():
return
cmd = self.msg.split()
if not cmd or cmd[0] not in self.COMMANDS.keys():
raise ProtocolError("not found") # Command not found
try:
self.cmd = cmd[0]
self.args = [ typ(arg) for typ, arg in zip(self.COMMANDS[self.cmd], cmd[1:]) ]
if not all(str(a) for a in self.args):
raise ValueError
except ValueError:
raise ProtocolError("wrong arg type for: %s %s" % (self.cmd, self.args))