-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclient.py
87 lines (68 loc) · 3.02 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
"""
Author: Eren Sezener ([email protected])
Date: May 17, 2014
Description: Sends updates to the server.
Status: Works correctly.
Dependencies:
Known bugs: -
"""
import socket
import settings
HEADER = '<Sen Type=\"ImFree\"><EStr>KRCnexxt - RSI Object ST_ETHERNET</EStr>'
FOOTER = '<Tech T21=\"1.09\" T22=\"2.08\" T23=\"3.07\" T24=\"4.06\" T25=\"5.05\" T26=\"6.04\" T27=\"7.03\" ' \
'T28=\"8.02\" T29=\"9.01\" T210=\"10.00\"/><DiO>125</DiO><IPOC>0000000000</IPOC></Sen>'
DEFAULT_RKORR = '<RKorr X=\"0.0000\" Y=\"0.0000\" Z=\"0.0000\" A=\"0.0000\" B=\"0.0000\" C=\"0.0000\"/>'
DEFAULT_AKORR = '<AKorr A1=\"0.0000\" A2=\"0.0000\" A3=\"0.0000\" A4=\"0.0000\" A5=\"0.0000\" A6=\"0.0000\"/>'
def initialize_socket():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((settings.SERVER_IP, settings.CLIENT_PORT))
s.listen(1)
conn, addr = s.accept()
return conn
def is_terminate_command(input_text):
if input_text == 'q' or input_text == 'Q':
return True
else:
return False
def process_text(input_text): #TODO This is not robust at all, regex?
text_without_spaces = input_text.replace(' ', '') #get rid of the spaces
string_list = text_without_spaces.split(',')
if string_list[0] == 'r' or string_list[0] == 'a':
coordinates = []
for i in range(1, len(string_list)):
coordinates.append(float(string_list[i]))
return string_list[0], coordinates
else: # Erroneous command
return None, []
def get_rkorr_string(command_list):
command_list = map(lambda x: str(x), command_list)
return '<RKorr X=\"' + command_list[0] + '\" Y=\"' + command_list[1] + '\" Z=\"' + command_list[2] + '\" A=\"' + \
command_list[3] + '\" B=\"' + command_list[4] + '\" C=\"' + command_list[5] + '\" />'
def get_akorr_string(command_list):
command_list = map(lambda x: str(x), command_list)
return '<AKorr A1=\"' + command_list[0] + '\" A2=\"' + command_list[1] + '\" A3=\"' + command_list[2] + '\" A4=\"' + \
command_list[3] + '\" A5=\"' + command_list[4] + '\" A6=\"' + command_list[5] + '\" />'
def create_command_string(type, command_list):
if type == 'r':
return HEADER + get_rkorr_string(command_list) + FOOTER
elif type == 'a':
return HEADER + get_akorr_string(command_list) + FOOTER
def run_client(connection):
sock = initialize_socket()
while True:
input_text = sock.recv(settings.BUFFER_SIZE)
sock.send('sent')
if input_text:
type, command_list = process_text(input_text)
if type is not None:
string_to_send = create_command_string(type, command_list)
#print "Sending"
#print string_to_send
connection.send(string_to_send)
elif is_terminate_command(input_text):
print "Terminating"
sock.shutdown(socket.SHUT_RDWR) # Don't allow reads & writes
sock.close()
return
else:
print "The command is not valid."