test_dimc_service.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 #  @file test_dimc_service.py
00004 #  @brief An example of using a DIM service.
00005 #  This file contains an example for using a DIM service from python. A DimTask 
00006 #  object is at the same time the DIM service and client.
00007 #  Like for the DIM functions there are several steps needed to be taken:
00008 #    -# server creates a service
00009 #    -# server start 'serving' (dis_start_serving() )
00010 #    -# server updates the service
00011 #    -# client registers to a service
00012 #    -# client wait to be updated
00013 # 
00014 # @author Radu Stoica (Radu.Stoica@cern.ch)
00015 # \addtogroup examples
00016 # @{
00017 # 
00018 
00019 import sys
00020 from debug import SAY, ERROR, DEBUG
00021 import debug
00022 debug.DEBUG = 1
00023 from pydim import dis_update_service, dis_add_service, dic_info_service, dis_start_serving, dis_stop_serving
00024 from time import sleep
00025 from threading import Thread, Event
00026 
00027 ##
00028 #  Creates 3 services and registeres to them. Can be run in multiple instances.
00029 #     The class will update "UPDATES" times the services.
00030 #     
00031 class DimTask:
00032     SRV1NAME='Test_Serv1_'
00033     SRV2NAME='Test_Serv2_'
00034     SRV3NAME='Test_Serv3_'
00035     SRV1FORMAT='F:1;I:1;D:2;C:10;C' # buffer size is 35 on 32 bits 
00036     SRV2FORMAT='D' 
00037     SRV3FORMAT='C'
00038     DELAY=0.03
00039     
00040     def __init__(self, name, clients, updates=10):
00041         self.name = name
00042         self.updates = updates
00043         self.clients = clients
00044         self.svc1 = dis_add_service(DimTask.SRV1NAME+self.name, 
00045                                     DimTask.SRV1FORMAT, 
00046                                     self.service1, 1)
00047         self.svc2 = dis_add_service(DimTask.SRV2NAME+self.name, 
00048                                     DimTask.SRV2FORMAT, 
00049                                     self.service2, 2)
00050         self.svc3 = dis_add_service(DimTask.SRV3NAME+self.name, 
00051                                     DimTask.SRV3FORMAT,
00052                                     self.service3, 3)
00053         DEBUG("Service 3 created with ID ", self.svc3)
00054         DEBUG("DIMTASK %s initialized" %self.name)
00055         sleep(1)  
00056 
00057     def run(self):
00058         dis_start_serving(self.name)
00059         print self.clients
00060         for x in self.clients: 
00061             DEBUG("DIMTASK %s : Registering to service %s" 
00062                   %(self.name, DimTask.SRV1NAME+x)) 
00063             dic_info_service(DimTask.SRV1NAME+x, DimTask.SRV1FORMAT, 
00064                                    self.client_callback1)
00065             DEBUG("DIMTASK %s : Registering to service %s" 
00066                   %(self.name, DimTask.SRV2NAME+x)) 
00067             dic_info_service(DimTask.SRV2NAME+x, DimTask.SRV2FORMAT, 
00068                                    self.client_callback2)
00069             DEBUG("DIMTASK %s : Registering to service %s" 
00070                   %(self.name, DimTask.SRV3NAME+x)) 
00071             dic_info_service(DimTask.SRV3NAME+x, DimTask.SRV3FORMAT,
00072                                    self.client_callback3)
00073         counter = 0
00074         DEBUG("DIMTASK %s : Starting service update " %self.name) 
00075         while counter < self.updates:
00076             counter += 1
00077 
00078             SAY("DIMTASK %s : Updating service nr. 1" %self.name)
00079             values = (counter, counter+1, 999.0, 999.0 , 'BAU', 'B')
00080             SAY('DIMTASK %s : Updated %d clients'  
00081                 %(self.name, dis_update_service(self.svc1, values))) 
00082 
00083             SAY("DIMTASK %s : Updating service nr. 2" %self.name) 
00084             values = (counter, )
00085             SAY('DIMTASK %s : Updated %d clients' \
00086                 %(self.name, dis_update_service(self.svc2, values)))
00087 
00088             SAY("DIMTASK %s : Updating service nr. 3" %self.name)
00089             values = ('ALABALAP\x00ORTOCALA', )
00090             SAY('DIMTASK %s : Updated %d clients' \
00091                 %(self.name, dis_update_service(self.svc3, values)))
00092             sleep(DimTask.DELAY)
00093         dis_stop_serving()
00094 
00095     def service1(self):
00096         return (1, 2, 3.3, 4.4, "A", "B")
00097 
00098     def service2(self):
00099         return (666.666)
00100     
00101     def service3(self):
00102         return ("ALABALAPORTOCALA")
00103     
00104     def client_callback1(self, *args):
00105         SAY('DIMTASK %s : client callback called for service1. Args are %s' 
00106                %(self.name, args))
00107     
00108     def client_callback2(self, *args):
00109         SAY('DIMTASK %s : client callback called for service2. Args are %s' 
00110                %(self.name, args))
00111     
00112     def client_callback3(self, *args):
00113         SAY('DIMTASK %s : client callback called for service3. Args are %s' 
00114                %(self.name, args))
00115 
00116 
00117 def usage():
00118     print """Usage: python <script name> <DIM Task name> <Other DIM Clients> (comma separated)""" 
00119                
00120       
00121 if __name__=='__main__':
00122     import sys
00123     print sys.argv
00124     try:
00125         task_name = sys.argv[1]
00126     except:
00127         task_name = "task1"
00128     client_names = sys.argv[2:]
00129     if not client_names:
00130         client_names = ("task1",)
00131     task = DimTask(task_name, client_names, updates=10/DimTask.DELAY)
00132     task.run()
00133 
00134 

Generated on 5 Feb 2014 for PyDIM by  doxygen 1.4.7