/* 更新新加入节点的前继的前继和后继的后继 * 通过向前继节点发出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(); }
/*根据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; }