Ejemplo n.º 1
0
 def create_message(message):
     """extract command, address and data from message.
     
     Expected format: MESSAGE host:port data
     returns Message instance"""
     # 2 maximum splits: data may contain spaces
     items = message.split(' ', 2)
     if not len(items) >= 2:
         raise ValueError("%s should define command & host's address"\
                          % message)
     message = Message(items[0])
     message.ip, port = parse_address(items[1])
     message.port = int(port)
     # check data
     if len(items) > 2:
         message.data = items[2]
     return message
Ejemplo n.º 2
0
 def lineReceived(self, line):
     """incomming connection from other peer"""
     print "Client Manager received from %s:"\
           % self.transport.getPeer().host, line
     # on greeting, stores info about remote host (profile id)
     if line.startswith(SERVER_SEND_ID):
         # get remote information
         remote_host, remote_port = parse_address(line[len(SERVER_SEND_ID):])
         # store remote information
         client = self.factory.get_dedicated_client(remote_host)
         client.set_remote(self.factory.manager.remote_ids[remote_host],
                           remote_port)
         # download profile
         client.get_profile().addCallback(self.factory._on_profile_complete,
                                          client.peer_id)
     else:
         print "Cl.Manager received unexpected line:", line
Ejemplo n.º 3
0
def parse_message(message):
    """extract command, address and data from message.

    Expected format: MESSAGE host:port data
    returns [COMMAND, HOST, port, DATA"""
    result = []
    # 2 maximum splits: data may contain spaces
    items = message.split(' ', 2)
    message = items[0]
    # check command
    if message not in SERVICES_MESSAGES:
        raise ValueError("%s should be in %s"% (message, SERVICES_MESSAGES))
    result.append(message)
    # check address
    result += parse_address(items[1])
    # check data
    if len(items) > 2:
        data = items[2]
        result.append(data)
    else:
        result.append(None)
    return result