aboutsummaryrefslogtreecommitdiffstats
path: root/echld_test.c
diff options
context:
space:
mode:
authorLuis Ontanon <luis.ontanon@gmail.com>2013-07-01 01:49:34 +0000
committerLuis Ontanon <luis.ontanon@gmail.com>2013-07-01 01:49:34 +0000
commitbaee2e21f7020c77f41001bd759f8d41063eba3f (patch)
treed192ec49151c48a6862b1694b0aefba14fb8b158 /echld_test.c
parente03edbf713c9af37b452e1c3f1917f1e2c69ca72 (diff)
ANother iteration,
- started harvesting pieces from tshark. - fixed? signal handlers - interactive test program very hard to debug... set follow-on-fork doesn't seem to work on my mac. Need linux I guess... svn path=/trunk/; revision=50273
Diffstat (limited to 'echld_test.c')
-rw-r--r--echld_test.c264
1 files changed, 168 insertions, 96 deletions
diff --git a/echld_test.c b/echld_test.c
index dd0a6407c3..3829518789 100644
--- a/echld_test.c
+++ b/echld_test.c
@@ -44,6 +44,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
@@ -54,143 +55,214 @@
#include "epan/epan.h"
#include "wsutil/str_util.h"
-int pings = 0;
-int errors = 0;
+typedef char* (*cmd_cb_t)(char** params, char** err);
-void ping_cb(long usec, void* data _U_) {
+typedef struct _cmd_t {
+ const char* txt;
+ cmd_cb_t cb;
+ int args_taken;
+ const char* help;
+} cmd_t;
- fprintf(stderr, "Ping ping=%d usec=%d\n", pings ,(int)usec );
+static char* new_child_cmd(char** params _U_, char** err) {
+ int child = echld_new(NULL);
- if (usec >= 0) {
- pings++;
- } else {
- errors++;
+ if (child <= 0) {
+ *err = g_strdup("No child\n");
+ return NULL;
}
+
+ return g_strdup_printf("New chld_id=%d\n",child);;
}
-int got_param = 0;
-void param_cb(const char* param, const char* value, const char* error, void* data _U_) {
- if (error) {
- fprintf(stderr, "Param Set Error msg=%s\n", error );
- got_param = 0;
- return;
+void ping_cb(long usec, void* data) {
+ int ping_id = *((int*)data);
+
+ if (usec >= 0) {
+ fprintf(stdout, "Ping ping_id=%d returned in %dus\n",ping_id,(int)usec);
+ } else {
+ fprintf(stdout, "Ping ping_id=%d erored\n",ping_id);
}
- got_param = 1;
- fprintf(stderr, "Param: param='%s' val='%s'\n", param, value );
+ g_free(data);
}
-int main(int argc _U_, char** argv _U_) {
- struct timeval tv;
- int tot_cycles = 0;
- int max_cycles = 50;
- int child = -1;
- int keep_going = 1;
+static char* ping_cmd(char** params, char** err) {
+ int child = (int) strtol(params[1], NULL, 10);
+ static int ping_id = 0;
+ int* ping_data = g_new(int,1);
- tv.tv_sec = 0;
- tv.tv_usec = 250000;
+ *ping_data = ping_id++;
- echld_set_parent_dbg_level(5);
+ if (!echld_ping(child,ping_cb,ping_data)) {
+ *err = g_strdup_printf("Could not send ping child=%d",child);
+ return NULL;
+ } else {
+ return g_strdup_printf("Ping sent child=%d",child);
+ }
+}
- echld_initialize(ECHLD_ENCODING_JSON,argv[0],main);
+void param_cb(const char* param, const char* value, const char* error, void* data _U_) {
+ if (error) {
+ fprintf(stdout, "Param Set Error msg=%s\n", error );
+ } else {
+ fprintf(stdout, "Param: param='%s' val='%s'\n", param, value );
+ }
+}
- do {
- if ( tot_cycles == 2 ) echld_ping(0,ping_cb,NULL);
+static char* set_cmd(char** params, char** err) {
+ int child = (int) strtol(params[1], NULL, 10);
+ char* param = params[2];
+ char* value = params[3];
- if ( tot_cycles == 4 ) {
- if (pings!=1) {
- fprintf(stderr, "No Dispatcher PONG 0\n");
- break;
- }
- echld_ping(0,ping_cb,NULL);
- }
+ if ( ! echld_set_param(child,param,value,param_cb,NULL) ) {
+ *err = g_strdup_printf("Failed to SET child=%d param='%s' value='%s'",child,param,value);
+ return NULL;
+ } else {
+ return g_strdup_printf("Set command sent child=%d param='%s' value='%s'",child,param,value);
+ }
+}
- if ( tot_cycles == 6 ) {
- if (pings!=2) {
- fprintf(stderr, "No Dispatcher PONG 1\n");
- break;
- }
+static char* get_cmd(char** params, char** err) {
+ int child = (int) strtol(params[1], NULL, 10);
+ char* param = params[2];
- echld_set_param(0,"dbg_level","5",param_cb,NULL);
- }
+ if ( ! echld_get_param(child,param,param_cb,NULL) ) {
+ *err = g_strdup_printf("Failed to GET child=%d param='%s'",child,param);
+ return NULL;
+ } else {
+ return g_strdup_printf("Get command sent child=%d param='%s'",child,param);
+ }
+}
- if ( tot_cycles == 8) {
- if (! got_param ) {
- fprintf(stderr, "Set Dispatcher Param Failed\n");
- break;
- }
+static void close_cb(const char* error, void* data) {
+ if (error) {
+ fprintf(stdout, "Close Error msg=%s\n", error );
+ } else {
+ fprintf(stdout, "Closed: child=%d\n", *((int*)data) );
+ }
+}
- echld_get_param(0,"interfaces",param_cb,NULL);
- }
+static char* close_cmd(char** params, char** err) {
+ int child = (int) strtol(params[1], NULL, 10);
+ int* cmdp = g_new(int,1);
+ *cmdp = child;
+ if ( ! echld_close(child,close_cb,cmdp) ) {
+ *err = g_strdup_printf("Could not close child=%d",child);
+ return NULL;
+ } else {
+ return g_strdup_printf("CLose command sent child=%d",child);
+ }
+}
+int keep_going = 1;
- if ( tot_cycles == 10) {
- if (! got_param ) {
- fprintf(stderr, "Set Dispatcher Param Failed\n");
- break;
- }
+static char* quit_cmd(char** params _U_, char** err _U_) {
+ keep_going = 0;
+ return g_strdup("Quitting");
+}
- child = echld_new(NULL);
-
- fprintf(stderr, "New chld_id=%d\n",child);
+static char* help_cmd(char**, char**);
- if (child <= 0) {
- fprintf(stderr, "No child\n");
- break;
- }
- }
+static char* nothing_at_last_cmd(char** pars _U_, char** err _U_) {
+ return g_strdup("");
+}
- if ( tot_cycles == 16 ) echld_ping(child,ping_cb,NULL);
+cmd_t commands[] = {
+ { "QUIT", quit_cmd, 0, "QUIT"},
+ { "HELP", help_cmd, 0, "HELP"},
+ { "NEW", new_child_cmd, 0, "NEW"},
+ { "PING", ping_cmd, 1, "PING child_id"},
+ { "SET", set_cmd, 3, "SET child_id param_name param_val"},
+ { "GET", get_cmd, 2, "GET child_id param_name"},
+ { "CLOSE", close_cmd, 1, "CLOSE child_id"},
+ { "", nothing_at_last_cmd,0,""},
+ { NULL, NULL, 0, NULL }
+};
+
+static char* help_cmd(char** params _U_, char** err _U_) {
+ GString* out = g_string_new("Commands:\n");
+ cmd_t* c = commands;
+ char* s;
+
+ for (;c->txt;c++) {
+ g_string_append_printf(out,"%s\n",c->help);
+ }
+ s = out->str;
+ g_string_free(out,FALSE);
+ return s;
+}
- if ( tot_cycles == 18 ) {
- if (pings!=3) {
- fprintf(stderr, "No Child PONG 0\n");
- break;
- }
- echld_ping(child,ping_cb,NULL);
- }
- if ( tot_cycles == 20 ) {
- if (pings!=4) {
- fprintf(stderr, "No Child PONG 1\n");
- break;
- }
- echld_set_param(child,"cookie","hello-hello",param_cb,NULL);
- }
+int got_param = 0;
- if ( tot_cycles == 22 ) {
- if (!got_param) {
- fprintf(stderr, "Set Child Param Failed\n");
- break;
- }
+int main(int argc _U_, char** argv _U_) {
+ struct timeval tv;
+ int tot_cycles = 0;
- echld_get_param(child,"dbg_level",param_cb,NULL);
- }
+ tv.tv_sec = 5;
+ tv.tv_usec = 0;
- if ( tot_cycles == 24 ) {
- if (!got_param) {
- fprintf(stderr, "Get Child Param Failed\n");
- break;
- }
+ echld_set_parent_dbg_level(5);
+
+ echld_initialize(ECHLD_ENCODING_JSON,argv[0],main);
- keep_going = 0;
+ do {
+ fd_set rfds;
+ fd_set efds;
+ int nfds;
+
+ FD_ZERO(&rfds);
+ FD_ZERO(&efds);
+ FD_SET(0,&rfds);
+ FD_SET(0,&efds);
+
+ nfds = echld_select(FD_SETSIZE, &rfds, NULL, &efds, &tv);
+
+ if (FD_ISSET(0,&rfds)) {
+ size_t len;
+ char* cmd_line;
+
+ if(( cmd_line = fgetln(stdin,&len) )) {
+ cmd_t* c = commands;
+ cmd_line[len] = 0;
+ g_strchomp(cmd_line);
+
+ for (;c->txt;c++) {
+ if ( strcasestr(cmd_line, c->txt) == cmd_line ) {
+ char** params = g_strsplit(cmd_line, " ", c->args_taken+1);
+ char* err = NULL;
+ char* str = c->cb(params,&err);
+
+ if (err) {
+ fprintf(stdout, "Error: %s\n", err);
+ g_free(err);
+ } else {
+ fprintf(stdout, "%s\n", str);
+ g_free(str);
+ }
+
+ g_strfreev(params);
+ goto cmd_executed;
+ }
+ }
+
+ fprintf(stdout, "Error: no such command %s\n", cmd_line);
+ }
}
+ cmd_executed:
tot_cycles++;
- echld_wait(&tv);
- } while( keep_going && (tot_cycles < max_cycles));
+ } while( keep_going );
- fprintf(stderr, "Done: pings=%d errors=%d tot_cycles=%d\n", pings, errors ,tot_cycles );
+ fprintf(stderr, "Done: tot_cycles=%d\n", tot_cycles );
echld_terminate();
return 0;
}
-
-
-void main_window_update(void) {}