annotate server/ts.py @ 330:7ac6b8846eea

- some fixes for server code - don't turn off bluetooth in avr code
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jun 2012 22:32:49 +0800
parents 740438e21ea0
children 2943f62c8e62
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
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 import httplib
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
15 import time
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
16 import traceback
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
17
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
18 from utils import monotonic_time, retry, readline
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 lightblue = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 try:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 import lightblue
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 except ImportError:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 import bluetooth
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 def get_socket(addr):
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 if lightblue:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 s = lightblue.socket()
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
29 s.connect((addr, 1))
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
30 s.settimeout(3)
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 else:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 s.connect((addr, 1))
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
35 s.setblocking(False)
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
36
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
37 time.sleep(30)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
38
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 return s
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 def crc16(buff, crc = 0, poly = 0x8408):
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
43 l = len(buff)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
44 i = 0
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
45 while i < l:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
46 ch = ord(buff[i])
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
47 uc = 0
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
48 while uc < 8:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
49 if (crc & 1) ^ (ch & 1):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
50 crc = (crc >> 1) ^ poly
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
51 else:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
52 crc >>= 1
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
53 ch >>= 1
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
54 uc += 1
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
55 i += 1
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
56 return crc
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
59 @retry()
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 def fetch(sock):
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
61 print "fetch"
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 sock.send("fetch\n")
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 = 0
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 lines = []
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if l != 'START\n':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 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
70 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 lines.append(l)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
74 while True:
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if l == 'END\n':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 break
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 lines.append(l)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
84 print lines
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
85
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 try:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 k, v = l.rstrip('\n').split('=')
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 if k == 'CRC':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 recv_crc = int(v)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 if recv_crc < 0 or recv_crc > 0xffff:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 except ValueError:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 pass
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 if recv_crc is None:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 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
99 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 if recv_crc != crc:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 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
103 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 return ''.join(lines)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
106
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
107 @retry()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
108 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
109 if TESTING:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
110 return None
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
111 sock.send("btoff\n");
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
112 # read newline
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
113 l = readline(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
114 if not l:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
115 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
116 return None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
117
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
118 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
119 if off != 'Off':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
120 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
121
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
122 return int(next_wake)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
123
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
124 def do_comms(sock):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
125 d = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
126 # 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
127 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
128 d = fetch(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
129 if d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
130 break
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
131 time.sleep(1)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
132 if not d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
133 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
134
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
135 res = send_results()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
136 if not res:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
137 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
138
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
139 clear_meas(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
140
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
141 next_wake = turn_off(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
142 sock.close()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
143 return next_wake
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
144
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
145 testcount = 0
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
146
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
147 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
148 until = monotonic_time() + secs
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
149 while True:
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
150 length = until - monotonic_time()
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
151 if length <= 0:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
152 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
153 time.sleep(length)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
154
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
155 def main():
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 while True:
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
158 sock = None
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
159 try:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
160 sock = get_socket(BTADDR)
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
161 except Exception, e:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
162 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
163 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
164 sleep_time = SLEEP_TIME
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
165 if sock:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
166 next_wake = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
167 try:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
168 next_wake = do_comms(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
169 except Exception, e:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
170 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
171 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
172 if next_wake:
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
173 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
174
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
175 if TESTING:
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
176 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
177 sleep_for(sleep_time)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
178
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
179 if __name__ == '__main__':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
180 main()