-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
98 lines (81 loc) · 2.81 KB
/
client.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
import socket
import time
import json
from queue import Queue
from threading import Thread
class CommunicationClient():
def __init__(self, port=12002, host_ip='127.0.0.1'):
self.port = port
self.host_ip = host_ip
self.__communicat_queue = Queue()
self.__init_socket(port = self.port, host_ip = self.host_ip)
self.__communicate_thread = Thread(
target = self.__communicate,
args=())
self.__communicate_thread.daemon = True
self.__communicate_thread.start()
def __init_socket(self, host_ip, port):
try:
self.socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
print('Socket error: %s' % err)
# connecting to the server
try:
self.socket_client.connect((host_ip, port))
print ("the socket has successfully connected")
except:
pass
def readline(self):
return self.__communicat_queue.get()
def isHasNewData(self):
return not self.__communicat_queue.empty()
def __handle_connection_receive_data(self, data_line):
self.__communicat_queue.put(data_line)
def sendline(self, data):
try:
data = self.__json_output(data)
self.__sendline_socket(data)
except:
print('not connected socketserver, wait msg')
self.__init_socket(self.host_ip, self.port)
def __readline_socket(self):
data = self.conn.recv(1)
data= data.decode()
if data != "\n":
self.__data_line += data
return None
else:
ret = self.__data_line
self.__data_line = ''
return ret
def __sendline_socket(self, data):
data = str(data)+"\r\n" #"\r\n" : end of line
encoded_data = data.encode('utf-8')
print(encoded_data)
self.socket_client.send(encoded_data)
def __json_output(self, send_string={}):
data_json = json.dumps(send_string)
return data_json
def __receiveAlways(self):
message = ''
while True:
try:
data = self.socket_client.recv(1024).decode()
data = json.loads(data) #str to dict
self.__handle_received_message_callback(data)
except:
pass
def __handle_connection(self):
while True:
try:
data_line = self.__readline_socket()
if data_line != None:
self.__handle_connection_receive_data(data_line)
except:
pass
def __communicate(self):
while True:
print("Receving data")
self.__handle_connection()
print("Done reiceive data")
time.sleep(0.1)