Service.
- Twisted, , . , SayStuffToServerService ( , , , , , :)), - :
class SayStuffToServerService:
def __init__(self, host, port):
def sendToServer(self, whatToSend):
def startService(self):
def stopService(self):
( , , -.)
startService() stopService() - , Twisted Services. , , Twisted Service, TCP . twisted.application.internet.TCPClient, , ProtocolFactory .
SayStuffToServerService, TCPClient:
from twisted.application import internet
class SayStuffToServerService(internet.TCPClient):
factoryclass = SayStuffToServerProtocolFactory
def __init__(self, host, port):
self.factory = self.factoryclass()
internet.TCPClient.__init__(self, host, port, self.factory)
def sendToServer(self, whatToSend):
(. SayStuffToServerProtocolFactory.)
; , , , . . application - , twistd, , , . , , .
from twisted.application import service
...
application = service.Application('say-stuff')
sttss = SayStuffToServerService('localhost', 65432)
sttss.setServiceParent(service.IServiceCollection(application))
. , twistd (.. , twistd -noy saystuff.py), application , , , SayStuffToServerService, : 65432, factory . reactor.run() .
, SayStuffToServerProtocolFactory. , , , ( sendToServer , ), factory ReconnectingClientFactory.
from twisted.internet import protocol
class SayStuffToServerProtocolFactory(protocol.ReconnectingClientFactory):
_my_live_proto = None
protocol = SayStuffToServerProtocol
, TCP- SayStuffToServerProtocol . , , , ( ). _my_live_proto factory resetDelay() , , . :
class SayStuffToServerProtocol(basic.LineReceiver):
def connectionMade(self):
self.factory.resetDelay()
self.factory._my_live_proto = self
def connectionLost(self, reason):
self.factory._my_live_proto = None
del self.factory
def sayStuff(self, stuff):
self.sendLine(stuff)
def lineReceived(self, line):
pass
twisted.protocols.basic.LineReceiver, , .
. factory _my_live_proto, , , ( ), . SayStuffToServerService.sendToServer:
class NotConnectedError(Exception):
pass
class SayStuffToServerService(internet.TCPClient):
...
def sendToServer(self, whatToSend):
if self.factory._my_live_proto is None:
raise NotConnectedError
self.factory._my_live_proto.sayStuff(whatToSend)
:
from twisted.application import internet, service
from twisted.internet import protocol
from twisted.protocols import basic
class SayStuffToServerProtocol(basic.LineReceiver):
def connectionMade(self):
self.factory.resetDelay()
self.factory._my_live_proto = self
def connectionLost(self, reason):
self.factory._my_live_proto = None
del self.factory
def sayStuff(self, stuff):
self.sendLine(stuff)
def lineReceived(self, line):
pass
class SayStuffToServerProtocolFactory(protocol.ReconnectingClientFactory):
_my_live_proto = None
protocol = SayStuffToServerProtocol
class NotConnectedError(Exception):
pass
class SayStuffToServerService(internet.TCPClient):
factoryclass = SayStuffToServerProtocolFactory
def __init__(self, host, port):
self.factory = self.factoryclass()
internet.TCPClient.__init__(self, host, port, self.factory)
def sendToServer(self, whatToSend):
if self.factory._my_live_proto is None:
raise NotConnectedError
self.factory._my_live_proto.sayStuff(whatToSend)
application = service.Application('say-stuff')
sttss = SayStuffToServerService('localhost', 65432)
sttss.setServiceParent(service.IServiceCollection(application))
, , . , , , , -, , .. .., .