[Twisted-Python] MultiService puzzler

Christian Simms christian.simms at gmail.com
Wed Dec 12 11:16:25 EST 2007

On Dec 12, 2007 12:08 AM, Don Smith <donwsmith at gmail.com> wrote:
> Hi all,
> I'm trying to build an app using MultiService. I need to have 2 services,
> one that listens for UDP datagrams and another that can send those datagrams
> to an ActiveMQ server via a Stomp client.
> I can set up the two services just fine, but I'm stuck with how to call the
> MQ service send method from the UDPServer instance.
> Some code to illustrate:
> class GetUDP(DatagramProtocol):
>     def datagramReceived(self, datagram, address):
>         print datagram,address
>         #Send datagram to the queue server
>         # I don't know how to access the send method for the Stomp client
> service!
> testq = internet.TCPClient(APPVARIABLES['queue_server'],
> int(APPVARIABLES['queue_port']), connections.StompClientFactory
> (username='',password='',queue='/queue/test'))
> testq.setName('Testq')
> testq.setServiceParent(MQService)
> #create a UDP listener
> #TODO: this should be driven by the config file
> #reactor.listenUDP(42000, GetUDP())
> internet.UDPServer(42000,GetUDP()).setServiceParent(MQService)
> # Create an application as normal
> application = service.Application("MQExample")
> # Connect our MultiService to the application, just like a normal service.
> MQService.setServiceParent(application)
> Note: this is won't run exactly as is, but should give an idea of my
> problem. I've setup the ActiveMQ Stomp client as 'testq'. I've setup the
> UDPServer service to listen for UDP packets. I can get the UDP packets and
> print them, but I can't figure out how to hand them to my Stomp client's
> send() method. This is possible, right?
> Thanks,
> Don
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

The answer is really easy -- you don't need any twisted magic to do
this, you just need to use standard Python mechanisms.  Your UDP
listener, when it receives some data, just needs a Python reference to
your Stomp client. You could have stuffed a reference to the Stomp
client in a module global variable (naughty!), or better, you could
have stuffed it in an attribute of your GetUDP object. So you could
change this line:


to these lines:

   udpService = GetUDP()
   udpService.stompClient = testq  # stuff it in!


More information about the Twisted-Python mailing list