-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
283 lines (227 loc) · 8.82 KB
/
Copy pathclient.py
File metadata and controls
283 lines (227 loc) · 8.82 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""AWB client implementation for python programs.
This contains both the client layer for communicating to AWB applications and
the general code for doing simple protobuf RPCs in python. The general
protobuf RPC code should eventually be refactored out.
"""
from google.protobuf.message import Message
from midi import Track
from midifile import Reader as MidiReader, Writer as MidiWriter
from spug.io.reactor import Address, INETAddress
from spug.io.proactor import getProactor, DataHandler, INETAddress
from threading import Condition
from typing import Callable, Dict, Tuple, Type
from awb_pb2 import GetMidiRequest, GetMidiResponse, RPCMessage, \
PutMidiRequest
class NotEnoughData(Exception):
"""Raised by deserialization functions when the buffer does not contain
enough data to deserialize a complete object.
"""
pass
class RemoteError(Exception):
"""Raised with an error message sent from the remote system."""
pass
def read_proto_varint(data: bytes, pos: int) -> Tuple[int, int]:
"""Returns the integer and the index of the byte after the encoded integer
in 'data'.
Throws NotEnoughData() if bytes isn't long enough to contain a complete
varint.
"""
b = 0x80
val = 0
offset = 0
while b & 0x80:
# make sure we've got data
if pos >= len(data):
raise NotEnoughData()
# see if we've got the last byte
b = data[pos]
pos += 1
val = val | ((b & 0x7f) << offset);
offset += 7;
lastUInt = val
return lastUInt, pos
def encode_proto_varint(val: int) -> bytes:
if not val:
return b'\0'
buf = []
while val:
b = val & 0x7f
val >>= 7
if val:
b |= 0x80;
buf.append(b);
return bytes(buf)
def read_proto_string(data: bytes, pos: int) -> Tuple[bytes, int]:
size, i = read_proto_varint(data, pos)
pos = i + size
if len(data) <= pos:
return data[i:pos], pos
else:
raise NotEnoughData()
class RPCContext:
def __init__(self, handler: 'RPCHandler') -> None:
self.handler = handler
def send_response(self, id: int, response: bytes) -> None:
self.handler.serialize(RPCMessage(id=id, response=response))
def send_error(self, id: int, error: bytes) -> None:
self.handler.serialize(RPCMessage(id=id, error=error))
class MethodWrapper:
def __init__(self, func: Callable[[RPCContext, RPCMessage], RPCMessage],
request_type: Type,
response_type: Type) -> None:
self.__func = func
self.__request_type = request_type
self.__response_Type = response_type
def __call__(self, context: RPCContext, request: RPCMessage) -> RPCMessage:
req = self.__request_type()
req.MergeFromString(request.request)
try:
resp = self.__func(context, req)
context.send_response(request.id, resp.SerializeToString())
except Exception as ex:
context.send_error(request.id, str(ex))
# Method objects should accept a context and the original RPCMessage (whose
# "request" field they are responsible for deserializing to the appropriate
# native type). They are also responsible for sending a response back
# through the RPCContext object.
MethodDict = Dict[bytes, Callable[[RPCContext, RPCMessage], None]]
class RPCHandler(DataHandler):
"""Communications thread."""
__outBuf : bytes
__inBuf : bytes
def __init__(self, methods: MethodDict):
self.__outBuf = b''
self.__inBuf = b''
self.__close_flag = False
self.__methods = methods
self.__waiters : Dict[int, Callable[[RPCMessage], None]] = {}
self.__last_id = 0
def readyToGet(self) -> bool:
return bool(self.__outBuf)
def readyToPut(self) -> bool:
return True
def readyToClose(self) -> bool:
return self.__close_flag
def peek(self, size: int) -> bytes:
return self.__outBuf[:size]
def get(self, size: int) -> None:
self.__outBuf = self.__outBuf[size:]
def put(self, data: bytes) -> None:
self.__inBuf += data
self.process()
def process(self):
while self.__inBuf:
try:
message, index = read_proto_string(self.__inBuf, 0)
except NotEnoughData as ex:
return
self.__inBuf = self.__inBuf[index:]
rpc_message = RPCMessage()
rpc_message.MergeFromString(message)
if rpc_message.method:
# This is a request.
method = self.__methods.get(rpc_message.method)
if method is None:
print(f'Unknown method {rpc_message.method} received')
self.serialize(RPCMessage(
id=rpc_message.id,
error=f'Unknown method {rpc_message.method} received'
))
continue
method(self.__context, rpc_message)
else:
# This is a response.
waiter = self.__waiters.get(rpc_message.id)
if waiter is None:
print(f'Got response for message id {rpc_message.id}, '
'which is not waiting for a response.')
continue
waiter(rpc_message)
def write(self, data: bytes) -> None:
self.__outBuf += data
def serialize(self, message: RPCMessage) -> None:
data = message.SerializeToString()
self.write(encode_proto_varint(len(data)) + data)
def __get_next_id(self):
self.__last_id += 1
return self.__last_id
def send(self, method: str, request: Message,
waiter: Callable[[RPCMessage], None]) -> None:
"""Send a method invocation message to the peer."""
message = RPCMessage(id=self.__get_next_id(), method=method,
request=request.SerializeToString()
)
self.__waiters[message.id] = waiter
self.serialize(message)
class Waiter:
"""A callable that allows another thread to wait for an RPC response."""
def __init__(self):
self.__error = None
self.__response = None
self.__cond = Condition()
def getResponse(self) -> Message:
"""Wait on a reply message and return the response object.
Throws a RemoteError if an error response was sent.
"""
with self.__cond:
self.__cond.wait()
if self.__error:
raise RemoteError(self.__error)
return self.__response
def __call__(self, message: RPCMessage) -> None:
with self.__cond:
self.__error = message.error
self.__response = message.response
self.__cond.notify()
class AWBProxy:
"""A proxy object for a remote instance implementing the AWB API."""
def __init__(self, address: Address) -> None:
self.__handler = RPCHandler({})
proactor = getProactor()
self.__conn = proactor.makeConnection(address, self.__handler)
self.__control = proactor.makeControlQueue(
lambda info: self.__handler.send(*info)
)
def __send(self, method: str, request: Message,
waiter: Callable[[RPCMessage], None]
) -> None:
self.__control.add((method, request, waiter))
def getMidi(self, name: bytes) -> Track:
waiter = Waiter()
self.__send('getMidiTrack', GetMidiRequest(name=name), waiter)
response = GetMidiResponse()
response.MergeFromString(waiter.getResponse())
return MidiReader(None).parseTrack(response.contents, response.name)
def putMidi(self, name: bytes, track: Track) -> None:
track_data = MidiWriter(None).encodeEvents(track)
waiter = Waiter()
self.__send('putMidiTrack',
PutMidiRequest(name=name, contents=track_data),
waiter
)
return waiter.getResponse()
if __name__ == '__main__':
from midi import NoteOn, NoteOff
from midifile import EndTrack
from threading import Thread
class MyEndTrack(EndTrack):
def asMidiString(self, status: int):
return 0, b'\xff\x2f\0'
# This has to happen before we start running the proactor otherwise the
# proactor will quit since it has no objects.
proxy = AWBProxy(INETAddress('127.0.0.1', 9021))
proactor_thread = Thread(target=lambda: getProactor().run())
proactor_thread.start()
track = Track('Foo', [
NoteOn(0, 0, 40, 100),
NoteOn(0, 0, 44, 100),
NoteOn(0, 0, 47, 100),
NoteOff(2880, 0, 40, 100),
NoteOff(2880, 0, 44, 100),
NoteOff(2880, 0, 47, 100),
MyEndTrack(5760)
])
print(track[0])
proxy.getMidi(b'Metronome')
proxy.putMidi(b'Foo', track)
print('all done')