-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebsocket.c
208 lines (182 loc) · 5.41 KB
/
websocket.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
#include "websocket.h"
noPollConn* websocket_conn = NULL;
int websocket_init( const char* hoststr, const char* portstr ) {
int ret_code = 0;
noPollCtx * ctx = nopoll_ctx_new ();
if (! ctx) {
fprintf(stderr, "ERROR: Could not create noPoll websocket context\n");
ret_code = 1;
goto cleanup;
}
//nopoll_log_enable(ctx, nopoll_true);
// call to create a connection
websocket_conn = nopoll_conn_new (ctx, hoststr, portstr, NULL, "/ws", NULL, NULL);
if (! nopoll_conn_is_ok (websocket_conn)) {
fprintf(stderr, "ERROR: Could not connect to %s:%s\n", hoststr, portstr);
ret_code = 1;
goto cleanup;
}
if (! nopoll_conn_wait_until_connection_ready (websocket_conn, 5)) {
fprintf(stderr, "ERROR: Timed out waiting for connection to become ready\n");
ret_code = 1;
goto cleanup;
}
cleanup:
if (ctx) nopoll_ctx_unref (ctx);
return ret_code;
}
int websocket_write( const char* cmdstr ) {
int bytes_written = 0;
int cmd_length = 0;
// do write operation
fprintf(stderr, "Sending websocket cmd: %s\n", cmdstr);
cmd_length = strlen( cmdstr );
bytes_written = nopoll_conn_send_text (websocket_conn, cmdstr, cmd_length);
// complete pending write by flushing and limitting operation for 2 seconds
bytes_written = nopoll_conn_flush_writes (websocket_conn, 2000000, bytes_written);
return bytes_written;
}
int websocket_send_cmds( Queue* queue ) {
int num_sent = 0;
unsigned int bytes_written = 0;
char cmd[MAX_CMD_LENGTH];
if (queue->size == 0)
return 0;
else {
while (queue->size > 0) {
queue->pop( queue, cmd );
bytes_written = websocket_write( cmd );
if (bytes_written != strlen(cmd)) {
fprintf(stderr, "ERROR: Only sent %d bytes of command: %s\n", bytes_written, cmd);
} else {
fprintf(stderr, " + Successfully sent cmd: %s\n", cmd);
num_sent++;
}
}
}
fprintf(stderr, "Sent %d commands\n", num_sent);
return num_sent;
}
int http_send_cmds( Queue* queue ) {
int num_sent = 0;
char cmd[MAX_CMD_LENGTH];
if (queue->size == 0)
return 0;
else {
while (queue->size > 0) {
queue->pop( queue, cmd );
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
// Set the URL for the http GET
curl_easy_setopt(curl, CURLOPT_URL, cmd);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s %s\n",
curl_easy_strerror(res), cmd);
} else {
fprintf(stderr, " + Successfully sent cmd: %s\n", cmd);
num_sent++;
}
/* always cleanup */
curl_easy_cleanup(curl);
} else {
fprintf(stderr, "Failed to establish curl instance\n");
}
}
}
fprintf(stderr, "Sent %d commands\n", num_sent);
return num_sent;
}
/**
* Push an item into queue, if this is the first item,
* both queue->head and queue->tail will point to it,
* otherwise the oldtail->next and tail will point to it.
*/
void push (Queue* queue, const char cmd[MAX_CMD_LENGTH]) {
// Create a new cmd
Node* n = (Node*) malloc (sizeof(Node));
strncpy( n->cmd, cmd, MAX_CMD_LENGTH );
n->next = NULL;
if (queue->head == NULL) { // no head
queue->head = n;
} else{
queue->tail->next = n;
}
queue->tail = n;
queue->size++;
}
/**
* Return and remove the first cmd.
*/
int pop (Queue* queue, char cmd[MAX_CMD_LENGTH]) {
if (queue->head == NULL) return -1;
// get the first cmd
Node* head = queue->head;
strncpy( cmd, head->cmd, MAX_CMD_LENGTH );
// move head pointer to next cmd, decrease size
queue->head = head->next;
queue->size--;
// free the memory of original head
free(head);
return 0;
}
/**
* Empty the queue.
*/
int clear (Queue* queue) {
char cmd[MAX_CMD_LENGTH];
if (queue->head == NULL) return -1;
while (queue->size > 0) {
queue->pop( queue, cmd );
}
return 0;
}
/**
* Return but not remove the first item.
*/
int peek (Queue* queue, char cmd[MAX_CMD_LENGTH]) {
if (queue->head == NULL) return -1;
Node* head = queue->head;
strncpy( cmd, head->cmd, MAX_CMD_LENGTH );
return 0;
}
/**
* Show all items in queue.
*/
void display (Queue* queue) {
printf("\nDisplay: ");
// no item
if (queue->size == 0)
printf("No item in queue.\n");
else { // has item(s)
Node* head = queue->head;
int i, size = queue->size;
printf("%d item(s):\n", queue->size);
for (i = 0; i < size; i++) {
//if (i > 0)
//printf(", ");
printf("%s", head->cmd);
head = head->next;
}
}
printf("\n\n");
}
/**
* Create and initiate a Queue
*/
Queue createQueue () {
Queue queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.push = &push;
queue.pop = &pop;
queue.clear = &clear;
queue.peek = &peek;
queue.display = &display;
return queue;
}