-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_timeq.c
More file actions
executable file
·230 lines (194 loc) · 4.28 KB
/
chat_timeq.c
File metadata and controls
executable file
·230 lines (194 loc) · 4.28 KB
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
#include <chat_timeq.h>
#include <chat_io.h>
#include <stdlib.h>
typedef struct chat_l_timeq_entry_s
{
struct timeval tm;
void * value;
struct chat_l_timeq_entry_s * next;
} chat_l_timeq_entry_t;
typedef struct chat_l_timeq_s
{
chat_l_timeq_entry_t * head;
int size;
} chat_l_timeq_t;
int
chat_l_tm_cmp(
const struct timeval * t1,
const struct timeval * t2)
{
if(t1->tv_sec > t2->tv_sec)
{
return 1;
}
else if(t1->tv_sec < t2->tv_sec)
{
return -1;
}
else
{
if(t1->tv_usec > t2->tv_usec)
{
return 1;
}
else if(t1->tv_usec < t2->tv_usec)
{
return -1;
}
else
{
return 0;
}
}
}
int
chat_timeq_init(
chat_timeq_t * out_timeq)
{
chat_l_timeq_t * l_tq;
l_tq = (chat_l_timeq_t *) calloc(1, sizeof(chat_l_timeq_t));
*out_timeq = l_tq;
return 0;
}
int
chat_timeq_destroy(
chat_timeq_t timeq)
{
free(timeq);
return 0;
}
int
chat_timeq_enqueue(
chat_timeq_t timeq,
struct timeval * tm,
void * value)
{
int done;
chat_l_timeq_entry_t * i;
chat_l_timeq_entry_t * j;
chat_l_timeq_entry_t * new;
struct timeval now;
gettimeofday(&now, NULL);
i = NULL;
j = timeq->head;
new = (chat_l_timeq_entry_t *) calloc(1, sizeof(chat_l_timeq_entry_t));
new->tm.tv_sec = tm->tv_sec + now.tv_sec;
new->tm.tv_usec = tm->tv_usec + now.tv_usec;
new->value = value;
done = FALSE;
while(j != NULL && !done)
{
if(chat_l_tm_cmp(&new->tm, &j->tm) < 0)
{
done = TRUE;
}
else
{
i = j;
j = j->next;
}
}
new->next = j;
if(i == NULL)
{
timeq->head = new;
}
else
{
i->next = new;
}
return 0;
}
void *
chat_timeq_dequeue(
chat_timeq_t timeq)
{
chat_l_timeq_entry_t * head;
struct timeval now;
void * val;
gettimeofday(&now, NULL);
head = timeq->head;
if(head == NULL)
{
return NULL;
}
if(chat_l_tm_cmp(&now, &head->tm) >= 0)
{
timeq->size--;
timeq->head = timeq->head->next;
val = head->value;
free(head);
return val;
}
return NULL;
}
void *
chat_timeq_peek(
chat_timeq_t timeq)
{
chat_l_timeq_entry_t * head;
struct timeval now;
gettimeofday(&now, NULL);
head = timeq->head;
if(head == NULL)
{
return NULL;
}
if(chat_l_tm_cmp(&now, &head->tm) >= 0)
{
return head->value;
}
return NULL;
}
int
chat_timeq_remove(
chat_timeq_t timeq,
void * value)
{
chat_l_timeq_entry_t * i;
chat_l_timeq_entry_t * j;
i = NULL;
j = timeq->head;
while(j != NULL)
{
if(j->value == value)
{
if(i == NULL)
{
timeq->head = j->next;
}
else
{
i->next = j->next;
}
free(j);
return 0;
}
i = j;
j = j->next;
}
return -1;
}
int
chat_timeq_next(
chat_timeq_t timeq,
struct timeval * out_tm)
{
chat_l_timeq_entry_t * head;
struct timeval now;
head = timeq->head;
if(head == NULL)
{
out_tm->tv_sec = -1;
out_tm->tv_usec = -1;
}
else
{
gettimeofday(&now, NULL);
out_tm->tv_sec = head->tm.tv_sec - now.tv_sec;
out_tm->tv_usec = head->tm.tv_usec - now.tv_usec;
if(out_tm->tv_sec < 0) out_tm->tv_sec = 0;
if(out_tm->tv_usec < 0) out_tm->tv_usec = 0;
}
return 0;
}