test_dimc_cmnd.py

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

Generated on 5 Feb 2014 for PyDIM by  doxygen 1.4.7