refactoring req interface
[src/app-framework-binder.git] / plugins / samples / HelloWorld.c
index c60cc70..946f4b0 100644 (file)
 #include "afb-plugin.h"
 #include "afb-req-itf.h"
 
-typedef struct queryHandleT {
-     char    *msg;
-     size_t  idx;
-     size_t  len;
-} queryHandleT;
-
-static int getQueryCB (queryHandleT *query, struct afb_arg arg) {
-    if (query->idx >= query->len)
-       return 0;
-    query->idx += (unsigned)snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value);
-    return 1; /* continue to iterate */
-}
-
-// Helper to retrieve argument from  connection
-static size_t getQueryAll(struct afb_req request, char *buffer, size_t len) {
-    queryHandleT query;
-    buffer[0] = '\0'; // start with an empty string
-    query.msg = buffer;
-    query.len = len;
-    query.idx = 0;
-
-    afb_req_iterate(request, (void*)getQueryCB, &query);
-    buffer[len-1] = 0;
-    return query.idx >= len ? len - 1 : query.idx;
-}
-
-static void ping (struct afb_req request, json_object *jresp)
+// Sample Generic Ping Debug API
+static void ping(struct afb_req request, json_object *jresp, const char *tag)
 {
     static int pingcount = 0;
-    char query [512];
-    size_t len;
+    json_object *query = afb_req_json(request);
 
-    // request all query key/value
-    len = getQueryAll (request, query, sizeof(query));
-    if (len == 0) strcpy (query,"NoSearchQueryList");
-    
-    // return response to caller
-//    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
-    afb_req_success_f(request, jresp, "Ping Binder Daemon %d query={%s}", pingcount++, query);
-    
-    fprintf(stderr, "%d: \n", pingcount);
+    afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
 }
 
 static void pingSample (struct afb_req request)
 {
-       ping(request, json_object_new_string ("Some String"));
+       ping(request, json_object_new_string ("Some String"), "pingSample");
 }
 
 static void pingFail (struct afb_req request)
@@ -78,17 +44,12 @@ static void pingFail (struct afb_req request)
 
 static void pingNull (struct afb_req request)
 {
-       ping(request, NULL);
+       ping(request, NULL, "pingNull");
 }
 
 static void pingBug (struct afb_req request)
 {
-    int a,b,c;
-    
-    b=4;
-    c=0;
-    a=b/c;
-    
+       ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug");
 }
 
 
@@ -106,7 +67,7 @@ static void pingJson (struct afb_req request) {
     
     json_object_object_add(jresp,"eobj", embed);
 
-    ping(request, jresp);
+    ping(request, jresp, "pingJson");
 }
 
 // NOTE: this sample does not use session to keep test a basic as possible