/*
 int ZHTClient::str2Sock(string str) { //give socket and update the vector of network entity
 int sock = 0;
 int index = -1;
 struct HostEntity dest = this->str2Host(str, index);
 cout<<"str2Sock: dest.sock = "<<dest.sock<<endl;
 if (dest.sock < 0) {
 sock = makeClientSocket(dest.host.c_str(), dest.port, 1);
 reuseSock(sock);
 dest.sock = sock;
 this->memberList.erase(this->memberList.begin() + index);
 this->memberList.insert(this->memberList.begin() + index, dest);

 }

 cout<<"str2Sock: after update: sock = "<<this->str2Host(str).sock<<endl;
 return dest.sock;
 }
 */
int ZHTClient::str2Sock(string str) { //give socket and update the vector of network entity
	int sock = 0;
	int index = -1;
	struct HostEntity dest = this->str2Host(str, index);
	if (TCP == true) {
		//	int sock = 0;
//		int index = -1;
//		struct HostEntity dest = this->str2Host(str, index);
//		cout<<"str2Sock: dest.sock = "<<dest.sock<<endl;
		if (dest.sock < 0) {
			sock = makeClientSocket(dest.host.c_str(), dest.port, TCP);
			reuseSock(sock);
			dest.sock = sock;
			this->memberList.erase(this->memberList.begin() + index);
			this->memberList.insert(this->memberList.begin() + index, dest);
		}
//		cout<<"str2Sock: after update: sock = "<<this->str2Host(str).sock<<endl;
		return dest.sock;
	} else { //UDP
		if (UDP_SOCKET < 0) {
			UDP_SOCKET = makeClientSocket(dest.host.c_str(), dest.port, TCP);
		}
		return UDP_SOCKET;

	}

}
Example #2
0
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;
}
Example #3
0
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;
}
Example #4
0
// 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;
}
Example #6
0
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;
}
Example #7
0
//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;
}
Example #8
0
void EpollServer::serve() {

#ifdef THREADED_SERVE
	init_thread();
#endif

	int sfd, s;
	int efd;
	struct epoll_event event;
	struct epoll_event *events;

	sfd = makeSvrSocket();
	if (sfd == -1)
		abort();

	s = make_socket_non_blocking(sfd);
	if (s == -1)
		abort();

	reuseSock(sfd);

	efd = epoll_create(1);
	if (efd == -1) {
		perror("epoll_create");
		abort();
	}

	event.data.ptr = new EpollData(sfd, NULL);
	event.events = EPOLLIN | EPOLLET;
	s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
	if (s == -1) {
		perror("epoll_ctl");
		abort();
	}

	/* Buffer where events are returned */
	events = (epoll_event *) calloc(MAX_EVENTS, sizeof event);

	/* The event loop */
	while (1) {

		int n, i;

		n = epoll_wait(efd, events, MAX_EVENTS, -1);

		for (i = 0; i < n; i++) {

			EpollData *edata = (EpollData*) events[i].data.ptr;

			if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)
					|| (!(events[i].events & EPOLLIN))) {

				/* An error has occured on this fd, or the socket is not
				 ready for reading (why were we notified then?) */
				fprintf(stderr, "epoll error\n");
				close(edata->fd());
				delete edata;
				continue;
			} else if (sfd == edata->fd()) {

				if (_tcp == true) {
					/* We have a notification on the listening socket, which
					 means one or more incoming connections. */
					while (1) {

						sockaddr *in_addr = (sockaddr *) calloc(1,
								sizeof(struct sockaddr));
						socklen_t in_len = sizeof(struct sockaddr);

						int infd = accept(sfd, in_addr, &in_len);
						if (infd == -1) {

							free(in_addr);

							if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {

								/* We have processed all incoming connections. */
								break;
							} else {

								perror("accept");
								break;
							}
						}

						/* fprintf(stdout,
						 "sin_family[%hu], sin_zero[%s], sin_addr.s_addr[%u], sin_port[%hu]\n",
						 in_addr.sin_family, in_addr.sin_zero,
						 in_addr.sin_addr.s_addr, in_addr.sin_port);
						 */

						/*
						 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
						 getnameinfo(in_addr, in_len, hbuf, sizeof hbuf, sbuf,
						 sizeof sbuf, 0);

						 if (s == 0) {

						 printf("Accepted connection on descriptor %d "
						 "(host=%s, _port=%s)\n", infd, hbuf, sbuf);
						 }*/

						/* Make the incoming socket non-blocking and add it to the
						 list of fds to monitor. */
						s = make_socket_non_blocking(infd);
						if (s == -1) {

							free(in_addr);
							abort();
						}

						reuseSock(infd);

						event.data.ptr = new EpollData(infd, in_addr);
						event.events = EPOLLIN | EPOLLET;
						s = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
						if (s == -1) {

							free(in_addr);
							perror("epoll_ctl");
							abort();
						}
					}
					continue;
				} else {

					int done = 0;

					while (1) {

						char buf[Env::BUF_SIZE];
						memset(buf, 0, sizeof(buf));
						//char *buf = (char*) calloc(Env::BUF_SIZE, sizeof(char));

						sockaddr fromaddr;
						socklen_t sender_len = sizeof(struct sockaddr);
						ssize_t count = recvfrom(edata->fd(), buf, sizeof buf,
								0, &fromaddr, &sender_len);
						//cout << "EpollServer.cpp: serve(): received "<< count << " bytes."<<endl;
						if (count == -1) {

							if (errno != EAGAIN) {

								perror("read");
								done = 1;
							}

						} else if (count == 0) {

							done = 1;
							break;

						} else {

#ifdef BIG_MSG
							bool ready = false;
							string bd = pbrb->getBdStr(sfd, buf, count, ready);

							if (ready) {

#ifdef THREADED_SERVE
								EventData eventData(edata->fd(), bd.c_str(), bd.size(),
										fromaddr);
								_eventQueue.push(eventData);

#else
								_ZProcessor->process(edata->fd(), bd.c_str(),
										fromaddr);
#endif
							}
#endif

#ifdef SML_MSG
#ifdef THREADED_SERVE
							EventData eventData(edata->fd(), buf, sizeof(buf),
									fromaddr);
							_eventQueue.push(eventData);

#else
							string bufstr(buf);
							_ZProcessor->process(edata->fd(), bufstr.c_str(),
									fromaddr);
#endif
#endif
						}

						//memset(buf, 0, sizeof(buf));
						//free(buf);
					}

					/*if (done) {

					 close(edata->fd());
					 delete edata;
					 }*/
				}

			} else {

				if (_tcp == true) {

					/* We have data on the fd waiting to be read. Read and
					 display it. We must read whatever data is available
					 completely, as we are running in edge-triggered mode
					 and won't get a notification again for the same
					 data. */
					int done = 0;

					while (1) {

						char buf[Env::BUF_SIZE];
						memset(buf, 0, sizeof(buf));
						//char *buf = (char*) calloc(Env::BUF_SIZE, sizeof(char));

						ssize_t count = recv(edata->fd(), buf, sizeof(buf), 0);

						if (count == -1) {

							/* If errno == EAGAIN, that means we have read all
							 data. So go back to the main loop. */
							if (errno != EAGAIN) {

								perror("read");
								done = 1;
							} /*else {

							 printf(
							 "Closed connection on descriptor %d, -1<--recv\n",
							 edata->fd());

							 close(edata->fd());
							 delete edata;
							 }*/
							break;
						} else if (count == 0) {

							/* End of file. The remote has closed the
							 connection. */
							done = 1;
							break;
						} else {

#ifdef BIG_MSG
							bool ready = false;
							string bd = pbrb->getBdStr(sfd, buf, count, ready);

							if (ready) {

#ifdef THREADED_SERVE
								EventData eventData(edata->fd(), bd.c_str(), bd.size(),
										*edata->sender());
								_eventQueue.push(eventData);
#else
								_ZProcessor->process(edata->fd(), bd.c_str(),
										*edata->sender());
#endif
							}
#endif

#ifdef SML_MSG
#ifdef THREADED_SERVE
							EventData eventData(edata->fd(), buf, sizeof(buf),
									*edata->sender());
							_eventQueue.push(eventData);
#else
							string bufstr(buf);
							_ZProcessor->process(edata->fd(), bufstr.c_str(),
									*edata->sender());
#endif
#endif
						}

						//memset(buf, 0, sizeof(buf));
						//free(buf);
					}

					if (done) {

						/*printf("Closed connection on descriptor %d, done.\n",
						 edata->fd());*/

						/* Closing the descriptor will make epoll remove it
						 from the set of descriptors which are monitored. */
						close(edata->fd());
						delete edata;
					}
				} //if TCP == true
			}
		}
	}

	free(events);

	close(sfd);

	EpollData *edata = (EpollData*) event.data.ptr;
	delete edata;
}
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;
}
Example #10
0
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;
}
Example #11
0
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;
}