-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathwhiteboard.c
187 lines (148 loc) · 5.4 KB
/
whiteboard.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "../endpoint_impl.h"
typedef struct {
char *event_name;
char *event_args;
} event_message;
static char *event_message_reg = "{\"name\":\"(.*?)\",\"args\":\\[([^\\]]*?)\\]}";
static gchar *get_match_result(GMatchInfo *match_info, gint index) {
gchar *match = g_match_info_fetch(match_info, index);
gchar *result = g_strdup(match);
g_free(match);
return result;
}
static void *message_2_struct(gchar *post_string, event_message *event_msg) {
GError *error = NULL;
GRegex *regex;
GMatchInfo *match_info;
regex = g_regex_new(event_message_reg, 0, 0, &error );
g_regex_match( regex, post_string, 0, &match_info );
if (g_match_info_matches(match_info)) {
event_msg->event_name = get_match_result(match_info, 1);
event_msg->event_args = get_match_result(match_info, 2);
} else {
event_msg = NULL;
}
g_match_info_free( match_info );
g_regex_unref( regex );
return event_msg;
}
static GHashTable *hashtable;
static void hashtable_init(void) {
hashtable = g_hash_table_new(g_str_hash, g_str_equal);
}
static void hashtable_add(const char *key, void *value) {
if (key) {
g_hash_table_insert(hashtable, g_strdup(key), value);
}
}
static gboolean hashtable_remove(const char *key) {
if (key)
return g_hash_table_remove(hashtable, key);
return 0;
}
static void *hashtable_lookup(const char *key) {
if (key == NULL)
return NULL;
return g_hash_table_lookup(hashtable, key);
}
/*static gint hashtable_size(void) {
return g_hash_table_size(hashtable);
}*/
static void hashtable_destroy(void) {
g_hash_table_destroy(hashtable);
}
/**
** use the struct to warpper the demo implment
**/
static char *endpoint_name;
static void on_init(const char *endpoint) {
hashtable_init();
log_debug("%s has been inited now", endpoint);
endpoint_name = g_strdup(endpoint);
}
static void on_connect(const char *sessionid) {
char messages[strlen(sessionid) + 50];
sprintf(messages, "5::%s:{\"name\":\"clientId\",\"args\":[{\"id\":\"%s\"}]}", endpoint_name, sessionid);
send_msg(sessionid, messages);
}
static void send_it(char *session_id, char *messaage) {
send_msg(session_id, messaage);
}
static void free_event_msg(event_message *event_msg) {
free(event_msg->event_name);
free(event_msg->event_args);
}
static void on_event(const char *sessionid, const message_fields *msg_fields) {
event_message event_msg;
if (!message_2_struct(msg_fields->message_data, &event_msg)) {
log_error("%s Parse Message Error !", msg_fields->ori_data);
return;
}
if (!strcmp(event_msg.event_name, "roomNotice")) {
/*5::/whiteboard:{"name":"roomNotice","args":[{"room":"myRoom"}]}*/
char target_room_id[strlen(event_msg.event_args) - 10];// = event_msg.event_args + 9;
strncpy(target_room_id, event_msg.event_args + 9, strlen(event_msg.event_args) - 11);
GPtrArray *list = (GPtrArray *)hashtable_lookup(target_room_id);
if (list == NULL) {
list = g_ptr_array_new();
hashtable_add(target_room_id, list);
}
g_ptr_array_add(list, g_strdup(sessionid));
int room_count = list->len;
char messages[strlen(sessionid) + 200];
sprintf(messages, "5::%s:{\"name\":\"roomCount\",\"args\":[{\"room\":\"%s\",\"num\":%d}]}", endpoint_name, target_room_id, room_count);
hashtable_add(g_strdup(sessionid), g_strdup(target_room_id));
g_ptr_array_foreach(list, (GFunc)send_it, messages);
free_event_msg(&event_msg);
return;
}
char messages[strlen(msg_fields->ori_data) + 200];
sprintf(messages, "5::%s:{\"name\":\"%s\",\"args\":[%s]}", endpoint_name, event_msg.event_name, event_msg.event_args);
free_event_msg(&event_msg);
char *target_room_id = (char *)hashtable_lookup(sessionid);
GPtrArray *list = (GPtrArray *)hashtable_lookup(target_room_id);
if(list == NULL){
return;
}
int i;
for (i = 0; i < list->len; i++) {
char *session_id = g_ptr_array_index(list, i);
if (strcmp(session_id, sessionid) == 0)
continue;
send_msg(session_id, messages);
}
}
static void on_disconnect(const char *sessionid, const message_fields *msg_fields) {
char *room_id = (char *)hashtable_lookup(sessionid);
if (room_id == NULL) {
log_error("the room_id is NULL\n");
return;
}
char notice_msg[strlen(endpoint_name) + strlen(room_id) + 70];
GPtrArray *list = (GPtrArray *)hashtable_lookup(room_id);
sprintf(notice_msg, "5::%s:{\"name\":\"roomCount\",\"args\":[{\"room\":\"%s\",\"num\":%d}]}", endpoint_name, room_id, list->len - 1);
int i, remove_index;
for (i = 0; i < list->len; i++) {
char *session_id = g_ptr_array_index(list, i);
if (strcmp(session_id, sessionid) == 0) {
remove_index = i;
continue;
}
send_msg(session_id, notice_msg);
}
g_ptr_array_remove_index(list, remove_index);
hashtable_remove(sessionid);
free(room_id);
}
static void on_destroy(const char *endpoint) {
log_info("%s has been destroy now", endpoint);
hashtable_destroy();
free(endpoint_name);
}
extern endpoint_implement *init_whiteboard_endpoint_implement(char *endpoint_name) {
return init_default_endpoint_implement(endpoint_name);
}