Example #1
0
int main(int argc, char *argv[]) {

    init_slog("hw_server", "config/log.cfg", 3, NULL);
    slog(0, SLOG_INFO, "**************************************************");
    slog(0, SLOG_INFO, "HomeWatch Server v1.0, &copy 2015, Matthew Goulart");
    slog(0, SLOG_INFO, "**************************************************");
    //Configure buffer to output immediately
    setvbuf(stdout, 0, _IONBF, 0);

    //Mysql version and initializations
    slog(0, SLOG_INFO, "MySQL client version: %s", mysql_get_client_info());

    //Initialize the FDs for the sockets
    int socketfd, clientfd;

    //Initialize the list of FDs to monitor for input
    fd_set fdMonitor;

    //Initialize timer for select()
    struct timeval select_timeout;
    select_timeout.tv_sec = 3;
    select_timeout.tv_usec = 0;

    //Initialize the thread id
    pthread_t thread;

    slog(0, SLOG_INFO, "Starting server...");
    slog(0, SLOG_INFO, "Creating listen socket...");

    //Create the listen socket
    socketfd = create_inet_server_socket("0.0.0.0", "5677", LIBSOCKET_TCP,
                                         LIBSOCKET_IPv4, 0);

    if (socketfd < 0) {
        slog(2, SLOG_ERROR, "Error creating listen socket!");
        return -1;
    } else {
        slog(0, SLOG_INFO, "Listening...");
    }

    while (1) {
        FD_ZERO(&fdMonitor);
        FD_SET(socketfd, &fdMonitor);
        FD_SET(STDIN_FILENO, &fdMonitor);
        select_timeout.tv_sec = 3;
        select_timeout.tv_usec = 0;

        select(sizeof(fdMonitor) * 8, &fdMonitor, NULL, NULL,
               &select_timeout);

        if (FD_ISSET(socketfd, &fdMonitor)) {
            clientfd = accept_inet_stream_socket(socketfd, 0, 0, 0, 0, 0, 0);
            pthread_create(&thread, NULL, async_sensor_communicate, clientfd);
        }
    }

    return 0;

}
Example #2
0
/* 
 * main - Main function initializes everything ans starts irc 
 * loop which is responsible for keep alive irc connection.
 */
int main(int argc, char *argv[])
{
    /* Used variables */
    int sock, bytes;
    char buf[MAXMSG];
    MagtiSunLib msl;

    /* Irc info */
    IRCUser usr;
    IRCInfo inf;

    /* Thread safe */
    sync_init(&lock);
    slog_set_mutex(&lock);

    /* Greet users */
    greet("IRC Bot Derpina");
    slog(0, SLOG_INFO, "Logger Version: %s", slog_version(1));

    /* Catch ilegal signal */
    signal(SIGINT, clean_prog);
    signal(SIGSEGV, clean_prog);
    signal(SIGILL , clean_prog);

    /* Initialize logger and irc info */
    init_slog("derpina", "conf.cfg", 2, NULL);
    init_irc_info(&usr, &inf);

    /* Initialize irc config */
    if (!parse_config(CONFIG_FILE, &usr, &inf)) 
    {
        slog(0, SLOG_ERROR, "Can not parse config file: %s", CONFIG_FILE);
        slog(0, SLOG_ERROR, "invalid config file or missing some value");
        exit(-1);
    }

    /* Parse cli arguments */
    parse_arguments(argc, argv, &usr, &inf);

    /* Fix missing user input */
    fix_missing_input(&usr, &inf);

    /* Check logout argument */
    if (usr.logout) 
    {
        slog(0, SLOG_LIVE, "Logging out");
        msl_logout();
    }

    /* Login for agent mode */
    if (usr.agent)
    {
        /* User input info */
        msl_init(&msl);

        if (!msl.logged) 
        {
            user_init_info(&msl);

            /* Do login */
            if(!msl_login(&msl)) 
            {
                slog(0, SLOG_ERROR, "Can not login as agent");
                usr.agent = 0;
            }

        }

        slog(0, SLOG_INFO, "SMS Agent Version: %s", msl_get_version(1));
        slog(0, SLOG_INFO, "SMS Agent logged in as: %s", msl.usr);
    }

    /* Print irc info */
    print_irc_info(&usr, &inf);

    /* Authorize user */
    sock = authorize_user(&usr, &inf);
    if (sock < 0) 
    {
        slog(0, SLOG_ERROR, "Disconnected from server: %s", inf.server);
        exit (-1);
    }

    /* Some debug line */
    slog(0, SLOG_DEBUG, "Here we go!");

    /* Main loop */
    while (1) 
    {
        /* Recieve data from socket */
        bytes = recv(sock, buf, MAXMSG-1, 0);
        buf[bytes] = '\0';

        /* Log recieved data */
        if (strlen(buf) > 0) 
        {
            sscanf(buf, "%512[^\n]\n", buf);
            slog(0, SLOG_LIVE, "%s", buf);
        }
 
        /* Handle messages */
        handle_msg(sock, buf, usr.nick, inf.channel, usr.agent);
    }

    return 0;
}