@@ -63,7 +63,7 @@ def __init__(self, d, role):
6363 def send (self , outport , outdata ):
6464 if not self ._engine :
6565 return
66- self ._engine ._send (outport , outdata )
66+ self ._engine ._send (self , outport , outdata )
6767
6868 def process (self , inport , inmsg ):
6969 raise NotImplementedError ('IParticipant.process()' )
@@ -119,6 +119,8 @@ def __init__(self, broker, discovery_period=None):
119119 if isinstance (haigha , Exception ):
120120 raise haigha
121121
122+ self .participant = None
123+
122124 # Prepare connection to AMQP broker
123125 vhost = '/'
124126 if self .broker_info .path :
@@ -168,8 +170,8 @@ def ack_message(self, msg):
168170 def nack_message (self , msg ):
169171 self ._channel .basic .nack (msg .delivery_info ["delivery_tag" ])
170172
171- def _send (self , outport , data ):
172- ports = self . participant .definition ['outports' ]
173+ def _send (self , participant , outport , data ):
174+ ports = participant .definition ['outports' ]
173175 logger .debug ("Publishing to message: %s, %s, %s" % (data ,outport ,ports ))
174176 serialized = json .dumps (data )
175177 msg = haigha_Message (serialized )
@@ -243,6 +245,7 @@ def __init__(self, broker):
243245 Engine .__init__ (self , broker )
244246
245247 self ._client = mqtt .Client ()
248+ self .participants = []
246249 self .connected = False
247250
248251 if self .broker_info .username :
@@ -260,16 +263,17 @@ def __init__(self, broker):
260263 self ._client .connect (host , port , 60 )
261264
262265 def add_participant (self , participant , iips = {}):
263- self .participant = participant
264- self .participant ._engine = self
265- self .participant ._iips = iips
266+ participant ._engine = self
267+ participant ._iips = iips
268+
269+ self .participants .append (participant )
266270
267271 def run (self ):
268272 self ._message_pump_greenlet = gevent .spawn (self ._message_pump_greenthread )
269273
270- def _send (self , outport , data ):
274+ def _send (self , participant , outport , data ):
271275 logger .debug ('Participant sent on %s' % outport )
272- ports = self . participant .definition ['outports' ]
276+ ports = participant .definition ['outports' ]
273277 serialized = json .dumps (data )
274278 port = [p for p in ports if outport == p ['id' ]][0 ]
275279 queue = port ['queue' ]
@@ -299,21 +303,24 @@ def _on_connect(self, client, userdata, flags, rc):
299303 self .connected = True
300304
301305 # Subscribe to queues for inports
302- subscriptions = [] # ("topic", QoS)
303- for port in self .participant .definition ['inports' ]:
304- topic = port ['queue' ]
305- logger .debug ('subscribing to %s' % topic )
306- subscriptions .append ((topic , 0 ))
307- self ._client .subscribe (subscriptions )
306+ for participant in self .participants :
307+ subscriptions = [] # ("topic", QoS)
308+ for port in participant .definition ['inports' ]:
309+ topic = port ['queue' ]
310+ logger .debug ('subscribing to %s' % topic )
311+ subscriptions .append ((topic , 0 ))
312+ self ._client .subscribe (subscriptions )
308313
309314 # Deliver IIPs
310- deliver_iips (self .participant )
315+ for p in self .participants :
316+ deliver_iips (p )
311317
312318 # Send discovery messsage
313319 def send_discovery ():
314- while self .participant and self . connected :
320+ while self .connected :
315321 delay = self .discovery_period / 2.2
316- self ._send_discovery (self .participant .definition )
322+ for p in self .participants :
323+ self ._send_discovery (p .definition )
317324 gevent .sleep (delay ) # yields
318325 gevent .Greenlet .spawn (send_discovery )
319326
@@ -336,10 +343,14 @@ def _on_subscribe(self, client, userdata, mid, granted_qos):
336343
337344 def _on_message (self , client , userdata , mqtt_msg ):
338345 logger .debug ('got message on %s' % mqtt_msg .topic )
346+
347+ participant = None
339348 port = ""
340- for inport in self .participant .definition ['inports' ]:
341- if inport ['queue' ] == mqtt_msg .topic :
342- port = inport ['id' ]
349+ for p in self .participants :
350+ for inport in p .definition ['inports' ]:
351+ if inport ['queue' ] == mqtt_msg .topic :
352+ port = inport ['id' ]
353+ participant = p
343354
344355 def notify ():
345356 msg = Message (mqtt_msg .payload )
@@ -354,9 +365,10 @@ def notify():
354365 logger .debug ('unknown error %s' % str (e ))
355366
356367 logger .debug ('Delivering message to %s' % port )
357- self . participant .process (port , msg )
368+ participant .process (port , msg )
358369
359- gevent .spawn (notify )
370+ if port and participant :
371+ gevent .spawn (notify )
360372
361373 def _send_discovery (self , definition ):
362374 m = {
@@ -369,7 +381,7 @@ def _send_discovery(self, definition):
369381 logger .debug ('sent discovery message %s' % msg )
370382 return
371383
372- def run (participant , broker = None , done_cb = None , iips = {}):
384+ def run (participants , broker = None , done_cb = None , iips = {}):
373385 if broker is None :
374386 broker = os .environ .get ('MSGFLO_BROKER' , 'amqp://localhost' )
375387
@@ -384,7 +396,8 @@ def run(participant, broker=None, done_cb=None, iips={}):
384396
385397 if done_cb :
386398 engine .done_callback (done_cb )
387- engine .add_participant (participant , iips )
399+ for p in participants :
400+ engine .add_participant (p , iips )
388401 engine .run ()
389402
390403 return engine
@@ -413,7 +426,7 @@ def main(Participant, role=None):
413426 participant = Participant (config .role )
414427 d = participant .definition
415428 waiter = gevent .event .AsyncResult ()
416- engine = run (participant , done_cb = waiter .set , iips = config .iips )
429+ engine = run ([ participant ] , done_cb = waiter .set , iips = config .iips )
417430 anon_url = "%s://%s" % (engine .broker_info .scheme , engine .broker_info .hostname )
418431 print ("%s(%s) running on %s" % (d ['role' ], d ['component' ], anon_url ))
419432 sys .stdout .flush ()
0 commit comments