APUE course source code
guowenxue
3 hours ago 805b28fc2e633bcd9c823a4b23614378ebfd50a1
project/2.socketd/socket_client.c
@@ -27,7 +27,7 @@
#include "database.h"
#define PROG_VERSION               "v1.0.0"
#define DAEMON_PIDFILE             "/tmp/.socketc.pid"
#define DAEMON_PIDFILE             "/tmp/socketc.pid"
static void print_usage(char *progname)
{
@@ -46,15 +46,15 @@
    return;
}
int check_sample_time(time_t *last_time, int interval);
int main (int argc, char **argv)
{
    char               *progname=NULL;
    int                 daemon = 1;
    int                 rv;
    int                 rowid = 0;
    time_t              now;
    char               *logfile="sock_client.log";
    char               *logfile="/tmp/socketc.log";
    int                 loglevel=LOG_LEVEL_INFO;
    int                 logsize=10; /* logfile size max to 10K */
@@ -119,7 +119,6 @@
            default:
                break;
        }
    }
    if( !serverip || !port )
@@ -128,28 +127,36 @@
        return 0;
    }
    /* initial socket context */
    socket_init(&sock, serverip, port);
    /* open logger system */
    if( log_open(logfile, loglevel, logsize, THREAD_LOCK_NONE) < 0 )
    {
        fprintf(stderr, "Initial log system failed\n");
        fprintf(stderr, "Initial logger failed\n");
        return 1;
    }
    /* install signal proc handler */
    install_default_signal();
    /* check program already running or not, if already running then exit, or set running as daemon */
    if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 )
        goto cleanup;
    log_info("socket client start running.\n");
    if( database_init("sock_client.db") < 0 )
    /* open database for cache data */
    if( db_open("sock_client.db") < 0 )
    {
        return 2;
        fprintf(stderr, "Initial database failed\n");
        rv = 2;
        goto cleanup;
    }
    socket_init(&sock, serverip, port);
    /* check whether the program is running; if not, start it in the background. */
    if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 )
    {
        fprintf(stderr, "Program is already running. Exiting.\n");
        rv = 3;
        goto cleanup;
    }
    /* install signal handler */
    install_default_signal();
    log_info("socket client start running.\n");
    while( ! g_signal.stop )
    {
@@ -158,11 +165,10 @@
         * +----------------------------------+*/
        sample_flag = 0; /* clear sample flag */
        time(&now);      /* get current time */
        if( check_sample_time(&last_time, interval) )
        if( difftime(now, last_time) >= interval )
        {
            log_debug("start DS18B20 sample termperature\n");
            if( (rv=ds18b20_get_temperature(&pack_info.temper)) < 0 )
            {
                log_error("DS18B20 sample temperature failure, rv=%d\n", rv);
@@ -176,6 +182,7 @@
            pack_bytes = pack_proc(&pack_info, (uint8_t *)pack_buf, sizeof(pack_buf));
            log_dump(LOG_LEVEL_DEBUG, NULL, pack_buf, pack_bytes);
            last_time = now; /* update last sample time */
            sample_flag = 1; /* set sample flag */
        }
@@ -184,7 +191,7 @@
         * +---------------------------------+*/
        /* start connect to server if not connected */
        if( !socket_connected(&sock) )
        if( !socket_check(&sock) )
        {
            socket_connect(&sock);
        }
@@ -192,13 +199,14 @@
        /* +-------------------------------+
         * |      socket disconnect        |
         * +-------------------------------+*/
        if( !socket_connected(&sock) )
        if( !sock.connected )
        {
            if( sample_flag )
            {
                database_push_packet(pack_buf, pack_bytes);
                db_write(pack_buf, pack_bytes);
            }
            /* Bypass the data send procedure for socket disconnect */
            continue;
        }
@@ -213,13 +221,13 @@
            if( socket_send(&sock, pack_buf, pack_bytes) < 0 )
            {
                log_warn("socket send sample packet failure, save it in database now.\n");
                database_push_packet(pack_buf, pack_bytes);
                db_write(pack_buf, pack_bytes);
                continue;
            }
        }
        /*  socket send packet in database  */
        if( !database_pop_packet(pack_buf, sizeof(pack_buf), &pack_bytes) )
        /*  socket send cache packet */
        if( !db_read(pack_buf, sizeof(pack_buf), &pack_bytes, &rowid) )
        {
            log_debug("socket send database packet bytes[%d]: %s\n", pack_bytes, pack_buf);
            if( socket_send(&sock, pack_buf, pack_bytes) < 0 )
@@ -230,34 +238,21 @@
            else
            {
                log_warn("socket send database packet okay, remove it from database now.\n");
                database_del_packet();
                db_remove(rowid);
            }
        }
        msleep(5);
    }
    rv = 0;
cleanup:
    socket_term(&sock);
    database_term();
    unlink(DAEMON_PIDFILE);
    db_close();
    log_close();
    unlink(DAEMON_PIDFILE);
    return 0;
    return rv;
}
int check_sample_time(time_t *last_time, int interval)
{
    int                  need = 0; /* no need sample now */
    time_t               now;
    time(&now);
    if( difftime(now, *last_time)>interval )
    {
        need = 1; /* need sample now  */
        *last_time = now;
    }
    return need;
}