~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-updates

« back to all changes in this revision

Viewing changes to libtransmission/stats.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * This file is licensed by the GPL version 2.  Works owned by the
5
5
 * Transmission project are granted a special exemption to clause 2(b)
6
 
 * so that the bulk of its code can remain under the MIT license. 
 
6
 * so that the bulk of its code can remain under the MIT license.
7
7
 * This exemption does not extend to derived works not owned by
8
8
 * the Transmission project.
9
9
 *
10
 
 * $Id: stats.c 6489 2008-08-11 19:05:00Z charles $
 
10
 * $Id: stats.c 6896 2008-10-14 03:03:29Z charles $
11
11
 */
12
12
 
13
13
#include <assert.h>
24
24
 
25
25
struct tr_stats_handle
26
26
{
27
 
    tr_session_stats single;
28
 
    tr_session_stats old;
29
 
    time_t startTime;
 
27
    tr_session_stats    single;
 
28
    tr_session_stats    old;
 
29
    time_t              startTime;
30
30
};
31
31
 
32
32
static char*
33
 
getOldFilename( const tr_handle * handle, char * buf, size_t buflen )
 
33
getOldFilename( const tr_handle * handle )
34
34
{
35
 
    tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
36
 
                               "stats.benc",
37
 
                               NULL );
38
 
    return buf;
 
35
    return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.benc", NULL );
39
36
}
40
37
 
41
38
static char*
42
 
getFilename( const tr_handle * handle, char * buf, size_t buflen )
 
39
getFilename( const tr_handle * handle )
43
40
{
44
 
    tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
45
 
                               "stats.json",
46
 
                               NULL );
47
 
    return buf;
 
41
    return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.json", NULL );
48
42
}
49
43
 
50
44
static void
51
 
loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
 
45
loadCumulativeStats( const tr_handle *  handle,
 
46
                     tr_session_stats * setme )
52
47
{
53
 
    int loaded = FALSE;
54
 
    char filename[MAX_PATH_LENGTH];
 
48
    int     loaded = FALSE;
 
49
    char   * filename;
55
50
    tr_benc top;
56
51
 
57
 
    getFilename( handle, filename, sizeof(filename) );
 
52
    filename = getFilename( handle );
58
53
    loaded = !tr_bencLoadJSONFile( filename, &top );
59
 
    if( !loaded ) {
60
 
        getOldFilename( handle, filename, sizeof(filename) );
 
54
    tr_free( filename );
 
55
 
 
56
    if( !loaded )
 
57
    {
 
58
        filename = getOldFilename( handle );
61
59
        loaded = !tr_bencLoadFile( filename, &top );
 
60
        tr_free( filename );
62
61
    }
 
62
 
63
63
    if( loaded )
64
64
    {
65
65
        int64_t i;
80
80
}
81
81
 
82
82
static void
83
 
saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
 
83
saveCumulativeStats( const tr_handle *        handle,
 
84
                     const tr_session_stats * s )
84
85
{
85
 
    char filename[MAX_PATH_LENGTH];
 
86
    char * filename;
86
87
    tr_benc top;
87
88
 
88
89
    tr_bencInitDict( &top, 5 );
92
93
    tr_bencDictAddInt( &top, "session-count",    s->sessionCount );
93
94
    tr_bencDictAddInt( &top, "uploaded-bytes",   s->uploadedBytes );
94
95
 
95
 
    getFilename( handle, filename, sizeof(filename) );
 
96
    filename = getFilename( handle );
96
97
    tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
97
98
    tr_bencSaveJSONFile( filename, &top );
98
99
 
 
100
    tr_free( filename );
99
101
    tr_bencFree( &top );
100
102
}
101
103
 
107
109
tr_statsInit( tr_handle * handle )
108
110
{
109
111
    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
 
112
 
110
113
    loadCumulativeStats( handle, &stats->old );
111
114
    stats->single.sessionCount = 1;
112
115
    stats->startTime = time( NULL );
117
120
tr_statsClose( tr_handle * handle )
118
121
{
119
122
    tr_session_stats cumulative;
 
123
 
120
124
    tr_sessionGetCumulativeStats( handle, &cumulative );
121
125
    saveCumulativeStats( handle, &cumulative );
122
126
 
142
146
}
143
147
 
144
148
static void
145
 
addStats( tr_session_stats       * setme,
 
149
addStats( tr_session_stats *       setme,
146
150
          const tr_session_stats * a,
147
151
          const tr_session_stats * b )
148
152
{
155
159
}
156
160
 
157
161
void
158
 
tr_sessionGetStats( const tr_handle   * handle,
159
 
                    tr_session_stats  * setme )
 
162
tr_sessionGetStats( const tr_handle *  handle,
 
163
                    tr_session_stats * setme )
160
164
{
161
165
    const struct tr_stats_handle * stats = getStats( handle );
 
166
 
162
167
    assert( stats );
163
168
    *setme = stats->single;
164
169
    setme->secondsActive = time( NULL ) - stats->startTime;
166
171
}
167
172
 
168
173
void
169
 
tr_sessionGetCumulativeStats( const tr_handle   * handle,
170
 
                              tr_session_stats  * setme )
 
174
tr_sessionGetCumulativeStats( const tr_handle *  handle,
 
175
                              tr_session_stats * setme )
171
176
{
172
177
    const struct tr_stats_handle * stats = getStats( handle );
173
 
    tr_session_stats current;
 
178
    tr_session_stats               current;
 
179
 
174
180
    assert( stats );
175
181
    tr_sessionGetStats( handle, &current );
176
182
    addStats( setme, &stats->old, &current );
180
186
tr_sessionClearStats( tr_handle * handle )
181
187
{
182
188
    tr_session_stats zero;
 
189
 
183
190
    zero.uploadedBytes = 0;
184
191
    zero.downloadedBytes = 0;
185
192
    zero.ratio = TR_RATIO_NA;
196
203
**/
197
204
 
198
205
void
199
 
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
 
206
tr_statsAddUploaded( tr_handle * handle,
 
207
                     uint32_t    bytes )
200
208
{
201
209
    struct tr_stats_handle * s;
202
 
    if(( s = getStats( handle )))
 
210
 
 
211
    if( ( s = getStats( handle ) ) )
203
212
        s->single.uploadedBytes += bytes;
204
213
}
205
214
 
206
215
void
207
 
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
 
216
tr_statsAddDownloaded( tr_handle * handle,
 
217
                       uint32_t    bytes )
208
218
{
209
219
    struct tr_stats_handle * s;
210
 
    if(( s = getStats( handle )))
 
220
 
 
221
    if( ( s = getStats( handle ) ) )
211
222
        s->single.downloadedBytes += bytes;
212
223
}
213
224
 
215
226
tr_statsFileCreated( tr_handle * handle )
216
227
{
217
228
    struct tr_stats_handle * s;
218
 
    if(( s = getStats( handle )))
 
229
 
 
230
    if( ( s = getStats( handle ) ) )
219
231
        s->single.filesAdded++;
220
232
}
 
233