/* 更新新加入节点的前继节点的后继和后继的后继, * 以及新加入节点的后继节点的前继和前继的前继 */ void Join::update_neighbour() { theApp.scout<<"++++update neighbour++++"<<endl; int suc = peer->fingerTable.sucId; string sucIp = peer->fingerTable.sucIP; int sucPort = peer->fingerTable.sucPort; int pre = peer->fingerTable.preId; string preIp = peer->fingerTable.preIP; int prePort = peer->fingerTable.prePort; //更新前继节点的后继和后继的后继 stringstream ss1,ss2,ss3,ss4,ss5,ss6; ss1<<peer->fingerTable.port;ss2<<sucPort;ss3<<prePort; ss4<<peer->getId();ss5<<suc;ss6<<pre; string cmd = "newSuc " + peer->fingerTable.ip + " " + ss1.str() + " " + ss4.str() + " " + ss5.str() + " " + sucIp + " " + ss2.str(); CSocket socket; socket.Connect(preIp.c_str(),prePort); socket.WriteLine(cmd); socket.Close(); //更新后继节点的前继和前继的前继 cmd = "newPre " + peer->fingerTable.ip + " " + ss1.str() + " " + ss4.str() + " " + ss6.str() + " " + preIp + " " + ss3.str(); socket.Connect(sucIp.c_str(),sucPort); socket.WriteLine(cmd); socket.Close(); //更新前继的前继和后继的后继,他们只需要部分字段 suc_suc_pre_pre(); }
/* 更新新加入节点的前继的前继和后继的后继 * 通过向前继节点发出yourpre命令找出前继的前继,向后继节点发出yoursuc命令找出后继的后继 */ void Join::init_suc_suc_pre_pre() { //后继的后继 string cmd = "yoursuc"; CSocket socket; socket.Connect(peer->fingerTable.sucIP.c_str(),peer->fingerTable.sucPort); socket.WriteLine(cmd); string k = socket.ReadLine(); vector<string> s = Tool::Split(k," "); peer->fingerTable.sucsucIP = s[0]; peer->fingerTable.sucsucPort = atoi(s[1].c_str()); peer->fingerTable.sucsucId = atoi(s[2].c_str()); socket.Close(); theApp.scout<<"@@@@@@@@@@@@@@@"<<endl; //前继的前继 cmd = "yourpre"; socket.Connect(peer->fingerTable.preIP.c_str(),peer->fingerTable.prePort); socket.WriteLine(cmd); k = socket.ReadLine(); s = Tool::Split(k," "); peer->fingerTable.prepreIP = s[0]; peer->fingerTable.preprePort = atoi(s[1].c_str()); peer->fingerTable.prepreId = atoi(s[2].c_str()); socket.Close(); }
/*更新前继的前继和后继的后继*/ void Join::suc_suc_pre_pre() { CSocket socket; socket.Connect(peer->fingerTable.sucsucIP.c_str(),peer->fingerTable.sucsucPort); string cmd = "set_your_pre_pre "; stringstream ss,ss1;ss<<peer->getId();ss1<<peer->getPort(); cmd += (ss.str() + " " + peer->getLocalIp() + " " + ss1.str()); socket.WriteLine(cmd); socket.Close(); socket.Connect(peer->fingerTable.prepreIP.c_str(),peer->fingerTable.preprePort); cmd = "set_your_suc_suc "; cmd += (ss.str() + " " + peer->getLocalIp() + " " + ss1.str()); socket.WriteLine(cmd); socket.Close(); }
/*根据ip和port来寻找pre*/ string Join::findPre(string ip, int port) { CSocket socket; socket.Connect(ip.c_str(),port);//连接服务器 string cmd = "yourpre"; socket.WriteLine(cmd); //读取查询结果 string result = socket.ReadLine(); theApp.scout<<result<<endl; return result; }
/*在要连接的节点的后继节点链表中,找出离新节点最近且是新节点的前继的节点*/ string Join::findClosestPrecedingFinger(int id, string remoteIp, int remotePort) { stringstream ss; ss<<id; string cmd = "closestprecedingfinger " + ss.str(); CSocket socket; socket.Connect(remoteIp.c_str(),remotePort); socket.WriteLine(cmd); //读取查询结果 string result = socket.ReadLine(); theApp.scout<<result<<endl; return result; }
/* * 新节点加入拓扑时,需要把本属于它的key的hash从别的节点上转移过来 */ void Join::transfer_keys() { theApp.scout<<"++++ In the transfer key++++"<<endl; int sucId = peer->fingerTable.sucId; string sucIp = peer->fingerTable.sucIP; int sucPort = peer->fingerTable.sucPort; stringstream ss1,ss2,ss3,ss4;ss1<<peer->fingerTable.port;ss2<<peer->getId();ss3<<peer->fingerTable.preId; string cmd = "transfer_key " + peer->fingerTable.ip + " " + ss1.str() + " " + ss2.str() + " " + ss3.str(); if (sucId == peer->getId()) return; CSocket socket; socket.Connect(peer->fingerTable.sucIP.c_str(),peer->fingerTable.sucPort); socket.WriteLine(cmd); socket.Close(); }
/* 更新环上每一个节点的FingerTable中的后继列表 */ void Join::update_others() { theApp.scout<<"++++update others++++"<<endl; int localId = peer->getId(); string localIp = peer->fingerTable.ip; int localport = peer->fingerTable.port; int suc = peer->fingerTable.sucId; string sucIp = peer->fingerTable.sucIP; int sucPort = peer->fingerTable.sucPort; stringstream ss,ss1;ss<<localport;ss1<<localId; string cmd = "new_node " + localIp + " " + ss.str() + " " + ss1.str(); CSocket socket; socket.Connect(sucIp.c_str(),sucPort); socket.WriteLine(cmd); socket.Close(); }