Ah, I realized I need to call the reference the stompClient like this 'self.stompClient'.<br><br>Unfortunately, what is returned by this:<br><br>testq = internet.TCPClient(APPVARIABLES['queue_server'], int(APPVARIABLES['queue_port']),
connections.StompClientFactory(username='',password='',queue='/queue/test'))<br><br>is the StompClientFactory object, not the StompProtocol object which contains the send() method.<br><br>So, any ideas on how I can reference the StompProtocol object (that is created by the StompClientFactory) from the GetUDP object?
<br><br>Thanks,<br><br>Don<br><br><div class="gmail_quote">On Dec 12, 2007 9:10 AM, Don Smith <<a href="mailto:donwsmith@gmail.com">donwsmith@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks for your response, Christian!<br><br>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.
<br><br>I changed my GetUDP class to this:<div class="Ih2E3d"><br><br>class GetUDP(DatagramProtocol):<br> <br> def datagramReceived(self, datagram, address):<br> print datagram,address<br> #Send datagram to the queue server
<br></div> stompclient.send(datagram)<br><br>And I changed the instantiation to what you gave me, but I get this error when I get a UDP packet:<br><br> File "Agent_service_test.py", line 103, in datagramReceived
<br> stompclient.send(datagram)<br> exceptions.NameError: global name 'stompclient' is not defined<br><br><br>Here are my stomp classes:<br><br>class StompProtocol(Protocol, stomper.Engine):<br><br>
def __init__(self, username='', password='', queue=''):
<br> stomper.Engine.__init__(self)<br> self.username = username<br> self.password = password<br> self.counter = 1<br> self.log = logging.getLogger("sender")<br> self.queue
= queue<br><br> def connected(self, msg):<br> """Once I've connected I want to subscribe to the message queue.<br> """<br> stomper.Engine.connected(self, msg)<br>
<br> #self.log.info("Connected: session %s. Begining say hello." % msg['headers']['session'])<br> #lc = LoopingCall(self.send)<br> #lc.start(1)<br><br> f = stomper.Frame
()<br> f.unpack(stomper.subscribe(self.queue))<br><br> # ActiveMQ specific headers:<br> #<br> # prevent the messages we send comming back to us.<br> f.headers['activemq.noLocal'] = 'true'
<br> <br> return f.pack()<br><br> <br> def ack(self, msg):<br> """Processes the received message. I don't need to <br> generate an ack message.<br> <br> """
<br> self.log.info("SENDER - received: %s " % msg['body'])<br> return stomper.NO_REPONSE_NEEDED<br><br><br> def send(self):<br> """Send out a hello message periodically.
<br> """<br> self.log.info("Saying hello (%d)." % self.counter)<br> self.counter += 1 <br><br> f = stomper.Frame()<br> f.unpack(stomper.send(self.queue, 'hello there (%d)' %
self.counter))<br><br> # ActiveMQ specific headers:<br> #<br> f.headers['persistent'] = 'true'<br><br> self.transport.write(f.pack())<br><br><br> def connectionMade(self):<br>
"""Register with stomp server.<br> """<br> cmd = stomper.connect(self.username, self.password)<br> self.transport.write(cmd)<br><br><br> def dataReceived(self, data):
<br> """Data received, react to it and respond if needed.<br> """<br> msg = stomper.unpack_frame(data)<br> <br> returned = self.react(msg)<br> <br> if returned:
<br> self.transport.write(returned)<br><br>class StompClientFactory(ReconnectingClientFactory):<br> def __init__(self, username='', password='', queue='',queue_type='',config=''):
<br> self.username=username<br> self.password=password<br> self.queue=queue<br> self.queue_type=queue_type<br> self.config_file=config<br> <br> <br> def buildProtocol(self, addr):
<br> if self.queue_type =='config':<br> return ConfigurationProtocol(self.queue,self.config_file,self.username, self.password)<br> else:<br> return StompProtocol(self.username,
self.password, self.queue)<br> <br> def clientConnectionLost(self, connector, reason):<br> """Lost connection<br> """<br> print 'Lost connection. Reason:', reason
<br> <br> <br> def clientConnectionFailed(self, connector, reason):<br> """Connection failed<br> """<br> print 'Connection failed. Reason:', reason
<br> ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)<div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Dec 12, 2007 8:16 AM, Christian Simms <<a href="mailto:christian.simms@gmail.com" target="_blank">
christian.simms@gmail.com
</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div>On Dec 12, 2007 12:08 AM, Don Smith <
<a href="mailto:donwsmith@gmail.com" target="_blank">donwsmith@gmail.com</a>> wrote:<br>> Hi all,<br>><br>> I'm trying to build an app using MultiService. I need to have 2 services,<br>> one that listens for UDP datagrams and another that can send those datagrams
<br>> to an ActiveMQ server via a Stomp client.<br>><br>> I can set up the two services just fine, but I'm stuck with how to call the<br>> MQ service send method from the UDPServer instance.<br>><br>> Some code to illustrate:
<br>><br>><br>><br>> class GetUDP(DatagramProtocol):<br>><br>> def datagramReceived(self, datagram, address):<br>> print datagram,address<br>> #Send datagram to the queue server
<br>> # I don't know how to access the send method for the Stomp client<br>> service!<br>><br>><br>><br>> testq = internet.TCPClient(APPVARIABLES['queue_server'],<br>> int(APPVARIABLES['queue_port']),
connections.StompClientFactory<br>> (username='',password='',queue='/queue/test'))<br>> testq.setName('Testq')<br>> testq.setServiceParent(MQService)<br>><br>><br>> #create a UDP listener
<br>> #TODO: this should be driven by the config file<br>> #reactor.listenUDP(42000, GetUDP())<br>> internet.UDPServer(42000,GetUDP()).setServiceParent(MQService)<br>> # Create an application as normal<br>> application =
service.Application("MQExample")<br>><br>> # Connect our MultiService to the application, just like a normal service.<br>> MQService.setServiceParent(application)<br>><br>><br>> Note: this is won't run exactly as is, but should give an idea of my
<br>> problem. I've setup the ActiveMQ Stomp client as 'testq'. I've setup the<br>> UDPServer service to listen for UDP packets. I can get the UDP packets and<br>> print them, but I can't figure out how to hand them to my Stomp client's
<br>> send() method. This is possible, right?<br>><br>> Thanks,<br>><br>> Don<br>><br></div></div>> _______________________________________________<br>> Twisted-Python mailing list<br>> <a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">
Twisted-Python@twistedmatrix.com</a><br>> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>><br><br>
The answer is really easy -- you don't need any twisted magic to do
<br>this, you just need to use standard Python mechanisms. Your UDP<br>listener, when it receives some data, just needs a Python reference to<br>your Stomp client. You could have stuffed a reference to the Stomp<br>client in a module global variable (naughty!), or better, you could
<br>have stuffed it in an attribute of your GetUDP object. So you could<br>change this line:<br><div><br> internet.UDPServer(42000,GetUDP()).setServiceParent(MQService)<br><br></div>to these lines:<br><br>
udpService = GetUDP()<br> udpService.stompClient = testq # stuff it in!<br> internet.UDPServer(42000,udpService).setServiceParent(MQService)<br><br>Cheers,<br>Christian<br><br>_______________________________________________
<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
</a><br></blockquote></div><br>
</div></div></blockquote></div><br>