def join_cluster(instance_path, source_name): print("Connecting to {}:1234".format(source_name)) # Get the peerid of the source conn, cg = network.connect(None, (source_name, 1234)) msg = pb2.JoinCluster() msg.peer_id = userconfig.get_user_config()["peerid"] msg.version = "0.1" msg.proto_version = 1 conn.send(pb2.JOIN_CLUSTER, msg) cluster_info_pkt = conn.get_result(pb2.CLUSTER_INFO) msg = cluster_info_pkt.message print("Joining cluster {}".format(msg.cluster_id)) static_info = {"cluster_id": msg.cluster_id} if not os.path.isdir(instance_path): os.makedirs(instance_path) inst = instance.Instance.create(instance_path, static_info) print("Unpickling file index") inst.fi = pickle.loads(cluster_info_pkt.data) peer_info_pkt = conn.get_result(pb2.PEER_ANNOUNCE) msg = peer_info_pkt.message conn.close() pi = inst.get_peer_info(msg.peer_id) pi.update_addr((source_name, 1234)) inst.save()
def test_joincluster(self): conn = self.pc[0] msg = pb2.JoinCluster() msg.peer_id = userconfig.get_user_config()["peerid"] msg.version = "0.1" msg.proto_version = 1 conn.send(pb2.JOIN_CLUSTER, msg) msg = conn.get_result(pb2.CLUSTER_INFO).message self.assertEqual(msg.cluster_id, self.inst.static_info["cluster_id"]) msg = conn.get_result(pb2.PEER_ANNOUNCE).message self.assertEqual(msg.cluster_id, self.inst.static_info["cluster_id"]) self.assertEqual(msg.peer_id, userconfig.get_user_config()["peerid"]) self.assertEqual(msg.peer_id, conn.peer_id)
def send_peer_announce(conn): if not conn.peer_announce_sent: msg = pb2.PeerAnnounce() msg.peer_id = userconfig.get_user_config()['peerid'] msg.version = "0.1" msg.proto_version = 1 msg.cluster_id = conn.inst.static_info['cluster_id'] conn.send(pb2.PEER_ANNOUNCE, msg) conn.peer_announce_sent = True
def test_peerannounce(self): conn = self.pc[0] conn.inst = self.inst remote.send_peer_announce(conn) msg = conn.get_result(pb2.PEER_ANNOUNCE).message self.assertEqual(msg.cluster_id, self.inst.static_info["cluster_id"]) self.assertEqual(msg.peer_id, userconfig.get_user_config()["peerid"]) self.assertEqual(msg.peer_id, conn.peer_id)