def dbcall(): with self.node.db.open_session() as sess: self.node.db.lock_table(sess, Peer) dbpeer = sess.query(Peer)\ .filter(Peer.node_id == peer.node_id).first() if not dbpeer: # An incoming connection from an unknown Peer. dbpeer = Peer() dbpeer.node_id = peer.node_id dbpeer.pubkey = peer.node_key.asbytes() dbpeer.distance = peer.distance dbpeer.direction = peer.direction if dbpeer.distance == 0: log.info("Peer is us! (Has the same ID!)") sess.rollback() return False, None dbpeer.address = peer.address sess.add(dbpeer) else: # Known Peer has connected to us. if dbpeer.distance == 0: log.warning("Found ourselves in the Peer table!") log.info("Peer is us! (Has the same ID!)") dbpeer.connected = False sess.delete(dbpeer) sess.commit() return False, None if dbpeer.connected: log.info("Already connected to Peer, disconnecting redundant connection.") sess.rollback() fetch_id_in_thread = dbpeer.id sess.expunge(dbpeer) return False, dbpeer # host, port = dbpeer.address.split(':') # if host != peer.protocol.address[0]: # log.info("Remote Peer host has changed, updating our db record.") # dbpeer.address = "{}:{}".format(\ # peer.protocol.address[0],\ # port) dbpeer.connected = True sess.commit() fetch_id_in_thread = dbpeer.id sess.expunge(dbpeer) return True, dbpeer
def add_peer(self, addr, process_check_connections=True): log.info("Adding peer (addr=[{}]).".format(addr)) peer = Peer() peer.address = addr added = yield from self.add_peers([peer], process_check_connections) if added: return added[0]
def dbcall(): with self.node.db.open_session() as sess: self.node.db.lock_table(sess, Peer) dbpeer = sess.query(Peer)\ .filter(Peer.node_id == peer.node_id).first() if not dbpeer: # An incoming connection from an unknown Peer. dbpeer = Peer() dbpeer.node_id = peer.node_id dbpeer.pubkey = peer.node_key.asbytes() dbpeer.distance = peer.distance dbpeer.direction = peer.direction if dbpeer.distance == 0: log.info("Peer is us! (Has the same ID!)") sess.rollback() return False, None dbpeer.address = peer.address sess.add(dbpeer) else: # Known Peer has connected to us. if dbpeer.distance == 0: log.warning("Found ourselves in the Peer table!") log.info("Peer is us! (Has the same ID!)") dbpeer.connected = False sess.delete(dbpeer) sess.commit() return False, None if dbpeer.connected: log.info( "Already connected to Peer, disconnecting redundant connection." ) sess.rollback() fetch_id_in_thread = dbpeer.id sess.expunge(dbpeer) return False, dbpeer # host, port = dbpeer.address.split(':') # if host != peer.protocol.address[0]: # log.info("Remote Peer host has changed, updating our db record.") # dbpeer.address = "{}:{}".format(\ # peer.protocol.address[0],\ # port) dbpeer.connected = True sess.commit() fetch_id_in_thread = dbpeer.id sess.expunge(dbpeer) return True, dbpeer