-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
79 lines (65 loc) · 2.33 KB
/
client.py
File metadata and controls
79 lines (65 loc) · 2.33 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
#!/usr/bin/env python3
# The major functions of client are converting given command into a separatable string
# and send the string to the server(ev3)
# Author(s):
# Wanjing Chen
import socket
import time
from controller_config import config
import sys
# Arguably dangerous to assume these exist, but OK to crash if they don't
# since these values are necessary
if len(sys.argv)>1:
HOST = sys.argv[2]
else:
HOST = config['robot']['ip']
PORT = config['robot']['port']
# Socket for connecting to robot
s = None
# Whether the connection is open
conn_open = False
# Open connection
def open():
global s
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Connecting to {}:{}".format(HOST, PORT))
s.connect((HOST, PORT))
conn_open = True
except Exception as exception:
conn_open = False
print("Connection failed: {}".format(exception))
# Close connection once done
def close():
s.close()
conn_open = False
# The following several functions are to convert the given commands into a later seperatable string
def move_piece(cellA,cellB, piece_type):
message = 'move_piece;' + str(cellA) + ';' + str(cellB) + ';' + str(piece_type)
send(message)
def move(cellA, cellB, piece_type):
message = 'move;' + str(cellA) + ';' + str(cellB) + ';' + str(piece_type)
send(message)
def take_piece(cellA, cellB, piece, piece_typeA, piece_typeB):
message = 'take_piece;' + str(cellA) + ';' + str(cellB) + ';' + str(piece) + ';' + str(piece_typeA) + ';' + str(piece_typeB)
send(message)
def perform_castling_at(cellA, cellB, cellC, cellD, piece_typeA, piece_typeB):
message = 'perform_castling_at;' + str(cellA) + ';' + str(cellB) + ';' + str(cellC) + ';' + str(cellD) + ';' + str(piece_typeA) + ';' + str(piece_typeB)
send(message)
def en_passant(cellA, cellB, cellTake, piece):
message = 'en_passant;' + str(cellA) + ';' + str(cellB) + ';' + str(cellTake) + ';' + str(piece)
send(message)
def reset():
message = 'reset;'
send(message)
# This function is to send the message to the server(ev3)
def send(message):
t0 = time.time()
print('Will send ' + message)
s.sendall(str.encode(message))
data = s.recv(4000)
print('Received', repr(data))
t1 = time.time()
print("Took: %f s" % (t1 - t0))
print("\n")
return data