Example #1
0
static void smsbox_thread(void *arg)
{
    Connection *conn;
    Msg *msg;
    Octstr *os;
    Octstr *reply_msg;
    unsigned long count;
    
    msg = msg_create(sms);
    msg->sms.sender = octstr_create("123");
    msg->sms.receiver = octstr_create("456");
    msg->sms.msgdata = octstr_create("hello world");
    reply_msg = msg_pack(msg);
    msg_destroy(msg);

    gwthread_sleep(1.0);
    conn = conn_open_tcp(bearerbox_host, port_for_smsbox, NULL);
    if (conn == NULL) {
	gwthread_sleep(2.0);
	conn = conn_open_tcp(bearerbox_host, port_for_smsbox, NULL);
    	if (conn == NULL)
	    panic(0, "Couldn't connect to bearerbox as smsbox");
    }

    while (!quitting && conn_wait(conn, -1.0) != -1) {
    	for (;;) {
	    os = conn_read_withlen(conn);
	    if (os == NULL) {
		if (conn_eof(conn) || conn_error(conn))
		    goto error;
		break;
	    }
	    
	    msg = msg_unpack(os);
	    if (msg == NULL || msg->type == wdp_datagram)
		error(0, "Bearerbox sent garbage to smsbox");

	    if (msg->type == sms) {
		if (first_from_bb == (time_t) -1)
		    time(&first_from_bb);
		count = counter_increase(num_from_bearerbox) + 1;
		debug("test.smpp", 0, 
		      "Bearerbox sent sms #%ld <%s> to smsbox, sending reply.",
		      count, octstr_get_cstr(msg->sms.msgdata));
		if (count == max_to_esme)
		    info(0, "Bearerbox has sent all messages to smsbox.");
		conn_write_withlen(conn, reply_msg);
		counter_increase(num_to_bearerbox);
	    }
	    msg_destroy(msg);
	    octstr_destroy(os);
	    time(&last_to_bb);
	}
    }
    
error:
    conn_destroy(conn);
    octstr_destroy(reply_msg);
    debug("test.smpp", 0, "%s terminates.", __func__);
}
Example #2
0
int pack_item(tlv_t * item, void * outbuf, uint32_t * outsize)
{
	int ret = 0;

	if ((NULL == item) || (NULL == outbuf) || (NULL == outsize)) {
		return -1;
	}

	switch (item->type) {
		case TLV_TYPE_INT8:
		case TLV_TYPE_UINT8:
			ret = pack_int8((uint8_t *)(item->value), outbuf, outsize);
			break;
		case TLV_TYPE_INT16:
		case TLV_TYPE_UINT16:
			ret = pack_int16((uint16_t *)(item->value), outbuf, outsize);
			break;
		case TLV_TYPE_INT32:
		case TLV_TYPE_UINT32:
			ret = pack_int32((uint32_t *)(item->value), outbuf, outsize);
			break;
		case TLV_TYPE_BYTES:
			ret = pack_bytes(item->value, item->length, outbuf, outsize);
			break;
		case TLV_TYPE_MSG:
			ret = msg_pack((message_t *)(item->value), outbuf, outsize);
			break;
		default:
			ret = -1;
			break;
	}

	return ret;
}
Example #3
0
static int send_msg(Boxc *boxconn, Msg *pmsg)
{
    Octstr *pack;

    pack = msg_pack(pmsg);

    if (pack == NULL)
        return -1;

    if (boxconn->boxc_id != NULL)
        debug("bb.boxc", 0, "send_msg: sending msg to boxc: <%s>",
          octstr_get_cstr(boxconn->boxc_id));
    else
        debug("bb.boxc", 0, "send_msg: sending msg to box: <%s>",
          octstr_get_cstr(boxconn->client_ip));

    if (conn_write_withlen(boxconn->conn, pack) == -1) {
    	error(0, "Couldn't write Msg to box <%s>, disconnecting",
	      octstr_get_cstr(boxconn->client_ip));
        octstr_destroy(pack);
        return -1;
    }

    octstr_destroy(pack);
    return 0;
}
Example #4
0
void write_to_bearerbox_real(Connection *conn, Msg *pmsg)
{
    Octstr *pack;

    pack = msg_pack(pmsg);
    if (conn_write_withlen(conn, pack) == -1)
    	error(0, "Couldn't write Msg to bearerbox.");

    msg_destroy(pmsg);
    octstr_destroy(pack);
}
Example #5
0
int msg_send(int sd, int count, unsigned char action)
{
	int result = count;
	netmsg_t msg;

	msg_pack(&msg, action);
	while (count--) {
		if (sendto(sd, &msg, sizeof(msg), 0, (void *)&gServerStruct, sizeof(gServerStruct)) != sizeof(msg)) {
			msg_erro("sendto() failed at %s, err=%d\n", __func__, errno);
			result--;
		}
	}

	return (result != count ? 0 : -1);
}
Example #6
0
 void ZMQUtil::ZMQPoll(zmq::pollitem_t *items, int count, 
     std::vector<zmq::socket_t*> &sockets,
     std::queue<std::shared_ptr<MsgPack>> &msg_queue)
 {
     if (zmq::poll(items, count, kZMQ_POLL_TIMEOUT) > 0)
     {
         for (int i = 0; i < count; ++i)
         {
             if (items[i].revents & ZMQ_POLLIN)
             {
                 std::shared_ptr<MsgPack> msg_pack(new MsgPack(sockets[i]));
                 msg_queue.push(msg_pack);
             }
         } // end for
     } // end if
 }
Example #7
0
int deliver_to_bearerbox_real(Connection *conn, Msg *msg) 
{
     
    Octstr *pack;
    
    pack = msg_pack(msg);
    if (conn_write_withlen(conn, pack) == -1) {
    	error(0, "Couldn't deliver Msg to bearerbox.");
        octstr_destroy(pack);
        return -1;
    }
                                   
    octstr_destroy(pack);
    msg_destroy(msg);
    return 0;
}
Example #8
0
static int send_msg(Connection *conn, Boxc *boxconn, Msg *pmsg)
{
    Octstr *pack;

    pack = msg_pack(pmsg);

    if (pack == NULL)
        return -1;

    if (conn_write_withlen(conn, pack) == -1) {
        error(0, "Couldn't write Msg to box <%s>, disconnecting",
          octstr_get_cstr(boxconn->client_ip));
        octstr_destroy(pack);
        return -1;
    }
    octstr_destroy(pack);
    return 0;
}
 // Sends messages to each server for creating a server table.
 void Multiverso::AddServerTable(integer_t table, integer_t rows, 
     integer_t cols, Type type, Format default_format)
 {
     for (int server = 0; server < reg_info_.server_count; ++server)
     {
         MsgPack msg_pack(MsgType::CreateTable, MsgArrow::Worker2Server, 
             reg_info_.proc_rank, server);
         zmq::message_t *msg = 
             new zmq::message_t(3 * sizeof(integer_t) + 2 * sizeof(int));
         integer_t *buf = static_cast<integer_t*>(msg->data());
         buf[0] = table;                                 // table id
         buf[1] = rows;                                  // rows
         buf[2] = cols;                                  // cols
         int *buf_i = static_cast<int*>(buf + 3);
         buf_i[0] = static_cast<int>(type);              // type
         buf_i[1] = static_cast<int>(default_format);    // default_format
         msg_pack.Push(msg);
         msg_pack.Send(socket_);
     }
 }
Example #10
0
File: srv.c Project: m101/ihf
int cmd_read(void) {
    uint8_t *buf, *msg;
    int fd;
    int len;

    fd = open(FIFO_OUTPUT, O_RDONLY);
    if (fd <= 0)
        return -1;

    buf = malloc(sizeof(char) * BUFMAX);
    if (!buf) {
        fprintf(stderr, "Error allocating reading buffer !");
        return -1;
    }
    len = read(fd, &buf, BUFMAX);
    msg = msg_pack(MSG_TYPE_DATA, buf, len);
    if (!msg) {
        free(buf);
        return -1;
    }

    len = write(STDOUT_FILENO, msg, IHF_FIXLEN + len);
    if (len <= 0) {
        /* XXX handle error */
        return -1;
    }
    else if (len < IHF_FIXLEN + len) {
        /* XXX handle error */
    }

    free(buf);
    free(msg);
    close(fd);

    return 0;
}
Example #11
0
//int main(void)
int
uart_thread(void *arg)
{

	int serial_fd, recv_fd, tran_fd;	

	
	fd_set inset, tmp_inset;
	struct timeval tv;
	unsigned loop = 1;
	int  real_read, i, real_write, data_len;
	int rsp_msg, rsp_counter, write_len = 0;
	static int step = 0;
	unsigned char rcv_buf[BUFFER_SIZE]; 
	unsigned char snd_buf[BUFFER_SIZE];
	unsigned char data_t;
	unsigned char errvalue;
	int rcv_trans = 0;
	int ret;
	

	/*The serial port data written to this file*/
	/*if ((recv_fd = open(RECV_FILE_NAME, O_CREAT|O_WRONLY, 0644))  < 0){
		perror("open");
		printf("\n\n\nzorro, can't open recv_file: %s\n", strerror(errno));
		return 1;
	}
	
	printf("the recv_file fd = %d,\n", recv_fd);
	*/
	/*The transmit data written to this file*/
	/*if ((tran_fd = open(TRAN_FILE_NAME, O_CREAT|O_WRONLY, 0644))  < 0){
		perror("open");
		printf("\n\n\nzorro, can't open tran_file: %s\n", strerror(errno));
		return 1;
	}
	
	printf("the tran_file fd = %d,\n", tran_fd);
	*/

	//fds[0] = STDIN_FILENO; /*The standard input*/

	if ((serial_fd = open_port(TARGET_COM_PORT)) < 0){
		perror("open_port");
		printf("\n\n\nzorro, can't open target com port: %s\n", strerror(errno));
		return 1;
	}

	printf("the serial_file fd = %d,\n", serial_fd);
	/*config com*/
	if (set_com_config(serial_fd, 115200, 8, 'N', 1) < 0){
		perror("set_com_config");
		printf("\n\n\nzorro, can't set com fonfig: %s\n", strerror(errno));
		return 1;
	}

	printf("Input some words(enter 'quit' to exit):\n");

	while (1)
	{

		if (!rcv_trans){  /*rcv uart data */
			/*read frame head from serial port*/
			real_read = read(serial_fd, &data_t, 1);

			if ((real_read <= 0) && (errno != EAGAIN)){
				  printf("\nzorro, ERROR:READ FRAME HEAD error1 real_read = %d, \n", real_read);
			 }

			 switch (step){
				 case 0x00:  //DA_DECODE_SYN_HEAD
					 if (data_t == 0xAA){
						 rcv_buf[0] = data_t;
						 step=1;
					 }
				 break;
				 case 0x01: //DA_DECODE_GET_SYN_COUNTER
					   if (data_t == 0xAA)
					   	step = 0;
					   else{
							   rcv_buf[1] = data_t;
							   step = 2;
						 }		  
					 break;
				 case 0x02: //DA_DECODE_GET_DATA_LENGTH				 
					 if ((data_t < 2)  ||(data_t > 40))
					           step = 0;
					else{
						data_len = rcv_buf[2] = data_t;
						 step  = 3;
						 i = 0;
					 }
					 break;
				 case 0x03: //DA_DECODE_GET_DATA and DA_DECODE_CHECK	
					rcv_buf[3+i++] = data_t;
					if (i == data_len){
						step = 0;	          //reset the unpack step
						int check;
						check = check_frame_sum(rcv_buf, (data_len+2));
					        	
						if (!check){
							if (msg_unpack(rcv_buf,&rsp_msg, &errvalue) < 0){
								printf("zorro, ERROR:cmd unpack err\n");
							}
							else{
								rcv_trans = 1;
							}
							
							//rcv_buf[data_len+3] = '\0';
							/*write the data to commen file*/
							//write(recv_fd, rcv_buf, data_len+4); 
						}
						else if (check < 0){
							printf("\nzorro, ERROR:check sum error \n");
						}
						
					}
					break;	 
					 
				 }
		}
		else{	/*transmit uart data */

			ret = msg_pack(snd_buf,rsp_msg, rsp_counter, errvalue, &write_len);
			/*ret > 0, send this frame and continue ; 
			   ret = 0, send this frame and , 
			   ret < 0, dont send this frame */
			if (ret <= 0){
				rcv_trans = 0;
				if (ret < 0)
					continue;
			}

			//snd_buf[write_len] = '\0';
			/*write the data to commen file*/
			//write(tran_fd, snd_buf, write_len+1);
			
			 /*read frame head from serial port*/
			real_write = write(serial_fd, &snd_buf, write_len);
			if ((real_write <= 0) && (errno != EAGAIN)){
				  printf("\nzorro, ERROR:write cmd error1 real_write = %d, \n", real_write);
			}
			
			if ((write_len - real_write) != 0)
				 printf("error!\n");
			else
				rsp_counter ++;


		}
	}

}
Example #12
0
static void msg_push(Msg *msg) {
	Octstr *os;
	os = msg_pack(msg);
	gwlist_append(list, os);
	//octstr_destroy(os);
}