/*Handle Client Request Function
  Variable Definition:
  -- client_socket: socket connected to the client
  Return Value: NULL
*/
void handleClientRequest(int client_socket) {
    FILE 	*channel;						//file stream for client socket
    char 	request_line[STRING_SIZE];		//client request line
    char	*return_value;					//each line header pointer

    //Initialize request_line buffer
    memset(request_line, 0, STRING_SIZE);
    //Create an input stream from the socket
    channel = fdopen(client_socket, "r");
    if (channel == NULL) {
        dieWithSystemMessage("fdopen() failed");
    }

    //Get client Request Line (Jump the blank line)
    do {
        return_value = fgets(request_line, STRING_SIZE, channel);
    } while (syntaxChecking(return_value, BLANK_LINE));
    //Output the client_socket id and Request Line
    printf("Got a call on %d: request = %s", client_socket, request_line);
    //Get client Header Lines & Response the client request
    respondClientRequest(request_line, getHeaderLines(channel), client_socket);

    //Close client socket
    close(client_socket);
    //Close file stream
    fclose(channel);

    return;
}
Exemplo n.º 2
0
 void sendRemainingHeaderLines()
 {
     if (!headerLines)
         return;
     unsigned which = sentHeaderLines->scanInvert(0, false);
     if (which < subFiles)
     {
         CMessageBuffer msgMb;
         bool someLeft=false;
         do
         {
             msgMb.append(which);
             unsigned &remaining = getHeaderLines(which);
             if (0 != remaining)
                 someLeft = true;
             msgMb.append(remaining);
             which = sentHeaderLines->scanInvert(which+1, false);
         }
         while (which < subFiles);
         if (someLeft)
             queryJobChannel().queryJobComm().send(msgMb, queryJobChannel().queryMyRank()+1, mpTag);
         else
             sendAllDone();
     }
 }
Exemplo n.º 3
0
/*Handle Server Response Function
  Variable Definition:
  -- widget: window widget
  -- client_socket: socket connected to the server
  Return value: if handle server response successful return true, else return false
*/
bool handleServerResponse(GtkWidget *widget, int client_socket){
	RTSP_HEADER		*header;						//_rtsp_header structure header pointer
	FILE 			*channel;						//file stream for client socket
	char 			response_line[STRING_SIZE];		//server response line
	char			version[STRING_SIZE];			//rtsp version field: RTSP/1.0
	char			status_code[NUMBER_SIZE];		//rtsp response status code
	char			status_message[STRING_SIZE];	//rtsp response status message
	char			field_value[HALFBUF_SIZE];		//field value string
	char			*content;						//rtsp response content
	char			*p;								//string pointer
	int				count;							//counter
	
	//Initialize response_line, version, status_code, status_message, and field_value buffer
	memset(response_line, 0, STRING_SIZE);
	memset(version, 0, STRING_SIZE);
	memset(status_code, 0, NUMBER_SIZE);
	memset(status_message, 0, STRING_SIZE);
	memset(field_value, 0, HALFBUF_SIZE);

	//Create an input stream from the socket
	channel = fdopen(client_socket, "r");
	if (channel == NULL){
		showErrorCallback(widget, "fdopen() failed: unable to create received channel!");
		return false;
	}
	
	//Get server Response Line
	if (fgets(response_line, STRING_SIZE, channel) == NULL){
		showErrorCallback(widget, "recv() failed: unable to receive RTSP Response Line!");
		return false;
	}
	//Output the client_socket id and Response Line
	printf("Got a call on %d: response = %s", client_socket, response_line);
	//Get server Header Lines
	header = getHeaderLines(channel);

#ifdef	DEBUG
	RTSP_HEADER		*debug_header_node;

	DEBUG_START;
	fputs("RTSP response header lines:\n", stdout);
	//Output the RTSP response header lines
	for (debug_header_node = header->next; debug_header_node != NULL; debug_header_node = debug_header_node->next){
		fputs(debug_header_node->field_name, stdout);
		fputs(": ", stdout);
		fputs(debug_header_node->field_value, stdout);
		fputc('\n', stdout);
	}
	DEBUG_END;
#endif

	//Get server response content
	content = getResponseContents(channel);

#ifdef	DEBUG
	DEBUG_START;
	fputs("RTSP response content:\n", stdout);
	fputs(content, stdout);
	DEBUG_END;
#endif

	//Get the rtsp version, status code, and status message
	sscanf(response_line, "%s%s%s", version, status_code, status_message);
	//Test the status code is neither 200 (OK) nor 304 (Not Modified)
	if ((strcmp(status_code, "200") != 0) && (strcmp(status_code, "304") != 0)){
		showErrorCallback(widget, content);
		return false;
	}
	
	//Test the session field
	if (fieldExist(header, "session", field_value)){
		//Set the session id
		session_id = atoi(field_value);
	}
	//Test the transport field
	if (fieldExist(header, "transport", field_value)){
		//Remove the protocol type, protocol method, client port, and server port
		for (count = 0; count < NUMBER_SIZE; count++){
			p = splitNameAndValue(field_value, ';');
		}
		//Get the server rtp port number
		splitNameAndValue(p, '=');
		//Set the server rtp port number
		server_rtp_port = atoi(p);
	}
	//Close file stream
	fclose(channel);

	return true;
}
Exemplo n.º 4
0
bool handleServerResponse(GtkWidget *widget, int client_socket) {
	RTSP_HEADER	*header;
	FILE 	*channel;
	char 	response_line[STRING_SIZE];
	char	version[STRING_SIZE];
	char	status_code[NUMBER_SIZE];
	char	status_message[STRING_SIZE];
	char	field_value[HALFBUF_SIZE];
	char	*content;		
	char	*p;
	int	count;
	
	//initialize all the strings to be empty
	memset(response_line, 0, STRING_SIZE);
	memset(version, 0, STRING_SIZE);
	memset(status_code, 0, NUMBER_SIZE);
	memset(status_message, 0, STRING_SIZE);
	memset(field_value, 0, HALFBUF_SIZE);

	//Create an input stream from the socket
	channel = fdopen(client_socket, "r");

	if (channel == NULL) {
		showErrorCallback(widget, "Unable to create received channel!");
		return false;
	}
	
	//Get server Response Line
	if (fgets(response_line, STRING_SIZE, channel) == NULL) {
		showErrorCallback(widget, "Unable to receive RTSP Response!");
		return false;
	}

	//Output the client_socket id and Response Line
	printf("Call on %d: response = %s", client_socket, response_line);
	//Get server Header Lines
	header = getHeaderLines(channel);

	//Get server response content
	content = getResponseContents(channel);

	//Get the rtsp version, status code, and status message
	sscanf(response_line, "%s%s%s", version, status_code, status_message);

	//Test the status code is neither 200 (OK) nor 304 (Not Modified)
	if ((strcmp(status_code, "200") != 0) && (strcmp(status_code, "304") != 0)) {
		showErrorCallback(widget, content);
		return false;
	}
	
	//Test the session field
	if (fieldExist(header, "session", field_value)) {
		//Set the session id
		session_id = atoi(field_value);
	}

	//Test the transport field
	if (fieldExist(header, "transport", field_value)) {
		//Remove the protocol type, protocol method, client port, and server port
		for (count = 0; count < NUMBER_SIZE; count++) {
			p = splitNameAndValue(field_value, ';');
		}

		//Get the server rtp port number
		splitNameAndValue(p, '=');
		//Set the server rtp port number
		server_rtp_port = atoi(p);
	}
	//Close file stream
	fclose(channel);

	return true;
}