-
Notifications
You must be signed in to change notification settings - Fork 11
/
txDevITelexCommon.py
305 lines (240 loc) · 9.37 KB
/
txDevITelexCommon.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/python3
"""
Telex Device - i-Telex Common Routines in Client and Server
"""
__author__ = "Jochen Krapf"
__email__ = "[email protected]"
__copyright__ = "Copyright 2018, JK"
__license__ = "GPL3"
__version__ = "0.0.1"
from threading import Thread
import socket
import time
import txCode
import txBase
import log
#######
def LOG(text:str, level:int=3):
log.LOG('\033[5;30;44m<'+text+'>\033[0m', level)
class TelexITelexCommon(txBase.TelexBase):
def __init__(self):
super().__init__()
self._rx_buffer = []
self._tx_buffer = []
self._connected = False
def __del__(self):
self.exit()
super().__del__()
def exit(self):
self._connected = False
# =====
def disconnect_client(self):
self._tx_buffer = []
self._connected = False
# =====
def process_connection(self, s:socket.socket, is_server:bool, is_ascii:bool): # Takes client socket as argument.
"""Handles a client or server connection."""
bmc = txCode.BaudotMurrayCode(False, False, True)
sent_counter = 0
received_counter = 0
timeout_counter = -1
time_next_send = None
s.settimeout(0.2)
self._connected = True
while self._connected:
try:
data = s.recv(1)
# lost connection
if not data:
break
# Telnet control sequence
elif data[0] == 255:
d = s.recv(2) # skip next 2 bytes from telnet command
# i-Telex packet
elif data[0] < 10:
packet_error = False
d = s.recv(1)
data += d
packet_len = d[0]
if packet_len:
data += s.recv(packet_len)
# Heartbeat
if data[0] == 0 and packet_len == 0:
#LOG('Heartbeat '+repr(data), 4)
pass
# Direct Dial
elif data[0] == 1 and (1 <= packet_len <= 10):
LOG('Direct Dial '+repr(data), 4)
if packet_len >= 5:
id = (data[3] << 24) | (data[4] << 16) | (data[5] << 8) | (data[6])
print(id)
self._rx_buffer.append('\x1bD'+str(data[2]))
self.send_ack(s, received_counter)
# Baudot Data
elif data[0] == 2 and packet_len >= 1 and packet_len <= 50:
#LOG('Baudot data '+repr(data), 4)
aa = bmc.decodeBM2A(data[2:])
for a in aa:
if a == '@':
a = '#'
self._rx_buffer.append(a)
received_counter += len(data[2:])
self.send_ack(s, received_counter)
# End
elif data[0] == 3 and packet_len == 0:
LOG('End '+repr(data), 4)
break
# Reject
elif data[0] == 4 and packet_len <= 20:
LOG('Reject '+repr(data), 4)
aa = bmc.translate(data[2:])
for a in aa:
self._rx_buffer.append(a)
break
# Acknowledge
elif data[0] == 6 and packet_len == 1:
#LOG('Acknowledge '+repr(data), 4)
unprinted = (sent_counter - int(data[2])) & 0xFF
#if unprinted < 0:
# unprinted += 256
LOG(str(data[2])+'/'+str(sent_counter)+'='+str(unprinted), 4)
if unprinted < 7: # about 1 sec
time_next_send = None
else:
time_next_send = time.time() + (unprinted-6)*0.15
pass
# Version
elif data[0] == 7 and packet_len >= 1 and packet_len <= 20:
#LOG('Version '+repr(data), 4)
if not is_server or data[2] != 1:
self.send_version(s)
# Self test
elif data[0] == 8 and packet_len >= 2:
LOG('Self test '+repr(data), 4)
pass
# Remote config
elif data[0] == 9 and packet_len >= 3:
LOG('Remote config '+repr(data), 4)
pass
# Wrong packet - will resync at next socket.timeout
else:
LOG('ERROR Packet '+repr(data), 3)
packet_error = True
if not packet_error:
is_ascii = False
# ASCII character(s)
else:
#LOG('Other', repr(data), 4)
is_ascii = True
data = data.decode('ASCII', errors='ignore').upper()
data = txCode.BaudotMurrayCode.translate(data)
for a in data:
if a == '@':
a = '#'
self._rx_buffer.append(a)
received_counter += 1
except socket.timeout:
#LOG('.', 4)
if is_ascii is not None: # unknown if ASCII or baudot
timeout_counter += 1
if is_server and timeout_counter == 1:
self._tx_buffer = []
self.send_welcome(s)
if is_ascii:
if self._tx_buffer:
sent = self.send_data_ascii(s)
sent_counter += sent
else: # baudot
if (timeout_counter % 5) == 0: # every 1 sec
self.send_ack(s, received_counter)
if self._tx_buffer:
if time_next_send and time.time() < time_next_send:
LOG('Wait'+str(int(time_next_send-time.time())), 4)
pass
else:
sent = self.send_data_baudot(s, bmc)
sent_counter += sent
if sent > 7:
time_next_send = time.time() + (sent-6)*0.15
elif (timeout_counter % 15) == 0: # every 3 sec
self.send_heartbeat(s)
except socket.error:
LOG('ERROR socket', 2)
break
if not is_ascii:
self.send_end(s)
time.sleep(1)
LOG('end connection', 3)
self._connected = False
def send_heartbeat(self, s):
'''Send heartbeat packet (0)'''
data = bytearray([0, 0])
s.sendall(data)
def send_ack(self, s, received:int):
'''Send acknowlage packet (6)'''
data = bytearray([6, 1, received & 0xff])
s.sendall(data)
def send_version(self, s):
'''Send version packet (7)'''
send = bytearray([7, 1, 1])
s.sendall(send)
def send_direct_dial(self, s, dial:str, id):
'''Send direct dial packet (1)'''
data = bytearray([1, 5]) # Direct Dial
if not dial.isnumeric():
number = 0
elif len(dial) == 2:
number = int(dial)
if number == 0:
number = 100
elif len(dial) == 1:
number = int(dial) + 100
if number == 100:
number = 110
else:
number = 0
data.append(number) # direct dial number
data.append(1) # id, 32 bit
data.append(2)
data.append(3)
data.append(4)
s.sendall(data)
def send_data_ascii(self, s):
'''Send ASCII data direct'''
a = ''
while self._tx_buffer and len(a) < 250:
b = self._tx_buffer.pop(0)
if b not in '<>~%':
a += b
data = a.encode('ASCII')
s.sendall(data)
return len(data)
def send_data_baudot(self, s, bmc):
'''Send baudot data packet (2)'''
data = bytearray([2, 0])
while self._tx_buffer and len(data) < 42:
a = self._tx_buffer.pop(0)
bb = bmc.encodeA2BM(a)
if bb:
for b in bb:
data.append(b)
l = len(data) - 2
data[1] = l
s.sendall(data)
return l
def send_end(self, s):
'''Send end packet (3)'''
send = bytearray([3, 0]) # End
s.sendall(send)
def send_reject(self, s):
'''Send reject packet (4)'''
send = bytearray([4, 3, ord('o'), ord('c'), ord('c')]) # Reject
s.sendall(send)
def send_welcome(self, s):
'''Send welcome message indirect as a server'''
#self._tx_buffer.extend(list('<<<\r\n')) # send text
#self._rx_buffer.append('\x1bT')
#self._rx_buffer.append('#')
#self._rx_buffer.append('@')
self._rx_buffer.append('\x1bI')
#######