Factory

The class that's responsible for creating a MessageLogger instance for each client that connects to our server is the MessageFactory class, which is an instance of twisted.internet.protocol.Factory. It is responsible for making protocols for each incoming connection.

buildProtocol is an event that will be called every time an incoming connection is found. It will assign a protocol to it. In this way, each connection will be tied to a protocol that's specified by the developer in this method. In this case, all connections will be handled through the same MessageLogger protocol, which forwards everything that's received.

We will make an instance of Factory that will be in charge of building the necessary objects. We will also specify that its protocol will be the class that we have made. Finally, we will make our program listen in a specific port with a reactor.

The following is the class we used for defining our Factory class. This will be instantiated every time an incoming connection is received:

class MessageFactory(Factory):
def buildProtocol(self, addr):
return MessageLogger()

The argument that buildProtocol receives is an instance of IPv4Address or IPv6Address, as appropriate. It contains information about the client, and the incoming connection, such as the IP address and port, among other things. This data can also be accessed in the protocol through the self.transport.getPeer function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset