[tuxdroid-svn] r353 - api/python/trunk
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2007-06-06 08:16:29
|
Author: remi Date: 2007-06-06 10:16:20 +0200 (Wed, 06 Jun 2007) New Revision: 353 Modified: api/python/trunk/tuxapi_class.py api/python/trunk/tuxapi_const.py Log: UPD System update: - LockMutex system added for the synchronous request of statuses. - Access to asynchronous statuses has been modified - Storage of events has been modified Functions updated: - To_string function has been modified Functions added: - Authorized voices from the ttsdaemon has been implemented - Function to simulate the receiving of a remote key has been added Modified: api/python/trunk/tuxapi_class.py =================================================================== --- api/python/trunk/tuxapi_class.py 2007-06-05 21:26:38 UTC (rev 352) +++ api/python/trunk/tuxapi_class.py 2007-06-06 08:16:20 UTC (rev 353) @@ -29,13 +29,14 @@ import thread import threading import string +import signal from tuxapi_const import * #============================================================================== # Constants #============================================================================== -api_version ="0.2.1" +api_version ="0.2.2" #============================================================================== # TUXTCPCommunicator class @@ -80,13 +81,16 @@ self.exit_flag=False self.tcp_mutex=threading.Lock() self.print_warnings_mutex=threading.Lock() - self.tcp_data=(' ')*6 - self.datas_threated=True - self.tcp_data_for_event=(' ')*6 self.my_name="Tux client" self.print_status=False self.print_warnings=True self.print_debug_thread=False + self.lock_list = [] + self.lock_list_mutex = threading.Lock() + self.tcp_data_fifo_lock = [] + self.tcp_data_fifo_lock_mutex = threading.Lock() + self.tcp_data_fifo_event = [] + self.tcp_data_fifo_event_mutex = threading.Lock() self.event=TUXevent(self) self.cmd=TUXcmd(self) self.sys=TUXsys(self) @@ -97,8 +101,6 @@ self.tts=TUXtts(self) self.misc=TUXmisc(self) self.connected=False #deprecated - self.last_threated_data_id=0 - self.current_data_id=0 t=threading.Thread(target=self.daemon._loop_auto_connect) t.setName('daemon._loop_auto_connect') t.start() @@ -137,48 +139,137 @@ """ while self.daemon.connected: try: - self.tcp_mutex.acquire() tmp_tcp_data=self.sock.recv(16) except socket.timeout: - self.tcp_mutex.release() time.sleep(0.01) continue except socket.error: self.sock.close() self.daemon.connected=False - self.tcp_mutex.release() time.sleep(0.01) continue if len(tmp_tcp_data)<16: - self.tcp_mutex.release() time.sleep(0.01) continue - self.current_data_id=(self.current_data_id+1)&0xFFFFFF - self.tcp_data=tmp_tcp_data - data=self.tcp_data - self.datas_threated=False + self.tcp_data_fifo_lock_mutex.acquire() + self.tcp_data_fifo_lock.append(tmp_tcp_data) + self.tcp_data_fifo_lock_mutex.release() + self.tcp_data_fifo_event_mutex.acquire() + self.tcp_data_fifo_event.append(tmp_tcp_data) + self.tcp_data_fifo_event_mutex.release() if self.print_status: struct_data = ["%.2x" % ord(datae) for datae in self.tcp_data] print " ".join(struct_data) - self.tcp_mutex.release() time.sleep(0.01) #-------------------------------------------------------------------------- # SYSTEM function #-------------------------------------------------------------------------- + def _loop_lock_list(self): + """ + Not a user function + self.lock_list + self.lock_list_mutex + self.tcp_data_fifo_lock + self.tcp_data_fifo_lock_mutex + """ + while self.daemon.connected: + # get a private lock_list from the class lock_list + self.lock_list_mutex.acquire() + lock_list = self.lock_list + self.lock_list_mutex.release() + # get a private data_fifo from the class data_fifo + self.tcp_data_fifo_lock_mutex.acquire() + if len(self.tcp_data_fifo_lock) > 0: + data_fifo = self.tcp_data_fifo_lock.pop(0) + else: + data_fifo = (chr(99))*16 + self.tcp_data_fifo_lock_mutex.release() + # get the time now in seconds + now = self.sys.time() + # check the whole of the lock elements + # lock[0] : list of data to match + # lock[1] : condition mutex + # lock[2] : data returned + for i,lock in enumerate(lock_list): + # for the whole of the lock data + for lock_data in lock[0]: + # if the lock data is matched + if lock_data[0] in [ord(data_fifo[0]), 999]\ + and lock_data[1] in [ord(data_fifo[1]), 999]\ + and lock_data[2] in [ord(data_fifo[2]), 999]\ + and lock_data[3] in [ord(data_fifo[3]), 999]\ + and lock_data[4] in [ord(data_fifo[4]), 999]\ + and lock_data[5] in [ord(data_fifo[5]), 999]\ + and lock_data[6] in [ord(data_fifo[6]), 999]\ + and lock_data[7] in [ord(data_fifo[7]), 999]\ + and lock_data[8] in [ord(data_fifo[8]), 999]\ + and lock_data[9] in [ord(data_fifo[9]), 999]\ + and lock_data[10] in [ord(data_fifo[10]), 999]\ + and lock_data[11] in [ord(data_fifo[11]), 999]\ + and lock_data[12] in [ord(data_fifo[12]), 999]\ + and lock_data[13] in [ord(data_fifo[13]), 999]\ + and lock_data[14] in [ord(data_fifo[14]), 999]\ + and lock_data[15] in [ord(data_fifo[15]), 999]: + # return the data ones who have unblocked the lock + lock[2].append(data_fifo) + # unblock the lock + lock[1].acquire() + lock[1].notify() + lock[1].release() + lock_list.pop(i) + continue + time.sleep(0.01) + # kill the whole of the lock elements from the list + self._close_lock_list() + + #-------------------------------------------------------------------------- + # SYSTEM function + #-------------------------------------------------------------------------- + def _close_lock_list(self): + self.lock_list_mutex.acquire() + # unblock the whole of the lock elements + for i,lock in enumerate(self.lock_list): + lock[1].acquire() + lock[1].notify() + lock[1].release() + self.lock_list.pop(i) + self.lock_list_mutex.release() + + #-------------------------------------------------------------------------- + # SYSTEM function + #-------------------------------------------------------------------------- + def _insert_lock_in_list(self,data_to_match_list,cond_lock_mutex,returned_data): + # lock[0] : list of the possible frames to match + # lock[1] : condition mutex + # lock[2] : data returned + lock = [] + lock.append(data_to_match_list) + lock.append(cond_lock_mutex) + lock.append(returned_data) + self.lock_list_mutex.acquire() + self.lock_list.append(lock) + self.lock_list_mutex.release() + + #-------------------------------------------------------------------------- + # SYSTEM function + #-------------------------------------------------------------------------- def _loop_tux_data_dispatching(self): """ Not a user function """ while self.daemon.connected: - while self.datas_threated: - if not self.daemon.connected: return + data = () + self.tcp_data_fifo_event_mutex.acquire() + if len(self.tcp_data_fifo_event) > 0: + data = self.tcp_data_fifo_event.pop(0) + else: + self.tcp_data_fifo_event_mutex.release() time.sleep(0.003) - self.tcp_mutex.acquire() - data=self.tcp_data - self.tcp_mutex.release() + continue + self.tcp_data_fifo_event_mutex.release() self._dispatch_data_main(data) - self.datas_threated=True + time.sleep(0.003) #-------------------------------------------------------------------------- # SYSTEM function @@ -219,7 +310,7 @@ if ord(data[2])==DATA_TP_RSP: if ord(data[3])==SUBDATA_TP_STATUS: if self.event.on_status!=None: - self.event.on_status() + self.event.on_status(data) #Head button if ord(data[4])==DATAS_STATUS_HEAD_PUSH_SWITCH: if ord(data[5])==1: @@ -259,9 +350,11 @@ #Remote button if ord(data[4])==DATAS_STATUS_IR_CODE: if self.event.on_remote_bt[ord(data[5])]!=None: - self.event.on_remote_bt[ord(data[5])]() + thread.start_new_thread(self.event.on_remote_bt[ord(data[5])], ()) + #self.event.on_remote_bt[ord(data[5])]() if self.event.on_remote!=None: - self.event.on_remote(ord(data[5])) + thread.start_new_thread(self.event.on_remote, (ord(data[5]),)) + #self.event.on_remote(ord(data[5])) #Mouth open if ord(data[4])==DATAS_STATUS_MOUTH_OPEN_POSITION: if ord(data[5])==0: @@ -347,11 +440,11 @@ #-------------------------------------------------------------------------- # Deprecated functions #-------------------------------------------------------------------------- - def explicit_status(self): + def explicit_status(self,frame): """ Deprecated : see 'tux.status.to_string' """ - return self.status.to_string() + return self.status.to_string(frame) def connect_to_daemon(self): """ @@ -374,10 +467,13 @@ Class which manages the system functions Functions list for the users: + tux.sys.add_notification tux.sys.add_time_event + tux.sys.clear_notification tux.sys.clear_time_events tux.sys.delayed_function tux.sys.looped_function + tux.sys.remove_notification tux.sys.shell tux.sys.shell_free tux.sys.time @@ -452,10 +548,6 @@ >>> tux.sys.shell_free('ls -al') """ thread.start_new_thread(self.shell,(command,)) - #t=threading.Thread(target=self.shell,args=(command,)) - #t.setName('sys.shell') - #t.start() - #self.parent.daemon.free_thread_list.append(t) #-------------------------------------------------------------------------- # Add a time event in the time event handler @@ -592,7 +684,7 @@ "on_head_bt_released" as pof : On tux head button released "on_lwing_bt_released" as pof : On tux left wing button released "on_rwing_bt_released" as pof : On tux right wing button released - "on_remote_bt" as list of pof : On remote controller button pressed + "on_remote_bt" as list of pof : On remote controller button pressed "on_status" as pof : On status arrival "on_remote" as pof : On remote controller event param 1 : Key as integer @@ -619,19 +711,14 @@ "on_spin_stop" as pof : On spin stop event (pof = pointer of function) - Example of associating a function to an event: - >>> def my_function(): - >>> print "hello" - >>> tux.event.on_bt_pushed=my_function - Example of associating a function to a remote event: >>> def my_function(key): >>> print "Button %s is pressed"%remote_bt_name[key] >>> tux.event.on_remote=my_function - Example of associating a function to a specific remote event: - >>> def play_pause(): - >>> tux.sys.shell("audacious --play-pause") + Example of associating a function to a specific remote event: + >>> def play_pause(): + >>> tux.sys.shell("audacious --play-pause") >>> tux.event.on_remote_bt[K_PLAYPAUSE]=play_pause Key constants of the remote controller: @@ -668,6 +755,7 @@ Constructor of class """ self.parent=parent + self.event_storage_fifo = [] self.clear() #-------------------------------------------------------------------------- @@ -739,37 +827,41 @@ Example: >>> tux.event.store() """ - self.s_on_bt_pushed=self.on_bt_pushed - self.s_on_head_bt_pushed=self.on_head_bt_pushed - self.s_on_lwing_bt_pushed=self.on_lwing_bt_pushed - self.s_on_rwing_bt_pushed=self.on_rwing_bt_pushed - self.s_on_bt_released=self.on_bt_released - self.s_on_head_bt_released=self.on_head_bt_released - self.s_on_lwing_bt_released=self.on_lwing_bt_released - self.s_on_rwing_bt_released=self.on_rwing_bt_released - self.s_on_remote_bt=self.on_remote_bt - self.s_on_status=self.on_status - self.s_on_remote=self.on_remote - self.s_on_light_level=self.on_light_level - self.s_on_connected=self.on_connected - self.s_on_disconnected=self.on_disconnected - self.s_on_mouth_open=self.on_mouth_open - self.s_on_mouth_close=self.on_mouth_close - self.s_on_power_plugged=self.on_power_plugged - self.s_on_power_unplugged=self.on_power_unplugged - self.s_on_left_blue_led_on=self.on_left_blue_led_on - self.s_on_left_blue_led_off=self.on_left_blue_led_off - self.s_on_right_blue_led_on=self.on_right_blue_led_on - self.s_on_right_blue_led_off=self.on_right_blue_led_off - self.s_on_eyes_open=self.on_eyes_open - self.s_on_eyes_close=self.on_eyes_close - self.s_on_rf_connected=self.on_rf_connected - self.s_on_rf_disconnected=self.on_rf_disconnected - self.s_on_pong_received=self.on_pong_received - self.s_on_mouth_stop=on_mouth_stop - self.s_on_eyes_stop=on_eyes_stop - self.s_on_wings_stop=on_wings_stop - self.s_on_spin_stop=on_spin_stop + event_storage = [] + event_storage.append(self.on_bt_pushed) + event_storage.append(self.on_head_bt_pushed) + event_storage.append(self.on_lwing_bt_pushed) + event_storage.append(self.on_rwing_bt_pushed) + event_storage.append(self.on_bt_released) + event_storage.append(self.on_head_bt_released) + event_storage.append(self.on_lwing_bt_released) + event_storage.append(self.on_rwing_bt_released) + event_storage.append(self.on_remote_bt) + event_storage.append(self.on_status) + event_storage.append(self.on_remote) + event_storage.append(self.on_light_level) + event_storage.append(self.on_connected) + event_storage.append(self.on_disconnected) + event_storage.append(self.on_mouth_open) + event_storage.append(self.on_mouth_close) + event_storage.append(self.on_power_plugged) + event_storage.append(self.on_power_unplugged) + event_storage.append(self.on_left_blue_led_on) + event_storage.append(self.on_left_blue_led_off) + event_storage.append(self.on_right_blue_led_on) + event_storage.append(self.on_right_blue_led_off) + event_storage.append(self.on_eyes_open) + event_storage.append(self.on_eyes_close) + event_storage.append(self.on_rf_connected) + event_storage.append(self.on_rf_disconnected) + event_storage.append(self.on_pong_received) + event_storage.append(self.on_mouth_stop) + event_storage.append(self.on_eyes_stop) + event_storage.append(self.on_wings_stop) + event_storage.append(self.on_spin_stop) + event_storage.append(self.parent.tts.on_sound_on) + event_storage.append(self.parent.tts.on_sound_off) + self.event_storage_fifo.append(event_storage) #-------------------------------------------------------------------------- # Restore all events @@ -781,37 +873,41 @@ Example: >>> tux.event.restore() """ - self.on_bt_pushed=self.s_on_bt_pushed - self.on_head_bt_pushed=self.s_on_head_bt_pushed - self.on_lwing_bt_pushed=self.s_on_lwing_bt_pushed - self.on_rwing_bt_pushed=self.s_on_rwing_bt_pushed - self.on_bt_released=self.s_on_bt_released - self.on_head_bt_released=self.s_on_head_bt_released - self.on_lwing_bt_released=self.s_on_lwing_bt_released - self.on_rwing_bt_released=self.s_on_rwing_bt_released - self.on_remote_bt=self.s_on_remote_bt - self.on_status=self.s_on_status - self.on_remote=self.s_on_remote - self.on_light_level=self.s_on_light_level - self.on_connected=self.s_on_connected - self.on_disconnected=self.s_on_disconnected - self.on_mouth_open=self.s_on_mouth_open - self.on_mouth_close=self.s_on_mouth_close - self.on_power_plugged=self.s_on_power_plugged - self.on_power_unplugged=self.s_on_power_unplugged - self.on_left_blue_led_on=self.s_on_left_blue_led_on - self.on_left_blue_led_off=self.s_on_left_blue_led_off - self.on_right_blue_led_on=self.s_on_right_blue_led_on - self.on_right_blue_led_off=self.s_on_right_blue_led_off - self.on_eyes_open=self.s_on_eyes_open - self.on_eyes_close=self.s_on_eyes_close - self.on_rf_connected=self.s_on_rf_connected - self.on_rf_disconnected=self.s_on_rf_disconnected - self.on_pong_received=self.s_on_pong_received - self.on_mouth_stop=s_on_mouth_stop - self.on_eyes_stop=s_on_eyes_stop - self.on_wings_stop=s_on_wings_stop - self.on_spin_stop=s_on_spin_stop + if len(self.event_storage_fifo) == 0: return + event_storage = self.event_storage_fifo.pop() + self.on_bt_pushed=event_storage.pop(0) + self.on_head_bt_pushed=event_storage.pop(0) + self.on_lwing_bt_pushed=event_storage.pop(0) + self.on_rwing_bt_pushed=event_storage.pop(0) + self.on_bt_released=event_storage.pop(0) + self.on_head_bt_released=event_storage.pop(0) + self.on_lwing_bt_released=event_storage.pop(0) + self.on_rwing_bt_released=event_storage.pop(0) + self.on_remote_bt=event_storage.pop(0) + self.on_status=event_storage.pop(0) + self.on_remote=event_storage.pop(0) + self.on_light_level=event_storage.pop(0) + self.on_connected=event_storage.pop(0) + self.on_disconnected=event_storage.pop(0) + self.on_mouth_open=event_storage.pop(0) + self.on_mouth_close=event_storage.pop(0) + self.on_power_plugged=event_storage.pop(0) + self.on_power_unplugged=event_storage.pop(0) + self.on_left_blue_led_on=event_storage.pop(0) + self.on_left_blue_led_off=event_storage.pop(0) + self.on_right_blue_led_on=event_storage.pop(0) + self.on_right_blue_led_off=event_storage.pop(0) + self.on_eyes_open=event_storage.pop(0) + self.on_eyes_close=event_storage.pop(0) + self.on_rf_connected=event_storage.pop(0) + self.on_rf_disconnected=event_storage.pop(0) + self.on_pong_received=event_storage.pop(0) + self.on_mouth_stop=event_storage.pop(0) + self.on_eyes_stop=event_storage.pop(0) + self.on_wings_stop=event_storage.pop(0) + self.on_spin_stop=event_storage.pop(0) + self.parent.tts.on_sound_on=event_storage.pop(0) + self.parent.tts.on_sound_off=event_storage.pop(0) #-------------------------------------------------------------------------- # Wait until the specified status arrives @@ -832,31 +928,28 @@ >>> var=tux.event.wait_status(DATAS_STATUS_HEAD_PUSH_SWITCH,1,2) (see 'tuxapi_const.py' for the complete list of statuses) """ - if not self.parent.daemon.connected: + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_RSP,\ + SUBDATA_TP_STATUS,DATA_STATUS,DATA_VALUE,0,0,0,0,0,\ + 0,0,0,0,0) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(time_out) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: return False - time_beginin=self.parent.sys.time() - while True: - if self.parent.daemon.connected: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=time_out and time_out!=9999: - return False - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_TUX: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_RSP: - if ord(data[3])==SUBDATA_TP_STATUS: - if ord(data[4])==DATA_STATUS: - if ord(data[5])==DATA_VALUE: - return True - if (self.parent.sys.time()-time_beginin)>=time_out and time_out!=9999: - return False - time.sleep(0.001) + else: + return True #-------------------------------------------------------------------------- # Wait for stable status @@ -903,37 +996,43 @@ Example: >>> tux.event.wait_bt_pushed(10) """ - if not self.parent.daemon.connected: - return 0 - time_beginin=self.parent.sys.time() - while True: - if self.parent.daemon.connected: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=time_out and time_out!=9999: - return 0 - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_TUX: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_RSP: - if ord(data[3])==SUBDATA_TP_STATUS: - if ord(data[4])==DATAS_STATUS_HEAD_PUSH_SWITCH: - if ord(data[5])==1: - return HEAD_BT - elif ord(data[4])==DATAS_STATUS_LEFT_WING_PUSH: - if ord(data[5])==1: - return LEFT_WING_BT - elif ord(data[4])==DATAS_STATUS_RIGHT_WING_PUSH: - if ord(data[5])==1: - return RIGHT_WING_BT - if (self.parent.sys.time()-time_beginin)>=time_out and time_out!=9999: - return 0 - time.sleep(0.001) + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_RSP,\ + SUBDATA_TP_STATUS,DATAS_STATUS_HEAD_PUSH_SWITCH,1,0,0,0,0,0,\ + 0,0,0,0,0) + data_to_match_list.append(data_to_match) + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_RSP,\ + SUBDATA_TP_STATUS,DATAS_STATUS_LEFT_WING_PUSH,1,0,0,0,0,0,\ + 0,0,0,0,0) + data_to_match_list.append(data_to_match) + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_RSP,\ + SUBDATA_TP_STATUS,DATAS_STATUS_RIGHT_WING_PUSH,1,0,0,0,0,0,\ + 0,0,0,0,0) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(time_out) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: + return NONE_BT + else: + if ord(returned_data[0][4]) == DATAS_STATUS_HEAD_PUSH_SWITCH: + return HEAD_BT + if ord(returned_data[0][4]) == DATAS_STATUS_LEFT_WING_PUSH: + return LEFT_WING_BT + if ord(returned_data[0][4]) == DATAS_STATUS_RIGHT_WING_PUSH: + return RIGHT_WING_BT #-------------------------------------------------------------------------- # Wait until head button is pushed @@ -956,7 +1055,7 @@ #-------------------------------------------------------------------------- # Wait until head button is released #-------------------------------------------------------------------------- - def wait_head_bt_released(self): + def wait_head_bt_released(self,time_out): """ Wait until head button is released @@ -969,7 +1068,7 @@ Example: >>> tux.event.wait_head_bt_released(2) """ - return self.wait_status(DATAS_STATUS_HEAD_PUSH_SWITCH,0,9999) + return self.wait_status(DATAS_STATUS_HEAD_PUSH_SWITCH,0,time_out) #-------------------------------------------------------------------------- # Wait until left wing is pushed @@ -992,7 +1091,7 @@ #-------------------------------------------------------------------------- # Wait until left wing is released #-------------------------------------------------------------------------- - def wait_lwing_bt_released(self): + def wait_lwing_bt_released(self,time_out): """ Wait until left wing is released @@ -1005,7 +1104,7 @@ Example: >>> tux.event.wait_lwing_bt_released(2) """ - return self.wait_status(DATAS_STATUS_LEFT_WING_PUSH,0,9999) + return self.wait_status(DATAS_STATUS_LEFT_WING_PUSH,0,time_out) #-------------------------------------------------------------------------- # Wait until right wing is pushed @@ -1028,7 +1127,7 @@ #-------------------------------------------------------------------------- # Wait until right wing is released #-------------------------------------------------------------------------- - def wait_rwing_bt_released(self): + def wait_rwing_bt_released(self,time_out): """ Wait until right wing is released @@ -1041,7 +1140,7 @@ Example: >>> tux.event.wait_rwing_bt_released(2) """ - return self.wait_status(DATAS_STATUS_RIGHT_WING_PUSH,0,9999) + return self.wait_status(DATAS_STATUS_RIGHT_WING_PUSH,0,time_out) #-------------------------------------------------------------------------- # Wait until a specified key of the remote is pressed @@ -1191,27 +1290,28 @@ """ Not a user function """ - time_beginin=self.parent.sys.time() - while True: - if self.parent.daemon.connected: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=2: - return ACK_CMD_TIMEOUT - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_TUX: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_ACK_CMD: - result=ord(data[4]) - return result - if (self.parent.sys.time()-time_beginin)>=2: - return ACK_CMD_TIMEOUT - time.sleep(0.001) + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_ACK_CMD,\ + 999,999,999,999,999,999,999,999,\ + 999,999,999,999,999) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(2) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: + return ACK_CMD_TIMEOUT + else: + return ord(returned_data[0][4]) #-------------------------------------------------------------------------- # Send a structured command to tux @@ -1963,8 +2063,10 @@ Example: >>> tux.cmd.audio_channel_general() """ + self.no_ack=True self.last_ack=self.structured(TUX_CMD_STRUCT_AUDIO_CHANNEL,\ TUX_CMD_STRUCT_SUB_CH_GENERAL,0,0,0) + self.no_ack=False #-------------------------------------------------------------------------- # Send a command to tux for selecting the "TTS" audio channel in the dongle @@ -1977,8 +2079,10 @@ Example: >>> tux.cmd.audio_channel_tts() """ + self.no_ack=True self.last_ack=self.structured(TUX_CMD_STRUCT_AUDIO_CHANNEL,\ TUX_CMD_STRUCT_SUB_CH_TTS,0,0,0) + self.no_ack=False #============================================================================== # TUXTCPCommunicator - status - class @@ -2044,16 +2148,19 @@ """ while self.parent.daemon.connected: if self.parent.event.on_light_level!=None: - self.parent.event.on_light_level(self.light_level()) + self.parent.event.on_light_level(self.get_light_level()) self.parent.sys.wait(1) #-------------------------------------------------------------------------- # Convert the current raw statuses to an explicit string #-------------------------------------------------------------------------- - def to_string(self): + def to_string(self,frame): """ Convert the current raw statuses to an explicit string + Parameters: + "frame" as tuple of char : raw statuses + Return a string Example: @@ -2070,7 +2177,7 @@ line="" status=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] self.parent.tcp_mutex.acquire() - for i, data in enumerate(self.parent.tcp_data): + for i, data in enumerate(frame): status[i]=ord(data) self.parent.tcp_mutex.release() # -- Tux status @@ -2170,26 +2277,28 @@ """ Not a user function """ - time_beginin=self.parent.sys.time() - while True: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=2: - return False - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_TUX: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_RSP: - if ord(data[4])==DATA_STATUS: - return True - if (self.parent.sys.time()-time_beginin)>=2: - return False - time.sleep(0.001) + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_TUX,SS_DEFAULT,DATA_TP_RSP,\ + 999,DATA_STATUS,999,999,999,999,999,999,\ + 999,999,999,999,999) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(2) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: + return None + else: + return returned_data[0] #-------------------------------------------------------------------------- # Get a specified status @@ -2216,6 +2325,22 @@ return self.rsp_status(DATA_STATUS) #-------------------------------------------------------------------------- + # Get a specified status ( one value ) + #-------------------------------------------------------------------------- + def get_one_status(self,DATA_STATUS): + """ + Not a user function + """ + frame = self.get(DATA_STATUS) + try: + if len(frame) > 0: + return ord(frame[5]) + else: + return 0 + except: + return 0 + + #-------------------------------------------------------------------------- # Get the last state of wings motor backward status #-------------------------------------------------------------------------- def get_wings_motor_backward(self): @@ -2233,11 +2358,7 @@ Example: >>> var = tux.status.wings_backward() """ - if self.get(DATAS_STATUS_WINGS_MOTOR_BACKWARD): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_WINGS_MOTOR_BACKWARD) #-------------------------------------------------------------------------- # Get the last state of spin motor backward status @@ -2256,11 +2377,7 @@ Example: >>> var = tux.status.spin_backward() """ - if self.get(DATAS_STATUS_SPIN_MOTOR_BACKWARD): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_SPIN_MOTOR_BACKWARD) #-------------------------------------------------------------------------- # Get the last state of spin motor forward status @@ -2279,11 +2396,7 @@ Example: >>> var = tux.status.spin_forward() """ - if self.get(DATAS_STATUS_SPIN_MOTOR_FORWARD): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_SPIN_MOTOR_FORWARD) #-------------------------------------------------------------------------- # Get the last state of mouth open position status @@ -2302,11 +2415,7 @@ Example: >>> var = tux.status.mouth_opened() """ - if self.get(DATAS_STATUS_MOUTH_OPEN_POSITION): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_MOUTH_OPEN_POSITION) #-------------------------------------------------------------------------- # Get the last state of mouth closed position status @@ -2325,11 +2434,7 @@ Example: >>> var = tux.status.mouth_closed() """ - if self.get(DATAS_STATUS_MOUTH_CLOSED_POSITION): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_MOUTH_CLOSED_POSITION) #-------------------------------------------------------------------------- # Get the last state of head push position status @@ -2348,11 +2453,7 @@ Example: >>> var = tux.status.head_bt() """ - if self.get(DATAS_STATUS_HEAD_PUSH_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_HEAD_PUSH_SWITCH) #-------------------------------------------------------------------------- # Get the last state of charger inhibit signal status @@ -2371,11 +2472,7 @@ Example: >>>var = tux.status.charger_state() """ - if self.get(DATAS_STATUS_CHARGER_INHIBIT_SIGNAL): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_CHARGER_INHIBIT_SIGNAL) #-------------------------------------------------------------------------- # Get the last state of wings position switch status @@ -2394,11 +2491,7 @@ Example: >>> var = tux.status.wings_bt() """ - if self.get(DATAS_STATUS_WINGS_POSITION_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_WINGS_POSITION_SWITCH) #-------------------------------------------------------------------------- # Get the last state of motor for wings status @@ -2417,11 +2510,7 @@ Example: >>> var = tux.status.wings_motor() """ - if self.get(DATAS_STATUS_MOTOR_FOR_WINGS): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_MOTOR_FOR_WINGS) #-------------------------------------------------------------------------- # Get the last state of left blue led status @@ -2440,11 +2529,7 @@ Example: >>> var = tux.status.lled() """ - if self.get(DATAS_STATUS_LEFT_BLUE_LED): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_LEFT_BLUE_LED) #-------------------------------------------------------------------------- # Get the last state of right blue led status @@ -2463,11 +2548,7 @@ Example: >>> var = tux.status.rled() """ - if self.get(DATAS_STATUS_RIGHT_BLUE_LED): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_RIGHT_BLUE_LED) #-------------------------------------------------------------------------- # Get the last state of head motor for mouth status @@ -2486,11 +2567,7 @@ Example: >>> var = tux.status.mouth_motor() """ - if self.get(DATAS_STATUS_HEAD_MOTOR_FOR_MOUTH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_HEAD_MOTOR_FOR_MOUTH) #-------------------------------------------------------------------------- # Get the last state of head motor for eyes status @@ -2509,11 +2586,7 @@ Example: >>> var = tux.status.eyes_motor() """ - if self.get(DATAS_STATUS_HEAD_MOTOR_FOR_EYES): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_HEAD_MOTOR_FOR_EYES) #-------------------------------------------------------------------------- # Get the last state of IR receiver signal status @@ -2532,11 +2605,7 @@ Example: >>> var = tux.status.ir_signal() """ - if self.get(DATAS_STATUS_IR_RECEIVER_SIGNAL): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_IR_RECEIVER_SIGNAL) #-------------------------------------------------------------------------- # Get the last state of spin position switch status @@ -2555,11 +2624,7 @@ Example: >>> var = tux.status.spin_bt() """ - if self.get(DATAS_STATUS_SPIN_POSITION_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_SPIN_POSITION_SWITCH) #-------------------------------------------------------------------------- # Get the last state of wings motor forward status @@ -2578,11 +2643,7 @@ Example: >>> var = tux.status.wings_forward() """ - if self.get(DATAS_STATUS_WINGS_MOTOR_FORWARD): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_WINGS_MOTOR_FORWARD) #-------------------------------------------------------------------------- # Get the last state of IR led status @@ -2601,11 +2662,7 @@ Example: >>> var = tux.status.ir_led() """ - if self.get(DATAS_STATUS_IR_LED): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_IR_LED) #-------------------------------------------------------------------------- # Get the last state of eyes open position switch status @@ -2624,11 +2681,7 @@ Example: >>> var = tux.status.eyes_opened() """ - if self.get(DATAS_STATUS_EYES_OPEN_POSITION_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_EYES_OPEN_POSITION_SWITCH) #-------------------------------------------------------------------------- # Get the last state of eyes closed position switch status @@ -2647,11 +2700,7 @@ Example: >>> var = tux.status.eyes_closed() """ - if self.get(DATAS_STATUS_EYES_CLOSED_POSITION_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_EYES_CLOSED_POSITION_SWITCH) #-------------------------------------------------------------------------- # Get the last state of left wing push status @@ -2670,11 +2719,7 @@ Example: >>> var = tux.status.lwing_bt() """ - if self.get(DATAS_STATUS_LEFT_WING_PUSH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_LEFT_WING_PUSH) #-------------------------------------------------------------------------- # Get the last state of right wing push status @@ -2693,11 +2738,7 @@ Example: >>> var = tux.status.rwing_bt() """ - if self.get(DATAS_STATUS_RIGHT_WING_PUSH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_RIGHT_WING_PUSH) #-------------------------------------------------------------------------- # Get the last state of power plug switch status @@ -2716,11 +2757,7 @@ Example: >>> var = tux.status.power_plug() """ - if self.get(DATAS_STATUS_POWER_PLUG_SWITCH): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_POWER_PLUG_SWITCH) #-------------------------------------------------------------------------- # Get the last state of head push switch status @@ -2748,11 +2785,7 @@ Example: >>> var = tux.status.sound_muted() """ - if self.get(DATAS_STATUS_MUTE_STATUS): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_MUTE_STATUS) #-------------------------------------------------------------------------- # Get the last state of light level status @@ -2771,11 +2804,14 @@ Example: >>> var = tux.status.light_level() """ - if self.get(DATAS_STATUS_LIGHT_LEVEL): - self.parent.tcp_mutex.acquire() - result=(ord(self.parent.tcp_data[5])*256)+ord(self.parent.tcp_data[6]) - self.parent.tcp_mutex.release() - return result + frame = self.get(DATAS_STATUS_LIGHT_LEVEL) + try: + if len(frame) > 0: + return (ord(frame[5])*256)+ord(frame[6]) + else: + return 0 + except: + return 0 #-------------------------------------------------------------------------- # Get the last state of eyes position counter status @@ -2794,11 +2830,7 @@ Example: >>> var = tux.status.eyes_counter() """ - if self.get(DATAS_STATUS_EYES_POSITION_COUNTER): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_EYES_POSITION_COUNTER) #-------------------------------------------------------------------------- # Get the last state of mouth position counter status @@ -2817,11 +2849,7 @@ Example: >>> var = tux.status.mouth_counter() """ - if self.get(DATAS_STATUS_MOUTH_POSITION_COUNTER): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_MOUTH_POSITION_COUNTER) #-------------------------------------------------------------------------- # Get the last state of wings position counter status @@ -2840,11 +2868,7 @@ Example: >>> var = tux.status.wings_counter() """ - if self.get(DATAS_STATUS_WINGS_POSITION_COUNTER): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_WINGS_POSITION_COUNTER) #-------------------------------------------------------------------------- # Get the last state of spin position counter status @@ -2863,11 +2887,7 @@ Example: >>> var = tux.status.spin_counter() """ - if self.get(DATAS_STATUS_SPIN_POSITION_COUNTER): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_SPIN_POSITION_COUNTER) #-------------------------------------------------------------------------- # Get the last state of the RF status @@ -2886,11 +2906,7 @@ Example: >>>var = tux.status.rf_state() """ - if self.get(DATAS_STATUS_RF_CONNECTED): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_RF_CONNECTED) #-------------------------------------------------------------------------- # Get the number of sound in flash @@ -2909,13 +2925,8 @@ Example: >>> var = tux.status.sounds_count() """ - if self.get(DATAS_STATUS_SOUND_COUNT): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + return self.get_one_status(DATAS_STATUS_SOUND_COUNT) - #============================================================================== # TUXTCPCommunicator - daemon - class #============================================================================== @@ -3071,6 +3082,10 @@ t.setName('_loop_tux_tcp_msg') t.start() self.con_thread_list.append(t) + t=threading.Thread(target=self.parent._loop_lock_list) + t.setName('_loop_lock_list') + t.start() + self.con_thread_list.append(t) t=threading.Thread(target=self.parent._loop_tux_data_dispatching) t.setName('_loop_tux_data_dispatching') t.start() @@ -3117,25 +3132,28 @@ """ Not a user function """ - time_beginin=self.parent.sys.time() - while True: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=2: - return ACK_CMD_TIMEOUT - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_SUB_DAEMON: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_ACK_CMD: - return ord(data[4]) - if (self.parent.sys.time()-time_beginin)>=2: - return ACK_CMD_TIMEOUT - time.sleep(0.001) + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_SUB_DAEMON,SS_DEFAULT,DATA_TP_ACK_CMD,\ + 999,999,999,999,999,999,999,999,\ + 999,999,999,999,999) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(2) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: + return ACK_CMD_TIMEOUT + else: + return ord(returned_data[0][4]) #-------------------------------------------------------------------------- # Wait a specified response status @@ -3144,27 +3162,28 @@ """ Not a user function """ - time_beginin=self.parent.sys.time() - while True: - self.parent.tcp_mutex.acquire() - data=self.parent.tcp_data - if self.parent.current_data_id==self.parent.last_threated_data_id: - self.parent.tcp_mutex.release() - time.sleep(0.001) - if (self.parent.sys.time()-time_beginin)>=2: - return False - continue - self.parent.last_threated_data_id=self.parent.current_data_id - self.parent.tcp_mutex.release() - if ord(data[0])==SOURCE_SUB_DAEMON: - if ord(data[1])==SS_DEFAULT: - if ord(data[2])==DATA_TP_RSP: - if ord(data[3])==SUBDATA_TP_INFO: - if ord(data[4])==DATA_INFO: - return True - if (self.parent.sys.time()-time_beginin)>=2: - return False - time.sleep(0.001) + # create a condition mutex + cond_lock_mutex = threading.Condition(threading.Lock()) + # create a list of frames to match + data_to_match_list = [] + # create and insert a frame to match + data_to_match = (SOURCE_SUB_DAEMON,SS_DEFAULT,DATA_TP_RSP,\ + SUBDATA_TP_INFO,DATA_INFO,999,999,999,999,999,999,\ + 999,999,999,999,999) + data_to_match_list.append(data_to_match) + # create empty frame returned + returned_data = [] + # insert theses settings in the lock list + self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) + # wait the lock has unblocked. With a time-out + cond_lock_mutex.acquire() + cond_lock_mutex.wait(2) + cond_lock_mutex.release() + # returns + if len(returned_data) == 0: + return () + else: + return returned_data[0] #-------------------------------------------------------------------------- # Send a command to tuxdaemon @@ -3217,6 +3236,7 @@ Example: >>> tux.daemon.disconnect_client(0) """ + if not self.connected : return self.last_ack=self.cmd(SUB_D_CMD_STRUC_DISCONNECT_CLIENT,id_client,0,0) #-------------------------------------------------------------------------- @@ -3226,6 +3246,7 @@ """ Not a user function """ + if not self.connected : return self.last_ack=self.cmd_no_ack(SUB_D_CMD_STRUC_DISCONNECT_CLIENT_ME,0,0,0) #-------------------------------------------------------------------------- @@ -3238,6 +3259,7 @@ Example: >>> tux.daemon.kill() """ + if not self.connected : return self.last_ack=self.cmd(SUB_D_CMD_STRUC_KILL_DAEMON,0,0,0) #-------------------------------------------------------------------------- @@ -3282,14 +3304,14 @@ Example: >>> print tux.daemon.get_version() """ - if self.request(SUB_D_REQ_INFO_VERSION,0,0,0): - result=[0,0,0] - self.parent.tcp_mutex.acquire() - result[0]=ord(self.parent.tcp_data[5]) - result[1]=ord(self.parent.tcp_data[6]) - result[2]=ord(self.parent.tcp_data[7]) - self.parent.tcp_mutex.release() - return "%d.%d.%d"%(result[0],result[1],result[2]) + if not self.connected : return '' + frame = self.request(SUB_D_REQ_INFO_VERSION,0,0,0) + result=[0,0,0] + if len(frame) > 0: + result[0]=ord(frame[5]) + result[1]=ord(frame[6]) + result[2]=ord(frame[7]) + return "%d.%d.%d"%(result[0],result[1],result[2]) #-------------------------------------------------------------------------- # Get the number of clients connected to tuxdaemon @@ -3303,11 +3325,12 @@ Example: >>> print tux.daemon.get_client_count() """ - if self.request(SUB_D_REQ_INFO_CLIENT_COUNT,0,0,0): - self.parent.tcp_mutex.acquire() - result=ord(self.parent.tcp_data[5]) - self.parent.tcp_mutex.release() - return result + if not self.connected : return 0 + frame = self.request(SUB_D_REQ_INFO_CLIENT_COUNT,0,0,0) + if len(frame) > 0: + return ord(frame[5]) + else: + return 0 #-------------------------------------------------------------------------- # Get the name of a client of tuxdaemon @@ -3321,11 +3344,13 @@ Example: >>> print tux.daemon.get_client_name(0) """ - if self.request(SUB_D_REQ_INFO_CLIENT_NAME,id_client,0,0): - self.parent.tcp_mutex.acquire() - struct_data = [self.parent.tcp_data[i] for i in range(5,16,1)] - self.parent.tcp_mutex.release() + if not self.connected : return '' + frame = self.request(SUB_D_REQ_INFO_CLIENT_NAME,id_client,0,0) + if len(frame) > 0: + struct_data = [frame[i] for i in range(5,16,1)] return "".join(struct_data) + else: + return '' #-------------------------------------------------------------------------- # Print the name of all the clients connected to tuxdaemon @@ -3337,6 +3362,7 @@ Example: >>> tux.daemon.print_clients_name() """ + if not self.connected : return for i in range(self.get_client_count()): print "Client %.3d : %s"%(i,self.get_client_name(i)) @@ -3352,11 +3378,12 @@ Example: >>> print tux.daemon.get_my_client_id() """ - if self.request(SUB_D_REQ_INFO_MY_CLIENT_ID,0,0,0): - self.parent.tcp_mutex.acquire() - result=ord(self.pare... [truncated message content] |