Ignore:
Timestamp:
Apr 27, 2014, 10:01:51 PM (8 years ago)
Author:
jordan
Message:

(trunk, daemon) #5660 'Daemon should use event loop' -- fixed with patch by missionsix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r14241 r14265  
    1919#include <unistd.h> /* daemon */
    2020
     21#include <event.h>
    2122#include <event2/buffer.h>
     23#include <event2/event.h>
    2224
    2325#include <libtransmission/transmission.h>
     
    6264
    6365static bool paused = false;
    64 static bool closing = false;
    6566static bool seenHUP = false;
    6667static const char *logfileName = NULL;
     
    6869static tr_session * mySession = NULL;
    6970static tr_quark key_pidfile = 0;
     71static struct event_base *ev_base = NULL;
    7072
    7173/***
     
    184186        case SIGINT:
    185187        case SIGTERM:
    186             closing = true;
     188            event_base_loopexit(ev_base, NULL);
    187189            break;
    188190    }
     
    345347}
    346348
     349static void
     350reportStatus (void)
     351{
     352    const double up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP);
     353    const double dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN);
     354
     355    if (up>0 || dn>0)
     356        sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn);
     357    else
     358        sd_notify (0, "STATUS=Idle.\n");
     359}
     360
     361static void
     362periodicUpdate (evutil_socket_t fd UNUSED, short what UNUSED, void *watchdir)
     363{
     364    dtr_watchdir_update (watchdir);
     365
     366    pumpLogMessages (logfile);
     367
     368    reportStatus ();
     369}
     370
    347371static tr_rpc_callback_status
    348372on_rpc_callback (tr_session            * session UNUSED,
     
    352376{
    353377    if (type == TR_RPC_SESSION_CLOSE)
    354         closing = true;
     378        event_base_loopexit(ev_base, NULL);
    355379    return TR_RPC_OK;
    356380}
     
    371395    bool pidfile_created = false;
    372396    tr_session * session = NULL;
     397    struct event *status_ev;
    373398
    374399    key_pidfile = tr_quark_new ("pidfile",  7);
     
    514539
    515540    sd_notifyf (0, "MAINPID=%d\n", (int)getpid());
     541
     542    /* setup event state */
     543    ev_base = event_base_new();
     544    if (ev_base == NULL)
     545    {
     546        char buf[256];
     547        tr_snprintf(buf, sizeof(buf), "Failed to init daemon event state: %s", tr_strerror(errno));
     548        printMessage (logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__);
     549        exit (1);
     550    }
    516551
    517552    /* start the session */
     
    580615#endif
    581616
    582   sd_notify( 0, "READY=1\n" );
    583 
    584   while (!closing)
    585     {
    586       double up;
    587       double dn;
    588 
    589       tr_wait_msec (1000); /* sleep one second */
    590 
    591       dtr_watchdir_update (watchdir);
    592       pumpLogMessages (logfile);
    593 
    594       up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP);
    595       dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN);
    596       if (up>0 || dn>0)
    597         sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn);
    598       else
    599         sd_notify (0, "STATUS=Idle.\n");
    600     }
    601 
     617    /* Create new timer event to report daemon status */
     618    {
     619        struct timeval one_sec = { 1, 0 };
     620        status_ev = event_new(ev_base, -1, EV_PERSIST, &periodicUpdate, watchdir);
     621        if (status_ev == NULL)
     622        {
     623            tr_logAddError("Failed to create status event %s", tr_strerror(errno));
     624            goto cleanup;
     625        }
     626        if (event_add(status_ev, &one_sec) == -1)
     627        {
     628            tr_logAddError("Failed to add status event %s", tr_strerror(errno));
     629            goto cleanup;
     630        }
     631    }
     632
     633    sd_notify( 0, "READY=1\n" );
     634
     635    /* Run daemon event loop */
     636    if (event_base_dispatch(ev_base) == -1)
     637    {
     638        tr_logAddError("Failed to launch daemon event loop: %s", tr_strerror(errno));
     639        goto cleanup;
     640    }
     641
     642cleanup:
    602643    sd_notify( 0, "STATUS=Closing transmission session...\n" );
    603644    printf ("Closing transmission session...");
     645
     646    if (status_ev)
     647    {
     648        event_del(status_ev);
     649        event_free(status_ev);
     650    }
     651    event_base_free(ev_base);
     652
    604653    tr_sessionSaveSettings (mySession, configDir, &settings);
    605654    dtr_watchdir_free (watchdir);
Note: See TracChangeset for help on using the changeset viewer.