Changeset 1708


Ignore:
Timestamp:
Apr 12, 2007, 2:41:19 PM (16 years ago)
Author:
joshe
Message:

Minor cleanup of client code.
Display ratio in transmission-remote -l output.

Location:
branches/daemon/daemon
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/daemon/daemon/client.c

    r1696 r1708  
    7676{
    7777    int64_t            tag;
    78     cl_listfunc        listcb;
    7978    cl_infofunc        infocb;
    80     cl_hashfunc        hashcb;
    8179    cl_statfunc        statcb;
    8280    RB_ENTRY( resp )   links;
     
    546544
    547545int
    548 client_list( cl_listfunc func )
     546client_list( cl_infofunc func )
    549547{
    550548    struct req  * req;
     
    557555    }
    558556
    559     resp->listcb = func;
     557    resp->infocb = func;
    560558    req->types   = IPC_INF_NAME | IPC_INF_HASH;
    561559
     
    582580
    583581int
    584 client_hashids( cl_hashfunc func )
     582client_hashids( cl_infofunc func )
    585583{
    586584    struct req  * req;
     
    593591    }
    594592
    595     resp->hashcb = func;
     593    resp->infocb = func;
    596594    req->types   = IPC_INF_HASH;
    597595
     
    613611    resp->statcb = func;
    614612    req->types   = IPC_ST_STATE | IPC_ST_ETA | IPC_ST_COMPLETED |
    615         IPC_ST_DOWNSPEED | IPC_ST_UPSPEED | IPC_ST_ERROR | IPC_ST_ERRMSG;
     613        IPC_ST_DOWNSPEED | IPC_ST_UPSPEED | IPC_ST_DOWNTOTAL | IPC_ST_UPTOTAL |
     614        IPC_ST_ERROR | IPC_ST_ERRMSG;
    616615
    617616    return 0;
     
    869868         void * arg UNUSED )
    870869{
    871     benc_val_t  * dict;
    872     int           ii;
    873     int64_t       id, size;
    874     char        * name, * hash;
    875     struct resp * resp, key;
     870    benc_val_t   * dict;
     871    int            ii;
     872    struct cl_info inf;
     873    int64_t        id;
     874    struct resp  * resp, key;
    876875
    877876    assert( IPC_MSG_INFO == msgid );
    878877
    879     if( TYPE_LIST != list->type )
     878    if( TYPE_LIST != list->type || NULL == resp->infocb )
    880879    {
    881880        return;
     
    899898        }
    900899
    901         id   = getinfoint( msgid, dict, IPC_INF_ID,   -1   );
    902         name = getinfostr( msgid, dict, IPC_INF_NAME, NULL );
    903         hash = getinfostr( msgid, dict, IPC_INF_HASH, NULL );
    904         size = getinfoint( msgid, dict, IPC_INF_SIZE, -1   );
     900        id       = getinfoint( msgid, dict, IPC_INF_ID,   -1   );
     901        inf.name = getinfostr( msgid, dict, IPC_INF_NAME, NULL );
     902        inf.hash = getinfostr( msgid, dict, IPC_INF_HASH, NULL );
     903        inf.size = getinfoint( msgid, dict, IPC_INF_SIZE, -1   );
    905904
    906905        if( !TORRENT_ID_VALID( id ) )
     
    909908        }
    910909
    911         if( NULL != resp->infocb )
    912         {
    913             resp->infocb( id, name, size );
    914         }
    915         else if( NULL != resp->listcb )
    916         {
    917             resp->listcb( id, name, hash );
    918         }
    919         else if( NULL != resp->hashcb )
    920         {
    921             resp->hashcb( id, hash );
    922         }
     910        inf.id = id;
     911        resp->infocb( &inf );
    923912    }
    924913
     
    931920         void * arg UNUSED )
    932921{
    933     benc_val_t  * dict;
    934     int           ii;
    935     int64_t       id, eta, done, down, up;
    936     char        * state, * error, * errmsg;
    937     struct resp * resp, key;
     922    benc_val_t   * dict;
     923    int            ii;
     924    int64_t        id;
     925    struct cl_stat st;
     926    struct resp  * resp, key;
    938927
    939928    assert( IPC_MSG_STAT == msgid );
    940929
    941     if( TYPE_LIST != list->type )
     930    if( TYPE_LIST != list->type || NULL == resp->statcb )
    942931    {
    943932        return;
     
    961950        }
    962951
    963         id     = getinfoint( msgid, dict, IPC_ST_ID,        -1   );
    964         state  = getinfostr( msgid, dict, IPC_ST_STATE,     NULL );
    965         eta    = getinfoint( msgid, dict, IPC_ST_ETA,       -1   );
    966         done   = getinfoint( msgid, dict, IPC_ST_COMPLETED, -1   );
    967         down   = getinfoint( msgid, dict, IPC_ST_DOWNSPEED, -1   );
    968         up     = getinfoint( msgid, dict, IPC_ST_UPSPEED,   -1   );
    969         error  = getinfostr( msgid, dict, IPC_ST_ERROR,     NULL );
    970         errmsg = getinfostr( msgid, dict, IPC_ST_ERRMSG,    NULL );
     952        id           = getinfoint( msgid, dict, IPC_ST_ID,        -1   );
     953        st.state     = getinfostr( msgid, dict, IPC_ST_STATE,     NULL );
     954        st.eta       = getinfoint( msgid, dict, IPC_ST_ETA,       -1   );
     955        st.done      = getinfoint( msgid, dict, IPC_ST_COMPLETED, -1   );
     956        st.ratedown  = getinfoint( msgid, dict, IPC_ST_DOWNSPEED, -1   );
     957        st.rateup    = getinfoint( msgid, dict, IPC_ST_UPSPEED,   -1   );
     958        st.totaldown = getinfoint( msgid, dict, IPC_ST_DOWNTOTAL, -1   );
     959        st.totalup   = getinfoint( msgid, dict, IPC_ST_UPTOTAL,   -1   );
     960        st.error     = getinfostr( msgid, dict, IPC_ST_ERROR,     NULL );
     961        st.errmsg    = getinfostr( msgid, dict, IPC_ST_ERRMSG,    NULL );
    971962
    972963        if( !TORRENT_ID_VALID( id ) )
     
    975966        }
    976967
    977         if( NULL != resp->statcb )
    978         {
    979             resp->statcb( id, state, eta, done, down, up, error, errmsg );
    980         }
     968        st.id = id;
     969        resp->statcb( &st );
    981970    }
    982971
     
    10271016    if( NULL != resp->infocb )
    10281017    {
    1029         resp->infocb( -1, NULL, -1 );
    1030     }
    1031     else if( NULL != resp->listcb )
    1032     {
    1033         resp->listcb( -1, NULL, NULL );
    1034     }
    1035     else if( NULL != resp->hashcb )
    1036     {
    1037         resp->hashcb( -1, NULL );
     1018        resp->infocb( NULL );
    10381019    }
    10391020    else if( NULL != resp->statcb )
    10401021    {
    1041         resp->statcb( -1, NULL, -1, -1, -1, -1, NULL, NULL );
     1022        resp->statcb( NULL );
    10421023    }
    10431024}
  • branches/daemon/daemon/client.h

    r1635 r1708  
    2929struct strlist;
    3030
    31 typedef void ( * cl_listfunc )( int, const char *, const char * );
    32 typedef void ( * cl_infofunc )( int, const char *, int64_t );
    33 typedef void ( * cl_statfunc )( int, const char *, int64_t, int64_t, int64_t,
    34                                 int64_t, const char *, const char * );
    35 typedef void ( * cl_hashfunc )( int, const char * );
     31struct cl_info
     32{
     33    int          id;
     34    const char * name;
     35    const char * hash;
     36    int64_t      size;
     37};
     38
     39struct cl_stat
     40{
     41    int          id;
     42    const char * state;
     43    int64_t      eta;
     44    int64_t      done;
     45    int64_t      ratedown;
     46    int64_t      rateup;
     47    int64_t      totaldown;
     48    int64_t      totalup;
     49    const char * error;
     50    const char * errmsg;
     51};
     52
     53typedef void ( * cl_infofunc )( const struct cl_info * );
     54typedef void ( * cl_statfunc )( const struct cl_stat * );
    3655
    3756int  client_init     ( struct event_base * );
     
    4968int  client_stop     ( size_t, const int * );
    5069int  client_remove   ( size_t, const int * );
    51 int  client_list     ( cl_listfunc );
     70int  client_list     ( cl_infofunc );
    5271int  client_info     ( cl_infofunc );
    53 int  client_hashids  ( cl_hashfunc );
     72int  client_hashids  ( cl_infofunc );
    5473int  client_status   ( cl_statfunc );
    5574
  • branches/daemon/daemon/remote.c

    r1696 r1708  
    7878    int64_t eta;
    7979    int64_t done;
    80     int64_t down;
    81     int64_t up;
     80    int64_t ratedown;
     81    int64_t rateup;
     82    int64_t totaldown;
     83    int64_t totalup;
    8284    char  * errorcode;
    8385    char  * errormsg;
     
    103105static int    hasharg      ( const char *, struct strlist *, int * );
    104106static int    fileargs     ( struct strlist *, int, char * const * );
    105 static void   listmsg      ( int, const char *, const char * );
    106 static void   infomsg      ( int, const char *, int64_t );
    107 static void   statmsg      ( int, const char *, int64_t, int64_t, int64_t,
    108                              int64_t, const char *, const char * );
    109 static void   hashmsg      ( int, const char * );
     107static void   listmsg      ( const struct cl_info * );
     108static void   infomsg      ( const struct cl_info * );
     109static void   statmsg      ( const struct cl_stat * );
     110static void   hashmsg      ( const struct cl_info * );
    110111static float  fmtsize      ( int64_t, const char ** );
    111112static char * strdup_noctrl( const char * );
     
    533534
    534535void
    535 listmsg( int id, const char * name, const char * hash )
     536listmsg( const struct cl_info * inf )
    536537{
    537538    char * newname;
    538539    size_t ii;
    539540
    540     if( !TORRENT_ID_VALID( id ) )
     541    if( NULL == inf )
    541542    {
    542543        return;
    543544    }
    544545
    545     if( NULL == name )
     546    if( NULL == inf->name )
    546547    {
    547548        errmsg( "missing torrent name from server" );
    548549        return;
    549550    }
    550     else if( NULL == hash )
     551    else if( NULL == inf->hash )
    551552    {
    552553        errmsg( "missing torrent hash from server" );
     
    554555    }
    555556
    556     newname = strdup_noctrl( name );
     557    newname = strdup_noctrl( inf->name );
    557558    if( NULL == newname )
    558559    {
     
    560561    }
    561562
    562     for( ii = 0; '\0' != hash[ii]; ii++ )
    563     {
    564         if( isxdigit( hash[ii] ) )
    565         {
    566             putchar( tolower( hash[ii] ) );
     563    for( ii = 0; '\0' != inf->hash[ii]; ii++ )
     564    {
     565        if( isxdigit( inf->hash[ii] ) )
     566        {
     567            putchar( tolower( inf->hash[ii] ) );
    567568        }
    568569    }
     
    573574
    574575void
    575 infomsg( int id, const char * name, int64_t size )
    576 {
    577     struct torinfo * info, key;
     576infomsg( const struct cl_info * cinfo )
     577{
     578    struct torinfo * tinfo, key;
    578579
    579580    gl_gotlistinfo = 1;
    580581
    581     if( !TORRENT_ID_VALID( id ) )
     582    if( NULL == cinfo )
    582583    {
    583584        if( gl_gotliststat )
     
    588589    }
    589590
    590     if( NULL == name || 0 >= size )
     591    if( NULL == cinfo->name || 0 >= cinfo->size )
    591592    {
    592593        errmsg( "missing torrent info from server" );
     
    595596
    596597    bzero( &key, sizeof key );
    597     key.id = id;
     598    key.id = cinfo->id;
     599    tinfo = RB_FIND( torlist, &gl_torinfo, &key );
     600    if( NULL == tinfo )
     601    {
     602        tinfo = calloc( 1, sizeof *tinfo );
     603        if( NULL == tinfo )
     604        {
     605            mallocmsg( sizeof *tinfo );
     606            return;
     607        }
     608        tinfo->id = cinfo->id;
     609        RB_INSERT( torlist, &gl_torinfo, tinfo );
     610    }
     611
     612    free( tinfo->name );
     613    tinfo->name = strdup_noctrl( cinfo->name );
     614    tinfo->size = cinfo->size;
     615}
     616
     617void
     618statmsg( const struct cl_stat * st )
     619{
     620    struct torinfo * info, key;
     621
     622    gl_gotliststat = 1;
     623
     624    if( NULL == st )
     625    {
     626        if( gl_gotlistinfo )
     627        {
     628            printlisting();
     629        }
     630        return;
     631    }
     632
     633    if( NULL == st->state || 0 > st->done ||
     634        0 > st->ratedown  || 0 > st->rateup ||
     635        0 > st->totaldown || 0 > st->totalup )
     636    {
     637        errmsg( "missing torrent status from server" );
     638        return;
     639    }
     640
     641    bzero( &key, sizeof key );
     642    key.id = st->id;
    598643    info = RB_FIND( torlist, &gl_torinfo, &key );
    599644    if( NULL == info )
     
    605650            return;
    606651        }
    607         info->id = id;
     652        info->id = st->id;
    608653        RB_INSERT( torlist, &gl_torinfo, info );
    609654    }
    610655
    611     free( info->name );
    612     info->name = strdup_noctrl( name );
    613     info->size = size;
     656    free( info->state );
     657    info->state     = strdup_noctrl( st->state );
     658    info->eta       = st->eta;
     659    info->done      = st->done;
     660    info->ratedown  = st->ratedown;
     661    info->rateup    = st->rateup;
     662    info->totaldown = st->totaldown;
     663    info->totalup   = st->totalup;
     664    if( NULL != st->error && '\0' != st->error[0] )
     665    {
     666        info->errorcode = strdup_noctrl( st->error );
     667    }
     668    if( NULL != st->errmsg && '\0' != st->errmsg[0] )
     669    {
     670        info->errormsg = strdup_noctrl( st->errmsg );
     671    }
    614672}
    615673
    616674void
    617 statmsg( int id, const char * state, int64_t eta, int64_t done,
    618          int64_t down, int64_t up, const char * terr, const char * terrmsg )
    619 {
    620     struct torinfo * info, key;
    621 
    622     gl_gotliststat = 1;
    623 
    624     if( !TORRENT_ID_VALID( id ) )
    625     {
    626         if( gl_gotlistinfo )
    627         {
    628             printlisting();
    629         }
    630         return;
    631     }
    632 
    633     if( NULL == state || 0 > done || 0 > down || 0 > up )
    634     {
    635         errmsg( "missing torrent status from server" );
    636         return;
    637     }
    638 
    639     bzero( &key, sizeof key );
    640     key.id = id;
    641     info = RB_FIND( torlist, &gl_torinfo, &key );
    642     if( NULL == info )
    643     {
    644         info = calloc( 1, sizeof *info );
    645         if( NULL == info )
    646         {
    647             mallocmsg( sizeof *info );
    648             return;
    649         }
    650         info->id = id;
    651         RB_INSERT( torlist, &gl_torinfo, info );
    652     }
    653 
    654     free( info->state );
    655     info->state = strdup_noctrl( state );
    656     info->eta   = eta;
    657     info->done  = done;
    658     info->down  = down;
    659     info->up    = up;
    660     if( NULL != terr && '\0' != terr[0] )
    661     {
    662         info->errorcode = strdup_noctrl( terr );
    663     }
    664     if( NULL != terrmsg && '\0' != terrmsg[0] )
    665     {
    666         info->errormsg = strdup_noctrl( terrmsg );
    667     }
    668 }
    669 
    670 void
    671 hashmsg( int id, const char * hash )
     675hashmsg( const struct cl_info * inf )
    672676{
    673677    struct torhash key, * found;
    674678
    675     if( !TORRENT_ID_VALID( id ) )
     679    if( NULL == inf )
    676680    {
    677681        sendidreqs();
     
    679683    }
    680684
    681     if( NULL == hash )
     685    if( NULL == inf->hash )
    682686    {
    683687        errmsg( "missing torrent hash from server" );
     
    686690
    687691    bzero( &key, sizeof key );
    688     strlcpy( key.hash, hash, sizeof key.hash );
     692    strlcpy( key.hash, inf->hash, sizeof key.hash );
    689693    found = RB_FIND( torhashes, &gl_hashids, &key );
    690694    if( NULL != found )
    691695    {
    692         found->id = id;
     696        found->id = inf->id;
    693697    }
    694698}
     
    868872    struct torinfo * ii, * next;
    869873    const char     * units, * name, * upunits, * downunits;
    870     float            size, progress, upspeed, downspeed;
     874    float            size, progress, upspeed, downspeed, ratio;
    871875
    872876    /* sort the torrents by name */
     
    886890        /* massage some numbers into a better format for printing */
    887891        size      = fmtsize( ii->size, &units );
    888         upspeed   = fmtsize( ii->up, &upunits );
    889         downspeed = fmtsize( ii->down, &downunits );
     892        upspeed   = fmtsize( ii->rateup, &upunits );
     893        downspeed = fmtsize( ii->ratedown, &downunits );
    890894        name      = ( NULL == ii->name ? "???" : ii->name );
    891895        progress  = ( float )ii->done / ( float )ii->size * 100.0;
     
    915919        else if( 0 == strcasecmp( "seeding", ii->state ) )
    916920        {
    917             printf( "100%% seeding at %.*f %s/s",
    918                     BESTDECIMAL( upspeed ), upspeed, upunits );
     921            if( 0 == ii->totalup && 0 == ii->totaldown )
     922            {
     923                printf( "100%% seeding at %.*f %s/s [N/A]",
     924                        BESTDECIMAL( upspeed ), upspeed, upunits );
     925            }
     926            else if( 0 == ii->totaldown )
     927            {
     928                printf( "100%% seeding at %.*f %s/s [INF]",
     929                        BESTDECIMAL( upspeed ), upspeed, upunits );
     930            }
     931            else
     932            {
     933                ratio = ( float )ii->totalup / ( float )ii->totaldown;
     934                printf( "100%% seeding at %.*f %s/s [%.*f]",
     935                        BESTDECIMAL( upspeed ), upspeed, upunits,
     936                        BESTDECIMAL( ratio ), ratio );
     937            }
    919938        }
    920939        /* print stopping message */
Note: See TracChangeset for help on using the changeset viewer.