[Twisted-Python] Porting Apache Mina to Twisted

Hello everybody. Hope you have a good weekend. I'm trying to port my distributed job server project to twisted but I have a few questions. I just tested twisted with protocol.Protocol socket handler class and it seems like that it creates a new instance for each connection. I want to put those connection's ip addresses into an array. Where should I do that exactly? How can I get ip address and port information out of self.transport.getPeer()? That's all for now. Thank you. -- Best Regards, Umut AYDIN Skype: umut-aydin Blog: http://umut.mobi

On Sun, Dec 5, 2010 at 4:07 PM, Umut Aydin <devnetwork@gmail.com> wrote:
Each created protocol comes with a refrence to factory that is creating them, which is a right place of doing all the inter protocol operations . You can access it via factory attribute of protocol. So, in a typical twisted program you can refer to the factory by doing self.factory in the created protocol. You can define an array called peers in factory and access it like self.factory.peers. Check out buildProtocol() method of twisted factories to customize protcol instantiation. Yes, transport.getPeers() gives you the remote end host and port. -- Thanks & Regards, Godson Gera Twisted Python India <http://blog.godson.in>

Thank you for your response. I have another question. In my job server I collect worker connections into a key/value structure by their categories which means jobs and I send them some messages when a client connection made a request suitable with one of worker connections. The problem is that worker connection may not be available at the moment. I am looking for a method (like isConnected in Apache Mina) to check if remote socket is available. By the way may be I store wrong object. Maybe I should store something else instead of storing self.transport Any advice will be appreciated! Hope I could explain myself clearly. Thank you. On Sun, Dec 5, 2010 at 4:43 PM, Godson Gera <godson.g@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi

Hello again I couldn't get any answer to my question and I think I couldn't point my problem clearly. https://github.com/naimon/Jobbar/blob/master/src/lib/server.py On the line #130 I need to check if the socket is still connected. That worker variable is a copy of self.transport actually. I have found it's an instance of twisted.internet.tcp.Server I also found getHandle method to reach the actual socket but I couldn't find any method to check its availability. Help please. Thank you. On Sun, Dec 5, 2010 at 9:18 PM, Umut Aydin <devnetwork@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi

On Mon, Dec 6, 2010 at 7:41 PM, Umut Aydin <devnetwork@gmail.com> wrote:
Store a reference to the protocol object it self instead of protcol's transport. Connectiong checking can be done easily by simply adding a state attribute to your protocol class. In your protocol define self.isConnected = False . Set that attribute to True inside connectionMade() event method and set that to False inside connectionLost() event. -- Thanks & Regards, Godson Gera. Twisted Python Consultant India<http://blog.godson.in/2008/10/starpy-100a13-released-asterisk.html>

Thank you for your response. Probably that works and also itamar's approach seems right. If connectionLost method is being called every disconnection (no matter what reason is) my unregisterBySocket method handles that situation already. I mean It s not possible to have disconnected socket in my job pool(work). Right? On Mon, Dec 6, 2010 at 5:16 PM, Godson Gera <godson.g@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi

On Sun, Dec 5, 2010 at 4:07 PM, Umut Aydin <devnetwork@gmail.com> wrote:
Each created protocol comes with a refrence to factory that is creating them, which is a right place of doing all the inter protocol operations . You can access it via factory attribute of protocol. So, in a typical twisted program you can refer to the factory by doing self.factory in the created protocol. You can define an array called peers in factory and access it like self.factory.peers. Check out buildProtocol() method of twisted factories to customize protcol instantiation. Yes, transport.getPeers() gives you the remote end host and port. -- Thanks & Regards, Godson Gera Twisted Python India <http://blog.godson.in>

Thank you for your response. I have another question. In my job server I collect worker connections into a key/value structure by their categories which means jobs and I send them some messages when a client connection made a request suitable with one of worker connections. The problem is that worker connection may not be available at the moment. I am looking for a method (like isConnected in Apache Mina) to check if remote socket is available. By the way may be I store wrong object. Maybe I should store something else instead of storing self.transport Any advice will be appreciated! Hope I could explain myself clearly. Thank you. On Sun, Dec 5, 2010 at 4:43 PM, Godson Gera <godson.g@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi

Hello again I couldn't get any answer to my question and I think I couldn't point my problem clearly. https://github.com/naimon/Jobbar/blob/master/src/lib/server.py On the line #130 I need to check if the socket is still connected. That worker variable is a copy of self.transport actually. I have found it's an instance of twisted.internet.tcp.Server I also found getHandle method to reach the actual socket but I couldn't find any method to check its availability. Help please. Thank you. On Sun, Dec 5, 2010 at 9:18 PM, Umut Aydin <devnetwork@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi

On Mon, Dec 6, 2010 at 7:41 PM, Umut Aydin <devnetwork@gmail.com> wrote:
Store a reference to the protocol object it self instead of protcol's transport. Connectiong checking can be done easily by simply adding a state attribute to your protocol class. In your protocol define self.isConnected = False . Set that attribute to True inside connectionMade() event method and set that to False inside connectionLost() event. -- Thanks & Regards, Godson Gera. Twisted Python Consultant India<http://blog.godson.in/2008/10/starpy-100a13-released-asterisk.html>

Thank you for your response. Probably that works and also itamar's approach seems right. If connectionLost method is being called every disconnection (no matter what reason is) my unregisterBySocket method handles that situation already. I mean It s not possible to have disconnected socket in my job pool(work). Right? On Mon, Dec 6, 2010 at 5:16 PM, Godson Gera <godson.g@gmail.com> wrote:
-- Best Regards, Umut AYDIN Email: me@umut.mobi Skype: umut-aydin Blog: http://umut.mobi
participants (4)
-
exarkun@twistedmatrix.com
-
Godson Gera
-
Itamar Turner-Trauring
-
Umut Aydin