私はkivyおよびtwistedフレームワークに基づくP2Pチャットアプリケーションに取り組んできました.土地のレイアウトを取得したいと思っていましたが、クライアントが(サーバーを介して)別のクライアントに接続する必要がある場合にこの問題に遭遇しました.ある種の握手をするために、
最初のステップは、クライアントに接続することでした。
conn = reactor.connectTCP(host, port, CommCoreClientFactory(self))
次に、接続に書き込みます。
conn.transport.write("data..\r\n")
ここで、接続は成功しますが、回線は通過しません。上記のコードを私の意図に従って要約しました。comm/twisscomm.py のメソッド add_peer_to_swarm(self, pid, host) を参照してください。
私の clientProtocol/Factory および serverProtocol/Factory コードは以下にあります。(それらは comm/commcoreclient.py と comm/commcoreserver.py にあります。)
クライアント プロトコル
class CommCoreClientProtocol(LineReceiver):
"""
Communications core client protocol code.
"""
def __init__(self, factory):
self._peer_host = None
self._peer_port = None
self._peer_repr = None
self.factory = factory
def connectionMade(self):
"Run when connection is established with server."
self._peer_host = self.transport.getPeer().host
self._peer_port = self.transport.getPeer().port
self._peer_repr = self._peer_host + " on " + str(self._peer_port)
Logger.debug(
"Connection success! Connected to {}".format(self._peer_repr))
def connectionLost(self, reason):
"Run when connection is lost with server."
Logger.warn("Lost connection with peer {}".format(self._peer_repr))
def lineReceived(self, line):
"Run when response is recieved from server."
response = self.factory.app.handle_response(line)
if response:
print response
Logger.debug("Recieved : {}".format(base64.b64encode(line)))
クライアントファクトリー
class CommCoreClientFactory(protocol.ReconnectingClientFactory):
protocol = CommCoreClientProtocol
def __init__(self, app):
self.app = app
def startedConnecting(self, connector):
"Run when initiaition of connection takes place."
Logger.debug("Attempting connection...")
def buildProtocol(self, addr):
"Build protocol on successful connection."
Logger.debug("Connected.")
Logger.debug("Resetting reconnection delay.")
# Reset the delay on connection success
self.resetDelay()
# Overridden build protocol
#client_protocol = self.protocol()
#client_protocol.factory = self
#return client_protocol
return CommCoreClientProtocol(self)
def clientConnectionLost(self, connector, reason):
"Run when connection with server is lost."
#self.app.print_message("connection lost")
Logger.debug("Lost connection: {}".format(reason.getErrorMessage()))
return protocol.ReconnectingClientFactory.clientConnectionLost(
self, connector, reason
)
def clientConnectionFailed(self, connector, reason):
"Run when attempt to connect with server fails."
#self.app.print_message("connection failed")
Logger.debug("Connection failed. {}".format(reason.getErrorMessage()))
return protocol.ReconnectingClientFactory.clientConnectionFailed(
self, connector, reason
)
サーバープロトコル
class CommCoreServerProtocol(LineReceiver):
"Server backend to pocess the commands"
def __init__(self):
self._peer_host = None
self._peer_port = None
self._peer_repr = None
def connectionMade(self):
"Run when connection is established with server."
self._peer_host = self.transport.getPeer().host
self._peer_port = self.transport.getPeer().port
self._peer_repr = self._peer_host + " on " + str(self._peer_port)
Logger.debug(
"Connection success! Connected to {}".format(self._peer_repr))
def connectionLost(self, reason):
"Run when connection is lost with server."
Logger.error("Lost connection with peer {}".format(self._peer_repr))
def lineReceived(self, line):
print "REVCD LINE!", line
response = self.factory.app.handle_recieved_data(line)
if response:
#self.transport.write(response)
self.sendLine(response)
サーバー工場
class CommCoreServerFactory(protocol.Factory):
protocol = CommCoreServerProtocol
def __init__(self, app):
self.app = app
(見苦しいインデントを許してください!)
どこが間違っているのか知りたいです。また、興味がある場合は、この問題を提出しました。私のコード (comm/twiscomm.py) を調べてみると、特に handle_received_data() を使用して、サーバー側で完全に機能しないものがあることがわかりますが、データが受信されないため、これは呼び出されません。