-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagequeue.c
92 lines (75 loc) · 1.4 KB
/
messagequeue.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum State {
STARTED,
FINISHED,
} State;
typedef struct Message {
char *msg;
int fs2a_fd;
int fa2s_fd;
struct Message *next;
} Message;
typedef struct MessageQueue {
Message *head;
Message *tail;
Message *last; // Last command in the queue which was read
State state;
} MessageQueue;
MessageQueue mq;
/*
*
* Helper Functions
*
*/
void mq_init() {
mq.head = NULL;
mq.tail = NULL;
mq.last = NULL;
mq.state = FINISHED;
}
void mq_push(char *cmd, int fs2a_fd, int fa2s_fd) {
Message *new_msg = calloc(1, sizeof(Message));
new_msg->msg = calloc(strlen(cmd) + 1, sizeof(char));
strncpy(new_msg->msg, cmd, strlen(cmd));
new_msg->msg[strlen(cmd)] = 0;
new_msg->fs2a_fd = fs2a_fd;
new_msg->fa2s_fd = fa2s_fd;
new_msg->next = NULL;
if(mq.head == NULL) {
mq.head = new_msg;
mq.tail = new_msg;
} else {
mq.tail->next = new_msg;
mq.tail = new_msg;
}
}
void mq_pop() {
Message *popped;
if(mq.head != NULL) {
popped = mq.head;
mq.head = mq.head->next;
free(popped->msg);
free(popped);
} else {
mq.head = NULL;
mq.tail = NULL;
mq.last = NULL;
}
}
void mq_cleanup() {
while(mq.head != NULL) {
mq_pop();
}
mq_init();
}
void mq_print() {
Message *i = mq.head;
printf("state: %d\n", mq.state);
printf("Contents:\n");
while(i != NULL) {
printf("[%d, %d] %s\n", i->fs2a_fd, i->fa2s_fd, i->msg);
i = i->next;
}
}