from twisted.web import proxy
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)
class myProtocolFactory():
    protocol = proxy.Proxy
reactor.connectTCP('localhost', 80, myProtocolFactory())
reactor.run()
This barebones throws a whole lot of trace backs which helps understand the code flow a little bit easily. You can keep supplying the functions and rerun to see all the required methods of protocol factory and how the code flows or is structured.
Sequence is:
- Reactor calls : tcp.connectTCP
- Create a connector instance - connection
- connection->connect()
- protocol factory -> doStart()
- connection->_makeTransport()
- factory -> startedConnecting(connection)
- When connection is made , connection -> _connectDone() is called
- Now is the time to build a protocol instance, by calling connector-> buildProtocol()
- protocol factory -> buildProtocol (address), returns the protocol instance
- protocol instance -> makeConnection ()
    +-------+
    |reactor|------------connect interface
    +-------+                  | 1
                           tcp.connectTCP
    +---------+                | 2               +----------+
    |connector|............. connection instance |connection|
    +---------+                | 3               +----------+
       | |...................  connect()
       | |.........            | 4                  +----------------+
       |          .          5 |_ doStart() ....... |protocol factory|
       |     _makeTransport() _|  6                 +----------------+
       |                       |_ startedConnecting(connection)....||
       |                                                            |
       |                                                            |
       |                  When connection is made                   |
       |                       | 21                                 |
       | .................  _connectDone()                          |
       |                       | 22                                 |
       | ......................|_buildProtocol()                    |
                               |    | 23                            |
                               |    |_buildProtocol(address)........|
                               |24      |
                               |       returns protocol   +--------+
                   makeConnection().....  instance ...... |Protocol|
                                                          +--------+
Sequence of Failure is also called upwards:
- Failure in select : self._disconnectSelectable(selectable, why, method=="doRead")
- Calls selectable.connectionLost(failure.Failure(why))
- Communicate to Transport : Connection.connectionLost(self, reason)
- Communicate upwards: protocol.connectionLost(reason)
- Calls factory -> doStop()
failure in select -----> _disconnectSelectable(selectable,
                                 .   |              why,
           +-------+             .   |              method)
           |reactor|..............   |
           +-------+             .   |
                              selectable.connectionLost(failure(why))
                                     |
                                     |
                                     |
          +----------+               |
          |connection|....... connectionLost(reason)
          +----------+               |
                                     |
           +--------+                |
           |protocol|........ connectionLost(reason)
           +--------+                |
                                     |
             +-------+               |
             |factory|........... doStop()
             +-------+
So finally, here are the few methods that a protocol factory must support:
from twisted.web import proxy
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)
class myProtocolFactory():
    protocol = proxy.Proxy
    
    def doStart(self):
        pass
        
    def startedConnecting(self, connectorInstance):
        print connectorInstance
        
    def buildProtocol(self, address):
        print address
        return self.protocol()
        
    def clientConnectionLost(self, connection, reason):
        print reason
        print connection
        
    def doStop(self):
        pass
reactor.connectTCP('localhost', 80, myProtocolFactory())
reactor.run()
 
Hi,
ReplyDeleteYour post are great. There is only one thing that I don't understand. I discovered that Factory instance is never deleted. I have a memory leak in my program because this. For example, if I call connectTCP() 100 of times, I got 100 instances of factory that are only deleted when the program shutdown. They are not deleted even if the connection are lost.
Any hint about this?
I suggest you call reactor.connectTCP as described in the example above:
Delete# this is wrong when you are calling for connectTCP
# multiple times
reactor.connectTCP('localhost', 80, myProtocolFactory())
You should create factory only once and then reuse factory instance:
f = myProtocolFactory()
for i in range(0,100):
reactor.connectTCP('localhost', 80, f) # <- same factory instance
vavoo apk
ReplyDeleteyesmovies
ReplyDeletefinance whatsapp groups
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYour article is very helpful.You can visit my website:zedge zedge zedge
ReplyDeleteI Love your article. You can visit my website : animeid movil
ReplyDeleteDigital Tech Updates is an online resource for Latest technology Updates and Trends for the busy professionals like you.
ReplyDeleteWe provide original Industry news and trends. Throughout the day our Editorial team works on updating you with the latest news and top trends in Technology Industry.
tamilrockers proxy