-
Notifications
You must be signed in to change notification settings - Fork 9
/
cdata.c
343 lines (308 loc) · 7.4 KB
/
cdata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* Asynchronous SSE Server
* Author: Peter Deak ([email protected])
* License: GPL
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "cdata.h"
#include "hasses.h"
#include "cio.h"
struct CliBank
{
struct CliConn *clis;
int next;
int max;
struct CliBank *n;
};
struct CliCursor
{
struct CliBank *bank;
int current;
};
static const char client_status_name[][5] = { "UNK", "NEW", "SSL", "HEA", "SSE", "END" };
struct CliBank *bFirst = NULL; //Fist CliConn bank
struct CliBank *bCurr = NULL; //Current CliConn bank
struct CliCursor cursor;
int client_init(void)
{
bFirst = (struct CliBank*)malloc(sizeof(struct CliBank));
bFirst->clis = (struct CliConn*)malloc(sizeof(struct CliConn) * BANKSIZE);
bFirst->next = 0;
bFirst->max = BANKSIZE-1;
bFirst->n = NULL;
bCurr = bFirst;
client_start();
return 0;
}
int client_start(void)
{
cursor.bank = bFirst;
cursor.current = -1;
return 0;
}
struct CliConn * client_current(void)
{
if(cursor.current < 0)
return NULL;
if(cursor.current >= cursor.bank->next)
return NULL;
return cursor.bank->clis + cursor.current;
}
struct CliConn * client_next(void)
{
++(cursor.current);
if(cursor.current >= cursor.bank->next)
{
if(cursor.bank->n == NULL)
return NULL;
cursor.bank = cursor.bank->n;
if(cursor.bank->next == 0)
return NULL;
cursor.current = 0;
}
return cursor.bank->clis + cursor.current;
}
struct CliConn * client_get(int descr)
{
int i;
struct CliBank *b = bFirst;
while(b != NULL)
{
for(i=0;i<b->next;++i)
{
if(b->clis[i].descr == descr)
{
cursor.bank = b;
cursor.current = i;
return b->clis + i;
}
}
b = b->n;
}
client_start();
return NULL;
}
struct CliConn * client_add(int descr)
{
struct CliConn *newitem;
if(bCurr->next <= bCurr->max)
{
newitem = bCurr->clis + bCurr->next;
++bCurr->next;
}
else
{
//Allocate a new bank
bCurr->n = (struct CliBank*)malloc(sizeof(struct CliBank));
bCurr->n->clis = (struct CliConn*)malloc(sizeof(struct CliConn) * BANKSIZE);
bCurr->n->next = 1;
bCurr->n->max = BANKSIZE-1;
bCurr->n->n = NULL;
bCurr = bCurr->n;
newitem = bCurr->clis;
}
newitem->descr = descr;
newitem->cio = NULL;
newitem->err = 0;
newitem->created = time(NULL);
newitem->firstc = newitem->created;
newitem->status = STATUS_UNKNOWN;
newitem->allsubs = 0;
newitem->subs = NULL;
newitem->message = 0;
newitem->reinit = 0;
h_strlcpy(newitem->info,"",64);
h_strlcpy(newitem->uniq_id,"",64);
h_strlcpy(newitem->agent,"",192);
cursor.bank = bCurr;
cursor.current = bCurr->next - 1;
return newitem;
}
int client_del(int descr)
{
struct CliConn *toDelete;
toDelete = client_get(descr);
if(toDelete == NULL) //Can't find the item
return 1;
//Clear subsribed tokens to free memory
client_subscribe_clear(toDelete);
//Find the last item, and copy to the toDelete
memcpy(toDelete,bCurr->clis + (bCurr->next - 1),sizeof(struct CliConn));
if(bCurr->next > 0)
--bCurr->next;
//If we deleted the last item in a bank need to delete the last (now enpty) bank
if(bCurr->next == 0 && bCurr != bFirst)
{
struct CliBank *b = bFirst;
while(b != NULL)
{
if(b->n == bCurr)
{
if(cursor.bank == bCurr)
{
cursor.bank = b;
cursor.current = cursor.bank->next - 1;
}
free(b->n);
b->n = NULL;
bCurr = b;
break;
}
b = b->n;
}
}
//Cursor leaved at toDelete which set by client_get.
return 0;
}
int client_count(void)
{
int c=0;
struct CliBank *b = bFirst;
while(b != NULL)
{
c += b->next;
b = b->n;
}
return c;
}
int subscribed_client_count(char *sub_to_search)
{
int i=0;
client_start();
while(client_next())
{
struct CliSubsribe *sub = client_current()->subs;
while(sub != NULL)
{
if(strcmp(sub->token, sub_to_search) == 0)
{
++i;
break;
}
sub = sub->next;
}
}
return i;
}
int client_count_commstate(void)
{
int i=0;
client_start();
while(client_next())
if(client_current()->status == STATUS_COMM)
++i;
return i;
}
void client_list(int level)
{
char sbuf[512];
char ciobuf[512];
char dt1[64];
char dt2[64];
time_t now=time(NULL);
int i=1;
toLog(level,"List of clients:\n");
struct CliConn *c;
client_start();
while((c=client_next()) != NULL)
{
client_subscribe_list(c,sbuf,512);
diffsec_to_str(now - c->created,dt1,64);
diffsec_to_str(now - c->firstc,dt2,64);
toLog(level,"#%d - <%d> info: %s S:%s Err: %d Messages: %d Reinit: %d SSL:%s\n",
i,c->descr,c->info,client_status_name[c->status],c->err,c->message,c->reinit,
(c->cio == NULL?"No":"Yes"));
toLog(level," Connection time: %s Handshaked time: %s UniqId:%s\n",dt2,dt1,c->uniq_id);
if(strlen(c->agent) > 0)
toLog(level," Agent: %s\n",c->agent);
if(cio_info_text(c,ciobuf,512))
toLog(level," %s\n",ciobuf);
toLog(level," Subscribes: %s\n",sbuf);
++i;
}
}
void client_subscribe_list(struct CliConn *cli,char *sbuf,int max)
{
int tl;
int left = max;
char *br = sbuf;
struct CliSubsribe *s = cli->subs;
sbuf[0] = '\0';
if(cli->allsubs)
{
h_strlcpy(br,"*",left);
--left;
++br;
}
while(s != NULL)
{
if(left <= 0)
return;
if(sbuf[0] != '\0')
{
h_strlcpy(br,",",left);
--left;
++br;
}
tl = strlen(s->token);
h_strlcpy(br,s->token,left);
br += tl;
left -= tl;
s = s->next;
}
}
void client_subscribe_add(struct CliConn *cli,char *ss)
{
struct CliSubsribe *s,*ns;
if(strcmp(ss,"*") == 0)
{
cli->allsubs = 1;
return;
}
ns = (struct CliSubsribe *)malloc(sizeof(struct CliSubsribe));
ns->next = NULL;
h_strlcpy(ns->token,ss,31);
if(cli->subs == NULL)
cli->subs = ns;
else
{
s = cli->subs;
while(s->next != NULL)
s = s->next;
s->next = ns;
}
}
int client_subscribe_exists(struct CliConn *cli,char *ss,int mmatch,char *rejectId)
{
struct CliSubsribe *s;
if(strcmp(rejectId,"") != 0 && strcmp(rejectId,cli->uniq_id) == 0)
return 0;
if(cli->allsubs || strcmp(ss,"*") == 0)
return 1;
s = cli->subs;
while(s != NULL)
{
if(strncmp(s->token,ss,mmatch) == 0)
return 1;
s = s->next;
}
return 0;
}
void client_subscribe_clear(struct CliConn *cli)
{
struct CliSubsribe *s,*del;
cli->allsubs = 0;
s = cli->subs;
while(s != NULL)
{
del = s;
s = s->next;
free(del);
}
cli->subs = NULL;
}
//end.