annotate server/ts.py @ 339:449272fc63a3

- Debug log file for server - Fix some variable sizes, turn off local echo for avr code - symlink config for wrt server
author Matt Johnston <matt@ucc.asn.au>
date Fri, 15 Jun 2012 23:34:24 +0800
parents 12123e390169
children 79124d7d3f79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
3 BTADDR = "00:12:03:27:70:88"
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
4 SLEEP_TIME = 180
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
5 # time that the bluetooth takes to get going?
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
6 EXTRA_WAKEUP = 0
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
7
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
8 FETCH_TRIES = 3
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
9
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
10 # avoid turning off the bluetooth etc.
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
11 TESTING = True
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
12
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 import sys
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
14 # for wrt
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
15 sys.path.append('/root/python')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
16 import httplib
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
17 import time
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
18 import traceback
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
19 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
20 import hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
21
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
22 import config
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
23
332
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
24 from utils import monotonic_time, retry, readline, crc16
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 lightblue = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 try:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 import lightblue
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 except ImportError:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 import bluetooth
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 def get_socket(addr):
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 if lightblue:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 s = lightblue.socket()
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
35 s.connect((addr, 1))
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
36 s.settimeout(3)
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 else:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 s.connect((addr, 1))
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
41 s.setblocking(False)
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
42
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 return s
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
46 @retry()
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 def fetch(sock):
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
48 print "fetch"
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 sock.send("fetch\n")
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 crc = 0
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 lines = []
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 if l != 'START\n':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 lines.append(l)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
61 while True:
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if l == 'END\n':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 break
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
69 lines.append(l.rstrip('\n'))
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
71 print lines
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
72
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 try:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 k, v = l.rstrip('\n').split('=')
331
5de3fc71ce48 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 330
diff changeset
77 print k,v
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 if k == 'CRC':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 recv_crc = int(v)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if recv_crc < 0 or recv_crc > 0xffff:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 except ValueError:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 pass
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 if recv_crc is None:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 print>>sys.stderr, "Bad expected CRC line '%s'\n" % l.rstrip('\n')
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 if recv_crc != crc:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
93 return lines
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
94
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
95 @retry()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
96 def turn_off(sock):
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
97 if TESTING:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
98 return None
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
99 sock.send("btoff\n");
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
100 # read newline
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
101 l = readline(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
102 if not l:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
103 print>>sys.stderr, "Bad response to btoff\n"
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
104 return None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
105
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
106 off, next_wake = l.rstrip().split(':')
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
107 if off != 'Off':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
108 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
109
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
110 return int(next_wake)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
111
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
112 @retry()
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
113 def clear_meas(sock):
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
114 sock.send("clear\n");
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
115 l = readline(sock)
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
116 if l and l.rstrip() == 'cleared':
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
117 return True
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
118
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
119 print>>sys.stderr, "Bad response to clear %s\n" % str(l)
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
120 return False
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
121
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
122 def send_results(lines):
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
123 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines)))
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
124 hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
125
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
126 url_data = urllib.url_encode( ('lines', enc_lines), ('hmac', mac) )
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
127 con = urllib2.urlopen(config.UPDATE_URL, url_data)
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
128 result = con.read(100)
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
129 if result == 'OK':
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
130 return True
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
131 else:
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
132 print>>sys.stderr, "Bad result '%s'" % result
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
133 return False
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
134
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
135 def do_comms(sock):
331
5de3fc71ce48 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 330
diff changeset
136 print "do_comms"
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
137 d = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
138 # serial could be unreliable, try a few times
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
139 for i in range(FETCH_TRIES):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
140 d = fetch(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
141 if d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
142 break
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
143 time.sleep(1)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
144 if not d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
145 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
146
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
147 res = send_results(d)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
148 if not res:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
149 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
150
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
151 clear_meas(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
152
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
153 next_wake = turn_off(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
154 sock.close()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
155 return next_wake
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
156
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
157 testcount = 0
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
158
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
159 def sleep_for(secs):
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
160 until = monotonic_time() + secs
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
161 while True:
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
162 length = until - monotonic_time()
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
163 if length <= 0:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
164 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
165 time.sleep(length)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
166
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
167 def main():
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
168
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
169 while True:
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
170 sock = None
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
171 try:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
172 sock = get_socket(BTADDR)
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
173 except Exception, e:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
174 print>>sys.stderr, "Error connecting:"
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
175 traceback.print_exc(file=sys.stderr)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
176 sleep_time = SLEEP_TIME
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
177 if sock:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
178 next_wake = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
179 try:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
180 next_wake = do_comms(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
181 except Exception, e:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
182 print>>sys.stderr, "Error in do_comms:"
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
183 traceback.print_exc(file=sys.stderr)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
184 if next_wake:
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
185 sleep_time = min(next_wake+EXTRA_WAKEUP, sleep_time)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
186
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
187 if TESTING:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
188 print "Sleeping for %d" % sleep_time
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
189 sleep_for(sleep_time)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
190
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
191 if __name__ == '__main__':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
192 main()