Example #1
0
//Main Method
int main(int argc, char *argv[])
{
	if(argc != 5) {
		quit("Usage: %s <server_port> <robot_IP/robot_hostname> <robot_ID> <image_id>", argv[0]);
	}
	//read args
	unsigned short localUDPPort = atoi(argv[1]);
	char* robotAddress = argv[2];
	char* robotID = argv[3];
	char* imageID = argv[4];

	plog("Read arguments");
	plog("Robot address: %s", robotAddress);
	plog("Robot ID: %s", robotID);
	plog("Image ID: %s", imageID);

	//listen for ctrl-c and call flushBuffersAndExit()
	signal(SIGINT, flushBuffersAndExit);

	//Create socket for talking to clients
	int clientSock;
	if((clientSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		quit("could not create client socket - socket() failed");
	}

	plog("Created client socket: %d", clientSock);

	//Construct local address structure for talking to clients
	struct sockaddr_in localAddress;
	memset(&localAddress, 0, sizeof(localAddress));		//Zero out structure
	localAddress.sin_family = AF_INET;					//Any Internet address family
	localAddress.sin_addr.s_addr = htonl(INADDR_ANY);	//Any incoming interface
	localAddress.sin_port = htons(localUDPPort);		//The port clients will sendto

	if(bind(clientSock, (struct sockaddr *) &localAddress, sizeof(localAddress)) < 0) {
		quit("could not bind to client socket - bind() failed");
	}

	plog("binded to client socket");

	//Loop for each client request
	for(;;) {
		plog("Start loop to handle client request");

		//Receive request from client
		struct sockaddr_in clientAddress;
		unsigned int clientAddressLen = sizeof(clientAddress);	//in-out parameter
		char clientBuffer[MAXLINE+1];	//Buffer for incoming client requests
		memset(clientBuffer, 0, MAXLINE+1);

		int recvMsgSize;
		//Block until receive a guess from a client
		if((recvMsgSize = recvfrom(clientSock, clientBuffer, MAXLINE, 0,
						(struct sockaddr *) &clientAddress, &clientAddressLen)) < 0) {
			quit("could not receive client request - recvfrom() failed");
		}

		plog("Received request of %d bytes", recvMsgSize);

		//Interpret client request
		char* requestRobotID = getRobotID(clientBuffer);
		if(strcmp(robotID, requestRobotID) != 0) {
			fprintf(stderr, "invalid request - robot ID's don't match\n");
			continue;
		}

		plog("Requested robot ID: %s", requestRobotID);

		char* requestStr = getRequestStr(clientBuffer);
		char* robotPort = getRobotPortForRequestStr(requestStr);

		plog("Request string: %s", requestStr);
		plog("Calculated port: %s", robotPort);

		//Send HTTP request to robot
		int robotSock;
		if((robotSock = setupClientSocket(robotAddress, robotPort, SOCKET_TYPE_TCP)) < 0) {
			quit("could not connect to robot");
		}

		plog("Set up robot socket: %d", robotSock);

		char* httpRequest = generateHTTPRequest(robotAddress, robotID, requestStr, imageID);


		plog("Created http request: %s", httpRequest);

		if(write(robotSock, httpRequest, strlen(httpRequest)) != strlen(httpRequest)) {
			quit("could not send http request to robot - write() failed");
		}

		plog("Sent http request to robot");

		free(httpRequest);

		plog("freed http request");

		//Read response from Robot
		int pos = 0;
		char* httpResponse = malloc(MAXLINE);
		int n;
		char recvLine[MAXLINE+1]; //holds one chunk of read data at a time
		while((n = read(robotSock, recvLine, MAXLINE)) > 0) {
			memcpy(httpResponse+pos, recvLine, n);
			pos += n;
			httpResponse = realloc(httpResponse, pos+MAXLINE);
		}
		uLong complen, ucomplen;

		plog("Received http response of %d bytes", pos);
		plog("http response: ");
#ifdef DEBUG
		int j;
		for(j = 0; j < pos; j++)
			fprintf(stderr, "%c", httpResponse[j]);
#endif

		//Parse Response from Robot
		char* httpBody = strstr(httpResponse, "\r\n\r\n")+4;
		ucomplen = (uLong)atoi((strstr(httpResponse, "Content-Length: ")+16));
		complen = (((int)ceil(ucomplen * 1.001)) +12);
		char complen_str[10];
		char ucomplen_str[10];
		char* compressed=malloc(complen);
		printf("ucomplen: %lu\n", ucomplen);

		compress((Bytef *)compressed, &complen, (Bytef *)httpBody, ucomplen);

		int httpBodyLength = (int)complen +(sprintf(complen_str, "%lus\t", complen) + sprintf(ucomplen_str, "%lus\t", ucomplen));
		char* send = malloc(httpBodyLength);
		int temp = sprintf(send, "%lus\t%lus\t", complen, ucomplen);
		memcpy((send+(temp*sizeof(char))), compressed, complen);

		plog("http body of %d bytes", httpBodyLength);
		plog("http body: ");
#ifdef DEBUG
		for(j = 0; j < httpBodyLength; j++)
			fprintf(stderr, "%c", httpBody[j]);
#endif

		//Send response back to the UDP client
		uint32_t requestID = getRequestID(clientBuffer);
		sendResponse(clientSock, &clientAddress, clientAddressLen, requestID, send, httpBodyLength);

		plog("sent http body response to client");

		free(httpResponse);
		free(send);
		free(compressed);

		plog("freed http response");
		plog("End loop to handle client request");
	}
}
Example #2
0
/*Main Function
  Variable Definition:
  -- argc: the number of command arguments
  -- argv[]: each vairable of command arguments(argv[0] is the path of execution file forever)
  Return Value: Client exit number
*/
int main(int argc, char *argv[]){
	//Test for correct number of arguments
	if (argc != 3){
		dieWithUserMessage("Parameter(s)", "<Server Address/Name> <Server Port/Service>");
	}

	struct sigaction	handler;								//signal handler
	const char			*host = argv[1];						//first argument: host name/ip address
	const char			*service = argv[2];						//second argument: server listening port number
	int					rcvd_buffer_size = 50 * BUFFER_SIZE;	//received buffer size

	//Initialize Pinger
	initPinger();
	//Create a unreliable UDP socket
	client_socket = setupClientSocket(host, service);
	if (client_socket < 0){
		dieWithSystemMessage("setupClientSocket() failed");
	}
	//Set the received buffer size
	setsockopt(client_socket, SOL_SOCKET, SO_RCVBUF, &rcvd_buffer_size, sizeof(rcvd_buffer_size));
	//Set signal handler for alarm signal
	handler.sa_handler = catchAlarm;
	//Blocking everything in handler
	if (sigfillset(&handler.sa_mask) < 0){
		dieWithSystemMessage("sigfillset() failed");
	}
	//No flags
	handler.sa_flags = 0;
	//Set the "SIGALRM" signal
	if (sigaction(SIGALRM, &handler, 0) < 0){
		dieWithSystemMessage("sigaction() failed for SIGALRM");
	}
	//Set the "SIGINT" signal
	if (sigaction(SIGINT, &handler, 0) < 0){
		dieWithSystemMessage("sigaction() failed for SIGINT");
	}

	//Output the title
	printf("PING %s (", host);
	printSocketAddress((struct sockaddr*)address->ai_addr, stdout);
	printf(") result:\n");

	//Sleep for 1 second
	sleep(TIMEOUT);
	//Start to send ping message
	while (send_count < PING_SIZE){
		sendPingMessage();
		rcvdPingMessage(TIMEOUT);
	}
	
    
    //You should put sleep function inside the while loop and after rcvdPingMessage, and you should sleep for TIMEOUT - rtt value for this cycle #13 -4
    
    
    //Determine that no more replies that comes in
	if (send_count != rcvd_count){
		rcvdPingMessage(REPLY_TIMEOUT);
	}
	//SIGINT signal: construct ping statistics
	catchAlarm(SIGINT);
	
	return 0;
}