-
Notifications
You must be signed in to change notification settings - Fork 5
/
HDCS_Link.py
149 lines (121 loc) · 4.72 KB
/
HDCS_Link.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
"""
This file is part of ADCS.
ADCS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ADCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ADCS. If not, see <http://www.gnu.org/licenses/>.
"""
import socket
import ast
from time import time
"""-----------------------------------------------------------------------------
Primary HDCS Link occurs via TCP in the main process
-----------------------------------------------------------------------------"""
class HDCS_Link(object):
def __init__(self):
""" Create TCP socket, bind, and try to initiate connection """
self.tcp_host = ''
#self.tcp_port = 50010
self.tcp_port = 50030
self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.tcp.settimeout(0.001)
print('tcp binding...')
while True:
try:
self.tcp_port = 50010
self.tcp.bind((self.tcp_host, self.tcp_port))
break
except socket.error:
try:
self.tcp_port = 50020
self.tcp.bind((self.tcp_host, self.tcp_port))
break
except socket.error:
try:
self.tcp_port = 50030
self.tcp.bind((self.tcp_host, self.tcp_port))
break
except socket.error:
pass
pass
self.connected = False
self.tcp.listen(1)
self.tcp_connect()
print('tcp Host:',self.tcp_host,'tcp Port:',self.tcp_port)
def tcp_connect(self):
""" Attempt to connect to HDCS via TCP """
#print('tcp LOS')
try:
try:
self.tcp_connection.close()
self.tcp_connection,self.addr = self.tcp.accept()
self.connected = True
print('tcp AOS')
except (AttributeError):
self.tcp_connection,self.addr = self.tcp.accept()
self.connected = True
print('tcp AOS')
except (socket.timeout):
if self.connected:
# Only do one
print('tcp LOS, timeout')
self.connected = False
def tcp_close(self):
self.tcp.shutdown(socket.SHUT_RDWR)
self.tcp.close()
def receive(self):
""" Receive cmd string from connection """
if self.connected:
try:
self.tcp_connection.settimeout(0.001)
return self.tcp_connection.recv(4096)
except (socket.timeout,ConnectionResetError) as e:
#self.connected = False
return None
def cmd_parser(self,data):
""" Parse the latest received cmd data string into a dict """
data = data.decode()
# The TCP data has old data mixed in, so be sure to only get the latest
start,end = data.rfind('{'), data.rfind('}')+1 # INCLUDE BRACES
if start>=end:
#If TCP got only string with } BEFORE {, then EOF will occur
# Get the actual start, BEFORE the last }
start = data.rfind('{',0,end)
new_data = data[start:end]
try:
return ast.literal_eval(new_data)
except Exception as e:
print(e)
pass
def send(self,state):
""" Send state dictionary to connection """
if self.connected:
try:
self.tcp_connection.sendall(bytes(str(state),'utf-8'))
except (ConnectionResetError, BrokenPipeError,socket.timeout) as e:
self.connected = False
print(e)
else:
self.tcp_connect()
def transceive(self,state):
""" Receive + Send from the TCP connection """
cmd = self.receive()
self.send(state)
return cmd
def transceive_and_parse(self,state):
""" Do both receiving and parsing, and return the new cmds """
if self.connected:
state['hdcs'] = 1
data = self.transceive(state)
if data is not None:
return self.cmd_parser(data)
else:
state['hdcs'] = 0
self.tcp_connect()