| 9 | | class _WrappingProtocol(Protocol): |
| 10 | | """I wrap another protocol in order to notify my user when a connection has |
| 11 | | been made. |
| 12 | | """ |
| 13 | | def __init__(self, factory, wrappedProtocol): |
| 14 | | self.factory = factory |
| 15 | | self.wrappedProtocol = wrappedProtocol |
| 16 | | |
| 17 | | def connectionMade(self): |
| 18 | | """XXX: As soon as I am connected, I connect my wrappedProtocol, giving |
| 19 | | it my transport. Is it okay for a transport to be associated with more |
| 20 | | than one protocol? Transport calls dataReceived on me and I in turn call |
| 21 | | dataReceived on my wrappedProtocol. The wrappedProtocol may call |
| 22 | | transport.write or transport.loseConnection etc |
| 23 | | """ |
| 24 | | |
| 25 | | self.wrappedProtocol.makeConnection(self.transport) |
| 26 | | self.factory.deferred.callback(self.wrappedProtocol) |
| 27 | | |
| 28 | | def dataReceived(self, data): |
| 29 | | return self.wrappedProtocol.dataReceived(data) |
| 31 | | def connectionLost(self, reason): |
| 32 | | return self.wrappedProtocol.connectionLost(reason) |
| 33 | | |
| 34 | | class _WrappingFactory(ClientFactory): |
| 35 | | protocol = _WrappingProtocol |
| | 10 | class CallbackWhenFirstCalledMethodWrapper(object): |
| | 11 | def __init__(self, wrapped, onCalled): |
| | 12 | self.wrapped = wrapped |
| | 13 | self.onCalled = onCalled |
| | 14 | |
| | 15 | def __call__(self, otherSelf, *args, **kwargs): |
| | 16 | self.onCalled.callback(otherSelf) |
| | 17 | boundMethodName = self.wrapped.__name__ |
| | 18 | setattr(otherSelf, boundMethodName, self.wrapped) |
| | 19 | del self.wrapped |
| | 20 | return getattr(otherSelf, boundMethodName)(*args, **kwargs) |