comparison visualclient/visclient.py @ 32:cb57e554ae80

some bug fixes while testing connection
author gyorgyf
date Tue, 09 Jul 2013 16:20:08 +0100
parents 1233c13c17d9
children fe04ff3547c7
comparison
equal deleted inserted replaced
31:1233c13c17d9 32:cb57e554ae80
47 FADE = 15 47 FADE = 15
48 DIST = 0.15 # blob equivalence tolerance 48 DIST = 0.15 # blob equivalence tolerance
49 FRAMERATE = 60 49 FRAMERATE = 60
50 50
51 # Connection: 51 # Connection:
52 # IP = "127.0.0.1:8030" 52 IP = "127.0.0.1:8030"
53 # IP = "192.168.2.158:8030" 53 # IP = "192.168.2.158:8030"
54 IP = "138.37.95.215" 54 # IP = "138.37.95.215"
55 HTTP_TIMEOUT = 3 55 HTTP_TIMEOUT = 3
56 SERVER_UPDATE_INTERVAL = 0.8 56 SERVER_UPDATE_INTERVAL = 0.8
57 57
58 58
59 class Indicator(object): 59 class Indicator(object):
263 r,g,b = map(lambda x: int(x*255), cs.hls_to_rgb(h,l,s)) 263 r,g,b = map(lambda x: int(x*255), cs.hls_to_rgb(h,l,s))
264 return r,g,b 264 return r,g,b
265 265
266 def start_update_thread(self): 266 def start_update_thread(self):
267 '''Start the thread that reads data from the server.''' 267 '''Start the thread that reads data from the server.'''
268 self.running = True
269 self.thread = Thread(target = self.update_thread) 268 self.thread = Thread(target = self.update_thread)
270 self.thread.daemon = True 269 self.thread.daemon = True
271 self.thread.start() 270 self.thread.start()
271 self.running = True
272 print "OK. Update thread started."
272 273
273 def stop_update_thread(self): 274 def stop_update_thread(self):
274 '''Stop the thread and allow some time fot the connections to close.''' 275 '''Stop the thread and allow some time fot the connections to close.'''
275 self.running = False 276 self.running = False
276 try : 277 try :
279 print "No update thread to join." 280 print "No update thread to join."
280 281
281 def update_thread(self): 282 def update_thread(self):
282 '''The server update thread''' 283 '''The server update thread'''
283 while self.running : 284 while self.running :
285 # self.update()
284 try : 286 try :
285 self.update() 287 self.update()
286 # self.indicators["update"].visible = True 288 # self.indicators["update"].visible = True
287 except Exception, e: 289 except Exception, e:
288 if str(e).strip() : print "Exception: ", str(e), type(e), len(str(e).strip()) 290 if str(e).strip() : print "Exception: ", str(e), type(e), len(str(e).strip())
492 494
493 495
494 def update_server_config(self,age,dist,ninp,retry = 3): 496 def update_server_config(self,age,dist,ninp,retry = 3):
495 '''Send the server some configuration data.''' 497 '''Send the server some configuration data.'''
496 try : 498 try :
499 print "Sending configuration data."
497 self.conn.putrequest("GET","/moodconductor/config?age=%(age)s&dist=%(dist)s&ninp=%(ninp)s" %locals(), skip_host=True) 500 self.conn.putrequest("GET","/moodconductor/config?age=%(age)s&dist=%(dist)s&ninp=%(ninp)s" %locals(), skip_host=True)
498 self.conn.putheader("Host", "www.isophonics.net") 501 self.conn.putheader("Host", "www.isophonics.net")
499 self.conn.endheaders() 502 self.conn.endheaders()
500 res = self.conn.getresponse() 503 res = self.conn.getresponse()
504 res.read()
501 if not res.status == 200 : 505 if not res.status == 200 :
502 print "Server response:", res.status, res.reason 506 print "Server response:", res.status, res.reason
503 self.indicators["conn"].off() 507 self.indicators["conn"].off()
504 time.sleep(0.5) 508 time.sleep(0.5)
505 self.conn.putrequest("GET","/moodconductor/getconf", skip_host=True) 509 self.conn.putrequest("GET","/moodconductor/getconf", skip_host=True)
506 self.conn.putheader("Host", "www.isophonics.net") 510 self.conn.putheader("Host", "www.isophonics.net")
507 self.conn.endheaders() 511 self.conn.endheaders()
508 res = self.conn.getresponse() 512 res = self.conn.getresponse()
509 if not res.status == 200 : 513 if not res.status == 200 :
510 print "Server response:", res.status, res.reason 514 print "Server response:", res.status, res.reason
511 self.indicators["conn"].off() 515 self.indicators["conn"].off()
512 print "Server configuration:", res.read() 516 print "Server configuration:", res.read()
513 except: 517 except:
518 print "Failed to send configuration data."
514 time.sleep(2) 519 time.sleep(2)
515 retry -= 1 520 retry -= 1
516 self.update_server_config(age,dist,ninp,retry) 521 self.update_server_config(age,dist,ninp,retry)
517 522
518 523
523 pg.quit() 528 pg.quit()
524 raise SystemExit 529 raise SystemExit
525 if retry < 5 : 530 if retry < 5 :
526 time.sleep(3) 531 time.sleep(3)
527 try : 532 try :
533 print "connecting to server..."
528 self.conn = ht.HTTPConnection(IP,timeout=HTTP_TIMEOUT) 534 self.conn = ht.HTTPConnection(IP,timeout=HTTP_TIMEOUT)
529 # self.start_update_thread()
530 self.indicators["conn"].on() 535 self.indicators["conn"].on()
531 except : 536 except :
532 self.indicators["conn"].off() 537 self.indicators["conn"].off()
533 self.connect(retry = retry-1) 538 self.connect(retry = retry-1)
539 print "connection failed."
534 540
535 try: 541 try:
542 print "Testing connection."
536 self.conn.putrequest("GET","/moodconductor/index.html", skip_host=True) 543 self.conn.putrequest("GET","/moodconductor/index.html", skip_host=True)
537 self.conn.putheader("Host", "www.isophonics.net") 544 self.conn.putheader("Host", "www.isophonics.net")
538 self.conn.endheaders() 545 self.conn.endheaders()
539 res = self.conn.getresponse() 546 res = self.conn.getresponse()
547 res.read()
540 if res.status == 200 : 548 if res.status == 200 :
541 self.indicators["conn"].on() 549 self.indicators["conn"].on()
542 else : 550 else :
543 print "Server response:", res.status, res.reason 551 print "Server response:", res.status, res.reason
544 self.indicators["conn"].off() 552 self.indicators["conn"].off()
545 if not hasattr(self,"noretry") and raw_input("Go offline? ") in ['y',''] : 553 if not hasattr(self,"noretry") and raw_input("Go offline? ") in ['y',''] :
546 return False 554 return False
547 else : 555 else :
556 print "Failed. retrying."
548 self.noretry = None 557 self.noretry = None
549 self.connect(retry = retry-1) 558 self.connect(retry = retry-1)
550 except : 559 except :
551 print "Exception while testing connection." 560 print "Exception while testing connection."
552 self.indicators["conn"].off() 561 self.indicators["conn"].off()
555 return False 564 return False
556 else : 565 else :
557 self.noretry = None 566 self.noretry = None
558 self.connect(retry = retry-1) 567 self.connect(retry = retry-1)
559 self.configure_server() 568 self.configure_server()
569 print "OK. Starting update thread..."
560 self.start_update_thread() 570 self.start_update_thread()
561 return True 571 return True
562 572
563 573
564 574