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()
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 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)
else: LOGGER.logInfo( 'Last event from supplier received, counter is now ' + str(counter1)) contFlag = False count = count + 1 return #------------------------------------------------------------------------------ if __name__ == "__main__": #Create a contNcTestCounterConsumer LOGGER.logInfo('Creating contNcTestCounterConsumer') g = Consumer(COUNTER.CHANNELNAME_COUNTER) #Subscribe to statusBlockEvent events (see contNcTest_IF.idl) and register #this handler to process those events g.addSubscription(COUNTER.statusBlockEvent, counterDataHandler) #Let the Notification Service know we are ready to start processing events. g.consumerReady() #After five events have been received, disconnect from the channel LOGGER.logInfo("Waiting for events . . .") while (contFlag): sleep(0.5) LOGGER.logInfo("Events all done (%d) . . . exiting" % count) g.disconnect()
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
''' Consumer event handler ''' 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)
triplet = some_param.text[pos:] 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")
LOGGER.logInfo('The temperature difference is ' + str(someParam.absoluteDiff)) return #------------------------------------------------------------------------------ if __name__ == "__main__": print 'Making sure there is a fridge available...' #Start publishing events through a C++ Supplier simpleClient = PySimpleClient() aFridge = simpleClient.getComponent("FRIDGE1") aFridge.on() #Create a FridgeConsumer simpleClient.getLogger().logInfo('Creating FridgeConsumer') g = Consumer(FRIDGE.CHANNELNAME_FRIDGE) #Subscribe to temperatureDataBlockEvent events and register this handler to process #those events g.addSubscription(FRIDGE.temperatureDataBlockEvent, fridgeDataHandler) #Let the Notification Service know we are ready to start processing events. g.consumerReady() #After five events have been received, disconnect from the channel simpleClient.getLogger().logInfo("Waiting for events . . .") while(count<5): sleep(1) simpleClient.getLogger().logInfo("Events all done . . . exiting") g.disconnect()
count = count + 1 if count < 5: for data in someParam: print "TIME STAMP: ", data.sampTime print "VALUE: ", data.sampVal.value() print "Received: ", count return #------------------------------------------------------------------------------ if __name__ == "__main__": client = PySimpleClient() samp = client.getComponent(argv[1]) sampObj = samp.initSampObj("LAMP1", "brightness", 1000000, 10000000) print "acssampConsumer entering ..." g = Consumer("NC_LAMP1_brightness_1000000_10000000") g.addSubscription("SampDataBlockSeq", handlerFunction=dataHandler) g.consumerReady() sampObj.start() print " ACS sampling started" sleep(5) sampObj.suspend() print "ACS sampling suspended" sleep(5) sampObj.resume() print "ACS sampling resumed" sleep(6) sampObj.stop()
#!/usr/bin/env python from time import sleep from sys import argv from Acspy.Clients.SimpleClient import PySimpleClient from Acspy.Nc.Consumer import Consumer import bulkdata def dataHandler(someParam): print "----------------------------" print someParam.flow print someParam.status print someParam.timestamp print "----------------------------" return #------------------------------------------------------------------------------ if __name__ == "__main__": g = Consumer(bulkdata.CHANNELNAME_ERR_PROP) g.addSubscription(bulkdata.errorStatusBlock, handler_function=dataHandler) g.consumerReady() #After five events have been received, disconnect from the channel print "Waiting for events . . ." doit = True while(doit): try: sleep(0.1) except KeyboardInterrupt: doit = False g.disconnect()