def main(argv): consumers = [] wait_sec = int(argv[0]) for ch in argv[1:]: ch = int(ch) print "Creating channel %d" % (ch) consumer = None if ch == 1: consumer = Consumer(TEST_NS_STATISTICS_SERVICE.CHANNEL_1) consumer.addSubscription(TEST_NS_STATISTICS_SERVICE.Test1EventData,handler_function=dataHandler1) elif ch == 2: consumer = Consumer(TEST_NS_STATISTICS_SERVICE.CHANNEL_2) consumer.addSubscription(TEST_NS_STATISTICS_SERVICE.Test1EventData,handler_function=dataHandler2) elif ch == 3: consumer = Consumer(TEST_NS_STATISTICS_SERVICE.CHANNEL_3) consumer.addSubscription(TEST_NS_STATISTICS_SERVICE.Test1EventData,handler_function=dataHandler3) elif ch == 4: consumer = Consumer(TEST_NS_STATISTICS_SERVICE.CHANNEL_4) consumer.addSubscription(TEST_NS_STATISTICS_SERVICE.Test1EventData,handler_function=dataHandler4) if consumer is None: raise BaseException("Unknown channel. Allowed values are from 1 to 4: %d"%(ch)) else: consumers.append(consumer) print "Enable %d consumers"%(len(consumers)) for consumer in consumers: consumer.consumerReady() if wait_sec > 0: print "Wait %d seconds"%(wait_sec) sleep(wait_sec) # Disconnect consumers print "Disconnect %d consumers"%(len(consumers)) for consumer in consumers: consumer.disconnect()
pos = triplet.find(">") if pos!=-1: triplet = triplet[:pos+1] print "Triplet received",triplet.strip() sys.stdout.flush() msgCount += 1 return if len(sys.argv) < 2: print "\n\nUsage: \n\nTestAcsAlarmSending <NUM_ALARMS_TO_SEND>\n\nwhere NUM_ALARMS_TO_SEND is how many alarms you wish to send.\n\n" else: numAlarmsToSend = int(sys.argv[1]) c = Consumer("CMW.ALARM_SYSTEM.ALARMS.SOURCES.ALARM_SYSTEM_SOURCES",None,ACS_NC_DOMAIN_ALARMSYSTEM) c.addSubscription(ACSJMSMessageEntity_idl._0_com.cosylab.acs.jms.ACSJMSMessageEntity, alarmDataHandler) c.consumerReady() # Test data for our fault family = 'Mount' member = 'ALARM_SOURCE_MOUNT' print "Testing long-hand style of sending alarms" Acsalarmpy.AlarmSystemInterfaceFactory.init() alarmSource = Acsalarmpy.AlarmSystemInterfaceFactory.createSource("ALARM_SYSTEM_SOURCES") # The heart of the test for code in range(1,numAlarmsToSend+1): # Create a test fault fltstate = Acsalarmpy.AlarmSystemInterfaceFactory.createFaultState(family,member, code)
Parameters: someParam is the real CORBA type extracted from the event. In this case, it will always be a ACSCOURSE_MOUNT.MountEventData. Returns: event handler functions return nothing. Raises: If any exception is thrown by this function, the Consumer class will catch it and call processEvent(...) which will hopefully have been overriden. ''' print "The commanded Az/El received by this consumer are:", someParam.Azimuth, ",", someParam.Elevation return #------------------------------------------------------------------------------ if __name__ == "__main__": #Create a Consumer g = Consumer(ACSCOURSE_MOUNT.MOUNT_CHANNEL) #Subscribe to MountEventData events (see the IDL for a definition) and register #this handler to process those events g.addSubscription(ACSCOURSE_MOUNT.MountEventData, handler_function=mountDataHandler) #Let the Notification Service know we are ready to start processing events. g.consumerReady() #Give suppliers 50 seconds to send events. print "Waiting for events . . ." for i in range(0,50): sleep(1) #cleanly disconnect the consumer g.disconnect() #------------------------------------------------------------------------------
#----------------------------------------------------------------------------- def dataHandler(someParam): ''' ''' global count global g if count < magicNumber: if count == (magicNumber - 1): g.removeSubscription(acsnc.EventDescription) count = count + 1 sleep(1.5) return #----------------------------------------------------------------------------- g = Consumer(str(argv[1])) g.addSubscription(acsnc.EventDescription, dataHandler) g.consumerReady() sleep(int(argv[2])) if count == magicNumber: print "Test passed!" else: print "Test failed!" g.disconnect() #-------------------------------------------------------------------------------
def responseHelper(self, incoming_channel_name, incoming_ifr_id, outgoing_channel_name, outgoing_ifr_id, delay, missed_event_chance, event_instance): ''' A fairly complex helper method which: - adds a subscription to a consumer for the incoming_ifr_id event type - whenever an event is received by the consumer, an event of outgoing_ifr_id type is PROBABLY published on the outgoing_channel_name channel after 'delay' seconds - there's a chance the event will not be published at all if 'missed_event_chance' is close to 1.0. ''' #sanity check to ensure a consumer is around if not self.consumers.has_key(incoming_channel_name): #add a consumer consumer = Consumer(incoming_channel_name) consumer.consumerReady() self.consumers[incoming_channel_name] = consumer #consumer cons = self.consumers[incoming_channel_name] #define the event handler method def eventHandler(data): ''' This event handler method: - checks the probability to ensure an event should actually be published. - pauses for a set amount of time - sends another event presumably of a different type ''' #first check the probability to see if we can #ignore the call entirely ran_float = (randrange(0, 100)) / 100.0 if ran_float > missed_event_chance: #bail self.logger.logDebug("Randomly skipped an event: " + str(ran_float)) return self.logger.logDebug( "Publishing a '" + outgoing_ifr_id + "' event on the '" + outgoing_channel_name + "' in response to receiving an event of type'" + incoming_ifr_id + "' on the '" + incoming_channel_name) #first we sleep sleep(delay) #send an event in response... if event_instance == None: supplyEventByType(self.comp_name, outgoing_channel_name, outgoing_ifr_id) else: supplyEventByInstance(self.comp_name, outgoing_channel_name, event_instance) return #ends the function definition #add the subscription; first getting at the event type event_type = incoming_ifr_id.split(":")[1].split('/').pop() #now add the subscription with the function we just defined cons.addSubscription(event_type, eventHandler)
class CounterConsumer( COUNTER__POA.CounterConsumer, #CORBA stubs for IDL interface ACSComponent, #Base IDL interface ContainerServices, #Developer niceties ComponentLifecycle): #HLA stuff ''' Simple component implementation provided as a reference for developers. ''' def __init__(self): ''' Just call superclass constructors here. ''' ACSComponent.__init__(self) ContainerServices.__init__(self) self.eventCount = 0 self.contFlag = True self.LOGGER = getLogger("CounterConsumer") return #------------------------------------------------------------------------------ #--Override ComponentLifecycle methods----------------------------------------- #------------------------------------------------------------------------------ def initialize(self): ''' Override this method inherited from ComponentLifecycle ''' self.LOGGER.logTrace("CounterConsumer.CounterConsumer") #------------------------------------------------------------------------------ def cleanUp(self): ''' Override this method inherited from ComponentLifecycle ''' if self.name == None: self.LOGGER.logInfo("Stopping main ...") else: self.LOGGER.logInfo("Destroying " + self.name + "...") #cleanly disconnect from the channel if self.Consumer != None: self.Consumer.disconnect() self.Consumer = None #------------------------------------------------------------------------------ def counterDataHandler(self, someParam): ''' This function serves only one purpose...it must do something with the extracted data from the structured event. That is, it must be capable of processing filterable_data[0].any in the structured event. We can be certain of the real type of someParam because handlers are registered only for specific types (i.e., the type_name field of a structured event). Parameters: someParam is the real CORBA type extracted from the CORBA Any at filterable_data[0]. In this case, it will always be a COUNTER.temperatureDataBlockEvent. Returns: event handler functions return nothing. Raises: If any exception is thrown by this function, the Consumer class will catch it and call processEvent(...) which will hopefully have been overriden. ''' if self.contFlag: #pattern = someParam.status onOff = someParam.onOff myString = someParam.myString counter1 = someParam.counter1 counter2 = someParam.counter2 counter3 = someParam.counter3 lastFlag = someParam.flipFlop period = someParam.period if (onOff == COUNTER.ON) and (not lastFlag): self.LOGGER.logInfo( 'Counter now %d (max %d), flag will flip at %d' % (counter1, counter2, counter3)) #self.LOGGER.logInfo('bitmask: %x' % pattern) else: self.LOGGER.logInfo(myString + ' received, counter is now ' + str(counter1)) #self.LOGGER.logInfo('bitmask: %x' % pattern) self.contFlag = False self.eventCount = self.eventCount + 1 return #------------------------------------------------------------------------------ #--Implementation of IDL methods----------------------------------------------- #------------------------------------------------------------------------------ def getBlocks(self): #Create a Consumer self.LOGGER.logInfo('Creating an instance of Consumer') self.Consumer = Consumer(COUNTER.CHANNELNAME_COUNTER) #Subscribe to statusBlockEvent events (see nctest_IF.idl) and register #this handler to process those events self.Consumer.addSubscription(COUNTER.statusBlockEvent, self.counterDataHandler) #Let the Notification Service know we are ready to start processing events. #After consumerReady() is invoked, receive(...) is invoked #by the notification channel. That is, we have no control over when #that method is called. self.Consumer.consumerReady() self.LOGGER.logInfo( "CounterConsumer is ready to receive 'status' events.") return def waitTillDone(self): while self.contFlag and (self.Consumer != None): self.LOGGER.logInfo( "CounterConsumer received %d blocks so far ..." % self.eventCount) sleep(1.0) self.LOGGER.logInfo("CounterConsumer received total of %d blocks" % self.eventCount) #cleanly disconnect from the channel if self.Consumer != None: self.Consumer.disconnect() self.Consumer = None return self.eventCount
try: consumer = Consumer(CHANNELNAME_CONTROLSYSTEM) except Exception, e: print str(e) exit() try: started = 0 ended = 0 # Create a consumer consumer.addSubscription(ExecBlockStartedEvent, handlerStarted) consumer.addSubscription(ExecBlockEndedEvent, handlerEnded) consumer.consumerReady() except ACSErrTypeCommon.UnknownEx, e: simpleClient.getLogger().logCritical("Caught an ACSException....") helperException = ACSErrTypeCommonImpl.UnknownExImpl(exception=e) helperException.Print() helperException.log() # Release it except Exception, e: simpleClient.getLogger().logAlert("Caught an exception") simpleClient.getLogger().logDebug("The exception was:" + str(e)) time.sleep(900) consumer.disconnect()
ifr_id = event._NP_RepositoryId #sanity check if not EVENT_COUNTER.has_key(ifr_id): EVENT_COUNTER[ifr_id] = 0 #increment EVENT_COUNTER[ifr_id] = EVENT_COUNTER[ifr_id] + 1 if __name__=="__main__": ec_cons = Consumer("ALMA_EVENT_CHANNEL") ec_cons.addSubscription("temperatureDataBlockEvent", eventHandler) ec_cons.addSubscription("XmlEntityStruct", eventHandler) ec_cons.consumerReady() erc_cons = Consumer("ALMA_EVENT_RESPONSE_CHANNEL") erc_cons.addSubscription("Duration", eventHandler) erc_cons.consumerReady() #create the event dispatcher ed = EventDispatcher(FAKE_MS) #sleep for awhile giving consumers a chance to process a few #events sleep(60) ec_cons.disconnect() erc_cons.disconnect() ed.destroy()
ifr_id = event._NP_RepositoryId #sanity check if not EVENT_COUNTER.has_key(ifr_id): EVENT_COUNTER[ifr_id] = 0 #increment EVENT_COUNTER[ifr_id] = EVENT_COUNTER[ifr_id] + 1 if __name__ == "__main__": ec_cons = Consumer("ALMA_EVENT_CHANNEL") ec_cons.addSubscription("temperatureDataBlockEvent", eventHandler) ec_cons.addSubscription("XmlEntityStruct", eventHandler) ec_cons.consumerReady() erc_cons = Consumer("ALMA_EVENT_RESPONSE_CHANNEL") erc_cons.addSubscription("Duration", eventHandler) erc_cons.consumerReady() #create the event dispatcher ed = EventDispatcher(FAKE_MS) #sleep for awhile giving consumers a chance to process a few #events sleep(60) ec_cons.disconnect() erc_cons.disconnect() ed.destroy()
sys.stdout.flush() msgCount += 1 return if len(sys.argv) < 2: print "\n\nUsage: \n\nTestAcsAlarmSending <NUM_ALARMS_TO_SEND>\n\nwhere NUM_ALARMS_TO_SEND is how many alarms you wish to send.\n\n" else: numAlarmsToSend = int(sys.argv[1]) c = Consumer("CMW.ALARM_SYSTEM.ALARMS.SOURCES.ALARM_SYSTEM_SOURCES", None, ACS_NC_DOMAIN_ALARMSYSTEM) c.addSubscription( ACSJMSMessageEntity_idl._0_com.cosylab.acs.jms.ACSJMSMessageEntity, alarmDataHandler) c.consumerReady() # Test data for our fault family = 'Mount' member = 'ALARM_SOURCE_MOUNT' print "Testing long-hand style of sending alarms" Acsalarmpy.AlarmSystemInterfaceFactory.init() alarmSource = Acsalarmpy.AlarmSystemInterfaceFactory.createSource( "ALARM_SYSTEM_SOURCES") # The heart of the test for code in range(1, numAlarmsToSend + 1): # Create a test fault
def responseHelper(self, incoming_channel_name, incoming_ifr_id, outgoing_channel_name, outgoing_ifr_id, delay, missed_event_chance, event_instance): ''' A fairly complex helper method which: - adds a subscription to a consumer for the incoming_ifr_id event type - whenever an event is received by the consumer, an event of outgoing_ifr_id type is PROBABLY published on the outgoing_channel_name channel after 'delay' seconds - there's a chance the event will not be published at all if 'missed_event_chance' is close to 1.0. ''' #sanity check to ensure a consumer is around if not self.consumers.has_key(incoming_channel_name): #add a consumer consumer = Consumer(incoming_channel_name) consumer.consumerReady() self.consumers[incoming_channel_name] = consumer #consumer cons = self.consumers[incoming_channel_name] #define the event handler method def eventHandler(data): ''' This event handler method: - checks the probability to ensure an event should actually be published. - pauses for a set amount of time - sends another event presumably of a different type ''' #first check the probability to see if we can #ignore the call entirely ran_float = (randrange(0,100))/100.0 if ran_float > missed_event_chance: #bail self.logger.logDebug("Randomly skipped an event: " + str(ran_float)) return self.logger.logDebug("Publishing a '" + outgoing_ifr_id + "' event on the '" + outgoing_channel_name + "' in response to receiving an event of type'" + incoming_ifr_id + "' on the '" + incoming_channel_name) #first we sleep sleep(delay) #send an event in response... if event_instance==None: supplyEventByType(self.comp_name, outgoing_channel_name, outgoing_ifr_id) else: supplyEventByInstance(self.comp_name, outgoing_channel_name, event_instance) return #ends the function definition #add the subscription; first getting at the event type event_type = incoming_ifr_id.split(":")[1].split('/').pop() #now add the subscription with the function we just defined cons.addSubscription(event_type, eventHandler)
class CounterConsumer(COUNTER__POA.CounterConsumer, #CORBA stubs for IDL interface ACSComponent, #Base IDL interface ContainerServices, #Developer niceties ComponentLifecycle): #HLA stuff ''' Simple component implementation provided as a reference for developers. ''' def __init__(self): ''' Just call superclass constructors here. ''' ACSComponent.__init__(self) ContainerServices.__init__(self) self.eventCount = 0 self.contFlag = True self.LOGGER = getLogger("CounterConsumer") return #------------------------------------------------------------------------------ #--Override ComponentLifecycle methods----------------------------------------- #------------------------------------------------------------------------------ def initialize(self): ''' Override this method inherited from ComponentLifecycle ''' self.LOGGER.logTrace("CounterConsumer.CounterConsumer") #------------------------------------------------------------------------------ def cleanUp(self): ''' Override this method inherited from ComponentLifecycle ''' if self.name == None: self.LOGGER.logInfo("Stopping main ...") else: self.LOGGER.logInfo("Destroying " + self.name + "...") #cleanly disconnect from the channel if self.Consumer != None: self.Consumer.disconnect() self.Consumer = None #------------------------------------------------------------------------------ def counterDataHandler(self,someParam): ''' This function serves only one purpose...it must do something with the extracted data from the structured event. That is, it must be capable of processing filterable_data[0].any in the structured event. We can be certain of the real type of someParam because handlers are registered only for specific types (i.e., the type_name field of a structured event). Parameters: someParam is the real CORBA type extracted from the CORBA Any at filterable_data[0]. In this case, it will always be a COUNTER.temperatureDataBlockEvent. Returns: event handler functions return nothing. Raises: If any exception is thrown by this function, the Consumer class will catch it and call processEvent(...) which will hopefully have been overriden. ''' if self.contFlag: #pattern = someParam.status onOff = someParam.onOff myString = someParam.myString counter1 = someParam.counter1 counter2 = someParam.counter2 counter3 = someParam.counter3 lastFlag = someParam.flipFlop period = someParam.period if (onOff == COUNTER.ON) and (not lastFlag): self.LOGGER.logInfo('Counter now %d (max %d), flag will flip at %d' % (counter1, counter2, counter3)) #self.LOGGER.logInfo('bitmask: %x' % pattern) else: self.LOGGER.logInfo(myString + ' received, counter is now ' + str(counter1)) #self.LOGGER.logInfo('bitmask: %x' % pattern) self.contFlag = False self.eventCount = self.eventCount + 1 return #------------------------------------------------------------------------------ #--Implementation of IDL methods----------------------------------------------- #------------------------------------------------------------------------------ def getBlocks(self): #Create a Consumer self.LOGGER.logInfo('Creating an instance of Consumer') self.Consumer = Consumer(COUNTER.CHANNELNAME_COUNTER) #Subscribe to statusBlockEvent events (see nctest_IF.idl) and register #this handler to process those events self.Consumer.addSubscription(COUNTER.statusBlockEvent, self.counterDataHandler) #Let the Notification Service know we are ready to start processing events. #After consumerReady() is invoked, receive(...) is invoked #by the notification channel. That is, we have no control over when #that method is called. self.Consumer.consumerReady() self.LOGGER.logInfo("CounterConsumer is ready to receive 'status' events.") return def waitTillDone(self): while self.contFlag and (self.Consumer != None): self.LOGGER.logInfo("CounterConsumer received %d blocks so far ..." % self.eventCount) sleep(1.0) self.LOGGER.logInfo("CounterConsumer received total of %d blocks" % self.eventCount) #cleanly disconnect from the channel if self.Consumer != None : self.Consumer.disconnect() self.Consumer = None return self.eventCount