Question

I have two servers:

application = service.Application("APP")

factory_a = MyFactory()
service_1 = internet.TCPServer(LPORT_1, factory_a)
service_1.setServiceParent(application)

factory_b = MyOtherFactory()
service_2 = internet.TCPServer(LPORT_2, factory_b)
service_2.setServiceParent(application)

And now how to send data received from service_1 to service_2? service_2 is only as echo server from data received from service_1

Was it helpful?

Solution

Neither service actually sends or receives any data. All services do is manage startup and shutdown.

Neither do the factories actually send or receive any data. All factories do is create protocols to handle connections.

But since factories are long-lived and protocols are transient, factories can often serve as an object to coordinate activities between other objects.

Give factory_a a reference to factory_b and/or vice versa. Now protocols created by these factories can each get a reference to the other factory:

factory_a.factory_b = factory_b
factory_b.factory_a = factory_a

class FactoryAProtocol(Protocol):
    def foo(self):
        self.factory.factory_b

You need to go a step further if you want to get data into protocols created by the other factory. The base factory classes in Twisted don't keep track of the protocols they create. However, twisted.protocols.policies.WrappingFactory does (or you can implement the simple tracking logic yourself for your factories).

Once factory_b has a collection of the protocols it has created...

class FactoryAProtocol(Protocol):
    def foo(self):
        self.factory.factory_b.those_protocols[7]

In other words, How do I make input on one connection result in output on another?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top