def start(self): log(INFO,"Start serving DIM....") pydim.dis_start_serving(self.__name) self.handleCommand(FSM.CMD_LOAD) if self.__auto: self.handleCommand(FSM.CMD_CONFIGURE) self.handleCommand(FSM.CMD_START)
def main(): global mylog, services sids= {} mylogfn= os.path.join(os.environ["VMEWORKDIR"], "WORK/masksServer") #mylog= pylog.Pylog(None,"ttyYES") # only tty (no file log) mylog= pylog.Pylog(mylogfn) dnsnode= pydim.dis_get_dns_node() if not dnsnode: mylog("No Dim DNS node found. Please set the environment variable DIM_DNS_NODE") sys.exit(1) mypid= str(os.getpid()) mylog.logm("dns:"+dnsnode+ " mypid:"+mypid) mypidfn= os.path.join(os.environ["VMEWORKDIR"], "WORK/masksServer.pid") f= open(mypidfn,"w") f.write(mypid) ; f.close() signal.signal(signal.SIGUSR1, signal_handler) # 10 SIGUSR1 signal.signal(signal.SIGHUP, signal_handler) # 1 kill -s SIGHUP mypid #signal.signal(signal.SIGKILL, signal_handler) # ? signal.signal(signal.SIGQUIT, signal_handler) # 3 signal.signal(signal.SIGTERM, signal_handler) # 15 -default signal.signal(signal.SIGINT, signal_handler) # 2 CTRL C mtall= ['B','A','C','S','SA','SC','D','E','I'] for mtag in range(len(mtall)): services.append(Service(mtall[mtag], mtag)) updateAll() pydim.dis_start_serving("CTPBCM") mylog.logm("Starting the server ...") while True: a="" try: #a= raw_input('enter 1 2 3: update from VALID.BCMASKS or q:\n') time.sleep(600) except: mylog.logm("exception:"+str(sys.exc_info()[0])) if quit=='q': a='q' else: continue if a=='q' or quit=='q': break elif a=='1': # case1 tist= epochtime() mylog.logm("updating at time:%s = %s"%(loctime(tist), str(tist))) #pydim.dis_update_service(sids[mt]) services[0].update() elif a=='2': # case2, explicit update tist= epochtime() msg="update service directly,time:%s = %s"%(loctime(tist), str(tist)) mylog.logm(msg) #pydim.dis_update_service(sids[mt],("%s"%(tist+"\0"),)) services[0].update(msg) elif a=='3': # read VALID.BCMASKS and update all service updateAll() else: #mylog.logm('bad input:%s'%a) ; continue pass pydim.dis_stop_serving() #sys.stdout.flush() mylog.close() os.remove(mypidfn)
def server_create_command_services(): pydim.dis_add_cmnd('test1', CMND1FORMAT, dummy, 1) SAY('Added command test1 with DIM format %s' %CMND1FORMAT) pydim.dis_add_cmnd('test2', CMND2FORMAT, MyClass().dummy, 2) SAY('Added command test2 with DIM format %s' %CMND2FORMAT) pydim.dis_add_cmnd('test3', CMND3FORMAT, dummy, 2) SAY('Added command test3 with DIM format %s' %CMND3FORMAT) pydim.dis_start_serving()
def server_create_command_services(): #adding the first command pydim.dis_add_cmnd('test1', CMND1FORMAT, dummy, 1) SAY('Added command test1 with DIM format %s' % CMND1FORMAT) #adding the second command pydim.dis_add_cmnd('test2', CMND2FORMAT, MyClass().dummy, 2) SAY('Added command test2 with DIM format %s' % CMND2FORMAT) #adding the third command pydim.dis_add_cmnd('test3', CMND3FORMAT, dummy, 2) SAY('Added command test3 with DIM format %s' % CMND3FORMAT) #starting DIM pydim.dis_start_serving()
def main(): cmd1 = pydim.dis_add_cmnd('example-command-1', 'F', command_callback1, 2) cmd2 = pydim.dis_add_cmnd('example-command-2', 'I:1;C', command_callback2, 3) if not cmd1 or not cmd2: print "An error occurred while registering the commands" sys.exit(1) pydim.dis_start_serving("example-commands") print "Starting the server" while True: time.sleep(1)
def __init__(self, utgid_suffix): self.__status = State.UNKNOWN from multiprocessing import Pipe, Condition, Lock self.__master_end, self.__process_end = Pipe() self.__callback_lock = Lock() utgid = self.utgid(utgid_suffix) pydim.dis_add_cmnd(utgid, "C", self.__command_callback, 1) self.__info_svc = pydim.dis_add_service(utgid + "/status", "C", self.__status_callback, 0) pydim.dis_start_serving(utgid) pydim.dis_update_service(self.__info_svc)
def runVoidTask(name=None,auto=False): utgid = '' partition = 0x103 if name is not None: utgid = name for i in xrange(len(sys.argv)): a = sys.argv[i] if a[:1] == '-a': auto=True if a[:2] == '-u': utgid = sys.argv[i+1] if a[:2] == '-p': partition = int(sys.argv[i+1]) if not len(utgid) and os.environ.has_key('UTGID'): utgid = os.environ['UTGID'] log(INFO,"FSM task: utgid:"+utgid+" auto:"+str(auto)) fsm = FSM(name=utgid, partitionid=partition, auto=auto) pydim.dis_start_serving(utgid) fsm.handleCommand(FSM.CMD_LOAD) fsm.start() while fsm.keepAlive(): time.sleep(200)
def run(self): helper = Helper(self.client, _host(), self.dns) self.process = helper.process self.output('++ Start serving: %s->%s\n' % ( helper.dns, helper.process, )) pydim.dis_set_dns_node(helper.dns) pydim.dic_set_dns_node(helper.dns) pydim.dis_start_serving(self.process) self.cmd_common = DimServiceClient((helper.dns, helper.proc_all), output=sys.stdout, raw_handler=self.common_handler) while self.enabled: try: self.startMe() self.app.wait() except Exception, X: self.output('Server Exception: %s' % (str(X), )) self.enabled = False
def __init__(self, client, node, dns): self.dns = dns self.node = node self.mode = MODE_LOCAL self.client = client self.terminate = False self.worker_log = None self.worker_cmd = None self.dim_output_buffer = '' helper = Helper(self.client, self.node, self.dns) pydim.dis_set_dns_node(helper.dns) pydim.dic_set_dns_node(helper.dns) self.serviceID = pydim.dis_add_service(helper.proc_all, 'C', self.dim_output_handler, 1) self.publish_common('') pydim.dis_start_serving('MASTER/' + helper.proc_all) output('++ DIM common service: %s->%s\n' % ( helper.dns, helper.proc_all, ))
def run(self): dis_start_serving(self.name) print self.clients for x in self.clients: DEBUG("DIMTASK %s : Registering to service %s" % (self.name, DimTask.SRV1NAME + x)) dic_info_service(DimTask.SRV1NAME + x, DimTask.SRV1FORMAT, self.client_callback1) DEBUG("DIMTASK %s : Registering to service %s" % (self.name, DimTask.SRV2NAME + x)) dic_info_service(DimTask.SRV2NAME + x, DimTask.SRV2FORMAT, self.client_callback2) DEBUG("DIMTASK %s : Registering to service %s" % (self.name, DimTask.SRV3NAME + x)) dic_info_service(DimTask.SRV3NAME + x, DimTask.SRV3FORMAT, self.client_callback3) counter = 0 DEBUG("DIMTASK %s : Starting service update " % self.name) while counter < self.updates: counter += 1 SAY("DIMTASK %s : Updating service nr. 1" % self.name) values = (counter, counter + 1, 999.0, 999.0, 'BAU', 'B') SAY('DIMTASK %s : Updated %d clients' % (self.name, dis_update_service(self.svc1, values))) SAY("DIMTASK %s : Updating service nr. 2" % self.name) values = (counter, ) SAY('DIMTASK %s : Updated %d clients' \ %(self.name, dis_update_service(self.svc2, values))) SAY("DIMTASK %s : Updating service nr. 3" % self.name) values = ('ALABALAP\x00ORTOCALA', ) SAY('DIMTASK %s : Updated %d clients' \ %(self.name, dis_update_service(self.svc3, values))) sleep(DimTask.DELAY) dis_stop_serving()
import pydim, time counter = 2 def myDummyFunc(): global counter counter += 1 if counter%3 == 1: return (1, {1:(1,2,3)}, 'CUCU') elif counter%3 == 2: return (1, {12312 : ('fdsa',1,2,3), 'fdsa': 434432243}) else: raise Exception('Unimplemented') if __name__=='__main__': pydim.PyDimRpcProxy((myDummyFunc,)) pydim.dis_start_serving() while True: time.sleep(1)
def runVoidTask(name=None,auto=False): utgid = '' partition = 0x103 if name is not None: utgid = name for i in xrange(len(sys.argv)): a = sys.argv[i] if a[:1] == '-a': auto=True if a[:2] == '-u': utgid = sys.argv[i+1] if a[:2] == '-p': partition = int(sys.argv[i+1]) if not len(utgid) and os.environ.has_key('UTGID'): utgid = os.environ['UTGID'] log(INFO,"FSM task: utgid:"+utgid+" auto:"+str(auto)) fsm = FSM(name=utgid, partitionid=partition, auto=auto) pydim.dis_start_serving(utgid) fsm.handleCommand(FSM.CMD_LOAD) fsm.start() while fsm.keepAlive(): time.sleep(200) if __name__ == "__main__": utgid = 'UTGID' if os.environ.has_key('UTGID'): utgid = os.environ['UTGID'] c = RunFileClient('/HLT/Deferred/Runs','C') f = FSM(utgid) pydim.dis_start_serving(utgid) f.handleCommand(FSM.CMD_LOAD) while 1: time.sleep(200)
def test_dis_interface(): print 80 * '-' x = pydim.dis_get_dns_node() pydim.dis_set_dns_node('tralala') if not pydim.dis_get_dns_node() == 'tralala': ERORR('get/set dns failed') sys.exit(1) pydim.dis_set_dns_node(x) print 'dis_get/dns_node functions tested' print 80 * '-' x = pydim.dis_get_dns_port() pydim.dis_set_dns_port(-2525) if not pydim.dis_get_dns_port() == 2525: ERROR('get/set dns port failed. Received ports', x, pydim.dis_get_dns_port()) pydim.dis_set_dns_port(x) print 'dis_get/dns_port functions tested' s = Struct() pydim.dis_add_exit_handler(dummy) pydim.dis_add_exit_handler(s.dummy) pydim.dis_add_error_handler(dummy) pydim.dis_add_error_handler(s.dummy) pydim.dis_add_client_exit_handler(dummy) pydim.dis_add_client_exit_handler(s.dummy) pydim.dis_start_serving() pydim.dis_stop_serving() print 'dis_update_service', pydim.dis_update_service(1) print 'dis_selective_update_service', pydim.dis_selective_update_service( 1, (1, 2, 3)) print 'dis_selective_update_service', pydim.dis_selective_update_service( 1, [1, 2, 3]) print 'dis_set_quality', pydim.dis_set_quality(1, 10) print 'dis_set_timestamp: ', pydim.dis_set_timestamp(1, 1, 1) print 'dis_remove_service: ', pydim.dis_remove_service(1) print 'dis_get_next_cmnd: ', pydim.dis_get_next_cmnd(10) print 'dis_get_client: ', pydim.dis_get_client('fdas') print 'dis_get_conn_id: ', pydim.dis_get_conn_id() print 'dis_get_timeout: ', pydim.dis_get_timeout(1, 1) print 'dis_get_client_services: ', pydim.dis_get_client_services(10) print 'dis_set_client_exit_handler', pydim.dis_set_client_exit_handler( 1, 1) print 'dis_get_error_services', pydim.dis_get_error_services() i = 1 sleep(1) pydim.dis_add_cmnd('test1', "C:20", dummy, 1) pydim.dis_add_cmnd('test2', "F:2;D:2;I:3;S:1;C:5;C:1", Struct().dummy, 2) svc1 = pydim.dis_add_service('Test Service Nr.1', "F:1;I:1;D:2;C:1;C:1;", service1, 1) svc2 = pydim.dis_add_service('Test Service Nr.2', "D:1", Struct().service2, 2) print("Starting serving services 1 and 2. Their ids are", svc1, svc2) pydim.dis_start_serving() while True: global counter if counter % 2: counter += 1 print("Updating service nr. 1") print pydim.dis_update_service( svc1, (counter, counter + 1, 999.0, 999.0, 'BAU', 'B')) print("Updating service nr. 2") print pydim.dis_update_service(svc2, 100) else: print("Updating service nr. 1") print pydim.dis_update_service(svc1) print("Updating service nr. 2") print pydim.dis_update_service(svc2) sleep(5)
def startServing(utgid): pydim.dis_start_serving(utgid)
print svname # Register another service # A service must be updated before using it. for x,y in svc.iteritems(): pydim.dis_update_service(y) # CMND3FORMAT="I:1;F" pydim.dis_add_cmnd('/WIENER/SetVoltage', CMND3FORMAT, setvoltage,0) pydim.dis_add_cmnd('/WIENER/SetCurrent', CMND3FORMAT, setcurrent,0) pydim.dis_add_cmnd('/WIENER/Switch',"I:2", switch,0) pydim.dis_add_cmnd('/WIENER/SetPeriod',"I:1", setperiod,0) pydim.dis_add_cmnd('/WIENER/ReadChannel',"I:1",readchannel,0) # Start the DIM server pydim.dis_start_serving('lyoilchv01-control') while True: # Update the service periodically print "Updating ",period,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") for x,y in svc.iteritems(): pydim.dis_update_service(y) time.sleep(period)
def main(): if not pydim.dis_get_dns_node(): print "No Dim DNS node found. Please set the environment variable DIM_DNS_NODE" sys.exit(1) print "dns:",pydim.dis_get_dns_node() scopes = pydim.dis_add_service("TTCMI/SCOPE", "C", scope_cb, 0) if not scopes: sys.stderr.write("Error registering the service TTCMI/SCOPE\n") sys.exit(1) miclock = pydim.dic_info_service("TTCMI/MICLOCK", "C", miclock_cb) beammode = pydim.dic_info_service("CTPDIM/BEAMMODE", "L:1", beammode_cb) if not miclock: print "Error registering TTCMI/MICLOCK" sys.exit(1) # A service must be updated before using it. print "Updating the services ..." pydim.dis_update_service(scopes) pydim.dis_start_serving("TTCMISCOPE") print "Starting the server ..." npass=0; tn=None # i.e. telnet closed while True: # Update the services periodically # Case 1: When `dis_update_service` is called without arguments the # callback function will be executed and its return value # will be sent to the clients. #pydim.dis_update_service(scopes) #time.sleep(1) # Case 2: When `dis_update_service` is called with arguments, they are # sent directly to the clients as the service value, *without* executing the # callback function. Please note that the number and the type of the # arguments must correspond to the service description. # #print "pass:"******"BEAM1") and ((BeamMode>="9") and (BeamMode<=11)): # # in dbg mode: alwasy with BEAM1 and flip/flop with LOCAL: if ((AliceClock=="LOCAL") and ((npass % 10)<5)) or\ (AliceClock=="BEAM1"): if tn == None: print "opening telnet..." tn= sctel.TN() if tn.prompt1=="": tn= None # can't open telnet, it's time to restart infinium: tist= epochtime() print "%s restart:"%loctime(tist), tist pydim.dis_update_service(scopes,("%s"%(tist+"\0"),)) else: scopedata= tn.measure() sd_ar= string.split(scopedata) print "%s measured:"%loctime(sd_ar[0]), scopedata if len(sd_ar)<4: print "restarting telnet (close + open with next measurement)..." tn.close() tn= None else: pydim.dis_update_service(scopes,("%s"%(scopedata+"\0"),)) else: if tn != None: print "not closing telnet..." #tn.close() ; tn= None #ts= time.strftime("%X") # hh:mm:ss #pydim.dis_update_service(scopes,("%s %s %s"%(ts, AliceClock,BeamMode),)) sys.stdout.flush() time.sleep(10) ; npass= npass+1
DEBUG=6 def stress_callback( *args): global i if i % 10000 == 0: print "stress_callback: %d" % i i += 1 def service_callback(tag): global i if i % 10000 == 0: print "stress service updated: %d" % i return (i,) if __name__=='__main__': import sys n = 500000 if len(sys.argv) > 1: n = int(sys.argv[1]) si = dic_info_service("STRESS_SVC", "I", stress_callback) s = dis_add_service("PYSTRESS_SVC", "I", service_callback, 0) dis_start_serving("PYSTRESS") while i < n or n == 0: res = dic_cmnd_service('STRESS_CMD', ('STRESSYOU!\0',), "C") dis_update_service(s) sleep(0.001) print "Stopping stress serving" dis_stop_serving() print "Stopping subscription" dic_release_service(si) print "Waiting for cleanup" sleep(5)
def main(): """ A simple DIM server with two services. """ #Server configuration # First of all check if a Dim DNS node has been configured. # Normally this is done by setting an environment variable named DIM_DNS_NODE # with the host name, e.g. 'localhost'. # pydim.dis_set_dns_node("localhost") pydim.dis_set_dns_port(631) if not pydim.dis_get_dns_node(): print("No Dim DNS node found. Please set the environment variable DIM_DNS_NODE") sys.exit(1) if not pydim.dis_get_dns_port(): print("No Dim DNS port found. Please set the environment variable DIM_DNS_PORT") sys.exit(1) print(pydim.dis_get_dns_port()) print(pydim.dis_get_dns_node()) # The function dis_add_service is used to register the service in DIM # The arguments used are the following: # 1. Service name. It must be a unique name within a DNS server. # 2. Service description string. # 3. A callback function that will be executed for getting the value of # the service. # 4. Tag. A parameter to be sent to the callback in order to identify # the service. Normally this parameter is rarely used (but it's still # mandatory, though). svc = pydim.dis_add_service("example-service-1", "C", service_callback, 0) #original # Register another service svc2 = pydim.dis_add_service("example-service-2", "D:1;I:1;", service_callback2, 0) svc3 = pydim.dis_add_service("example-service-sync","I",service_sync_callback,0) # The return value is the service identifier. It can be used to check # if the service was registered correctly. if not svc or not svc2 or not svc3: sys.stderr.write("An error occurred while registering the service\n") sys.exit(1) print("Services correctly registered") # A service must be updated before using it. print("Updating the services ...") pydim.dis_update_service(svc) pydim.dis_update_service(svc2) pydim.dis_update_service(svc3) print("") # Start the DIM server. pydim.dis_start_serving("server-name") print("Starting the server ...") # Initial values for the service 2. Please see below. val1 = 3.11 val2 = 0 # starting the loop while True: # Update the services periodically (each 5 seconds) time.sleep(5) print("") # Case 1: When `dis_update_service` is called without arguments the # callback function will be executed and its return value # will be sent to the clients. print("Updating the service 1 with the callback function") #update service 1 every 5 seconds pydim.dis_update_service(svc) # Case 2: When `dis_update_service` is called with arguments, they are # sent directly to the clients as the service value, *without* executing the # callback function. Please note that the number and the type of the # arguments must correspond to the service description. # # Update the second server each 10 seconds/time interval = 5 seconds # if val2 % 2: print("Updating the service 2 with direct values") pydim.dis_update_service(svc2, (val1, val2)) # For the sake of the example, update the values passed to svc2: val1 = val1 + 11.30 val2 = val2 + 1
pass time.sleep(1) ## # @} # ############################################################################# # Execute tests ############################################################################# if __name__ == '__main__': DEBUG("Creating PyDimRpc ...") myrpc = PyRpc() DEBUG("Creating PyDimRpcInfo ...") myrpcCaller = PyRpcInfo() DEBUG("Starting DIM ...") dis_start_serving() t = Thread(target = myrpcCaller.run) t.start() try: while True: time.sleep(1) except: DEBUG("Stopping test") e.set()