Beispiel #1
0
static int process_msg(struct ipc *ipc, void *buf, size_t len)
{
    ipc_handler_t handler;
    uint32_t func_id = 0;
    char out_arg[1024];
    size_t out_len;
    size_t ret_len;
    struct ipc_packet *pkt = (struct ipc_packet *)buf;
    if (-1 == unpack_msg(pkt, &func_id, &out_arg, &out_len)) {
        logd("unpack_msg failed!\n");
        return -1;
    }
    if (find_ipc_handler(func_id, &handler) == 0 ) {
        handler.cb(ipc, out_arg, out_len, _arg_buf, &ret_len);//direct call cb is not good, will be change to workq
    } else {
        logi("no callback for this MSG ID in process_msg\n");
    }
    if (ret_len > 0) {
        if (0 > pack_msg(pkt, func_id, _arg_buf, ret_len)) {
            loge("pack_msg failed!\n");
            return -1;
        }
        ipc->ops->send(ipc, pkt, sizeof(ipc_packet_t) + ret_len);
    }
    return 0;
}
Beispiel #2
0
extern Buf slurm_persist_msg_pack(slurm_persist_conn_t *persist_conn,
				  persist_msg_t *req_msg)
{
	Buf buffer;

	xassert(persist_conn);

	if (persist_conn->flags & PERSIST_FLAG_DBD)
		buffer = pack_slurmdbd_msg((slurmdbd_msg_t *)req_msg,
					   persist_conn->version);
	else {
		slurm_msg_t msg;

		slurm_msg_t_init(&msg);

		msg.data      = req_msg->data;
		msg.data_size = req_msg->data_size;
		msg.msg_type  = req_msg->msg_type;
		msg.protocol_version = persist_conn->version;

		buffer = init_buf(BUF_SIZE);

		pack16(req_msg->msg_type, buffer);
		if (pack_msg(&msg, buffer) != SLURM_SUCCESS) {
			free_buf(buffer);
			return NULL;
                }
	}

	return buffer;
}
Beispiel #3
0
// Sends empty data message to first sensor to gather data
void send_data_msg(int num)
{
    int sockfd = sock_first;
    struct sockaddr_in addr = first_addr;
    char* nums = "first";
    if(num == LAST)
    {
        sockfd = sock_last;
        addr = last_addr;
        nums = "last";
    }
    union msg msg;
    msg.data.type = DATA_MSG;
    msg.data.count = 0;
    msg.data.data = NULL;
    int buf_len = sizeof(msg.data);
    unsigned char* buf = malloc(buf_len);
    if(pack_msg(&msg, buf, buf_len) < 0)
    {
        print_error("Failed to pack data msg");
        exit(-1);
    }
    unsigned first_len = sizeof(addr);
    if(sendto(sockfd, buf, buf_len, 0, (struct sockaddr* )&addr, first_len) < 0)
    {
        print_error("Failed to send data msg to %d", num);
        exit(-1);
    }
    print_info("Data message sent to %s sensor", nums);
    free(buf);
}
Beispiel #4
0
int pomme_client_put_data(
	uuid_t id,
	int handle, 
	void *buffer,
	int len,
	int flags)
{
	/*
	 * what should consider is whether we should
	 * use async way to send, of use sync interface
	 */
    int ret = 0;
    int first_msg_send;
    pomme_protocol_t pro; 
    memset(&pro, 0, sizeof(pomme_protocol_t));
    pro.op = put_data ;
    pro.total_len = len ;

    uuid_copy(pro.id, id);


    first_msg_send = len > POMME_MAX_PROTO_DATA ? POMME_MAX_PROTO_DATA:len;
    pro.len = first_msg_send;
    // how to handle the data should be very serious considered
    // reduce the cost of memcopy ,that mean we should reorganize the
    // struct of pomme_protocol_t

    pomme_pack_t *buf = NULL;

    ret = pack_msg( &pro , &buf);
    if( ret < 0 )
    {
    	goto err;
    }

//    ret = pomme_send(handle, buf->data,
//    		pomme_msg_len((&pro)), flags);

    ret = pomme_send(handle, buf->data,
    		buf->cur, flags);
    if( ret < 0 )
    {
    	debug("send package failure");
    	goto err;
    }

    if( len - first_msg_send > 0  )
    {
    	ret = pomme_send(handle, buf->data+ POMME_PACKAGE_SIZE,
    			len - first_msg_send, flags);
    	if( ret < 0 )
    	{
    		debug("send rest data of the package failure");
    	}
    }

err:
    pomme_pack_distroy(&buf);
    return ret;
}
Beispiel #5
0
// Sends error message
int send_error_msg(int num)
{
    int sockfd = sock_first;
    struct sockaddr_in addr = first_addr;
    char* nums = "first";
    if(num == LAST)
    {
        sockfd = sock_last;
        addr = last_addr;
        nums = "last";
    }
    union msg msg;
    msg.info.type = ERR_MSG;
    int buf_len = sizeof(msg.info);
    unsigned char* buf = malloc(buf_len);
    if(pack_msg(&msg, buf, buf_len) < 0)
    {
        print_error("Failed to pack info msg");
        exit(-1);
    }
    unsigned len = sizeof(addr);
    if(sendto(sockfd, buf, buf_len, 0, (struct sockaddr* )&addr, len) < 0)
    {
        print_error("Failed to send error msg\nClosing socket");
        close(sockfd);
        free(buf);
        return -1;
    }
    print_info("Error message sent to %s sensor", nums);
    free(buf);
    return 0;
}
Beispiel #6
0
int rpc_call(struct rpc *r, uint32_t msg_id,
                const void *in_arg, size_t in_len,
                void *out_arg, size_t out_len)
{
    if (!r) {
        loge("invalid parament!\n");
        return -1;
    }
    size_t pkt_len = pack_msg(&r->send_pkt, msg_id, in_arg, in_len);
    if (pkt_len == 0) {
        loge("pack_msg failed!\n");
        return -1;
    }
    if (0 > rpc_send(r, in_arg, in_len)) {
        loge("skt_send failed, fd = %d!\n", r->fd);
        return -1;
    }
    if (IS_RPC_MSG_NEED_RETURN(msg_id)) {
        if (thread_sem_wait(r->dispatch_thread, 2000) == -1) {
            loge("wait response failed %d:%s\n", errno, strerror(errno));
            return -1;
        }
        logi("recv_pkt.len = %d\n", r->recv_pkt.header.payload_len);
        memcpy(out_arg, r->recv_pkt.payload, out_len);
    } else {

    }
    return 0;
}
Beispiel #7
0
int pomme_client_close(int handle)
{
    int ret;
    int flags = 0;
    pomme_protocol_t pro;
    memset( &pro, 0, sizeof(pro));

    pro.op = data_close;
    pomme_pack_t *buf = NULL;
    if( ( ret = pack_msg(&pro, &buf) ) < 0 )
    {
	debug("pack msg failure");
	goto err;
    }

    if( ( ret = pomme_send(handle, buf->data, 
		    pomme_msg_get_len((&pro)),flags )) < 0)
    {
	debug("send data fail");
	goto buf_err;
    }
    close(handle);
buf_err:
    pomme_pack_distroy(&buf);
err:
    return ret;

}
int pack_and_send(int sockfd, char *msg, uint32_t tag, uint32_t flags, struct packet *buf)
{
				int sent=0;
				pack_msg(msg, tag, flags, buf);
				sent += send_header(sockfd, buf);
				sent += send_msg(sockfd, msg);
				return sent;
}
Beispiel #9
0
void say_hello_to_ttu()
{
    void * pmsg = pack_msg(CACHE_REQ, -1, 30, "Nice to meet you!");
    if(SM_OK != send_msg(pmsg)){
        perror("send msg failed");
    }
    //exit(1);
}
Beispiel #10
0
void simu_fire::update()
{
	if(function_type == "change_vel") func_change_vel();

	internal_msg msg = pack_msg();
	entity_ptr->ent_mgr->async_call(boost::bind(&entity_manager::handle_internal_msg_check, entity_ptr->ent_mgr, msg.ent_id, msg));

	if(function_type == "change_vel" && !is_end_change_vel()) timer_scheduler_ptr->add_timer(ent_id, time_interval, boost::bind(&simu_fire::update, shared_from_this()), tid);
}
Beispiel #11
0
void proc_cache_resp(msg_t* data)
{
    fsm_msg_head* fsm_head = (fsm_msg_head*) data->data;
    req_t* r = (req_t*) fsm_head->data;
    printf("ttu:%s\n", r->what);
    
    void* pmsg = pack_msg(CACHE_QUERY_REQ, r->src_fsmid, 31, "How are you?");
    if(SM_OK != send_msg(pmsg)){
        perror("send msg failed");
    }
    //exit(1);
}
Beispiel #12
0
int ipc_call(struct ipc *ipc, uint32_t func_id,
                const void *in_arg, size_t in_len,
                void *out_arg, size_t out_len)
{
    if (!ipc) {
        loge("invalid parament!\n");
        return -1;
    }
    if (0 > pack_msg(_pkt_sbuf, func_id, in_arg, in_len)) {
        loge("pack_msg failed!\n");
        return -1;
    }
    ipc->ops->send(ipc, _pkt_sbuf, sizeof(ipc_packet_t) + in_len);
    if (IS_IPC_MSG_NEED_RETURN(func_id)) {
        struct timeval now;
        struct timespec abs_time;
        uint32_t timeout = 2000;//msec
        gettimeofday(&now, NULL);
        /* Add our timeout to current time */
        now.tv_usec += (timeout % 1000) * 1000;
        now.tv_sec += timeout / 1000;
        /* Wrap the second if needed */
        if ( now.tv_usec >= 1000000 ) {
            now.tv_usec -= 1000000;
            now.tv_sec ++;
        }
        /* Convert to timespec */
        abs_time.tv_sec = now.tv_sec;
        abs_time.tv_nsec = now.tv_usec * 1000;
        if (-1 == push_async_cmd(ipc, func_id)) {
            loge("push_async_cmd failed!\n");
        }
        if (-1 == sem_timedwait(&ipc->sem, &abs_time)) {
            loge("response failed %d:%s\n", errno, strerror(errno));
            return -1;
        }
        memcpy(out_arg, ipc->resp_buf, out_len);
    } else {

    }
    return 0;
}
Beispiel #13
0
// Sends reconf message to sensors
void send_reconf_msg()
{
    union msg msg;
    msg.info.type = RECONF_MSG;

    int buf_len = sizeof(msg.info);
    unsigned char* buf = malloc(buf_len);
    if(pack_msg(&msg, buf, buf_len) < 0)
    {
        print_error("Failed to pack reconf msg");
        exit(-1);
    }
    unsigned first_len = sizeof(first_addr);
    if(sendto(sock_first, buf, buf_len, 0, (struct sockaddr* )&first_addr, first_len) < 0)
    {
        print_error("Failed to send reconf msg");
        exit(-1);
    }
    print_info("Reconf message sent");
    free(buf);
}
Beispiel #14
0
// Sends initializing message to sensors
void send_init_msg()
{
    union msg msg;
    msg.init.type = INIT_MSG;
    msg.init.timeout = sensor_timeout;
    msg.init.period = sensor_period;

    int buf_len = sizeof(msg.init);
    unsigned char* buf = malloc(buf_len);
    if(pack_msg(&msg, buf, buf_len) < 0)
    {
        print_error("Failed to pack init msg");
        exit(-1);
    }
    unsigned first_len = sizeof(first_addr);
    if(sendto(sock_first, buf, buf_len, 0, (struct sockaddr* )&first_addr, first_len) < 0)
    {
        print_error("Failed to send init msg");
        exit(-1);
    }
    print_info("Init message sent");
    free(buf);
}
/***
 ** subthread main program
 ***/
void *soldier_mission(void *task) {
    /** set subthread unable cancel **/
    //int setcancel_rst;
    //int oldstate = 0;
    //int oldtype = 0;
    //setcancel_rst = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
    //setcancel_rst = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

    struct subthread_task local_task = *((struct subthread_task *)task);
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    struct sockaddr_in servaddr;
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(local_task.port);
    if(inet_pton(AF_INET, local_task.hostname, &servaddr.sin_addr) <= 0) {
        printf("[%s] is not avalid IP address!\n", local_task.hostname);
        return NULL;
    }
    if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) {
        perror("connect error");
        return NULL;
    }
    
    struct msg_synack syn_msg;
    struct synack syn_pack;

    syn_pack.downloadable = 1;
    syn_pack.thread_num = THREAD_LIMITION;
    syn_pack.file_info = local_task.file_info;

    syn_msg = pack_msg_synack(SYN, 0, sizeof(syn_pack), syn_pack);
    send(sockfd, &syn_msg, sizeof(struct msg_synack), 0);

    /** set socket timeout time **/
    struct timeval tv;
    tv.tv_sec = SOCK_TIMEOUT_SEC;
    tv.tv_usec = SOCK_TIMEOUT_USEC;
    if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        printf("socket option  SO_RCVTIMEO not support\n");
        close(sockfd);
        return NULL;
    }

    /*receive syn_ack from sever*/
    int times = 0;
    char temp_buf[MAXLEN];
    struct synack synack_pack;
    while(soldierLive == YES){
        int recv_len = recv(sockfd, temp_buf, sizeof(struct msg_synack) , 0);
        if(recv_len == -1) {
            printf("[thread: %d]timeout or read error\n", local_task.thread_no);
            send(sockfd, &syn_msg, sizeof(struct msg_synack), 0);
            if(++times > 3) {
                printf("[thread: %d] failed to receive syn_ack, transmit abort !\n", local_task.thread_no);
                return NULL;
            }
            continue;
        }
        struct message temp_msg = depack_msg(temp_buf, recv_len);
        if(temp_msg.type == SYN_ACK) {
            memcpy(&synack_pack, temp_msg.body, temp_msg.length);
            printf("[thread: %d] get syn_ack\n", local_task.thread_no);
            break;
        }else {
            printf("[thread: %d] received type: %d, not syn_ack we wanted, receive again\n", local_task.thread_no, temp_msg.type);
            continue;
        }
    }
    
    //setcancel_rst = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
    //setcancel_rst = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
    //pthread_cleanup_push(close_cocket, &sockfd);

    int bytes_send = 0;
    int index = 0;
    int selfLive = YES;

    char target_full[100];
    strcpy(target_full, path);
    strcat(target_full, local_task.file_info.filename);

    FILE *targHandler = fopen(target_full, "rb");
    if(targHandler == NULL){
        printf("[thread: %d] open target file failed!\n", local_task.thread_no);
        return NULL;
    }
    while(soldierLive == YES && selfLive == YES){
        index = popIndex(NEW, PENDING, 1, local_task.thread_no);
        if(index == -1){
            printf("[thread: %d] reach the end index, exiting\n", local_task.thread_no);
            break;
        }
        char body[BODYLEN];
        int seekNum = index * BODYLEN;

        int seek_rst = fseek(targHandler, seekNum, SEEK_SET);
        int bytes_read = fread(body, sizeof(char), BODYLEN, targHandler);
        /*printf("[thread: %d] read length = %d, seek_rst = %d\n", local_task.thread_no, bytes_read, seek_rst);*/

        struct message ack_msg = pack_msg(DATA, index, bytes_read, body);
        bytes_send = sendto(sockfd, &ack_msg, sizeof(struct message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
        //printf("[thread: %d] ========= send DATA index = %d, bytes = %d, \n", local_task.thread_no, index, bytes_send);
    }

    //printf("will enter second cycle in 5s ... \n");
    //sleep(5);
    ///* second cycle check */
    //while(soldierLive == YES && selfLive == YES){
        //if(index >= block_num){
            ////printf("[thread: %d] reach the end index, exiting\n", local_task.thread_no);
            //continue;
        //}
        //index = popIndex(PENDING, OLD_TASK, 2, local_task.thread_no);
        //char body[BODYLEN];
        //int seekNum = index * BODYLEN;

        //int seek_rst = fseek(targHandler, seekNum, SEEK_SET);
        //int bytes_read = fread(body, sizeof(char), BODYLEN, targHandler);

        //struct message ack_msg = pack_msg(DATA, index, bytes_read, body);
        //bytes_send = sendto(sockfd, &ack_msg, sizeof(struct message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
        //printf("[thread: %d] ========= send DATA index = %d, bytes = %d, \n", local_task.thread_no, index, bytes_send);
    //}

    /** send FIN pack to server **/
    struct synack fin_pack ;
    struct msg_synack fin_msg = pack_msg_synack(FIN, 0, sizeof(struct synack), fin_pack);
    sendto(sockfd, &fin_msg, sizeof(struct msg_synack), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
    printf("[thread: %d] send FIN to finish transmition...\n", local_task.thread_no);

    fclose(targHandler);
    close(sockfd);
    //pthread_cleanup_pop(1);
    //thread_cleanup_pop(1);
    return NULL;
}
Beispiel #16
0
int pomme_client_get_data(uuid_t id,
	size_t off,
	size_t len,
	int handle,
	void *buffer,
	int *r_len)
{
    assert( buffer !=NULL );
    assert( r_len  != NULL );
    int ret = 0,flags=0;

    pomme_protocol_t pro,rpro;
    memset( &pro, 0, sizeof(pro));
    memset( &rpro, 0, sizeof(rpro));

    pro.op = get_data;
    pro.total_len = len;
    pro.offset = off;
    uuid_copy(pro.id, id);

    pomme_pack_t *buf = NULL;
    if( ( ret = pack_msg(&pro, &buf) ) < 0 )
    {
	debug("pack msg failure");
	goto err;
    }

    if( ( ret = pomme_send(handle, buf->data, 
		    pomme_msg_get_len((&pro)),flags )) < 0)
    {
	debug("send data fail");
	goto err;
    }
    /*
     * recv data package
     */
    unsigned char t_buffer[POMME_PACKAGE_SIZE];
    size_t t_len = 0;

    if( ( ret = pomme_recv(handle, t_buffer, 
		    POMME_PACKAGE_SIZE,
	    &t_len, flags) ) < 0 )
    {
	debug("recv first fail");
	goto err;
    }

    pomme_pack_t *p_buffer = NULL;
    if( (ret = pomme_pack_create(&p_buffer,t_buffer,
		    t_len)) < 0 )
    {
	debug("create buffer error");
	goto err;
    }	

    if( (ret = unpack_msg( &rpro, p_buffer) ) < 0 )
    {
	debug("unpack msg fail");
	goto data_err;
    }

    pomme_print_proto(&rpro,NULL);
    if( rpro.op != put_data )
    {
	debug("wrong operation");
	goto data_err;
    }
    if( rpro.total_len > len )
    {
	debug("too much data recved");
	goto data_err;
    }

    size_t tr_len = rpro.len;
    memcpy(buffer,t_buffer, tr_len);

    while( tr_len < rpro.total_len )
    {
	size_t tmp = 0;
	ret = pomme_recv(handle, buffer+tr_len, 
		rpro.total_len - tr_len, 
		&tmp, flags);
	if( ret < 0 )
	{
	    debug("recv err,data total get:%d",tr_len);
	    break;
	}
	tr_len += tmp;
    }
    *r_len = tr_len;
    debug("data len %d",tr_len);
data_err:
    pomme_pack_distroy(&p_buffer);

err:
    pomme_pack_distroy(&buf); 
    return ret;
}