def BuildMessage(self, message, version=None): """ Build protocol data from message. """ lines = [] args = message.args.__dict__ payload = "" version = min(version or message.version, VERSION) # 0. Remove unnecessary fields self.StripMessage(message, version) # 1. Request and protocol version first_line = message.request + " " + banner(version) lines.append(first_line) # print ">", first_line # 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
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 ""
ARG_SERVICE_ADDRESS: (lambda a: str(a)), ARG_VERSION: (lambda v: float(v)), } _to_string = { ARG_ACCEPT_LANGUAGES: _accept_languages_to_string, ARG_ACCEPT_SERVICES: _accept_services_to_string, ARG_ADDRESS: (lambda a: a.ToString()), 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_NEEDS_MIDDLEMAN: (lambda x: x and "yes" or "no"), 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 ""), ARG_VERSION: (lambda v: str(v)), } ARGS_FROM_STRING = _init_table(_from_string) ARGS_TO_STRING = _init_table(_to_string) # # Declaration of expected arguments for each protocol request # # This dict contains a request table for each Solipsis protocol version