aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libraries/SledjHamr.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2016-02-05 16:38:58 +1000
committerDavid Walter Seikel2016-02-05 16:38:58 +1000
commit76faf8a71ca29aef0de066fb3a31823227b6d95d (patch)
treee7bd6bf9ddb63569c4b98e1679631fa1103d0300 /src/libraries/SledjHamr.c
parentAuthors++ (diff)
downloadSledjHamr-76faf8a71ca29aef0de066fb3a31823227b6d95d.zip
SledjHamr-76faf8a71ca29aef0de066fb3a31823227b6d95d.tar.gz
SledjHamr-76faf8a71ca29aef0de066fb3a31823227b6d95d.tar.bz2
SledjHamr-76faf8a71ca29aef0de066fb3a31823227b6d95d.tar.xz
The next step in cleaning up the network Lua messages.
Diffstat (limited to '')
-rw-r--r--src/libraries/SledjHamr.c211
1 files changed, 128 insertions, 83 deletions
diff --git a/src/libraries/SledjHamr.c b/src/libraries/SledjHamr.c
index b44c1cb..7092c10 100644
--- a/src/libraries/SledjHamr.c
+++ b/src/libraries/SledjHamr.c
@@ -88,78 +88,41 @@ static boolean checkConnection(Connection *conn, char *func, connType wanted, bo
88 return result; 88 return result;
89} 89}
90 90
91void send2(Connection *conn, const char *SID, const char *message, ...)
92{
93 va_list args;
94 char buf[PATH_MAX * 2];
95 int length = strlen(SID);
96
97 strncpy(buf, SID, length);
98 buf[length++] = '.';
99 va_start(args, message);
100 length += vsprintf(&buf[length], message, args);
101 va_end(args);
102 buf[length++] = '\n';
103 buf[length] = '\0';
104 91
105// TODO - Should check if this is always gonna be local? Likely not. 92typedef boolean (* notFoundCb)(Connection *conn, char *command, int len);
106// TODO - Figure out what the above line meant. Then ...
107// Check a hashtable somewhere for a command extracted from the message.
108// conn->commands[command](message ...)
109 if (checkConnection(conn, "send2", conn->type, FALSE))
110 {
111 switch (conn->type)
112 {
113 case CT_CLIENT :
114// PD("vvv send2(%*s", length, buf);
115 ecore_con_client_send(conn->conn.client.client, strndup(buf, length), length);
116 ecore_con_client_flush(conn->conn.client.client);
117 break;
118
119 case CT_SERVER :
120// PD("^^^ send2(%*s", length, buf);
121 ecore_con_server_send(conn->conn.server.server, strndup(buf, length), length);
122 ecore_con_server_flush(conn->conn.server.server);
123 break;
124
125 default :
126 PE("send2() unable to send to partially bogus Connection object!");
127 break;
128 }
129 }
130 else
131 PE("send2() unable to send to bogus Connection object!");
132}
133 93
134static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, void *ev) 94/* TODO -
95 It should loop through all lines, looking for the closing ) before dealing with it.
96 If we get to the end, and there's left over open ( or something, then return the length.
97 Bitch and move on if it's not well formed.
98*/
99static int _checkForCommand(Connection *conn, char *commands, notFoundCb notFound, boolean in)
135{ 100{
136 Connection *conn = data; 101 int result = 0;
137 char SID[PATH_MAX]; 102 boolean handled = FALSE;
138 const char *command; 103 char *ext, *ext2;
139 char *ext;
140 104
141 if (NULL == conn->stream) 105 while ((ext = index(commands, '\n')))
142 conn->stream = eina_strbuf_new(); 106 {
107 char SID[PATH_MAX * 3];
108 int length = ext - commands;
143 109
144 eina_strbuf_append_length(conn->stream, evData, evSize); 110 strncpy(SID, commands, length + 1);
145 command = eina_strbuf_string_get(conn->stream); 111
146 while ((ext = index(command, '\n'))) 112 SID[length] = '\0';
113
114 ext = index(SID, '.');
115 if (ext)
147 { 116 {
148 int length = ext - command; 117 char *command = ext + 1;
149 118
150 strncpy(SID, command, length + 1); 119 ext[0] = '\0';
151 SID[length] = '\0'; 120 ext2 = index(command, '(');
152 eina_strbuf_remove(conn->stream, 0, length + 1); 121 if (NULL == ext2)
153 ext = index(SID, '.'); 122 ext2 = index(command, ' ');
154 if (ext) 123 if (ext2)
155 { 124 {
156 ext[0] = '\0'; 125 ext2[0] = '\0';
157 command = ext + 1;
158 ext = index(command, '(');
159 if (NULL == ext)
160 ext = index(command, ' ');
161 if (ext)
162 {
163 // TODO - Currently SID.command(arguments), should change that to nameSpace.command(arguments) 126 // TODO - Currently SID.command(arguments), should change that to nameSpace.command(arguments)
164 // Not sure what to do with SID, but there maybe some common parameters we can shift around differently. 127 // Not sure what to do with SID, but there maybe some common parameters we can shift around differently.
165 // - First check if the connection has a hashtable of conn->commands. 128 // - First check if the connection has a hashtable of conn->commands.
@@ -171,22 +134,101 @@ static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, voi
171 // Finally pass it to conn->unknownCommand() 134 // Finally pass it to conn->unknownCommand()
172 // * The Lua check can live in unknownCommand(). 135 // * The Lua check can live in unknownCommand().
173 // else bitch. 136 // else bitch.
174 streamParser func = eina_hash_find(conn->commands, command); 137
175 138 // Check if it's in the connections hash table.
176 ext[0] = '\0'; 139 streamParser func = eina_hash_find(conn->commands, command);
177 // Need a callback if we can't find the command. 140
178 if (NULL == func) 141 if (NULL == func)
179 func = conn->unknownCommand; 142 {
180 if (func) 143 // Check if the connection has a function for handling it.
181 func(conn->pointer, conn, SID, (char *) command, ext + 1); 144 if (in)
182 else 145 func = conn->unknownInCommand;
183 PE("parseStream() No function found for command %s!", command); 146 else
184 } 147 func = conn->unknownOutCommand;
148 }
149
150 // Try it out if we have a function.
151 if (func)
152 {
153 handled = func(conn->pointer, conn, SID, command, ext2 + 1);
154 if (handled)
155 notFound = NULL;
185 } 156 }
186 157
187 // Get the next blob to check it. 158 // Last resort, let the caller deal with it.
188 command = eina_strbuf_string_get(conn->stream); 159 if (notFound)
160 handled = notFound(conn, commands, length + 1);
161 if (!handled)
162 PE("No function found for command %s(%s!", command, ext2 + 1);
163
164 result += length;
165 }
166 }
167
168 commands = &commands[length + 1];
169 }
170
171 return result;
172}
173
174static boolean _actuallySendIt(Connection *conn, char *command, int len)
175{
176 if (checkConnection(conn, "send2", conn->type, FALSE))
177 {
178 switch (conn->type)
179 {
180 case CT_CLIENT :
181// PD("vvv send2(%*s", len, command);
182 ecore_con_client_send(conn->conn.client.client, strndup(command, len), len);
183 ecore_con_client_flush(conn->conn.client.client);
184 return TRUE;
185
186 case CT_SERVER :
187// PD("^^^ send2(%*s", len, command);
188 ecore_con_server_send(conn->conn.server.server, strndup(command, len), len);
189 ecore_con_server_flush(conn->conn.server.server);
190 return TRUE;
191
192 default :
193 PE("send2() unable to send to partially bogus Connection object!");
194 break;
195 }
189 } 196 }
197 else
198 PE("send2() unable to send to bogus Connection object!");
199
200 return FALSE;
201}
202
203void send2(Connection *conn, const char *SID, const char *message, ...)
204{
205 va_list args;
206 char buf[PATH_MAX * 3];
207 int length = strlen(SID);
208
209 strncpy(buf, SID, length);
210 buf[length++] = '.';
211 va_start(args, message);
212 length += vsprintf(&buf[length], message, args);
213 va_end(args);
214 buf[length++] = '\n';
215 buf[length] = '\0';
216
217//PD("%s", (char *) buf);
218// TODO - Do we care about the returned length? Either the caller sent us proper commands, or they didn't.
219 _checkForCommand(conn, buf, _actuallySendIt, FALSE);
220}
221
222static Eina_Bool parseStream(void *data, int type, void *evData, int evSize, void *ev)
223{
224 Connection *conn = data;
225
226 if (NULL == conn->stream)
227 conn->stream = eina_strbuf_new();
228
229 eina_strbuf_append_length(conn->stream, evData, evSize);
230//PD("%s", (char *) eina_strbuf_string_get(conn->stream));
231 eina_strbuf_remove(conn->stream, 0, _checkForCommand(conn, (char *) eina_strbuf_string_get(conn->stream), NULL, TRUE));
190 232
191 if (conn->_data) 233 if (conn->_data)
192 conn->_data(conn->pointer, type, ev); 234 conn->_data(conn->pointer, type, ev);
@@ -234,7 +276,8 @@ static Eina_Bool clientAdd(void *data, int type, Ecore_Con_Event_Client_Add *ev)
234 conn->_add = connection->_add; 276 conn->_add = connection->_add;
235 conn->_data = connection->_data; 277 conn->_data = connection->_data;
236 conn->_del = connection->_del; 278 conn->_del = connection->_del;
237 conn->unknownCommand = connection->unknownCommand; 279 conn->unknownInCommand = connection->unknownInCommand;
280 conn->unknownOutCommand = connection->unknownOutCommand;
238 conn->commands = eina_hash_string_superfast_new(NULL); 281 conn->commands = eina_hash_string_superfast_new(NULL);
239 ecore_con_client_data_set(ev->client, conn); 282 ecore_con_client_data_set(ev->client, conn);
240 283
@@ -284,7 +327,7 @@ static Eina_Bool clientDel(void *data, int type, Ecore_Con_Event_Client_Del *ev)
284 return ECORE_CALLBACK_RENEW; 327 return ECORE_CALLBACK_RENEW;
285} 328}
286 329
287Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser) 330Connection *openArms(char *name, const char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser)
288{ 331{
289 Connection *conn = calloc(1, sizeof(Connection)); 332 Connection *conn = calloc(1, sizeof(Connection));
290 Ecore_Con_Server *server; 333 Ecore_Con_Server *server;
@@ -299,7 +342,8 @@ Connection *openArms(char *name, const char *address, int port, void *data, Ecor
299 conn->_add = _add; 342 conn->_add = _add;
300 conn->_data = _data; 343 conn->_data = _data;
301 conn->_del = _del; 344 conn->_del = _del;
302 conn->unknownCommand = _parser; 345 conn->unknownInCommand = _inParser;
346 conn->unknownOutCommand = _outParser;
303 conn->commands = eina_hash_string_superfast_new(NULL); 347 conn->commands = eina_hash_string_superfast_new(NULL);
304 348
305 conn->add = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb) clientAdd, conn); 349 conn->add = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb) clientAdd, conn);
@@ -449,7 +493,7 @@ static Eina_Bool _reachOutTimer(void *data)
449 return result; 493 return result;
450} 494}
451 495
452Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _parser) 496Connection *reachOut(char *name, char *command, char *address, int port, void *data, Ecore_Event_Handler_Cb _add, Ecore_Event_Handler_Cb _data, Ecore_Event_Handler_Cb _del, streamParser _inParser, streamParser _outParser)
453{ 497{
454 Connection *conn = calloc(1, sizeof(Connection)); 498 Connection *conn = calloc(1, sizeof(Connection));
455 499
@@ -464,7 +508,8 @@ Connection *reachOut(char *name, char *command, char *address, int port, void *d
464 conn->_add = _add; 508 conn->_add = _add;
465 conn->_data = _data; 509 conn->_data = _data;
466 conn->_del = _del; 510 conn->_del = _del;
467 conn->unknownCommand = _parser; 511 conn->unknownInCommand = _inParser;
512 conn->unknownOutCommand = _outParser;
468 conn->commands = eina_hash_string_superfast_new(NULL); 513 conn->commands = eina_hash_string_superfast_new(NULL);
469 514
470 conn->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) serverAdd, conn); 515 conn->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) serverAdd, conn);