[Twisted-Python] MultiService puzzler
donwsmith at gmail.com
Wed Dec 12 12:10:31 EST 2007
Thanks for your response, Christian!
However, this still does not work and I think it is related to the
StompClientFactory and the StompProtocol I'm using. I'm including those at
the end of this email so you can see what they do.
I changed my GetUDP class to this:
def datagramReceived(self, datagram, address):
#Send datagram to the queue server
And I changed the instantiation to what you gave me, but I get this error
when I get a UDP packet:
File "Agent_service_test.py", line 103, in datagramReceived
exceptions.NameError: global name 'stompclient' is not defined
Here are my stomp classes:
class StompProtocol(Protocol, stomper.Engine):
def __init__(self, username='', password='', queue=''):
self.username = username
self.password = password
self.counter = 1
self.log = logging.getLogger("sender")
self.queue = queue
def connected(self, msg):
"""Once I've connected I want to subscribe to the message queue.
#self.log.info("Connected: session %s. Begining say hello." %
#lc = LoopingCall(self.send)
f = stomper.Frame()
# ActiveMQ specific headers:
# prevent the messages we send comming back to us.
f.headers['activemq.noLocal'] = 'true'
def ack(self, msg):
"""Processes the received message. I don't need to
generate an ack message.
self.log.info("SENDER - received: %s " % msg['body'])
"""Send out a hello message periodically.
self.log.info("Saying hello (%d)." % self.counter)
self.counter += 1
f = stomper.Frame()
f.unpack(stomper.send(self.queue, 'hello there (%d)' % self.counter
# ActiveMQ specific headers:
f.headers['persistent'] = 'true'
"""Register with stomp server.
cmd = stomper.connect(self.username, self.password)
def dataReceived(self, data):
"""Data received, react to it and respond if needed.
msg = stomper.unpack_frame(data)
returned = self.react(msg)
def __init__(self, username='', password='',
def buildProtocol(self, addr):
if self.queue_type =='config':
return StompProtocol(self.username, self.password, self.queue)
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
print 'Connection failed. Reason:', reason
On Dec 12, 2007 8:16 AM, Christian Simms <christian.simms at gmail.com> wrote:
> 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
> > one that listens for UDP datagrams and another that can send those
> > 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
> > 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
> > 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
> > 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
> > 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!
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Twisted-Python