def connect(self): base = "" key = "" self.ws = None try: self.ws = websocket.create_connection("ws://" + self.host + ":" + self.port) except Exception as e: self.err = PSS_ESOCK self.errstr = "could not connect to pss " + self.name + " on " + self.host + ":" + self.port + ": " + repr( e) return False # get the node adress self.ws.send(rpc_call(self.seq, "baseAddr", [])) self.seq += 1 resp = rpc_parse(self.ws.recv()) # verify address try: base = clean_address(resp['result']) except ValueError as e: self.err = PSS_EREMOTEINVAL self.errstr = "received bogus base address " + resp['result'] return False # retrieve the node key data self.ws.send(rpc_call(self.seq, "getPublicKey", [])) self.seq += 1 resp = rpc_parse(self.ws.recv()) # verify key try: key = clean_pubkey(resp['result']) except ValueError as e: self.err = PSS_EREMOTEINVAL self.errstr = "received bogus pubkey " + resp['result'] return False # subscribe to incoming self.ws.send( rpc_call(self.seq, "subscribe", ['receive', topic, False, False])) self.seq += 1 resp = rpc_parse(self.ws.recv()) self.sub = resp['result'] # now we're in the clear # finish setting up object properties self.key = key self.base = base self.connected = True self.run = True return True
def add(self, nick, pubkey, address): # holds the newly created contact object contact = None # brief address and key for display in buffer addrLabel = "" keyLabel = "" # no use if we're not connected # \todo use exception instead if self.ws == None or not self.connected: self.err = PSS_ESTATE self.errstr = "pss " + self.name + " not connected" return False # create the contact object try: contact = PssContact(nick, pubkey, address, self.key) except ValueError as e: self.err = PSS_ELOCALINVAL self.errstr = "invalid input for add: " + repr(e) return False # add to node and object cache self.ws.send( rpc_call(self.seq, "setPeerPublicKey", [contact.key, topic, contact.address])) #self.ws.recv() self.seq += 1 self.contacts[nick] = contact return True
def send(self, contact, msg): # check if we have connection # \todo store outgoing messages until online on temporary network loss if not self.connected: raise IOError("not connected") # send the message self.ws.send(rpc_call(self.seq, "sendAsym", [rpchex(contact.get_public_key()), topic, "0x" + msg.encode("hex")])) self.seq += 1
def add(self, contact): # no use if we're not connected # \todo use exception instead if self.ws == None or not self.connected: raise IOError("not connected") # add to node and object cache pubkeyhx = rpchex(contact.get_public_key()) overlayhx = rpchex(contact.get_overlay()) self.ws.send(rpc_call(self.seq, "setPeerPublicKey", [pubkeyhx, topic, overlayhx])) #self.ws.recv() self.seq += 1
def send(self, nick, msg): # recipient must already be added if not nick in self.contacts: self.err = PSS_ELOCALINVAL self.errstr = "no such nick " + nick return False # check if we have connection # \todo store outgoing messages until online on temporary network loss if not self.connected: self.err = PSS_ESOCK self.errstr = "not connected" return False # send the message self.ws.send( rpc_call( self.seq, "sendAsym", [self.contacts[nick].key, topic, "0x" + msg.encode("hex")])) self.seq += 1 return True