| | |
| | | #include "socket.h" |
| | | #include "sqlite_blob.h" |
| | | #include "util_proc.h" |
| | | #include "util_time.h" |
| | | |
| | | |
| | | void print_usage(char *progname) |
| | |
| | | int interval = 30; /* default report termperature every 30 seconds */ |
| | | int sn = 1; /* default serial number for device ID */ |
| | | int debug = 0; /* running in debug mode or not */ |
| | | int loglevel = LOG_LEVEL_INFO; |
| | | char *logfile="client.log"; |
| | | |
| | | socket_ctx_t sock; |
| | |
| | | |
| | | case 'd': |
| | | debug=1; |
| | | logfile="stdout"; |
| | | loglevel=LOG_LEVEL_DEBUG; |
| | | break; |
| | | |
| | | case 'I': |
| | |
| | | { |
| | | print_usage(argv[0]); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | if( debug ) |
| | | { |
| | | /* set logger to standard output with level debug */ |
| | | logger_init(NULL, LOG_LEVEL_DEBUG); |
| | | log_info("set program running on debug now.\n"); |
| | | } |
| | | else |
| | | { |
| | | /* set logger to $logfile with level info */ |
| | | if( logger_init(logfile, LOG_LEVEL_INFO) < 0 ) |
| | | { |
| | | fprintf(stderr, "Initial logger file '%s' failure: %s\n", logfile, strerror(errno)); |
| | | return 1; |
| | | } |
| | | /* set logger to $logfile with level info */ |
| | | if( logger_init(logfile, loglevel) < 0 ) |
| | | { |
| | | fprintf(stderr, "Initial logger file '%s' failure: %s\n", logfile, strerror(errno)); |
| | | return 1; |
| | | } |
| | | |
| | | if( !debug ) |
| | | { |
| | | log_info("set program running on background now.\n"); |
| | | daemon(1, 1); /* don't change work path, don't close opened file descriptor */ |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | /* socket disconnected */ |
| | | if( sock.fd < 0 ) |
| | | { |
| | | if( sample_flag ) |
| | | { |
| | | blobdb_push_packet(pack_buf, pack_bytes); |
| | | } |
| | | } |
| | | else /* socket connected */ |
| | | { |
| | | /* +---------------------------------+ |
| | | * | socket send sample packet | |
| | | * +---------------------------------+*/ |
| | | if( sample_flag ) |
| | | { |
| | | log_debug("socket send sample packet bytes[%d]: %s\n", pack_bytes, pack_buf); |
| | | if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) |
| | | { |
| | | log_warn("socket send sample packet failure, save it in database now.\n"); |
| | | blobdb_push_packet(pack_buf, pack_bytes); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | } |
| | | /* +-------------------------------+ |
| | | * | socket disconnect | |
| | | * +-------------------------------+*/ |
| | | if( sock.fd < 0 ) |
| | | { |
| | | if( sample_flag ) |
| | | { |
| | | blobdb_push_packet(pack_buf, pack_bytes); |
| | | } |
| | | |
| | | /* +---------------------------------+ |
| | | * | socket send packet in database | |
| | | * +---------------------------------+*/ |
| | | if( !blobdb_pop_packet(pack_buf, sizeof(pack_buf), &pack_bytes) ) |
| | | { |
| | | log_debug("socket send database packet bytes[%d]: %s\n", pack_bytes, pack_buf); |
| | | if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) |
| | | { |
| | | log_error("socket send database packet failure"); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | else |
| | | { |
| | | log_warn("socket send database packet okay, remove it from database now.\n"); |
| | | blobdb_del_packet(); |
| | | } |
| | | } |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | sleep(1); |
| | | } |
| | | /* +-------------------------------+ |
| | | * | socket connected | |
| | | * +-------------------------------+*/ |
| | | |
| | | /* socket send sample packet */ |
| | | if( sample_flag ) |
| | | { |
| | | log_debug("socket send sample packet bytes[%d]: %s\n", pack_bytes, pack_buf); |
| | | if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) |
| | | { |
| | | log_warn("socket send sample packet failure, save it in database now.\n"); |
| | | blobdb_push_packet(pack_buf, pack_bytes); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | } |
| | | |
| | | /* socket send packet in database */ |
| | | if( !blobdb_pop_packet(pack_buf, sizeof(pack_buf), &pack_bytes) ) |
| | | { |
| | | log_debug("socket send database packet bytes[%d]: %s\n", pack_bytes, pack_buf); |
| | | if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) |
| | | { |
| | | log_error("socket send database packet failure"); |
| | | socket_term(&sock); /* close the soket */ |
| | | } |
| | | else |
| | | { |
| | | log_warn("socket send database packet okay, remove it from database now.\n"); |
| | | blobdb_del_packet(); |
| | | } |
| | | } |
| | | |
| | | msleep(100); |
| | | } |
| | | |
| | | socket_term(&sock); |
| | | database_term(); |