def readchannel(*args): print'Read Channel ', args chan=args[0][0] if (chan>56): for x,y in svc.iteritems(): pydim.dis_update_service(y) else: pydim.dis_update_service(svc[chan])
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, 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, 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 handleCommand(self,cmd): self.monitor.last = int(time.time()) log(INFO,'DIM Command: '+str(cmd)) sys.stdout.flush() cb = 'handle'+cmd[0].upper()+cmd[1:] if hasattr(self,cb): if not getattr(self,cb)(): self.state = FSM.ST_ERROR if hasattr(self,'on'+FSM.ST_ERROR): getattr(self,'on'+FSM.ST_ERROR)() cmd = '' if cmd == FSM.CMD_CONFIGURE: self._setState(FSM.ST_READY) elif cmd == FSM.CMD_START: self._setState(FSM.ST_RUNNING) elif cmd == FSM.CMD_STOP: self._setState(FSM.ST_STOPPED) elif cmd == FSM.CMD_RESET: self._setState(FSM.ST_NOT_READY) elif cmd == FSM.CMD_CONTINUE: self._setState(FSM.ST_RUNNING) elif cmd == FSM.CMD_LOAD: self._setState(FSM.ST_NOT_READY) elif cmd == FSM.CMD_UNLOAD: self._setState(FSM.ST_UNKNOWN) self.__keep = False elif cmd == FSM.CMD_FORCE_RESET: self._setState(FSM.ST_UNKNOWN) self.__keep = False elif cmd == FSM.CMD_GET_STATE: pass elif cmd == FSM.CMD_STOP_TRIGGER: return elif cmd == FSM.CMD_START_TRIGGER: return else: self._setState(FSM.ST_ERROR) if hasattr(self,'on'+self.__state): getattr(self,'on'+self.__state)() pydim.dis_update_service(self.__svcID,(self.__state,)) if self.__fsmID != 0: pydim.dis_update_service(self.__fsmID)
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()
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)
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" 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:
def publish_common(self, cmd): self.dim_output_buffer = cmd if _debug: output('Publish-ALL: %s\n' % (cmd, )) pydim.dis_update_service(self.serviceID, (self.dim_output_buffer, ))
def writeDevice(self, res): self.dim_output_buffer = res if _debug: output('D: %s' % (res, )) if self.enable_stdout: output(res) pydim.dis_update_service(self.serviceID, (self.dim_output_buffer, ))
def set_status(self, status): self.__status = status pydim.dis_update_service(self.__info_svc)
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 update(self, new_state): self.__value = new_state 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 update(self, value=None): if value==None or value=="": pydim.dis_update_service(self.sid) else: pydim.dis_update_service(self.sid,(str(value),))
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
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