2013-10-05 1 views
0

P2P chat application kivy 및 twisted 프레임 워크를 기반으로 작업 중이므로 토지를 얻으려고했지만이 문제에 부딪혔습니다. 클라이언트가 다른 클라이언트 (서버를 통해)에 연결해야만 핸드 셰이크를 수행해야합니다.twisted python self.transport가 서버에 연결 한 직후에 작동하지 않습니다.

초기 단계는 클라이언트에 연결하는 것이 었습니다.

conn = reactor.connectTCP(host, port, CommCoreClientFactory(self)) 

다음에 연결을 작성하십시오.

conn.transport.write("data..\r\n") 

가 여기에 연결이 성공하지만 라인이을 통과하지 않고, 내 의도에 따라 위의 코드를 응축 한 상업/twisscomm의 방법 add_peer_to_swarm (자기, PID, 호스트)를 참조하시기 바랍니다. py

내 clientProtocol/Factory 및 serverProtocol/Factory 코드는 아래에서 찾을 수 있습니다. 는 (그들은 상업/commcoreclient.py 및 상업/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 

합니다 (대충 들여 쓰기를 용서!)

내가 잘못 갈 수있는 위치를 알고 싶습니다

. 또한 관심이 있으시면 issue을 신청하셨습니다. 내 코드 (comm/twiscomm.py)를 살펴보면 handle_received_data()를 사용하여 서버 측에서 완벽하게 작동하지 않을 수도 있지만 데이터가 수신되지 않기 때문에 호출되지 않는다는 것을 알 수 있습니다.

답변

1

client howto은 Twisted의 네트워크 클라이언트 API를 사용하는 방법을 설명하고 보여줍니다.

reactor.connectTCP의 API 설명서에는 IConnector - 특히 transport 속성이없는 인터페이스가 있습니다.

+0

죄송합니다. conn.transport.write ("data \ r \ n")라고 말하려고합니다. 수정했다. 빠른 답변 감사합니다. – vaizguy

+0

변경된 질문을 반영하도록 답변을 업데이트했습니다 :-). – Glyph

+0

+1 내 기본 이해에 결함이있는 것으로 보입니다. 전송은 프로토콜 클래스를 통해서만 액세스해야합니다. 감사! – vaizguy

관련 문제