client_channel.py
3.65 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
#!/usr/bin/env python3
"""Socket Client (abstract)
To be used as a base class (interface) for any concrete socket client class
"""
# Standard library imports
# Third party imports
# None
# Local application imports
from devices_controller.logs import *
##class SocketClientAbstract():
class ClientChannel():
my_channel = None
buf = 1024
def __init__(self, server_host:str="localhost", server_port:int=11110, PROTOCOL:str="SOCKET-TCP", buffer_size=1024, DEBUG=False):
'''
:param server_host: server IP or hostname
:param server_port: server port
:param PROTOCOL: "SOCKET-TCP" or "SOCKET-UDP" or "SERIAL" or "USB" (to be continued...)
'''
self.DEBUG = DEBUG
self.HOST = server_host
self.PORT = server_port
self.PROTOCOL = PROTOCOL
self.buf = buffer_size
# Logger configuration
#self.set_logger()
##set_logger(DEBUG)
log_d("\n**************************")
log_d("Client CHANNEL instanciated")
# So that we can use this with the "with" statement (context manager)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
#log_d("Client channel killed")
print("Client channel killed")
def send_data(self, data:str):
##data_encapsulated = self.encapsulate_data_to_send(data)
self._send_data(data)
'''
#data_to_send = bytes(data_to_send + "\n", "utf-8")
#data_to_send = bytes(data_to_send + "\x00", "utf-8")
##data_to_send = bytes(data_to_send, "utf-8")
data_to_send_bytes = data_to_send.encode("utf-8")
#data_to_send_bytes = data_to_send.encode()
if self.PROTOCOL=="SOCKET-TCP":
# Unlike send(), this method sendall() continues to send data from bytes until either all data has been sent or an error occurs.
# None is returned on success.
# On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent
nb_bytes_sent = self.mysock.sendall(data_to_send_bytes)
else:
# SOCKET-UDP
nb_bytes_sent = self.mysock.sendto(data_to_send_bytes, (self.HOST, self.PORT))
log_i(f'Sent: {data_to_send_bytes}')
log_i(f"Sent {nb_bytes_sent} bytes")
'''
#@abstract
def _send_data(self, data_to_send:str):
pass
def receive_data(self)->str:
data_received = self._receive_data()
##data_received_bytes = self.mysock.recv(self.buf)
#log_d("Received (all data): {}".format(data_received))
#log_d("data in bytes: "+str(bytes(data_received, "utf-8")))
##data_received_uncaped = self.uncap_received_data(data_received)
#log_i("RECEIVED (useful data): {}".format(data_received))
return data_received
#@abstract
def _receive_data(self)->bytes:
pass
'''
data_received = self.mysock.recv(self.buf)
#log_d("data type is "+str(type(data_received)))
log_i(f"RECEIVED (ALL BYTES): {data_received}")
#return str(data_received, "utf-8")
return data_received
'''
#@abstract
def _connect_to_server(self):
pass
# Close socket
def close(self):
self.mysock.close()
'''
put(), read(), put_read() commands
'''
def put(self, data:str):
self.send_data(data)
def read(self)->str:
return self.receive_data()
def put_read(self, data:str)->str:
# send command
self.put(data)
# receive answer (or ack)
return self.read()