def plc_init(self): # Init self and container annName = 'cc_announce' self.ann_name = self.get_scoped_name('system', annName) self.start_time = pu.currenttime_ms() self.containers = {} self.contalive = {} self.last_identify = 0 # Declare CC announcement name messaging = {'name_type':'fanout', 'args':{'scope':'system'}} yield Container.configure_messaging(self.ann_name, messaging) logging.info("Declared CC anounce name: "+str(self.ann_name)) # Attach to CC announcement name annReceiver = Receiver(annName+'.'+self.receiver.label, self.ann_name) annReceiver.group = self.receiver.group self.ann_receiver = annReceiver self.ann_receiver.handle(self.receive) self.add_receiver(self.ann_receiver) annid = yield spawn(self.ann_receiver) logging.info("Listening to CC anouncements: "+str(annid)) # Start with an identify request. Will lead to an announce by myself #@TODO - Can not send a message to a base process which is not initialized! yield self.send(self.ann_name, 'identify', 'started', {'quiet':True}) # Convenience HACK: Add a few functions to container shell self._augment_shell()
def test_spawn_child(self): child1 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') self.assertEquals(child1.proc_state, 'DEFINED') pid1 = yield self.test_sup.spawn_child(child1) self.assertEquals(child1.proc_state, 'INIT_OK') proc = self._get_procinstance(pid1) self.assertEquals( str(proc.__class__), "<class 'ion.core.test.test_baseprocess.EchoProcess'>") self.assertEquals(pid1, proc.receiver.spawned.id) logging.info('Process 1 spawned and initd correctly') (cont, hdrs, msg) = yield self.test_sup.rpc_send(pid1, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly') # The following tests the process attaching a second receiver msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type': 'worker', 'args': {'scope': 'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc.proc_name, msgName) extraRec.handle(proc.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content456') self.assertEquals(cont['value'], 'content456') logging.info('Process 1 responsive correctly on second receiver')
def plc_init(self): # Init self and container annName = 'cc_announce' self.ann_name = self.get_scoped_name('system', annName) self.start_time = pu.currenttime_ms() self.containers = {} self.contalive = {} self.last_identify = 0 # Declare CC announcement name messaging = {'name_type': 'fanout', 'args': {'scope': 'system'}} yield Container.configure_messaging(self.ann_name, messaging) logging.info("Declared CC anounce name: " + str(self.ann_name)) # Attach to CC announcement name annReceiver = Receiver(annName + '.' + self.receiver.label, self.ann_name) annReceiver.group = self.receiver.group self.ann_receiver = annReceiver self.ann_receiver.handle(self.receive) self.add_receiver(self.ann_receiver) annid = yield spawn(self.ann_receiver) logging.info("Listening to CC anouncements: " + str(annid)) # Start with an identify request. Will lead to an announce by myself #@TODO - Can not send a message to a base process which is not initialized! yield self.send(self.ann_name, 'identify', 'started', {'quiet': True}) # Convenience HACK: Add a few functions to container shell self._augment_shell()
def test_message_during_init(self): child2 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') pid2 = yield self.test_sup.spawn_child(child2, init=False) proc2 = self._get_procinstance(pid2) proc2.plc_init = proc2.plc_noinit self.assertEquals(proc2.proc_state, 'NEW') msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type': 'worker', 'args': {'scope': 'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc2.proc_name, msgName) extraRec.handle(proc2.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) yield self.test_sup.send(pid2, 'init', {}, {'quiet': True}) logging.info('Sent init to process 1') yield pu.asleep(0.5) self.assertEquals(proc2.proc_state, 'INIT') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init') yield pu.asleep(2) self.assertEquals(proc2.proc_state, 'ACTIVE') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init')
def test_spawn_child(self): child1 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') self.assertEquals(child1.proc_state,'DEFINED') pid1 = yield self.test_sup.spawn_child(child1) self.assertEquals(child1.proc_state,'INIT_OK') proc = self._get_procinstance(pid1) self.assertEquals(str(proc.__class__),"<class 'ion.core.test.test_baseprocess.EchoProcess'>") self.assertEquals(pid1, proc.receiver.spawned.id) logging.info('Process 1 spawned and initd correctly') (cont,hdrs,msg) = yield self.test_sup.rpc_send(pid1,'echo','content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly') # The following tests the process attaching a second receiver msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type':'worker', 'args':{'scope':'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc.proc_name, msgName) extraRec.handle(proc.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) (cont,hdrs,msg) = yield self.test_sup.rpc_send(msgName,'echo','content456') self.assertEquals(cont['value'], 'content456') logging.info('Process 1 responsive correctly on second receiver')
def test_message_during_init(self): child2 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') pid2 = yield self.test_sup.spawn_child(child2, init=False) proc2 = self._get_procinstance(pid2) proc2.plc_init = proc2.plc_noinit self.assertEquals(proc2.proc_state, 'NEW') msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type':'worker', 'args':{'scope':'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc2.proc_name, msgName) extraRec.handle(proc2.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) yield self.test_sup.send(pid2, 'init',{},{'quiet':True}) logging.info('Sent init to process 1') yield pu.asleep(0.5) self.assertEquals(proc2.proc_state, 'INIT') (cont,hdrs,msg) = yield self.test_sup.rpc_send(msgName,'echo','content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init') yield pu.asleep(2) self.assertEquals(proc2.proc_state, 'ACTIVE') (cont,hdrs,msg) = yield self.test_sup.rpc_send(msgName,'echo','content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init')
def main(ns={}): from ion.data.datastore import registry from ion.data.dataobject import Resource yield Container.configure_messaging('mysys.registry', {'name_type':'worker', 'args':{'scope':'system'}, 'scope':'system'}) client = registry.RegistryClient(target='mysys.registry') r = Resource.create_new_resource() r.name = 'thing' yield client.register_resource(r) ref = r.reference() r2 = yield client.get_resource(ref) print r2 ns.update(locals())
def declare_messaging(messagingCfg, cgroup=None): """ Configures messaging resources. @todo this needs to go to the exchange registry service """ # for each messaging resource call Magnet to define a resource for name, msgResource in messagingCfg.iteritems(): scope = msgResource.get('args',{}).get('scope','global') msgName = name if scope == 'local': msgName = Container.id + "." + msgName elif scope == 'system': # @todo: in the root bootstrap this is ok, but HACK msgName = Container.id + "." + msgName # declare queues, bindings as needed logging.info("Messaging name config: name="+msgName+', '+str(msgResource)) yield Container.configure_messaging(msgName, msgResource)
def main(ns={}): from ion.data.datastore import registry from ion.data.dataobject import Resource yield Container.configure_messaging('mysys.registry', { 'name_type': 'worker', 'args': { 'scope': 'system' }, 'scope': 'system' }) client = registry.RegistryClient(target='mysys.registry') r = Resource.create_new_resource() r.name = 'thing' yield client.register_resource(r) ref = r.reference() r2 = yield client.get_resource(ref) print r2 ns.update(locals())
def _declare_service_name(self): # Ad hoc service exchange name declaration msgName = self.get_scoped_name('system', self.svc_name) messaging = {'name_type':'worker', 'args':{'scope':'system'}} yield Container.configure_messaging(msgName, messaging)
def _declare_service_name(self): # Ad hoc service exchange name declaration msgName = self.get_scoped_name('system', self.svc_name) messaging = {'name_type': 'worker', 'args': {'scope': 'system'}} yield Container.configure_messaging(msgName, messaging)