-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
118 lines (94 loc) · 3.33 KB
/
server.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
import socket
import time
import json
from queue import Queue
from threading import Thread
class CommunicationServer():
def __init__(self, host_ip = 12002):
self.host_ip = host_ip
self.__data_line = ''
self.conn_status = 'not_connected'
self.__init_socket(self.host_ip)
self.__communicat_queue = Queue()
self.__communicate_thread = Thread(
target = self.__communicate,
args=())
self.__communicate_thread.daemon = True
self.__communicate_thread.start()
def __init_socket(self, host_ip):
self.conn = None
self.addr = None
self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.serversocket.setblocking(0)
self.server_address=('', host_ip)
self.serversocket.bind(self.server_address)
self.prev_conn_status = self.conn_status
self.serversocket.listen(10)
print('done init socket')
def get_conn_status(self):
return self.conn_status
def server_socket_close(self):
self.serversocket.shutdown(socket.SHUT_RDWR)
self.serversocket.close()
def sendline(self, data):
try:
data = self.__json_output(data)
self.__sendline_socket(data)
return True
except:
self.__handle_connection_failure()
return False
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 __json_output(self, send_string={}):
data_json = json.dumps(send_string)
return data_json
def __accept_connection(self):
self.conn, self.addr = self.serversocket.accept()
self.conn.settimeout(2)
def __handle_connection_failure(self):
if self.conn != None:
self.conn.close()
self.conn_status = 'not connected'
self.conn = None
def __readline_socket(self):
data = self.conn.recv(1)
data= data.decode()
if data != "\n":
self.__data_line += data
self.conn_status = 'connected'
self.prev_conn_status = self.conn_status
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()
self.conn.send(encoded_data)
def __handle_connection(self):
while True:
try:
data_line = self.__readline_socket()
except:
self.__handle_connection_failure()
return False
if data_line != None:
self.__handle_connection_receive_data(data_line)
def __communicate(self):
while True:
try:
self.__accept_connection()
except BlockingIOError:
time.sleep(0.1)
continue
print("Receving data")
self.__handle_connection()
print("3- Done reiceive data")
time.sleep(0.1)