Changeset 2984


Ignore:
Timestamp:
Sep 7, 2007, 5:05:56 PM (15 years ago)
Author:
charles
Message:

periodic checkin. nothing to see here.

Location:
branches/encryption/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/peer-mgr-private.h

    r2978 r2984  
    1616#include <inttypes.h> /* uint16_t */
    1717#include <arpa/inet.h> /* struct in_addr */
     18#include "publish.h" /* tr_publisher_tag */
    1819
    1920struct tr_bitfield;
     
    4546
    4647    struct tr_peermsgs * msgs;
     48    tr_publisher_tag msgsTag;
    4749
    4850    struct tr_pex * lastPex;
  • branches/encryption/libtransmission/peer-mgr.c

    r2978 r2984  
    4040#define MAX_CONNECTED_PEERS 80
    4141
     42struct tr_block
     43{
     44    uint32_t block;
     45    uint16_t scarcity;
     46    uint8_t priority;
     47    uint8_t requestCount;
     48};
     49
    4250typedef struct
    4351{
     
    4654    tr_timer_tag choke_tag;
    4755    tr_torrent * tor;
     56
     57    struct tr_block * blocks;
     58    uint32_t blockCount;
    4859}
    4960Torrent;
     
    5566    int connectionCount;
    5667};
     68
     69/**
     70***
     71**/
    5772
    5873/**
     
    180195***
    181196**/
     197
     198static void
     199msgsCallbackFunc( void * source UNUSED, void * vevent, void * vt )
     200{
     201    Torrent * t = (Torrent *) vt;
     202    const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent;
     203
     204    switch( e->eventType )
     205    {
     206        case TR_PEERMSG_GOT_BITFIELD: {
     207            const uint32_t begin = 0;
     208            const uint32_t end = begin + t->blockCount;
     209            uint32_t i;
     210            for( i=begin; i<end; ++i ) {
     211                if( !tr_bitfieldHas( e->bitfield, i ) )
     212                    continue;
     213                assert( t->blocks[i].block == i );
     214                if( t->blocks[i].scarcity < UINT8_MAX )
     215                    t->blocks[i].scarcity++;
     216            }
     217            break;
     218        }
     219
     220        case TR_PEERMSG_GOT_HAVE: {
     221            const uint32_t begin = tr_torPieceFirstBlock( t->tor, e->pieceIndex );
     222            const uint32_t end = begin + (uint32_t)tr_torPieceCountBlocks( t->tor, (int)e->pieceIndex );
     223            uint32_t i;
     224            for( i=begin; i<end; ++i ) {
     225                assert( t->blocks[i].block == i );
     226                if( t->blocks[i].scarcity < UINT8_MAX )
     227                    t->blocks[i].scarcity++;
     228            }
     229            break;
     230        }
     231
     232        case TR_PEERMSG_GOT_PEX:
     233            /* FIXME */
     234            break;
     235
     236        case TR_PEERMSG_GOT_ERROR:
     237            /* FIXME */
     238            break;
     239
     240        case TR_PEERMSG_BLOCKS_RUNNING_LOW:
     241            /* FIXME */
     242            break;
     243
     244        default:
     245            assert(0);
     246    }
     247}
    182248
    183249static void
     
    226292        peer->port = port;
    227293        peer->msgs = tr_peerMsgsNew( t->tor, peer );
     294        peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
    228295        chokePulse( t );
    229296    }
     
    379446{
    380447    Torrent * t;
     448    uint32_t i;
    381449
    382450    assert( tor != NULL );
     
    389457                                chokePulse, t, NULL,
    390458                                RECHOKE_PERIOD_SECONDS );
     459    t->blockCount = tor->blockCount;
     460    t->blocks = tr_new0( struct tr_block, t->blockCount );
     461    for( i=0; i<t->blockCount; ++i ) {
     462        t->blocks[i].block = i;
     463        t->blocks[i].scarcity = tr_cpPieceIsComplete( t->tor->completion, i )
     464                                                    ? UINT32_MAX : 0;
     465    }
    391466    memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH );
    392467    tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare );
  • branches/encryption/libtransmission/peer-msgs.c

    r2983 r2984  
    4545
    4646/* the most requests we'll batch up for this peer */
    47 #define MAX_OUT_REQUESTS 10
     47#define OUT_REQUESTS_MAX 10
     48
     49/* when we get down to this many requests, we ask the manager for more */
     50#define OUT_REQUESTS_LOW 4
    4851
    4952enum
     
    9396
    9497static int
    95 peer_request_compare_func( const void * va, const void * vb )
     98peer_request_compare( const void * va, const void * vb )
    9699{
    97100    struct peer_request * a = (struct peer_request*) va;
     
    111114    tr_peerIo * io;
    112115
     116    tr_publisher_t * publisher;
     117
    113118    struct evbuffer * outMessages; /* buffer of all the non-piece messages */
    114119    struct evbuffer * outBlock;    /* the block we're currently sending */
     
    136141    int pexCount;
    137142};
     143
     144/**
     145***  EVENTS
     146**/
     147
     148static const tr_peermsgs_event blankEvent = { 0, 0, NULL };
     149
     150static void
     151publishEvent( tr_peermsgs * peer, int eventType )
     152{
     153    tr_peermsgs_event e = blankEvent;
     154    e.eventType = eventType;
     155    tr_publisherPublish( peer->publisher, peer, &e );
     156}
     157
     158static void
     159fireGotPex( tr_peermsgs * peer )
     160{
     161    publishEvent( peer, TR_PEERMSG_GOT_PEX );
     162}
     163
     164static void
     165fireGotBitfield( tr_peermsgs * peer, const tr_bitfield * bitfield )
     166{
     167    tr_peermsgs_event e = blankEvent;
     168    e.eventType = TR_PEERMSG_GOT_BITFIELD;
     169    e.bitfield = bitfield;
     170    tr_publisherPublish( peer->publisher, peer, &e );
     171}
     172
     173static void
     174fireGotHave( tr_peermsgs * peer, uint32_t pieceIndex )
     175{
     176    tr_peermsgs_event e = blankEvent;
     177    e.eventType = TR_PEERMSG_GOT_HAVE;
     178    e.pieceIndex = pieceIndex;
     179    tr_publisherPublish( peer->publisher, peer, &e );
     180}
     181
     182static void
     183fireGotError( tr_peermsgs * peer )
     184{
     185    publishEvent( peer, TR_PEERMSG_GOT_ERROR );
     186}
     187
     188static void
     189fireBlocksRunningLow( tr_peermsgs * peer )
     190{
     191    publishEvent( peer, TR_PEERMSG_BLOCKS_RUNNING_LOW );
     192}
    138193
    139194/**
     
    230285    int ret =-1;
    231286
    232     if( tr_list_size(peer->clientAskedFor) < MAX_OUT_REQUESTS )
     287    if( tr_list_size(peer->clientAskedFor) < OUT_REQUESTS_MAX )
    233288    {
    234289        const uint8_t bt_msgid = BT_REQUEST;
     
    335390    }
    336391
     392    fireGotPex( peer );
     393
    337394    tr_bencFree( &val );
    338395    tr_free( tmp );
     
    440497            tr_bitfieldAdd( peer->info->have, ui32 );
    441498            peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount;
     499            fireGotHave( peer, ui32 );
    442500            updateInterest( peer );
    443501            break;
     
    449507            peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount;
    450508            fprintf( stderr, "peer progress is %f\n", peer->info->progress );
     509            fireGotBitfield( peer, peer->info->have );
    451510            updateInterest( peer );
    452511            /* FIXME: maybe unchoke */
     
    474533            tr_peerIoReadUint32( peer->io, inbuf, &req.offset );
    475534            tr_peerIoReadUint32( peer->io, inbuf, &req.length );
    476             node = tr_list_find( peer->peerAskedFor, &req, peer_request_compare_func );
     535            node = tr_list_find( peer->peerAskedFor, &req, peer_request_compare );
    477536            if( node != NULL ) {
    478537                fprintf( stderr, "found the req that peer is cancelling... cancelled.\n" );
     
    537596}
    538597
    539 static int
    540 weAskedForThisBlock( const tr_peermsgs * peer, uint32_t index, uint32_t offset, uint32_t length )
    541 {
    542     struct peer_request tmp;
    543     tmp.index = index;
    544     tmp.offset = offset;
    545     tmp.length = length;
    546 
    547     return tr_list_find( peer->clientAskedFor, &tmp, peer_request_compare_func ) != NULL;
    548 }
    549 
    550598static void
    551599gotBlock( tr_peermsgs * peer, int index, int offset, struct evbuffer * inbuf )
    552600{
    553601    tr_torrent * tor = peer->torrent;
    554     const size_t len = EVBUFFER_LENGTH( inbuf );
     602    const size_t length = EVBUFFER_LENGTH( inbuf );
    555603    const int block = _tr_block( tor, index, offset );
     604    struct peer_request key, *req;
    556605
    557606    /* sanity clause */
     
    560609        return;
    561610    }
    562     if( (int)len != tr_torBlockCountBytes( tor, block ) ) {
     611    if( (int)length != tr_torBlockCountBytes( tor, block ) ) {
    563612        tr_dbg( "block is the wrong length..." );
    564613        return;
    565614    }
    566     if( !weAskedForThisBlock( peer, index, offset, len ) ) {
     615
     616    /* remove it from our `we asked for this' list */
     617    key.index = index;
     618    key.offset = offset;
     619    key.length = length;
     620    req = (struct peer_request*) tr_list_find( peer->clientAskedFor, &key,
     621                                               peer_request_compare );
     622    if( req == NULL ) {
    567623        tr_dbg( "we didn't ask the peer for this message..." );
    568624        return;
    569625    }
     626    tr_list_remove_data( &peer->clientAskedFor, req );
     627    tr_free( req );
    570628
    571629    /* write to disk */
    572     if( tr_ioWrite( tor, index, offset, len, EVBUFFER_DATA( inbuf )))
     630    if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf )))
    573631        return;
    574632
     
    580638    tr_cpBlockAdd( tor->completion, block );
    581639
    582     tor->downloadedCur += len;
    583     tr_rcTransferred( tor->download, len );
    584     tr_rcTransferred( tor->handle->download, len );
     640    tor->downloadedCur += length;
     641    tr_rcTransferred( tor->download, length );
     642    tr_rcTransferred( tor->handle->download, length );
     643
     644    if( tr_list_size(peer->clientAskedFor) <= OUT_REQUESTS_LOW )
     645        fireBlocksRunningLow( peer );
    585646}
    586647
     
    727788
    728789static void
    729 gotError( struct bufferevent * evbuf UNUSED, short what, void * vpeer )
    730 {
    731     tr_peermsgs * peer = (tr_peermsgs *) vpeer;
    732     fprintf( stderr, "peer %p got an error in %d\n", peer, (int)what );
     790gotError( struct bufferevent * evbuf UNUSED, short what UNUSED, void * vpeer )
     791{
     792    fireGotError( (tr_peermsgs*)vpeer );
    733793}
    734794
     
    899959
    900960    peer = tr_new0( tr_peermsgs, 1 );
     961    peer->publisher = tr_publisherNew( );
    901962    peer->info = info;
    902963    peer->handle = torrent->handle;
     
    929990    {
    930991fprintf( stderr, "peer %p destroying its pulse tag\n", p );
     992        tr_publisherFree( &p->publisher );
    931993        tr_timerFree( &p->pulseTag );
    932994        tr_timerFree( &p->pexTag );
     
    937999    }
    9381000}
     1001
     1002tr_publisher_tag
     1003tr_peerMsgsSubscribe( tr_peermsgs       * peer,
     1004                      tr_delivery_func    func,
     1005                      void              * userData )
     1006{
     1007    return tr_publisherSubscribe( peer->publisher, func, userData );
     1008}
     1009
     1010void
     1011tr_peerMsgsUnsubscribe( tr_peermsgs       * peer,
     1012                        tr_publisher_tag    tag )
     1013{
     1014    tr_publisherUnsubscribe( peer->publisher, tag );
     1015}
  • branches/encryption/libtransmission/peer-msgs.h

    r2982 r2984  
    1414#define TR_P_H
    1515
     16#include <inttypes.h>
     17#include "publish.h"
     18
    1619struct tr_torrent;
    1720struct tr_peer;
     21struct tr_bitfield;
    1822
    1923typedef struct tr_peermsgs tr_peermsgs;
     
    3135                                    uint32_t      length );
    3236
     37/**
     38***  PeerMsgs Publish / Subscribe
     39**/
     40
     41typedef enum
     42{
     43    TR_PEERMSG_GOT_BITFIELD,
     44    TR_PEERMSG_GOT_HAVE,
     45    TR_PEERMSG_GOT_PEX,
     46    TR_PEERMSG_GOT_ERROR,
     47    TR_PEERMSG_BLOCKS_RUNNING_LOW,
     48}
     49PeerMsgsEventType;
     50
     51typedef struct
     52{
     53    PeerMsgsEventType eventType;
     54    uint32_t pieceIndex; /* for TR_PEERMSG_GOT_HAVE */
     55    const struct tr_bitfield * bitfield; /* for TR_PEERMSG_GOT_BITFIELD */
     56}
     57tr_peermsgs_event;
     58
     59tr_publisher_tag  tr_peerMsgsSubscribe   ( tr_peermsgs       * peer,
     60                                           tr_delivery_func    func,
     61                                           void              * user );
     62
     63void              tr_peerMsgsUnsubscribe ( tr_peermsgs       * peer,
     64                                          tr_publisher_tag     tag );
     65
     66
    3367
    3468#endif
  • branches/encryption/libtransmission/publish.c

    r2942 r2984  
    1111 */
    1212
     13#include <assert.h>
    1314#include "list.h"
    1415#include "publish.h"
     
    3334
    3435void
    35 tr_publisherFree( tr_publisher_t * p )
     36tr_publisherFree( tr_publisher_t ** p )
    3637{
    37     tr_list_free( &p->list );
    38     tr_free( p );
     38    assert( p != NULL );
     39    assert( *p != NULL );
     40
     41    tr_list_free( &(*p)->list );
     42    tr_free( *p );
     43    *p = NULL;
    3944}
    4045
  • branches/encryption/libtransmission/publish.h

    r2849 r2984  
    4343tr_publisher_t *   tr_publisherNew         ( void );
    4444
    45 void               tr_publisherFree        ( tr_publisher_t    * publisher );
     45void               tr_publisherFree        ( tr_publisher_t   ** publisher );
    4646
    4747void               tr_publisherPublish     ( tr_publisher_t    * publisher,
  • branches/encryption/libtransmission/tracker.c

    r2954 r2984  
    383383    tr_timerFree( &tor->scrapeTag );
    384384    tr_timerFree( &tor->reannounceTag );
    385     tr_publisherFree( tor->publisher );
     385    tr_publisherFree( &tor->publisher );
    386386    tr_free( tor->trackerID );
    387387    tr_free( tor->lastRequest );
Note: See TracChangeset for help on using the changeset viewer.