Пример #1
0
//This store limited connections in a LRU cache, one item is one host VS one sock
int ZHTClient::str2SockLRU(string str, bool tcp) {
	struct HostEntity dest = this->str2Host(str);
	int sock = 0;
	if (tcp == true) {
		sock = connectionCache.fetch(dest.host, tcp);
		if (sock <= 0) {
//			cout << "host not found in cache, making connection..." << endl;
			sock = makeClientSocket(dest.host.c_str(), dest.port, tcp);
//			cout << "created sock = " << sock << endl;
			if (sock <= 0) {
				cerr << "Client insert:making connection failed." << endl;
				return -1;
			} else {
				int tobeRemoved = -1;
				connectionCache.insert(dest.host, sock, tobeRemoved);
				if (tobeRemoved != -1) {
//					cout << "sock " << tobeRemoved	<< ", will be removed, which shouldn't be 0."<< endl;
					close(tobeRemoved);
				}
			}
		} //end if sock<0
	} else { //UDP

		if (UDP_SOCKET <= 0) {
			sock = makeClientSocket(dest.host.c_str(), dest.port, TCP);
			UDP_SOCKET = sock;
		} else
			sock = UDP_SOCKET;
	}

	return sock;
}
Пример #2
0
/*
 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;

	}

}
Пример #3
0
int ZHTClient::index2SockLRU(int index, bool tcp) {
    struct HostEntity dest = this->index2Host(index);

    stringstream ss;
    ss << dest.host;
    ss << ":";
    ss << dest.port;
    string key = ss.str();

    int sock = 0;
    if (tcp == true) {
        sock = CONNECTION_CACHE.fetch(key, tcp);
        if (sock <= 0) {
            //			cout << "host not found in cache, making connection..." << endl;
            sock = makeClientSocket(dest.host.c_str(), dest.port, tcp);
            //			cout << "created sock = " << sock << endl;
            if (sock <= 0) {
                cerr << "Client insert:making connection failed." << endl;
                return -1;
            } else {
                int tobeRemoved = -1;
                CONNECTION_CACHE.insert(key, sock, tobeRemoved);
                if (tobeRemoved != -1) {
                    //					cout << "sock " << tobeRemoved	<< ", will be removed, which shouldn't be 0."<< endl;
                    close(tobeRemoved);
                }
            }
        } //end if sock<0
    } else { //UDP

        if (UDP_SOCKET <= 0) {
            sock = makeClientSocket(dest.host.c_str(), dest.port, TCP);
            UDP_SOCKET = sock;
        } else
            sock = UDP_SOCKET;
    }

    return sock;
}
Пример #4
0
int ftpConnect(ctxClient* contextClient, const char * ipServer, int serverPort)
{	
	if(contextClient == NULL)
	{
		return -1;
	}
	memset(contextClient, 0, sizeof(ctxClient));
	contextClient->serverAddress = (char*)realloc(contextClient->serverAddress,strlen(ipServer)*sizeof(char));
	strcpy(contextClient->serverAddress,ipServer);
	contextClient->controleSock = makeClientSocket(contextClient->serverAddress ,serverPort);

	return 0;
}