source: trunk/libtransmission/transmission.c @ 2185

Last change on this file since 2185 was 2185, checked in by charles, 15 years ago

add color-coded lines to the gtk debug window -- info is black, debug is grey, errors are red...

  • Property svn:keywords set to Date Rev Author Id
File size: 5.1 KB
Line 
1/******************************************************************************
2 * $Id: transmission.c 2185 2007-06-22 20:59:23Z charles $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include "transmission.h"
26#include "shared.h"
27
28/***********************************************************************
29 * tr_init
30 ***********************************************************************
31 * Allocates a tr_handle_t structure and initializes a few things
32 **********************************************************************/
33tr_handle_t * tr_init( const char * tag )
34{
35    tr_handle_t * h;
36    int           i, r;
37
38    tr_msgInit();
39    tr_netResolveThreadInit();
40
41    h = calloc( 1, sizeof( tr_handle_t ) );
42    if( NULL == h )
43    {
44        return NULL;
45    }
46
47    h->tag = strdup( tag );
48    if( NULL == h->tag )
49    {
50        free( h );
51        return NULL;
52    }
53
54    /* Generate a peer id : "-TRxxyz-" + 12 random alphanumeric
55       characters, where xx is the major version number, y is the
56       minor version number, and z is the maintenance number (Azureus-style) */
57    snprintf( h->id, sizeof h->id, "-TR%02d%01d%01d-",
58              VERSION_MAJOR, VERSION_MINOR, VERSION_MAINTENANCE );
59    for( i = 8; i < TR_ID_LEN; i++ )
60    {
61        r        = tr_rand( 36 );
62        h->id[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
63    }
64
65    /* Random key */
66    for( i = 0; i < TR_KEY_LEN; i++ )
67    {
68        r         = tr_rand( 36 );
69        h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
70    }
71
72    /* Azureus identity */
73    for( i = 0; i < TR_AZ_ID_LEN; i++ )
74    {
75        h->azId[i] = tr_rand( 0xff );
76    }
77
78    /* Don't exit when writing on a broken socket */
79    signal( SIGPIPE, SIG_IGN );
80
81    /* Initialize rate and file descripts controls */
82    h->upload   = tr_rcInit();
83    h->download = tr_rcInit();
84
85    tr_fdInit();
86    h->shared = tr_sharedInit( h );
87
88    tr_inf( TR_NAME " " VERSION_STRING " started" );
89
90    return h;
91}
92
93/***********************************************************************
94 * tr_setBindPort
95 ***********************************************************************
96 *
97 **********************************************************************/
98void tr_setBindPort( tr_handle_t * h, int port )
99{
100    h->isPortSet = 1;
101    tr_sharedSetPort( h->shared, port );
102}
103
104void tr_natTraversalEnable( tr_handle_t * h, int enable )
105{
106    tr_sharedLock( h->shared );
107    tr_sharedTraversalEnable( h->shared, enable );
108    tr_sharedUnlock( h->shared );
109}
110
111tr_handle_status_t * tr_handleStatus( tr_handle_t * h )
112{
113    tr_handle_status_t * s;
114
115    h->statCur = ( h->statCur + 1 ) % 2;
116    s = &h->stats[h->statCur];
117
118    tr_sharedLock( h->shared );
119
120    s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
121    s->publicPort = tr_sharedGetPublicPort( h->shared );
122
123    tr_sharedUnlock( h->shared );
124
125    return s;
126}
127
128void tr_setGlobalUploadLimit( tr_handle_t * h, int limit )
129{
130    tr_rcSetLimit( h->upload, limit );
131    tr_sharedSetLimit( h->shared, limit );
132}
133
134void tr_setGlobalDownloadLimit( tr_handle_t * h, int limit )
135{
136    tr_rcSetLimit( h->download, limit );
137}
138
139void tr_torrentRates( tr_handle_t * h, float * dl, float * ul )
140{
141    tr_torrent_t * tor;
142
143    *dl = 0.0;
144    *ul = 0.0;
145    tr_sharedLock( h->shared );
146    for( tor = h->torrentList; tor; tor = tor->next )
147    {
148        tr_lockLock( &tor->lock );
149        if( tor->status & TR_STATUS_DOWNLOAD )
150            *dl += tr_rcRate( tor->download );
151        *ul += tr_rcRate( tor->upload );
152        tr_lockUnlock( &tor->lock );
153    }
154    tr_sharedUnlock( h->shared );
155}
156
157int tr_torrentCount( tr_handle_t * h )
158{
159    return h->torrentCount;
160}
161
162void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d )
163{
164    tr_torrent_t * tor, * next;
165
166    for( tor = h->torrentList; tor; tor = next )
167    {
168        next = tor->next;
169        func( tor, d );
170    }
171}
172
173void tr_close( tr_handle_t * h )
174{
175    tr_rcClose( h->upload );
176    tr_rcClose( h->download );
177   
178    tr_sharedClose( h->shared );
179    tr_fdClose();
180    free( h->tag );
181    free( h );
182
183    tr_netResolveThreadClose();
184}
Note: See TracBrowser for help on using the repository browser.