1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
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. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: publish.c 7404 2008-12-16 00:20:44Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include "list.h" |
---|
15 | #include "publish.h" |
---|
16 | #include "utils.h" |
---|
17 | |
---|
18 | struct tr_publisher_node |
---|
19 | { |
---|
20 | tr_delivery_func * func; |
---|
21 | void * user_data; |
---|
22 | }; |
---|
23 | |
---|
24 | struct tr_publisher_s |
---|
25 | { |
---|
26 | tr_list * list; |
---|
27 | }; |
---|
28 | |
---|
29 | tr_publisher_t* |
---|
30 | tr_publisherNew( void ) |
---|
31 | { |
---|
32 | return tr_new0( tr_publisher_t, 1 ); |
---|
33 | } |
---|
34 | |
---|
35 | void |
---|
36 | tr_publisherFree( tr_publisher_t ** p ) |
---|
37 | { |
---|
38 | assert( p ); |
---|
39 | assert( *p ); |
---|
40 | |
---|
41 | tr_list_free( &( *p )->list, NULL ); |
---|
42 | tr_free( *p ); |
---|
43 | *p = NULL; |
---|
44 | } |
---|
45 | |
---|
46 | tr_publisher_tag |
---|
47 | tr_publisherSubscribe( tr_publisher_t * p, |
---|
48 | tr_delivery_func func, |
---|
49 | void * user_data ) |
---|
50 | { |
---|
51 | struct tr_publisher_node * node = tr_new( struct tr_publisher_node, 1 ); |
---|
52 | |
---|
53 | node->func = func; |
---|
54 | node->user_data = user_data; |
---|
55 | tr_list_append( &p->list, node ); |
---|
56 | return node; |
---|
57 | } |
---|
58 | |
---|
59 | void |
---|
60 | tr_publisherUnsubscribe( tr_publisher_t * p, |
---|
61 | tr_publisher_tag tag ) |
---|
62 | { |
---|
63 | tr_list_remove_data( &p->list, tag ); |
---|
64 | tr_free( tag ); |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | tr_publisherPublish( tr_publisher_t * p, |
---|
69 | void * source, |
---|
70 | void * event ) |
---|
71 | { |
---|
72 | tr_list * walk; |
---|
73 | |
---|
74 | for( walk = p->list; walk != NULL; ) |
---|
75 | { |
---|
76 | tr_list * next = walk->next; |
---|
77 | struct tr_publisher_node * node = |
---|
78 | (struct tr_publisher_node*)walk->data; |
---|
79 | ( node->func )( source, event, node->user_data ); |
---|
80 | walk = next; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|