-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyclient.py
More file actions
123 lines (99 loc) · 3.99 KB
/
pyclient.py
File metadata and controls
123 lines (99 loc) · 3.99 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
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
import sys
import argparse
import socket
import driver
if __name__ == '__main__':
pass
# Configure the argument parser
parser = argparse.ArgumentParser(description='Python client to connect to the TORCS SCRC server.')
parser.add_argument('--host', action='store', dest='host_ip', default='localhost',
help='Host IP address (default: localhost)')
parser.add_argument('--port', action='store', type=int, dest='host_port', default=3001,
help='Host port number (default: 3001)')
parser.add_argument('--id', action='store', dest='id', default='SCR',
help='Bot ID (default: SCR)')
parser.add_argument('--maxEpisodes', action='store', dest='max_episodes', type=int, default=1,
help='Maximum number of learning episodes (default: 1)')
parser.add_argument('--maxSteps', action='store', dest='max_steps', type=int, default=0,
help='Maximum number of steps (default: 0)')
parser.add_argument('--track', action='store', dest='track', default=None,
help='Name of the track')
parser.add_argument('--stage', action='store', dest='stage', type=int, default=3,
help='Stage (0 - Warm-Up, 1 - Qualifying, 2 - Race, 3 - Unknown)')
arguments = parser.parse_args()
# Print summary
print('Connecting to server host ip:', arguments.host_ip, '@ port:', arguments.host_port)
print('Bot ID:', arguments.id)
print('Maximum episodes:', arguments.max_episodes)
print('Maximum steps:', arguments.max_steps)
print('Track:', arguments.track)
print('Stage:', arguments.stage)
print('*********************************************')
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error as msg:
print('Could not make a socket.', msg)
sys.exit(-1)
# One second timeout
sock.settimeout(1.0)
shutdownClient = False
curEpisode = 0
verbose = False
d = driver.Driver(arguments.stage)
while not shutdownClient:
while True:
print('Sending id to server:', arguments.id)
buf = arguments.id + d.init()
print('Sending init string to server:', buf)
try:
sock.sendto(buf.encode('utf-8'), (arguments.host_ip, arguments.host_port))
except socket.error as msg:
print("Failed to send data...Exiting...", msg)
sys.exit(-1)
try:
buf, addr = sock.recvfrom(1000)
buf = buf.decode('utf-8') # Convert received bytes to string
except socket.error as msg:
print("Didn't get response from server...", msg)
continue # Retry
if '***identified***' in buf:
print('Received:', buf)
break
currentStep = 0
while True:
# Wait for an answer from server
buf = None
try:
buf, addr = sock.recvfrom(1000)
buf = buf.decode('utf-8') # Convert received bytes to string
except socket.error as msg:
print("Didn't get response from server...", msg)
if verbose:
print('Received:', buf)
if buf is not None and '***shutdown***' in buf:
d.onShutDown()
shutdownClient = True
print('Client Shutdown')
break
if buf is not None and '***restart***' in buf:
d.onRestart()
print('Client Restart')
break
currentStep += 1
if currentStep != arguments.max_steps:
if buf is not None:
buf = d.drive(buf)
else:
buf = '(meta 1)'
if verbose:
print('Sending:', buf)
if buf is not None:
try:
sock.sendto(buf.encode('utf-8'), (arguments.host_ip, arguments.host_port))
except socket.error as msg:
print("Failed to send data...Exiting...", msg)
sys.exit(-1)
curEpisode += 1
if curEpisode == arguments.max_episodes:
shutdownClient = True
sock.close()