[Twisted-Python] Client Logic

Folks, I’m writing an client using twisted, and I’m basing it off the Poetry client from http://krondo.com/wp-content/uploads/2009/08/twisted-intro.html. I am porting a previous pyserial based client, so I’m running into new territory here. And it’s working fine, there are two “issues" that I am running into. class WT_Protocol(Protocol): tag = '' def to_bytes(self, seq): """convert a sequence to a bytes type""" # # from pyserial.util if isinstance(seq, bytes): return seq elif isinstance(seq, bytearray): return bytes(seq) elif isinstance(seq, memoryview): return seq.tobytes() else: b = bytearray() for item in seq: b.append(item) # this one handles int and str for our emulation and ints for Python 3.x return bytes(b) def connectionMade(self): print "Reseting the network" reset = self.to_bytes(packets.network_reset() ) self.transport.write ( reset ) self.transport.write ( self.to_bytes(packets.Set_SiteCode(NodeID=255, SC1=0, SC2=0, SC3=0) )) self.transport.write ( self.to_bytes(packets.Set_Gain_Packet(NodeID=255, Gain=1) )) # high gain self.transport.write ( self.to_bytes(packets.Set_Protocol_No_Nulls (NetworkID=0, ReceiverID=0, NodeID=1) )) # high gain time.sleep (.5) Now the client works right now, but I’m concerned that I can’t figure out how to immediately read for a reply packet from the server. For example, when I send the Set_SiteCode packet, I may receive a reply. But how would that occur in the Twisted framework? In my testing the dataReceived function is not being called, so I can’t trap it there, plus I’m not sure how I would figure that logic. Can anyone suggest a method to do this? The second question, as far as I can tell, isn’t very solvable. I’d like to add a “Press Q to quit” type logic to the code. But I don’t see a way to do this easily in twisted? (platform unspecific, but it’s running Mac OS X / Unix right now). - Ben

On Oct 6, 2014, at 5:16 PM, Benjamin Schollnick <bschollnick@gmail.com> wrote:
Now the client works right now, but I’m concerned that I can’t figure out how to immediately read for a reply packet from the server.
First, if you want to "immediately" do anything, you really need to delete that "time.sleep(.5)" line from your code. It will block the whole reactor from getting work done :).
For example, when I send the Set_SiteCode packet, I may receive a reply.
But how would that occur in the Twisted framework?
dataReceived will be called with the data when it arrives. If you're expecting the data that is being sent to be a reply to some message, you should have the code that sends that message return a Deferred; that's what Deferreds are for, representing request/response patterns.
In my testing the dataReceived function is not being called, so I can’t trap it there, plus I’m not sure how I would figure that logic.
If dataReceived is not getting called, you're not getting data, so no reply has been sent. You haven't included enough of your program for me to tell why no data is getting sent, though. Presumably you've got some stuff hooking up your serial port and running the reactor, but without seeing how that works, I can't even guess at what's wrong.
Can anyone suggest a method to do this?
dataReceived it is :).
The second question, as far as I can tell, isn’t very solvable. I’d like to add a “Press Q to quit” type logic to the code. But I don’t see a way to do this easily in twisted? (platform unspecific, but it’s running Mac OS X / Unix right now).
Where do you want to press "Q"? Do you mean you want to pop up a GUI, or wait for the user to type a "Q" in a Terminal window, or do you mean you want the thing on the other end of the serial port to send a "Q"? Also, what is "UNIX"? -g

On Oct 6, 2014, at 5:16 PM, Benjamin Schollnick <bschollnick@gmail.com> wrote:
Now the client works right now, but I’m concerned that I can’t figure out how to immediately read for a reply packet from the server.
First, if you want to "immediately" do anything, you really need to delete that "time.sleep(.5)" line from your code. It will block the whole reactor from getting work done :).
For example, when I send the Set_SiteCode packet, I may receive a reply.
But how would that occur in the Twisted framework?
dataReceived will be called with the data when it arrives. If you're expecting the data that is being sent to be a reply to some message, you should have the code that sends that message return a Deferred; that's what Deferreds are for, representing request/response patterns.
In my testing the dataReceived function is not being called, so I can’t trap it there, plus I’m not sure how I would figure that logic.
If dataReceived is not getting called, you're not getting data, so no reply has been sent. You haven't included enough of your program for me to tell why no data is getting sent, though. Presumably you've got some stuff hooking up your serial port and running the reactor, but without seeing how that works, I can't even guess at what's wrong.
Can anyone suggest a method to do this?
dataReceived it is :).
The second question, as far as I can tell, isn’t very solvable. I’d like to add a “Press Q to quit” type logic to the code. But I don’t see a way to do this easily in twisted? (platform unspecific, but it’s running Mac OS X / Unix right now).
Where do you want to press "Q"? Do you mean you want to pop up a GUI, or wait for the user to type a "Q" in a Terminal window, or do you mean you want the thing on the other end of the serial port to send a "Q"? Also, what is "UNIX"? -g
participants (2)
-
Benjamin Schollnick
-
Glyph