d3372da9 |
1 | #ifdef HAVE_CONFIG_H |
2 | #include <config.h> |
3 | #endif |
4 | #include <string.h> |
5 | #include <stdlib.h> |
6 | #include <errno.h> |
7 | #include <doldaconnect/utils.h> |
8 | #include <panel-applet.h> |
9 | #include <time.h> |
10 | |
11 | #include "conduit.h" |
12 | |
13 | void (*cb_condstate)(struct conduit *conduit, void *data) = NULL; |
14 | void (*cb_trsize)(struct transfer *transfer, void *data) = NULL; |
15 | void (*cb_trpos)(struct transfer *transfer, void *data) = NULL; |
16 | void (*cb_trnew)(struct transfer *transfer, void *data) = NULL; |
17 | void (*cb_trfree)(struct transfer *transfer, void *data) = NULL; |
18 | |
19 | struct transfer *findtransferbytag(struct conduit *conduit, char *tag) |
20 | { |
21 | struct transfer *transfer; |
22 | |
23 | for(transfer = conduit->transfers; transfer != NULL; transfer = transfer->next) |
24 | { |
25 | if((transfer->tag != NULL) && !strcmp(transfer->tag, tag)) |
26 | break; |
27 | } |
28 | return(transfer); |
29 | } |
30 | |
e03797e4 |
31 | void transfersetsize(struct transfer *transfer, intmax_t size) |
d3372da9 |
32 | { |
33 | transfer->size = size; |
34 | cb_trsize(transfer, transfer->conduit->udata); |
35 | } |
36 | |
e03797e4 |
37 | void transfersetpos(struct transfer *transfer, intmax_t pos) |
d3372da9 |
38 | { |
39 | transfer->pos = pos; |
40 | cb_trpos(transfer, transfer->conduit->udata); |
41 | } |
42 | |
43 | static gboolean trupdatetime(struct transfer *transfer) |
44 | { |
45 | time_t now; |
46 | |
47 | if((transfer->size == -1) || (transfer->pos == -1)) |
48 | return(TRUE); |
49 | now = time(NULL); |
50 | if(now - transfer->ckptime >= 10) |
51 | { |
52 | transfer->cmptime = transfer->ckptime; |
53 | transfer->cmpsize = transfer->ckpsize; |
54 | transfer->ckptime = 0; |
55 | } |
56 | if(transfer->ckptime == 0) |
57 | { |
58 | transfer->ckptime = now; |
59 | transfer->ckpsize = transfer->pos; |
60 | } |
61 | return(TRUE); |
62 | } |
63 | |
e45d1d8a |
64 | struct transfer *newtransfer(struct conduit *conduit, char *tag, intmax_t size, intmax_t pos) |
d3372da9 |
65 | { |
66 | struct transfer *transfer; |
67 | |
68 | transfer = smalloc(sizeof(*transfer)); |
69 | memset(transfer, 0, sizeof(*transfer)); |
70 | if(tag != NULL) |
71 | transfer->tag = sstrdup(tag); |
72 | transfer->size = size; |
73 | transfer->pos = pos; |
74 | transfer->timeout = g_timeout_add(1000, (gboolean (*)(gpointer))trupdatetime, transfer); |
75 | transfer->next = conduit->transfers; |
76 | transfer->conduit = conduit; |
77 | if(conduit->transfers != NULL) |
78 | conduit->transfers->prev = transfer; |
79 | conduit->transfers = transfer; |
80 | cb_trnew(transfer, conduit->udata); |
81 | return(transfer); |
82 | } |
83 | |
84 | void freetransfer(struct transfer *transfer) |
85 | { |
86 | if(transfer->next != NULL) |
87 | transfer->next->prev = transfer->prev; |
88 | if(transfer->prev != NULL) |
89 | transfer->prev->next = transfer->next; |
90 | if(transfer->conduit->transfers == transfer) |
91 | transfer->conduit->transfers = transfer->next; |
92 | cb_trfree(transfer, transfer->conduit->udata); |
93 | g_source_remove(transfer->timeout); |
94 | if(transfer->tag != NULL) |
95 | free(transfer->tag); |
96 | free(transfer); |
97 | } |
98 | |
99 | struct conduit *newconduit(struct conduitiface *iface, void *udata) |
100 | { |
101 | struct conduit *conduit; |
102 | |
103 | conduit = smalloc(sizeof(*conduit)); |
104 | memset(conduit, 0, sizeof(*conduit)); |
105 | conduit->iface = iface; |
106 | conduit->udata = udata; |
107 | if(iface->init(conduit)) |
108 | { |
109 | free(conduit); |
110 | return(NULL); |
111 | } |
112 | return(conduit); |
113 | } |
114 | |
115 | void freeconduit(struct conduit *conduit) |
116 | { |
117 | conduit->iface->destroy(conduit); |
118 | while(conduit->transfers != NULL) |
119 | freetransfer(conduit->transfers); |
120 | free(conduit); |
121 | } |
122 | |
123 | int condtryconn(struct conduit *conduit) |
124 | { |
125 | if(conduit->state != CNDS_IDLE) |
126 | return(-1); |
127 | if(conduit->iface->connect(conduit)) |
128 | return(-1); |
129 | conduit->state = CNDS_SYN; |
130 | return(0); |
131 | } |
132 | |
133 | void conddisconn(struct conduit *conduit) |
134 | { |
135 | while(conduit->transfers != NULL) |
136 | freetransfer(conduit->transfers); |
137 | conduit->state = CNDS_IDLE; |
138 | cb_condstate(conduit, conduit->udata); |
139 | } |
140 | |
141 | void condconnected(struct conduit *conduit) |
142 | { |
143 | conduit->state = CNDS_EST; |
144 | cb_condstate(conduit, conduit->udata); |
145 | } |