| | |
| | | char pack_buf[1024]; |
| | | int pack_bytes = 0; |
| | | pack_info_t pack_info; |
| | | pack_proc_t pack_proc = packet_segmented_pack; /* use string packet */ |
| | | pack_proc_t pack_proc = packet_segmented_pack; /* use segmented string packet */ |
| | | //pack_proc_t pack_proc = packet_json_pack; /* use JSON string packet */ |
| | | //pack_proc_t pack_proc = packet_tlv_pack; /* use TLV(Tag Length Value) packet */ |
| | | |
| | | struct option opts[] = { |
| | | {"ipaddr", required_argument, NULL, 'i'}, |
| | |
| | | * +---------------------------------+*/ |
| | | |
| | | /* start connect to server if not connected */ |
| | | if( sock.fd < 0 ) |
| | | if( !socket_connected(&sock) ) |
| | | { |
| | | socket_connect(&sock); |
| | | } |
| | | |
| | | /* check socket connected or not */ |
| | | if( sock_check_connect(sock.fd) < 0 ) |
| | | { |
| | | if( sock.fd > 0 ) |
| | | { |
| | | log_error("socket got disconnected, terminate it and reconnect now.\n"); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | } |
| | | |
| | | /* +-------------------------------+ |
| | | * | socket disconnect | |
| | | * +-------------------------------+*/ |
| | | if( sock.fd < 0 ) |
| | | if( !socket_connected(&sock) ) |
| | | { |
| | | if( sample_flag ) |
| | | { |
| | |
| | | { |
| | | log_warn("socket send sample packet failure, save it in database now.\n"); |
| | | database_push_packet(pack_buf, pack_bytes); |
| | | socket_term(&sock); /* close the soket */ |
| | | continue; |
| | | } |
| | | } |
| | | |
| | |
| | | if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) |
| | | { |
| | | log_error("socket send database packet failure"); |
| | | socket_term(&sock); /* close the soket */ |
| | | continue; |
| | | } |
| | | else |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | msleep(50); |
| | | msleep(5); |
| | | } |
| | | |
| | | cleanup: |
| | |
| | | |
| | | time(&now); |
| | | |
| | | if( now >= *last_time+interval ) |
| | | if( difftime(now, *last_time)>interval ) |
| | | { |
| | | need = 1; /* need sample now */ |
| | | *last_time = now; |