Пример #1
0
static int osrfHttpTranslatorProcess(osrfHttpTranslator* trans) {
    if(trans->body == NULL)
        return HTTP_BAD_REQUEST;

    if(!osrfHttpTranslatorSetTo(trans))
        return HTTP_BAD_REQUEST;

    char* jsonBody = osrfHttpTranslatorParseRequest(trans);
    if (NULL == jsonBody)
        return HTTP_BAD_REQUEST;

    while(client_recv(trans->handle, 0))
        continue; // discard any old status messages in the recv queue

    // send the message to the recipient
    transport_message* tmsg = message_init(
        jsonBody, NULL, trans->thread, trans->recipient, NULL);
    message_set_osrf_xid(tmsg, osrfLogGetXid());
    client_send_message(trans->handle, tmsg);
    message_free(tmsg); 
    free(jsonBody);

    if(trans->disconnectOnly) {
        osrfLogDebug(OSRF_LOG_MARK, "exiting early on disconnect");
        osrfCacheRemove(trans->thread);
        return OK;
    }

    // process the response from the opensrf service
    int firstWrite = 1;
    while(!trans->complete) {
        transport_message* msg = client_recv(trans->handle, trans->timeout);

        if(trans->handle->error) {
            osrfLogError(OSRF_LOG_MARK, "Transport error");
            osrfCacheRemove(trans->thread);
            return HTTP_INTERNAL_SERVER_ERROR;
        }

        if(msg == NULL)
            return HTTP_GATEWAY_TIME_OUT;

        if(msg->is_error) {
            osrfLogError(OSRF_LOG_MARK, "XMPP message resulted in error code %d", msg->error_code);
            osrfCacheRemove(trans->thread);
            return HTTP_NOT_FOUND;
        }

        if(!osrfHttpTranslatorCheckStatus(trans, msg))
            continue;

        if(firstWrite) {
            osrfHttpTranslatorInitHeaders(trans, msg);
            if(trans->connecting)
                osrfHttpTranslatorCacheSession(trans, msg->sender);
            firstWrite = 0;
        }

        if(trans->multipart) {
            osrfHttpTranslatorWriteChunk(trans, msg);
            if(trans->connectOnly)
                break;
        } else {
            if(!trans->messages)
                trans->messages = osrfNewList();
            osrfListPush(trans->messages, msg->body);

            if(trans->complete || trans->connectOnly) {
                growing_buffer* buf = buffer_init(128);
                unsigned int i;
                OSRF_BUFFER_ADD(buf, osrfListGetIndex(trans->messages, 0));
                for(i = 1; i < trans->messages->size; i++) {
                    buffer_chomp(buf); // chomp off the closing array bracket
                    char* body = osrfListGetIndex(trans->messages, i);
                    char newbuf[strlen(body)];
                    sprintf(newbuf, "%s", body+1); // chomp off the opening array bracket
                    OSRF_BUFFER_ADD_CHAR(buf, ',');
                    OSRF_BUFFER_ADD(buf, newbuf);
                }

                ap_rputs(buf->buf, trans->apreq);
                buffer_free(buf);
            }
        }
    }

    if(trans->disconnecting) // DISCONNECT within a multi-message batch
        osrfCacheRemove(trans->thread);

    return OK;
}
Пример #2
0
void rt_rau_app_receive_msg_entry(void *parameter)
{
    char *recv_data;
    int bytes_received;


    recv_data = rt_malloc(BUFSZ);

reconnect:    
    rau_app_tcp_connect_server();
    while(1)
    {

        bytes_received = recv(RauSock, recv_data, BUFSZ - 1, 0);
        if (bytes_received <= 0)
        {

            lwip_close(RauSock);

            rt_free(recv_data);
            break;
        }
        else
        {
            recv_data[bytes_received] = '\0';
            if(rau_app_analysis_data(RauSock,recv_data,bytes_received) == CMD_GET_MEMBER_LIST)
            {
                break;
            }
        }
    }
    while(1)
    {
        char com_rbuf[MAX_LOG_LEN]={0};
        LClock_t StartClock;
        int rlen = 0; 
        rt_memset(com_rbuf,0,MAX_LOG_LEN);
        rt_memset(recv_data,0,BUFSZ);
         rlen = rcu_uart_read(com_rbuf, MAX_LOG_LEN);
        hlog("com data:%s\n",com_rbuf);
        StartClock=GetClock();
        hlog("StartClock:%d\n",StartClock);
        client_send_message(RauSock,member_list[0].client_name,com_rbuf);
        
        bytes_received = recv(RauSock, recv_data, BUFSZ - 1, 0);
        if (bytes_received <= 0)
        {

            lwip_close(RauSock);

            rt_free(recv_data);
            break;
        }
        else
        {
            recv_data[bytes_received] = '\0';
            if(rau_app_analysis_data(RauSock,recv_data,bytes_received) == CMD_GET_MEMBER_LIST)
            {
                break;
            }
        }
    }
    return;
}
Пример #3
0
/* connects and registers with the router */
int main( int argc, char** argv ) {

	if( argc < 5 ) {
		osrfLogError( OSRF_LOG_MARK, "Usage: %s <username> <host> <resource> <recipient> \n", argv[0] );
		return 99;
	}

	transport_message* send;
	transport_client* client = client_init( argv[2], 5222, 0 );

	// try to connect, allow 15 second connect timeout 
	if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15, AUTH_DIGEST ) ) 
		osrfLogInfo(OSRF_LOG_MARK, "Connected...\n");
	 else { 
		osrfLogError( OSRF_LOG_MARK, "NOT Connected...\n" ); 
		return -1;
	 }
	
	if( (pid=fork()) ) { /* parent */

		signal(SIGINT, sig_int);
		fprintf(stderr, "Listener: %ld\n", (long) getpid() );	
		char buf[300];
		osrf_clearbuf(buf, sizeof(buf));
		printf("=> ");

		while( fgets( buf, sizeof(buf), stdin) ) {

			// remove newline
			buf[strlen(buf)-1] = '\0';

			if( strcmp(buf, "exit")==0) { 
				client_free( client );	
				break; 
			}

			send = message_init( buf, "", "123454321", argv[4], NULL );
			client_send_message( client, send );
			message_free( send );
			printf("\n=> ");
			osrf_clearbuf(buf, sizeof(buf));
		}
		fprintf(stderr, "Killing child %d\n", pid );
		kill( pid, SIGKILL );
		return 0;

	} else {

		fprintf(stderr, "Sender: %ld\n", (long) getpid() );	

		transport_message* recv;
		while( (recv=client_recv( client, -1)) ) {
			if( recv->is_error )
				fprintf( stderr, "\nReceived Error\t: ------------------\nFrom:\t\t"
					"%s\nRouterFrom:\t%s\nBody:\t\t%s\nType %s\nCode %d\n=> ", 
					recv->sender, recv->router_from, recv->body, recv->error_type, recv->error_code );
			else
				fprintf( stderr, "\nReceived\t: ------------------\nFrom:\t\t"
					"%s\nRouterFrom:\t%s\nBody:\t\t%s\n=> ", recv->sender, recv->router_from, recv->body );

			message_free( recv );
		}

	}
	return 0;

}
Пример #4
0
void client_looper() {
    Client_Msg *client_msg;
    char *read_msg;
    int msg_len;
    int comp_result;
    char cm_num;

    struct sockaddr_un addr;
    struct pollfd poll_set[POLL_SIZE];
    int client_fd;

    if ((client_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        perror("socket error");
        exit(-1);
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1);

    poll_set[0].fd = STDIN_FILENO;
    poll_set[0].events = POLLIN;

    poll_set[1].fd = client_fd;
    poll_set[1].events = POLLIN;

    if (connect(client_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        perror("connect error");
        close(client_fd);
        exit(-1);
    }
 
    client_msg = new_Client_Msg();

    while (1) {
        if (poll(poll_set, POLL_SIZE, 100000) > 0) {
            printf("called poll\n");
            if (poll_set[0].revents & POLLIN) {
                read_msg = NULL;
                msg_len = client_read_command(poll_set[0].fd, &read_msg);
                if (msg_len >= 10 && (comp_result = strncasecmp(read_msg, COMMAND, 8) == 0)) {
                    if (read_msg[msg_len -1] == '\n') {
                        read_msg[msg_len -1] = '\0';
                    }
                    cm_num = read_msg[8];
                    switch(cm_num) {
                        case '1':
                            if (msg_len == 10) {
                                printf("command 1\n");
                                client_request_all_message(poll_set[1].fd);
                            } else {
                                printf("Please recommand\n");
                            }
                            break;
                        case '3':
                            if (msg_len >= 11 && (read_msg[9] == ' ')) {
                                printf("command 3\n");
                                client_send_message(poll_set[1].fd, read_msg + 10);
                            } else {
                                printf("Please recommand\n");
                            }
                            break;
                        case '5':
                            if (msg_len == 10) {
                                printf("command 5\n");
                                client_request_last_message_from_first_loaction(poll_set[1].fd);
                            } else {
                                printf("Please recommand\n");
                            }
                            break;
                        case '7':
                            if (msg_len == 10) {
                                printf("command 7\n");
                                client_request_last_message_from_last_location(poll_set[1].fd);
                            } else {
                                printf("Please recommand\n");
                            }
                            break;
                        default:
                            printf("Your command numser is %c Please recommand\n", cm_num);
                    }
                } else {
                    printf("Please recommand\n");
                }
                free(read_msg);
            }

            if (poll_set[1].revents & POLLHUP){
                printf("poll_hup\n");
                close(poll_set[1].fd);
                printf("Server is not connected\n");
                exit(-1);
            }

            if (poll_set[1].revents & POLLIN) {
                short op_code;
                int fd = poll_set[1].fd;
                op_code = get_op_code_with_fd(fd);
                printf("client_fd = %d, op_cde=%2x\n", poll_set[1].fd, op_code);
                switch (op_code) {
                    case RES_ALL_MSG:
                        client_receive_all_messages(client_msg, fd);
                        break;
                    case RCV_MSG:
                        client_receive_message(client_msg, fd);
                        break;
                    case RES_FIRST_MSG:
                        client_receive_all_messages(client_msg, fd);
                        break;
                    case RES_LAST_MSG:
                        client_receive_all_messages(client_msg, fd);
                        break;
                    default:
                        printf("Wrong opcode:%2x\n", op_code);
                }
            }
        }
    }
}
/**
 * Parse opensrf request and relay the request to the opensrf network.
 */
static size_t on_message_handler_body(void *data,
                const WebSocketServer *server, const int type, 
                unsigned char *buffer, const size_t buffer_size) {

    request_rec *r = server->request(server);

    jsonObject *msg_wrapper = NULL; // free me
    const jsonObject *tmp_obj = NULL;
    const jsonObject *osrf_msg = NULL;
    const char *service = NULL;
    const char *thread = NULL;
    const char *log_xid = NULL;
    char *msg_body = NULL;
    char *recipient = NULL;
    int i;

    if (buffer_size <= 0) return OK;

    // generate a new log trace for this request. it 
    // may be replaced by a client-provided trace below.
    osrfLogMkXid();

    osrfLogDebug(OSRF_LOG_MARK, "WS received message size=%d", buffer_size);

    // buffer may not be \0-terminated, which jsonParse requires
    char buf[buffer_size + 1];
    memcpy(buf, buffer, buffer_size);
    buf[buffer_size] = '\0';

    osrfLogInternal(OSRF_LOG_MARK, "WS received inbound message: %s", buf);

    msg_wrapper = jsonParse(buf);

    if (msg_wrapper == NULL) {
        osrfLogWarning(OSRF_LOG_MARK, "WS Invalid JSON: %s", buf);
        return HTTP_BAD_REQUEST;
    }

    osrf_msg = jsonObjectGetKeyConst(msg_wrapper, "osrf_msg");

    if (tmp_obj = jsonObjectGetKeyConst(msg_wrapper, "service")) 
        service = jsonObjectGetString(tmp_obj);

    if (tmp_obj = jsonObjectGetKeyConst(msg_wrapper, "thread")) 
        thread = jsonObjectGetString(tmp_obj);

    if (tmp_obj = jsonObjectGetKeyConst(msg_wrapper, "log_xid")) 
        log_xid = jsonObjectGetString(tmp_obj);

    if (log_xid) {

        // use the caller-provide log trace id
        if (strlen(log_xid) > MAX_THREAD_SIZE) {
            osrfLogWarning(OSRF_LOG_MARK, "WS log_xid exceeds max length");
            return HTTP_BAD_REQUEST;
        }

        osrfLogForceXid(log_xid);
    }

    if (thread) {

        if (strlen(thread) > MAX_THREAD_SIZE) {
            osrfLogWarning(OSRF_LOG_MARK, "WS thread exceeds max length");
            return HTTP_BAD_REQUEST;
        }

        // since clients can provide their own threads at session start time,
        // the presence of a thread does not guarantee a cached recipient
        recipient = (char*) apr_hash_get(
            trans->stateful_session_cache, thread, APR_HASH_KEY_STRING);

        if (recipient) {
            osrfLogDebug(OSRF_LOG_MARK, "WS found cached recipient %s", recipient);
        }
    }

    if (!recipient) {

        if (service) {
            int size = snprintf(recipient_buf, RECIP_BUF_SIZE - 1,
                "%s@%s/%s", trans->osrf_router, trans->osrf_domain, service);                                    
            recipient_buf[size] = '\0';                                          
            recipient = recipient_buf;

        } else {
            osrfLogWarning(OSRF_LOG_MARK, "WS Unable to determine recipient");
            return HTTP_BAD_REQUEST;
        }
    }

    osrfLogDebug(OSRF_LOG_MARK, 
        "WS relaying message to opensrf thread=%s, recipient=%s", 
            thread, recipient);

    msg_body = extract_inbound_messages(
        r, service, thread, recipient, osrf_msg);

    osrfLogInternal(OSRF_LOG_MARK, 
        "WS relaying inbound message: %s", msg_body);

    transport_message *tmsg = message_init(
        msg_body, NULL, thread, recipient, NULL);

    message_set_osrf_xid(tmsg, osrfLogGetXid());
    client_send_message(osrf_handle, tmsg);


    osrfLogClearXid();
    message_free(tmsg);                                                         
    jsonObjectFree(msg_wrapper);
    free(msg_body);

    last_activity_time = time(NULL);
    return OK;
}