def __init__(self, reactor, params):
        Entity.__init__(self)

        self.reactor = reactor
        self.params = params

        # Initial property values
        self.address = Address(params.host, params.port)
        if params.node_id:
            self.id_= safe_str(params.node_id)
        else:
            self.id_ = self.CreateId(params)
        if params.pos_x or params.pos_y:
            self.position = Position((params.pos_x, params.pos_y, 0))
        else:
            self.position = self.RandomPosition()
        if params.pseudo:
            self.pseudo = safe_unicode(params.pseudo)
        else:
            self.pseudo = self.RandomPseudo()
        print "Creating node '%s'" % self.id_

        # Call parent class constructor

        # Dummy test data
        self.languages = ['fr', 'en']
 def __init__(self, id_="", type='bidir', address=""):
     assert type in ('in', 'out', 'bidir'), "Wrong service type"
     # Make sure the ID is of type str and not unicode
     # (marshalling dicts with unicode keys fails with xmlrpclib
     # in Python <2.4)
     self.id_ = safe_str(id_)
     self.type = type
     self.address = address
     self.known = True
 def GetItemPseudo(self, id_):
     """
     Returns the pseudo corresponding to a specific item.
     """
     peer = self.GetPeer(id_)
     # TODO: properly handle the case when the hovered peer
     # has been removed from the viewport.
     if peer is not None:
         return safe_str(peer.pseudo, charset=self.charset)
     else:
         return ""
    def BuildMessage(self, message):
        """
        Build protocol data from message.
        """
        lines = []
        args = message.args.__dict__
        payload = ""

        # 1. Request and protocol version
        lines.append(message.request + " " + BANNER)
        # 2. Request arguments
        for k, v in args.iteritems():
            arg_id = ATTRIBUTE_NAMES.get_reverse(k)
            if arg_id == ARG_PAYLOAD:
                payload = safe_str(v, CHARSET)
            else:
                lines.append('%s: %s' % (PROTOCOL_STRINGS[arg_id], ARGS_TO_STRING[arg_id](v)))
        # 3. End of message (double CR-LF)
        data = "\r\n".join(lines) + "\r\n\r\n" + payload
        # In debug mode, parse our own message to check it is well-formed
        assert self.ParseMessage(data, parse_only=True), "Bad generated message: " + data
        return data
    ARG_SERVICE_ID:         str,
    ARG_SERVICE_ADDRESS:    (lambda a: str(a)),
}

_to_string = {
    ARG_ACCEPT_LANGUAGES:   _accept_languages_to_string,
    ARG_ACCEPT_SERVICES:    _accept_services_to_string,
    ARG_ADDRESS:            (lambda a: a.ToString()),
    # TODO: change all coord and distance types to float
    ARG_AWARENESS_RADIUS:   (lambda x: str(long(x))),
    ARG_BEST_DISTANCE:      (lambda x: str(long(x))),
    ARG_CLOCKWISE:          (lambda c: c and "+1" or "-1"),
    ARG_HOLD_TIME:          str,
    ARG_ID:                 str,
    ARG_POSITION:           (lambda p: p.ToString()),
    ARG_PSEUDO:             (lambda u: safe_str(u, CHARSET)),
    ARG_SEND_DETECTS:       (lambda x: x and "now" or "later"),
    ARG_SERVICE_ID:         str,
    ARG_SERVICE_ADDRESS:    (lambda a: a is not None and str(a) or ""),
}

ARGS_FROM_STRING = _init_table(_from_string)
ARGS_TO_STRING = _init_table(_to_string)


#
# Declaration of mandatory arguments for each protocol request
#
NODE_ARGS = [
    ARG_ADDRESS,
    ARG_AWARENESS_RADIUS,