Changeset 2388


Ignore:
Timestamp:
Jul 18, 2007, 5:27:45 AM (15 years ago)
Author:
charles
Message:

fold per-torrent ul/dl speed cap settings into libtransmission. synchronize gtk+ client with this change. breaks os x client.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr_torrent.c

    r2369 r2388  
    156156      g_assert(NULL == self->handle);
    157157      self->handle = g_value_get_pointer(value);
     158g_message ("setting handle...");
     159      if( self->handle ) {
     160          self->ul_cap         = tr_torrentGetMaxSpeedUL       ( self->handle );
     161          self->ul_cap_enabled = tr_torrentIsMaxSpeedEnabledUL ( self->handle );
     162          self->dl_cap         = tr_torrentGetMaxSpeedDL       ( self->handle );
     163          self->dl_cap_enabled = tr_torrentIsMaxSpeedEnabledDL ( self->handle );
     164g_message ("ul cap [%d] dl_cap [%d]", self->ul_cap, self->dl_cap );
     165      }
    158166      if(NULL != self->handle && NULL != self->dir)
    159167        tr_torrent_set_folder(self);
     
    389397  char *torrent, *hash, *dir;
    390398  gboolean paused = FALSE;
    391   gboolean ul_cap_enabled = FALSE;
    392   gboolean dl_cap_enabled = FALSE;
    393399  gboolean seeding_cap_enabled = FALSE;
    394   gint ul_cap = 0;
    395   gint dl_cap = 0;
    396400  gdouble seeding_cap = 0.0;
    397401
     
    414418      else if (!strcmp (key, "dir")) dir = val;
    415419      else if (!strcmp (key, "paused")) paused = !!data->val.i;
    416       else if (!strcmp (key, "ul-cap-speed")) ul_cap = data->val.i;
    417       else if (!strcmp (key, "ul-cap-enabled")) ul_cap_enabled = !!data->val.i;
    418       else if (!strcmp (key, "dl-cap-speed")) dl_cap = data->val.i;
    419       else if (!strcmp (key, "dl-cap-enabled")) dl_cap_enabled = !!data->val.i;
    420420      else if (!strcmp (key, "seeding-cap-ratio")) seeding_cap = (data->val.i / 100.0);
    421421      else if (!strcmp (key, "seeding-cap-enabled")) seeding_cap_enabled = !!data->val.i;
     
    453453
    454454  ret = maketorrent( handle );
    455   ret->ul_cap = ul_cap;
    456   ret->ul_cap_enabled = ul_cap_enabled;
    457   ret->dl_cap = dl_cap;
    458   ret->dl_cap_enabled = dl_cap_enabled;
    459455  ret->seeding_cap = seeding_cap;
    460456  ret->seeding_cap_enabled = seeding_cap_enabled;
     
    494490    tr_bencInitStr( tr_bencDictAdd( state, "dir" ),
    495491                    tr_torrentGetFolder( tor->handle ), -1, 1 );
    496 
    497492    tr_bencInitInt( tr_bencDictAdd( state, "paused" ),
    498493                    (refreshStat(tor)->status & TR_STATUS_INACTIVE) ? 1 : 0);
    499     tr_bencInitInt( tr_bencDictAdd( state, "ul-cap-speed" ),
    500                     tor->ul_cap );
    501     tr_bencInitInt( tr_bencDictAdd( state, "ul-cap-enabled" ),
    502                     tor->ul_cap_enabled ? 1 : 0 );
    503 
    504     tr_bencInitInt( tr_bencDictAdd( state, "dl-cap-speed" ),
    505                     tor->dl_cap );
    506     tr_bencInitInt( tr_bencDictAdd( state, "dl-cap-enabled" ),
    507                     tor->dl_cap_enabled ? 1 : 0);
    508 
    509494    tr_bencInitInt( tr_bencDictAdd( state, "seeding-cap-ratio" ),
    510495                    (int)(tor->dl_cap * 100.0)); /* two decimal places */
     
    544529}
    545530
    546 extern void tr_setUseCustomUpload( tr_torrent_t * tor, int limit );
    547 extern void tr_setUseCustomDownload( tr_torrent_t * tor, int limit );
    548 
    549 
    550 static void refresh_upload_cap ( TrTorrent *gtor ) {
    551   const int cap = gtor->ul_cap_enabled ? gtor->ul_cap : -1;
    552   g_message ("setting upload cap to %d...", cap);
    553   tr_setUseCustomUpload( gtor->handle, gtor->ul_cap_enabled );
    554   tr_setUploadLimit( gtor->handle, cap );
    555 }
    556 void
    557 tr_torrent_set_upload_cap_speed ( TrTorrent *gtor, int KiB_sec ) {
     531static void
     532refresh_upload_cap ( TrTorrent *gtor )
     533{
     534  tr_torrentEnableMaxSpeedUL( gtor->handle, gtor->ul_cap_enabled );
     535  tr_torrentSetMaxSpeedUL( gtor->handle, gtor->ul_cap );
     536}
     537void
     538tr_torrent_set_upload_cap_speed ( TrTorrent *gtor, int KiB_sec )
     539{
    558540  gtor->ul_cap = KiB_sec;
    559541  refresh_upload_cap ( gtor );
    560542}
    561543void
    562 tr_torrent_set_upload_cap_enabled ( TrTorrent *gtor, gboolean b ) {
     544tr_torrent_set_upload_cap_enabled ( TrTorrent *gtor, gboolean b )
     545{
    563546  gtor->ul_cap_enabled = b;
    564547  refresh_upload_cap ( gtor );
    565548}
    566549
    567 static void refresh_download_cap ( TrTorrent *gtor ) {
    568   const int cap = gtor->dl_cap_enabled ? gtor->dl_cap : -1;
    569   tr_setUseCustomDownload( gtor->handle, gtor->dl_cap_enabled );
    570   tr_setDownloadLimit( gtor->handle, cap );
    571 }
    572 void
    573 tr_torrent_set_download_cap_speed ( TrTorrent *gtor, int KiB_sec ) {
     550static void
     551refresh_download_cap ( TrTorrent *gtor )
     552{
     553  tr_torrentEnableMaxSpeedDL( gtor->handle, gtor->dl_cap_enabled );
     554  tr_torrentSetMaxSpeedDL( gtor->handle, gtor->dl_cap );
     555}
     556void
     557tr_torrent_set_download_cap_speed ( TrTorrent *gtor, int KiB_sec )
     558{
    574559  gtor->dl_cap = KiB_sec;
    575560  refresh_download_cap( gtor );
    576561}
    577562void
    578 tr_torrent_set_download_cap_enabled ( TrTorrent *gtor, gboolean b ) {
     563tr_torrent_set_download_cap_enabled ( TrTorrent *gtor, gboolean b )
     564{
    579565  gtor->dl_cap_enabled = b;
    580566  refresh_download_cap( gtor );
     
    582568
    583569void
    584 tr_torrent_check_seeding_cap ( TrTorrent *gtor) {
     570tr_torrent_check_seeding_cap ( TrTorrent *gtor)
     571{
    585572  const tr_stat_t * st = tr_torrent_stat( gtor );
    586573  if ((gtor->seeding_cap_enabled) && (st->ratio >= gtor->seeding_cap))
     
    588575}
    589576void
    590 tr_torrent_set_seeding_cap_ratio ( TrTorrent *gtor, gdouble ratio ) {
     577tr_torrent_set_seeding_cap_ratio ( TrTorrent *gtor, gdouble ratio )
     578{
    591579  gtor->seeding_cap = ratio;
    592580  tr_torrent_check_seeding_cap (gtor);
    593581}
    594582void
    595 tr_torrent_set_seeding_cap_enabled ( TrTorrent *gtor, gboolean b ) {
     583tr_torrent_set_seeding_cap_enabled ( TrTorrent *gtor, gboolean b )
     584{
    596585  if ((gtor->seeding_cap_enabled = b))
    597586    tr_torrent_check_seeding_cap (gtor);
  • trunk/libtransmission/fastresume.c

    r2387 r2388  
    5151typedef uint64_t tr_time_t;
    5252
    53 /* deprecated */
    54 #define FR_ID_PROGRESS_SLOTS    0x01
    55 /* number of bytes downloaded */
    56 #define FR_ID_DOWNLOADED        0x02
    57 /* number of bytes uploaded */
    58 #define FR_ID_UPLOADED          0x03
    59 /* IPs and ports of connectable peers */
    60 #define FR_ID_PEERS             0x04
    61 /* progress data:
    62  *  - 4 bytes * number of files: mtimes of files
    63  *  - 1 bit * number of blocks: whether we have the block or not
    64  */
    65 #define FR_ID_PROGRESS          0x05
    66 /* dnd and priority
    67  * char * number of files: l,n,h for low, normal, high priority
    68  * char * number of files: t,f for DND flags
    69  */
    70 #define FR_ID_PRIORITY          0x06
     53enum
     54{
     55    /* deprecated */
     56    FR_ID_PROGRESS_SLOTS = 1,
     57
     58    /* number of bytes downloaded */
     59    FR_ID_DOWNLOADED = 2,
     60
     61    /* number of bytes uploaded */
     62    FR_ID_UPLOADED = 3,
     63
     64    /* IPs and ports of connectable peers */
     65    FR_ID_PEERS = 4,
     66
     67    /* progress data:
     68     *  - 4 bytes * number of files: mtimes of files
     69     *  - 1 bit * number of blocks: whether we have the block or not */
     70    FR_ID_PROGRESS = 5,
     71
     72    /* dnd and priority
     73     * char * number of files: l,n,h for low, normal, high priority
     74     * char * number of files: t,f for DND flags */
     75    FR_ID_PRIORITY = 6,
     76
     77    /* transfer speeds
     78     * uint32_t: the dl speed rate to use when the flag is true
     79     * char: t,f for whether or not dl speed is capped
     80     * uint32_t: the ul speed rate to use when the flag is true
     81     * char: t,f for whether or not ul speed is capped
     82     */
     83    FR_ID_SPEED = 7
     84};
     85
    7186
    7287/* macros for the length of various pieces of the progress data */
     
    210225
    211226
     227    /* Write the torrent ul/dl speed caps */
     228    if( TRUE )
     229    {
     230        const int len = ( sizeof(uint32_t) + sizeof(char) ) * 2;
     231        char * buf = tr_new0( char, len );
     232        char * walk = buf;
     233        char enabled;
     234        uint32_t i;
     235
     236        i = (uint32_t) tr_torrentGetMaxSpeedDL( tor );
     237        memcpy( walk, &i, 4 ); walk += 4;
     238        enabled = tr_torrentIsMaxSpeedEnabledDL( tor ) ? 't' : 'f';
     239        *walk++ = enabled;
     240
     241        i = (uint32_t) tr_torrentGetMaxSpeedUL( tor );
     242        memcpy( walk, &i, 4 ); walk += 4;
     243        enabled = tr_torrentIsMaxSpeedEnabledUL( tor ) ? 't' : 'f';
     244        *walk++ = enabled;
     245
     246        assert( walk - buf == len );
     247        fastResumeWriteData( FR_ID_SPEED, buf, 1, walk-buf, file );
     248    }
     249
     250
    212251    /* Write download and upload totals */
    213252    total = tor->downloadedCur + tor->downloadedPrev;
     
    232271    tr_dbg( "Resume file '%s' written", path );
    233272}
     273
     274static int
     275loadSpeeds( tr_torrent_t * tor, FILE * file )
     276{
     277    const size_t len = 2 * (sizeof(uint32_t) + sizeof(char));
     278    char * buf = tr_new0( char, len );
     279    char * walk = buf;
     280    uint32_t rate;
     281    char enabled;
     282
     283    if( len != fread( buf, 1, len, file ) ) {
     284        tr_inf( "Couldn't read from resume file" );
     285        free( buf );
     286        return TR_ERROR_IO_OTHER;
     287    }
     288
     289    memcpy( &rate, walk, 4 ); walk += 4;
     290    memcpy( &enabled, walk, 1 ); walk += 1;
     291    tr_torrentSetMaxSpeedDL( tor, rate );
     292    tr_torrentEnableMaxSpeedDL( tor, enabled=='t' );
     293
     294    memcpy( &rate, walk, 4 ); walk += 4;
     295    memcpy( &enabled, walk, 1 ); walk += 1;
     296    tr_torrentSetMaxSpeedUL( tor, rate );
     297    tr_torrentEnableMaxSpeedUL( tor, enabled=='t' );
     298
     299    tr_free( buf );
     300    return TR_OK;
     301}
     302
    234303
    235304static int
     
    436505                {
    437506                    ret = loadPriorities( tor, file );
     507
     508                    if( ret && ( feof(file) || ferror(file) ) )
     509                    {
     510                        fclose( file );
     511                        return 1;
     512                    }
     513
     514                    continue;
     515                }
     516                break;
     517
     518            case FR_ID_SPEED:
     519                /*  read speed data */
     520                if( len == (uint32_t)(2*sizeof(uint32_t)+2) )
     521                {
     522                    ret = loadSpeeds( tor, file );
    438523
    439524                    if( ret && ( feof(file) || ferror(file) ) )
  • trunk/libtransmission/ratecontrol.c

    r2324 r2388  
    163163    tr_rwWriterUnlock( &r->lock );
    164164}
     165
     166int
     167tr_rcGetLimit( const tr_ratecontrol_t * r )
     168{
     169    return r->limit;
     170}
  • trunk/libtransmission/ratecontrol.h

    r2328 r2388  
    3030tr_ratecontrol_t * tr_rcInit( void );
    3131void               tr_rcSetLimit( tr_ratecontrol_t *, int );
     32int                tr_rcGetLimit( const tr_ratecontrol_t * );
    3233int                tr_rcCanTransfer( const tr_ratecontrol_t * );
    3334void               tr_rcTransferred( tr_ratecontrol_t *, int );
  • trunk/libtransmission/torrent.c

    r2381 r2388  
    6868
    6969void
    70 tr_setUseCustomUpload( tr_torrent_t * tor, int limit )
    71 {
    72     tr_torrentWriterLock( tor );
    73     tor->customUploadLimit = limit;
    74     tr_torrentWriterUnlock( tor );
    75 }
    76 
    77 void
    78 tr_setUseCustomDownload( tr_torrent_t * tor, int limit )
    79 {
    80     tr_torrentWriterLock( tor );
    81     tor->customDownloadLimit = limit;
    82     tr_torrentWriterUnlock( tor );
    83 }
    84 
    85 void
    86 tr_setUploadLimit( tr_torrent_t * tor, int limit )
    87 {
    88     tr_torrentWriterLock( tor );
    89     tr_rcSetLimit( tor->upload, limit );
    90     tr_torrentWriterUnlock( tor );
    91 }
    92 
    93 void
    94 tr_setDownloadLimit( tr_torrent_t * tor, int limit )
    95 {
    96     tr_torrentWriterLock( tor );
    97     tr_rcSetLimit( tor->download, limit );
    98     tr_torrentWriterUnlock( tor );
     70tr_torrentEnableMaxSpeedUL( tr_torrent_t * tor, char doThrottle )
     71{
     72    tor->customUploadLimit = doThrottle;
     73}
     74void
     75tr_torrentEnableMaxSpeedDL( tr_torrent_t * tor, char doThrottle )
     76{
     77    tor->customDownloadLimit = doThrottle;
     78}
     79void
     80tr_torrentSetMaxSpeedUL( tr_torrent_t * tor, int KiB_sec )
     81{
     82    tr_rcSetLimit( tor->upload, KiB_sec );
     83}
     84void
     85tr_torrentSetMaxSpeedDL( tr_torrent_t * tor, int KiB_sec )
     86{
     87    tr_rcSetLimit( tor->download, KiB_sec );
     88}
     89int
     90tr_torrentIsMaxSpeedEnabledUL( const tr_torrent_t * tor )
     91{
     92    return tor->customUploadLimit;
     93}
     94int
     95tr_torrentIsMaxSpeedEnabledDL( const tr_torrent_t * tor )
     96{
     97    return tor->customDownloadLimit;
     98}
     99int
     100tr_torrentGetMaxSpeedUL( const tr_torrent_t * tor )
     101{
     102    return tr_rcGetLimit( tor->upload );
     103}
     104int
     105tr_torrentGetMaxSpeedDL( const tr_torrent_t * tor )
     106{
     107    return tr_rcGetLimit( tor->download );
    99108}
    100109
  • trunk/libtransmission/transmission.h

    r2369 r2388  
    197197void tr_torrentIterate( tr_handle_t *, tr_callback_t, void * );
    198198
    199 void tr_setUseCustomLimit( tr_torrent_t * tor, int limit );
    200 void tr_setUploadLimit( tr_torrent_t * tor, int limit );
    201 void tr_setDownloadLimit( tr_torrent_t * tor, int limit );
     199/**
     200*** Speed Throttle
     201**/
     202
     203void tr_torrentEnableMaxSpeedUL ( tr_torrent_t * , char doThrottle );
     204void tr_torrentEnableMaxSpeedDL ( tr_torrent_t * , char doThrottle );
     205void tr_torrentSetMaxSpeedUL    ( tr_torrent_t * , int KiB_sec );
     206void tr_torrentSetMaxSpeedDL    ( tr_torrent_t * , int KiB_sec );
     207
     208int tr_torrentIsMaxSpeedEnabledUL ( const tr_torrent_t * );
     209int tr_torrentIsMaxSpeedEnabledDL ( const tr_torrent_t * );
     210int tr_torrentGetMaxSpeedUL       ( const tr_torrent_t * );
     211int tr_torrentGetMaxSpeedDL       ( const tr_torrent_t * );
    202212
    203213/***********************************************************************
Note: See TracChangeset for help on using the changeset viewer.