def __init__(self, name, partitionid=0, auto=False): self.monitor = Monitor(partitionid) self.__name = name self.__auto = auto self.__keep = True self.__state = FSM.ST_UNKNOWN self.__cmdID = pydim.dis_add_cmnd(name,'C',self.callback,1) self.__svcID = pydim.dis_add_service(name+'/status', 'C', self.service, 1) self.__fsmID = pydim.dis_add_service(name+'/fsm_status','L:2;I:1;C:4;I:1',self.fsm_service, 4) log(INFO,'DIM Command starting.... ') pydim.dis_update_service(self.__svcID, (self.__state,)) pydim.dis_update_service(self.__fsmID, self.fsm_service(self)) log(INFO,'DIM Command starting....Done ')
def __init__(self, name, clients, updates=10): self.name = name self.updates = updates self.clients = clients self.svc1 = dis_add_service(DimTask.SRV1NAME + self.name, DimTask.SRV1FORMAT, self.service1, 1) self.svc2 = dis_add_service(DimTask.SRV2NAME + self.name, DimTask.SRV2FORMAT, self.service2, 2) self.svc3 = dis_add_service(DimTask.SRV3NAME + self.name, DimTask.SRV3FORMAT, self.service3, 3) DEBUG("Service 3 created with ID ", self.svc3) DEBUG("DIMTASK %s initialized" % self.name) sleep(1)
def __init__(self, service, inputs, format='C'): WorkerOutput.__init__(self, inputs=inputs) dns, svc = service pydim.dis_set_dns_node(dns) self.enable_stdout = False self.dim_output_buffer = '' self.service = svc self.serviceID = pydim.dis_add_service(svc, format, self.dim_output_handler, 1)
def __init__(self, name, tag): self.name= name # "A", "E",... servicename= "CTPBCM/" + self.name self.tag= tag #mylog.logm("Adding service "+self.name) self.sid = pydim.dis_add_service(servicename, "C", scope_cb, tag) mylog.logm("%s: %d"%(servicename, self.sid)) # A service must be updated before using it. #pydim.dis_update_service(self.sid,(" ",)) -leads to error pydim.dis_update_service(self.sid) pass
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 __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 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
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 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)
imod=chan/8 ichan=chan%8 sem.acquire() if S==0: HT.setOutputSwitch(imod,ichan,HT.OFF) else: HT.setOutputSwitch(imod,ichan,HT.ON) sem.release() print 'Switch completed ' # The function dis_add_service is used to register the service in DIM for imod in range(0,7): for ichan in range(0,8): # svname="/WIENER/MODULE%d-CHANNEL%d/%d" % (imod,ichan,imod*8+ichan) srv= pydim.dis_add_service(svname, "I:1;F:4;", readhv_callback,imod*8+ichan ) svc[imod*8+ichan]=srv 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"
def __init__(self, name, format, init_state): self.__name = name self.__value = init_state self.__svcID = pydim.dis_add_service(self.__name, format, self.service, 1) pydim.dis_update_service(self.__svcID, (self.__value, ))
def __init__(self, name, init_state): self.__name = name self.__value = init_state self.__svcID = pydim.dis_add_service(self.__name, 'I', self.service, 1) pydim.dis_update_service(self.__svcID, (int(self.__value),))
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