int ZHTClient::removeMeta(string path) { Package package; package.set_operation(2); // 3 for insert, 1 for look up, 2 for remove package.set_replicano(3); //5: original, 3 not original package.set_virtualpath(path); string str = package.SerializeAsString(); int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; struct HostEntity dest = this->str2Host(str); sockaddr_in recvAddr; int sentSize = generalSendTo(dest.host.data() ,dest.port, sock, str.c_str(), TCP); // cout<<"remove sentSize "<< sentSize <<endl; int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0); generalReceive(sock, (void*)ret_buf, sizeof(int32_t),recvAddr,0, TCP); // generalSendTCP(sock, str.c_str()); // int32_t* ret = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0); int ret_1 = *(int32_t*) ret_buf; // cout<<"remove got: "<< ret_1 <<endl; // cout <<"Returned status: "<< *(int32_t*) ret<<endl; // d3_closeConnection(sock); free(ret_buf); return ret_1; }
int ZHTClient::remove(string str) { Package package; package.ParseFromString(str); char *c_str; int size = str.length(); c_str = (char*) malloc((size + 5 + 1) * sizeof(char)); if (c_str == NULL) { cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl; exit(1); } int len = copystring(c_str, str); if (package.virtualpath().empty()) //empty key not allowed. return -1; /*if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf! package.set_realfullpath(" ");*/ package.set_operation(2); //1 for look up, 2 for remove, 3 for insert package.set_replicano(3); //5: original, 3 not original str = package.SerializeAsString(); int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; struct HostEntity dest = this->str2Host(str); sockaddr_in recvAddr; //pthread_mutex_lock(&msg_lock); int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len, TCP); //pthread_mutex_unlock(&msg_lock); //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP); // cout<<"remove sentSize "<< sentSize <<endl; int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0); //pthread_mutex_lock(&msg_lock); generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP); //pthread_mutex_unlock(&msg_lock); // generalSendTCP(sock, str.c_str()); // int32_t* ret = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0); int ret_1 = *(int32_t*) ret_buf; // cout<<"remove got: "<< ret_1 <<endl; //cout <<"Returned status: "<< *(int32_t*) ret<<endl; // d3_closeConnection(sock); free(ret_buf); return ret_1; }
// general server to server send (for load information and task stealing) int32_t ZHTClient::svrtosvr(string str, int size, int index) { Package package; package.ParseFromString(str); if (package.virtualpath().empty()) //empty key not allowed. { return -1; } str = package.SerializeAsString(); char *c_str; c_str = (char*) malloc((size + 5 + 1) * sizeof(char)); if (c_str == NULL) { cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl; exit(1); } int len = copystring(c_str, str); if (package.virtualpath().empty()) //empty key not allowed. { return -1; } //cout << "C++ string len = " << str.length() << endl; cout << "C++ string: \n" << str << endl; /*cout << "C string: " << endl; for(int i = 0; i < len; i++) cout << c_str[i]; cout << endl; return 1;*/ int sock = this->index2SockLRU(index, TCP); reuseSock(sock); struct HostEntity dest = this->index2Host(index); sockaddr_in recvAddr; //cout << "going to acquire lock" << endl; //pthread_mutex_lock(&msg_lock); //cout << "lock acquired" << endl; //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP); //cout << "what is the f**k!" << dest.host.data() << endl; int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len, TCP); //cout << "svrsvr sent" << endl; //pthread_mutex_unlock(&msg_lock); int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); //pthread_mutex_lock(&msg_lock); generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP); //cout << "svrsvr recv" << endl; //pthread_mutex_unlock(&msg_lock); //cout << "lock released" << endl; int32_t ret_1 = *(int32_t*) ret_buf; free(ret_buf); //if(package.operation() == 14) { // cout << "ZHTClient::svrtosvr num_task = " << ret_1 << endl; //} return ret_1; }
//send a plain string to destination, receive return state. int ZHTClient::insert(string str) { /* int sock = -1; struct HostEntity dest = this->str2Host(str); // cout<<"Client: will send to "<<dest.host<<endl; //int ret = simpleSend(str, dest, sock); sock = makeClientSocket(dest.host.c_str(), dest.port, 1); // cout<<"client sock = "<< sock<<endl; reuseSock(sock); generalSendTCP(sock, str.c_str()); */ Package package; package.ParseFromString(str); if (package.virtualpath().empty()) //empty key not allowed. return -1; if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf! package.set_realfullpath(" "); package.set_operation(3); //1 for look up, 2 for remove, 3 for insert package.set_replicano(5); //5: original, 3 not original str = package.SerializeAsString(); int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; // int sentSize = generalSendTCP(sock, str.c_str()); struct HostEntity dest = this->str2Host(str); sockaddr_in recvAddr; int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), TCP); // cout <<"Client inseret sent: "<<sentSize<<endl; int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0); generalReceive(sock, (void*) ret_buf, 4, recvAddr, 0, TCP); int ret = *(int32_t*) ret_buf; if (ret < 0) { // cerr << "zht_util.h: Failed to insert." << endl; } //cout <<"Returned status: "<< *(int32_t*) ret<<endl; // d3_closeConnection(sock); free(ret_buf); //return ret_1; // cout<<"insert got: "<< *ret <<endl; return ret; }
int32_t ZHTClient::send(string str, int size) { Package package; package.ParseFromString(str); char *c_str; c_str = (char*) malloc((size + 5 + 1) * sizeof(char)); if (c_str == NULL) { cout << "ZHTClient::send: " << strerror(errno) << endl; exit(1); } int len = copystring(c_str, str); if (package.virtualpath().empty()) //empty key not allowed. return -1; //cout << "C++ string len = " << str.length() << endl; cout << "C++ string: \n" << str << endl; /*cout << "C string: " << endl; for(int i = 0; i < len; i++) cout << c_str[i]; cout << endl; return 1;*/ int sock = this->str2SockLRU(str, TCP); reuseSock(sock); struct HostEntity dest = this->str2Host(str); sockaddr_in recvAddr; //pthread_mutex_lock(&msg_lock); //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP); int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len, TCP); //pthread_mutex_unlock(&msg_lock); //cout << " Sent size = " << sentSize << endl; int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); //pthread_mutex_lock(&msg_lock); generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP); //pthread_mutex_unlock(&msg_lock); int ret_1 = *(int32_t*) ret_buf; free(ret_buf); return ret_1; }
//send a serialized string to destination, receive return state. int ZHTClient::insert(string str) { /* int sock = -1; struct HostEntity dest = this->str2Host(str); // cout<<"Client: will send to "<<dest.host<<endl; //int ret = simpleSend(str, dest, sock); sock = makeClientSocket(dest.host.c_str(), dest.port, 1); // cout<<"client sock = "<< sock<<endl; reuseSock(sock); generalSendTCP(sock, str.c_str()); */ int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; // int sentSize = generalSendTCP(sock, str.c_str()); struct HostEntity dest = this->str2Host(str); sockaddr_in recvAddr; int sentSize = generalSendTo(dest.host.data(),dest.port, sock, str.c_str(), TCP); // cout <<"Client inseret sent: "<<sentSize<<endl; int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t)); // generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0); generalReceive(sock, (void*)ret_buf, 4,recvAddr,0, TCP); int32_t* ret = (int32_t*) ret_buf; if (*ret < 0) { // cerr << "zht_util.h: Failed to insert." << endl; } //cout <<"Returned status: "<< *(int32_t*) ret<<endl; // d3_closeConnection(sock); free(ret_buf); //return ret_1; // cout<<"insert got: "<< *ret <<endl; return *ret; }
int ZHTClient::lookup(string str, string &returnStr) { Package package; package.ParseFromString(str); if (package.virtualpath().empty()) //empty key not allowed. return -1; if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf! package.set_realfullpath(" "); package.set_operation(1); // 1 for look up, 2 for remove, 3 for insert package.set_replicano(3); //5: original, 3 not original str = package.SerializeAsString(); struct HostEntity dest = this->str2Host(str); // cout << "client::lookup is called, now send request..." << endl; Package pack; pack.ParseFromString(str); // cout<<"ZHTClient::lookup: operation = "<<pack.operation()<<endl; // int ret = simpleSend(str, dest, sock); // sock = makeClientSocket(dest.host.c_str(), dest.port, 1); // cout<<"client sock = "<< sock<<endl; int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; sockaddr_in recvAddr; int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), TCP); // int ret = generalSendTCP(sock, str.c_str()); // cout << "ZHTClient::lookup: simpleSend return = " << ret << endl; char buff[MAX_MSG_SIZE]; //MAX_MSG_SIZE memset(buff, 0, sizeof(buff)); int rcv_size = -1; int status = -1; string sRecv; string sStatus; if (sentSize == str.length()) { //this only work for TCP. UDP need to make a new one so accept returns from server. // cout << "before protocol judge" << endl; rcv_size = generalReceive(sock, (void*) buff, sizeof(buff), recvAddr, 0, TCP); if (rcv_size < 0) { cout << "Lookup receive error." << endl; } else { sRecv.assign(buff); returnStr = sRecv.substr(3); //the left is real thing need to be deserilized. sStatus = sRecv.substr(0, 3); //the first three chars means status code, like -1, -2, 0, -98, -99 and so on. } // cout << "after protocol judge" << endl; } // d3_closeConnection(sock); if (!sStatus.empty()) status = atoi(sStatus.c_str()); return status; }
int ZHTClient::lookup(string str, string &returnStr) { Package package; package.ParseFromString(str); if (package.virtualpath().empty()) //empty key not allowed. return -1; /*if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf! package.set_realfullpath(" ");*/ package.set_operation(1); // 1 for look up, 2 for remove, 3 for insert package.set_replicano(3); //5: original, 3 not original str = package.SerializeAsString(); struct HostEntity dest = this->str2Host(str); // cout << "client::lookup is called, now send request..." << endl; char *c_str; int size = str.length(); c_str = (char*) malloc((size + 5 + 1) * sizeof(char)); if (c_str == NULL) { cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl; exit(1); } int len = copystring(c_str, str); Package pack; pack.ParseFromString(str); // cout<<"ZHTClient::lookup: operation = "<<pack.operation()<<endl; // int ret = simpleSend(str, dest, sock); // sock = makeClientSocket(dest.host.c_str(), dest.port, 1); // cout<<"client sock = "<< sock<<endl; int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; sockaddr_in recvAddr; //cout << "lookup str = " << str << endl; //pthread_mutex_lock(&msg_lock); int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len, TCP); //pthread_mutex_unlock(&msg_lock); //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP); // int ret = generalSendTCP(sock, str.c_str()); // cout << "ZHTClient::lookup: simpleSend return = " << ret << endl; char buff[Env::MAX_MSG_SIZE]; //Env::MAX_MSG_SIZE memset(buff, 0, sizeof(buff)); int rcv_size = -1; int status = -1; string sRecv; string sStatus; //if (sentSize == str.length()) { //this only work for TCP. UDP need to make a new one so accept returns from server. if (sentSize == len) { // cout << "before protocol judge" << endl; //pthread_mutex_lock(&msg_lock); rcv_size = generalReceive(sock, (void*) buff, sizeof(buff), recvAddr, 0, TCP); //pthread_mutex_unlock(&msg_lock); if (rcv_size < 0) { cout << "Lookup receive error." << endl; } else { //cout << "ZHTClient::lookup: buff = " << buff << " size = " << rcv_size << endl; sRecv.assign(buff); //cout << "ZHTClient::lookup: for key " << package.virtualpath() << " recd = " << sRecv << endl; try { returnStr = sRecv.substr(3); //the left is real thing need to be deserilized. } catch (exception& e) { cout << "ZHTClient::lookup: (substr(3)) " << " " << e.what() << endl; exit(1); } try { sStatus = sRecv.substr(0, 3); //the first three chars means status code, like 001, 002, -98, -99 and so on. } catch (exception& e) { cout << "ZHTClient::lookup: (substr(0, 3)) " << " " << e.what() << endl; exit(1); } } // cout << "after protocol judge" << endl; } // d3_closeConnection(sock); if (!sStatus.empty()) status = atoi(sStatus.c_str()); return status; }
int ZHTClient::lookup(string path, string &returnStr) { Package package; package.set_operation(1); // 3 for insert, 1 for look up, 2 for remove package.set_replicano(3); //5: original, 3 not original package.set_virtualpath(path); string str = package.SerializeAsString(); struct HostEntity dest = this->str2Host(str); // cout << "client::lookup is called, now send request..." << endl; Package pack; pack.ParseFromString(str); // cout<<"ZHTClient::lookup: operation = "<<pack.operation()<<endl; // int ret = simpleSend(str, dest, sock); // sock = makeClientSocket(dest.host.c_str(), dest.port, 1); // cout<<"client sock = "<< sock<<endl; int sock = this->str2SockLRU(str, TCP); reuseSock(sock); // cout<<"sock = "<<sock<<endl; sockaddr_in recvAddr; int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), TCP); // int ret = generalSendTCP(sock, str.c_str()); // cout << "ZHTClient::lookup: simpleSend return = " << ret << endl; char buff[MAX_MSG_SIZE]; //MAX_MSG_SIZE memset(buff, 0, sizeof(buff)); int rcv_size = -1; if (sentSize == str.length()) { //this only work for TCP. UDP need to make a new one so accept returns from server. // cout << "before protocol judge" << endl; rcv_size = generalReceive(sock, (void*)buff, sizeof(buff),recvAddr,0, TCP); /* if (TRANS_PROTOCOL == USE_TCP) { rcv_size = d3_recv_data(sock, buff, MAX_MSG_SIZE, 0); //MAX_MSG_SIZE } else if (TRANS_PROTOCOL == USE_UDP) { //int svrPort = dest.port;//surely wrong, it's just the 50000 port //int svrPort = 50001; srand(getpid() + clock()); // int z = rand() % 10000 + 10000; // cout<<"Client:lookup: random port: "<<z<<endl; // int server_sock = d3_svr_makeSocket(rand() % 10000 + 10000);// use random port to send, and receive lookup result from it too. // cout<<"Client:lookup: UDP socket: "<<server_sock<<endl; sockaddr_in tmp_sockaddr; memset(&tmp_sockaddr, 0, sizeof(sockaddr_in)); // cout << "lookup: before receive..." << endl; rcv_size = d3_svr_recv(sock, buff, MAX_MSG_SIZE, 0, &tmp_sockaddr); //receive lookup result //d3_closeConnection(server_sock); // cout << "lookup received " << rcv_size << " bytes." << endl; }*/ if (rcv_size < 0) { cout << "Lookup receive error." << endl; return rcv_size; } else { returnStr.assign(buff); } // cout << "after protocol judge" << endl; } // d3_closeConnection(sock); return rcv_size; }