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
00028 pydim.dis_add_cmnd('test1', CMND1FORMAT, dummy, 1)
00029 SAY('Added command test1 with DIM format %s' %CMND1FORMAT)
00030
00031 pydim.dis_add_cmnd('test2', CMND2FORMAT, MyClass().dummy, 2)
00032 SAY('Added command test2 with DIM format %s' %CMND2FORMAT)
00033
00034 pydim.dis_add_cmnd('test3', CMND3FORMAT, dummy, 2)
00035 SAY('Added command test3 with DIM format %s' %CMND3FORMAT)
00036
00037 pydim.dis_start_serving()
00038
00039
00040 if __name__ == '__main__':
00041
00042 server_create_command_services()
00043
00044
00045 counter = 0
00046 while counter<1000 or True:
00047 counter += 1
00048 print('\n\n'+80*'-')
00049 print('Iteration number %d' %counter)
00050
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
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
00071 list_args = range(counter, counter+5)
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