test_dimc_cmnd_callback.py

Go to the documentation of this file.
00001 from debug import SAY, ERROR, DEBUG
00002 import sys, pydim, debug
00003 from time import sleep
00004 import gc
00005 
00006 debug._DEBUG = 1
00007 
00008 CMND1FORMAT="C"
00009 CMND2FORMAT="F:2;D:2;I:1;S:1;C"
00010 CMND3FORMAT="I"
00011 COMMAND_DELAY = 1
00012 
00013 def dummy(*args):
00014     SAY('Server: I am an unbound dummy function. I\'ve received', args)
00015 
00016 
00017 class MyClass:
00018     def dummy(self, *args):    
00019         SAY("Server: I am dummy func bound to a 'MyClass' instance. I\'ve received", args)
00020 
00021 
00022 def client_callback(tag, ret):
00023     SAY('The client callback function has received tag %d and return code %d' %(tag, ret))
00024 
00025 
00026 def server_create_command_services():
00027     #adding the first command
00028     pydim.dis_add_cmnd('test1', CMND1FORMAT, dummy, 1)
00029     SAY('Added command test1 with DIM format %s' %CMND1FORMAT)
00030     #adding the second command
00031     pydim.dis_add_cmnd('test2', CMND2FORMAT, MyClass().dummy, 2)
00032     SAY('Added command test2 with DIM format %s' %CMND2FORMAT)
00033     #adding the third command
00034     pydim.dis_add_cmnd('test3', CMND3FORMAT, dummy, 2)
00035     SAY('Added command test3 with DIM format %s' %CMND3FORMAT)
00036     #starting DIM
00037     pydim.dis_start_serving()
00038   
00039   
00040 if __name__ == '__main__':
00041     # Server configuration:
00042     server_create_command_services()
00043   
00044     #The client command execution
00045     counter = 0  
00046     while  counter<1000 or True:    
00047         counter += 1
00048         print('\n\n'+80*'-')
00049         print('Iteration number %d' %counter)
00050         # executing the first command
00051         tuple_args = ('Test call no. %03d' %counter, )
00052         res = pydim.dic_cmnd_callback('test1', tuple_args, CMND1FORMAT, client_callback, 1)
00053         if res:   
00054             SAY('Client: Successfully executed command test1 (return code %d)' %res)
00055         else:
00056             SAY('Client: Command test1 execution failed (return code %d)' %res)
00057         sleep(COMMAND_DELAY)
00058         print
00059   
00060         # second command
00061         tuple_args = (counter, counter+1, counter*9, counter*1.9, counter, counter-1, 'ABRACADABRA')
00062         res = pydim.dic_cmnd_callback('test2', tuple_args, CMND2FORMAT, client_callback, 2)
00063         if res:   
00064             SAY('Client: Successfully executed command test2 (return code %d)' %res)
00065         else:
00066             SAY('Client: Command test2 execution failed (return code %d)' %res)
00067         sleep(COMMAND_DELAY)
00068         print
00069   
00070         # last command
00071         list_args = range(counter, counter+5) #also lists are accepted        
00072         res = pydim.dic_cmnd_callback('test3', list_args, CMND3FORMAT, client_callback, 3)
00073         if res:   
00074             SAY('Client: Successfully executed command test3 (return code %d)' %res)
00075         else:
00076             SAY('Client: Command test3 execution failed (return code %d)' %res)
00077         sleep(COMMAND_DELAY)
00078         print(80*'-')
00079 
00080 
00081 

Generated on 5 Feb 2014 for PyDIM by  doxygen 1.4.7