This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from jav/pybotwar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
220 lines (177 loc) · 5.2 KB
/
control.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright 2009-2014 Lee Harr
#
# This file is part of pybotwar.
# http://pybotwar.googlecode.com/
#
# Pybotwar 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.
#
# Pybotwar 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 Pybotwar. If not, see <http://www.gnu.org/licenses/>.
import os
from threading import Thread
from time import sleep
import util
from util import defaultNonedict
import conf
util.setup_conf()
_overtime_count = 0
def loop(r, i):
data = i.split('|')
sensors = defaultNonedict()
for d in data:
k, v = d.split(':')
if ';' in v:
# Some sensors send multiple values, separated by semicolon
v = v.split(';')
vconv = []
for vv in v:
try:
vvconv = int(vv)
except:
vvconv = vv
vconv.append(vvconv)
else:
try:
vconv = int(v)
except:
vconv = v
sensors[k] = vconv
timeout = conf.tick_timeout
user_thread = Thread(target=get_response, args=(r, sensors))
response = None
user_thread.start()
user_thread.join(timeout)
if user_thread.isAlive():
global _overtime_count
_overtime_count += 1
response = 'TIMEOUT'
if _overtime_count > 10:
os.kill(os.getpid(), 9)
else:
_overtime_count = 0
rresponse = r.response
if rresponse == 'LOG':
start_logging(r)
elif rresponse == 'NOLOG':
stop_logging(r)
return response or rresponse
def get_response(r, sensors):
try:
r.sensors = sensors
r.respond()
except Exception, e:
r.err()
import traceback
tb = traceback.format_exc()
r.log(tb)
def communicate(r):
while True:
line = sys.stdin.readline().strip()
if line == 'FINISH':
break
elif not line:
break
elif line == 'DEBUG':
start_logging(r)
continue
elif line == 'NODEBUG':
stop_logging(r)
continue
o = loop(r, line)
if o is not None:
oline = '%s\n' % (str(o))
try:
sys.stdout.write(oline)
sys.stdout.flush()
except IOError:
break
else:
oline = 'END\n'
try:
sys.stdout.write(oline)
sys.stdout.flush()
except IOError:
pass
break
def robot_logfile(robotname):
logfilename = '%s.log' % robotname
logdir = os.path.join(conf.base_dir, conf.logdir)
try:
if not os.path.exists(logdir):
os.mkdir(logdir)
logfilepath = os.path.join(logdir, logfilename)
logfile = open(logfilepath, 'a')
except (IOError, OSError):
logfile = None
logfile.write('Begin logging for %s.\n' % robotname)
logfile.flush()
return logfile
def start_logging(robot):
logfile = robot_logfile(robot._p__name)
robot._p__logfile = logfile
def stop_logging(robot):
robot._p__logfile = None
def build_robot(modname, rfile, robotname, testmode, rbox):
if testmode:
logfile = robot_logfile(robotname)
else:
logfile = None
try:
import imp
mod = imp.load_source(modname, rfile)
r = mod.TheRobot(robotname)
r._p__logfile = logfile
r.initialize()
except:
rbox.append(None)
import traceback
tb = traceback.format_exc()
if logfile is not None:
logfile.write(tb)
logfile.write('\n')
logfile.flush()
else:
import sys
sys.stderr.write(tb)
sys.stderr.write('\n')
sys.stderr.flush()
else:
rbox.append(r)
if __name__ == '__main__':
import sys
if len(sys.argv) != 5:
raise SystemExit
else:
modname = sys.argv[1]
rfile = sys.argv[2]
robotname = sys.argv[3]
testmode = bool(int(sys.argv[4]))
timeout = conf.init_timeout
rbox = [] # Store the robot here to pass it back from the thread
user_thread = Thread(target=build_robot, args=(modname, rfile, robotname, testmode, rbox))
user_thread.start()
user_thread.join(timeout)
if user_thread.isAlive():
rbox = [None]
robot = rbox[0]
if robot is None:
# robot failed to load properly
oline = 'ERROR\n'
sys.stdout.write(oline)
sys.stdout.flush()
else:
oline = 'START\n'
sys.stdout.write(oline)
sys.stdout.flush()
try:
communicate(robot)
except KeyboardInterrupt:
pass