---
 plugins/duda/duda_api.c      |   26 +++++++++
 plugins/duda/duda_api.h      |   11 ++++
 plugins/duda/example/hello.c |  117 ++++++++++++++++++++++++++++++++++++++---
 plugins/duda/webservice.c    |    2 +-
 plugins/duda/webservice.h    |    2 +
 5 files changed, 148 insertions(+), 10 deletions(-)

diff --git a/plugins/duda/duda_api.c b/plugins/duda/duda_api.c
index 28aece8..efe86f0 100644
--- a/plugins/duda/duda_api.c
+++ b/plugins/duda/duda_api.c
@@ -123,6 +123,25 @@ int _sendfile_enqueue(duda_request_t *dr, char *path)
     return 0;
 }
 
+/* Return ith parameter */
+char * duda_param_get(duda_request_t *dr, short int i)
+{
+    if(i >= dr->n_params){
+        return NULL;
+    }
+
+    return mk_api->str_copy_substr(dr->params[i].data,0,(int)dr->params[i].len);
+}
+
+/* Return the total no of parameters */
+short int duda_param_count(duda_request_t *dr)
+{
+    if(!dr){
+        return -1;
+    }
+    return dr->n_params;
+}
+
 /* Finalize the response process */
 int _end_response(duda_request_t *dr, void (*end_cb) (duda_request_t *))
 {
@@ -152,6 +171,7 @@ struct duda_api_objects *duda_api_master()
     objs->response = mk_api->mem_alloc(sizeof(struct duda_api_response));
     objs->debug    = mk_api->mem_alloc(sizeof(struct duda_api_debug));
     objs->global   = mk_api->mem_alloc(sizeof(struct duda_api_global));
+    objs->params   = mk_api->mem_alloc(sizeof(struct duda_api_params));
 
     /* MAP Duda calls */
     objs->duda->package_load = duda_package_load;
@@ -176,6 +196,12 @@ struct duda_api_objects *duda_api_master()
     objs->response->sendfile    = _sendfile_enqueue;
     objs->response->end = _end_response;
 
+    /* DEBUG object */
+
+    /* PARAMS object */
+    objs->params->count = duda_param_count;
+    objs->params->get = duda_param_get;
+
     /* Global data (thread scope) */
     objs->global->set  = duda_global_set;
     objs->global->get  = duda_global_get;
diff --git a/plugins/duda/duda_api.h b/plugins/duda/duda_api.h
index 805b0df..7766838 100644
--- a/plugins/duda/duda_api.h
+++ b/plugins/duda/duda_api.h
@@ -109,6 +109,11 @@ struct duda_param {
  * Debug
  * -----
  * A set of methods to debug the web service
+ * 
+ * 
+ * Params
+ * ------
+ * A set of methods to retrieve web service parameters
  *
  */
 
@@ -158,6 +163,11 @@ struct duda_api_debug {
     void (*stacktrace) (void);
 };
 
+/* PARAMS object: params->() */
+struct duda_api_params{
+    char *(*get) (duda_request_t *,short int);
+    short int (*count) (duda_request_t *);
+};
 
 /* Global data (thread scope) */
 struct duda_api_global {
@@ -179,6 +189,7 @@ struct duda_api_objects {
     struct duda_api_response *response;
     struct duda_api_debug *debug;
     struct duda_api_global *global;
+    struct duda_api_params *params;
 };
 
 struct duda_api_objects *duda_api_master();
diff --git a/plugins/duda/example/hello.c b/plugins/duda/example/hello.c
index f83af43..eaaea6c 100644
--- a/plugins/duda/example/hello.c
+++ b/plugins/duda/example/hello.c
@@ -3,8 +3,9 @@
 #include "webservice.h"
 #include "packages/json/json.h"
 
-#define FORMATTED "== Formatted JSON output ==\n"
-#define UNFORMATTED "\n\n== Unformatted JSON output ==\n"
+#define INCORRECT_PARAMETERS "Incorrect Parameters\n"
+#define FORMATTED "==Formatted JSON output==\n"
+#define UNFORMATTED "\n\n==Unformatted JSON output==\n"
 
 DUDA_REGISTER("Service Example", "service");
 
@@ -21,7 +22,12 @@ duda_global_t my_data_empty;
  * +--------------------------------------------------------------+
  * |                   sendfile                      0            |
  * +--------------------------------------------------------------+
- * |                   json                          0            |
+ * |                   json_first                    0            |
+ * +--------------------------------------------------------------+
+ * |                   json_second     action        6            |
+ * |                               (create/parse)                 |
+ * |                                   format        11           |
+ * |                          (formatted/unformatted)             |
  * +--------------------------------------------------------------+
  *
  */
@@ -53,7 +59,7 @@ void cb_sendfile(duda_request_t *dr)
 
 }
 
-void cb_json(duda_request_t *dr)
+void cb_json_first(duda_request_t *dr)
 {
     char *resp;
     const char strparse[]= " {                   \
@@ -103,18 +109,99 @@ void cb_json(duda_request_t *dr)
 
     response->body_write(dr, FORMATTED, sizeof(FORMATTED)-1);
     resp = json->print(jroot);
-
     response->body_write(dr, resp, strlen(resp));
-    resp = NULL;
 
+    resp = NULL;
     jparse = json->parse(strparse);
     response->body_write(dr, UNFORMATTED, sizeof(UNFORMATTED)-1);
     resp = json->print_unformatted(jparse);
     json->delete(jparse);
     response->body_write(dr, resp, strlen(resp));
+
     response->end(dr, cb_end);
 }
 
+void cb_json_second(duda_request_t *dr){
+    char *resp,*pvalue1="",*pvalue2="";
+    short int pnumber;
+    const char strparse[]= " {                   \
+        \"name\":   \"Michel Perez\",            \
+        \"age\":    22,                          \
+        \"address\":      {                      \
+             \"streetAddress\": \"Piso 15\",     \
+             \"city\": \"Valparaiso\",           \
+             \"country\": \"Chile\"              \
+         },                                      \
+        \"phoneNumber\":    [                    \
+             {                                   \
+              \"type\": \"work\",                \
+              \"number\": \"2 666 4567\"         \
+             },                                  \
+             {                                   \
+             \"type\": \"fax\",                  \
+             \"number\": null                    \
+             }                                   \
+         ]                                       \
+        }";
+    json_t *jroot,*jaddress,*jphone,*jphone1,*jphone2,*jparse;
+
+    response->http_status(dr, 200);
+    response->http_header(dr, "Content-Type: text/plain", 24);
+
+    pnumber = 0;
+    pvalue1 = params->get(dr, pnumber);
+    pnumber = 1;
+    pvalue2 = params->get(dr, pnumber);
+
+    if(strncmp(pvalue1, "create", strlen("create")) == 0 && strlen("create") == strlen(pvalue1)){
+        jroot = json->create_object();
+        json->add_to_object(jroot, "name", json->create_string("Michel Perez"));
+        json->add_to_object(jroot, "age", json->create_number(22.0));
+
+        jaddress = json->create_object();
+        json->add_to_object(jaddress, "streetAddress", json->create_string("Piso 15"));
+        json->add_to_object(jaddress, "city", json->create_string("Valparaiso"));
+        json->add_to_object(jaddress, "country", json->create_string("Chile"));
+        json->add_to_object(jroot, "address", jaddress);
+
+        jphone = json->create_array();
+        jphone1 = json->create_object();
+        json->add_to_object(jphone1, "type", json->create_string("work"));
+        json->add_to_object(jphone1, "number", json->create_string("2 666 4567"));
+        jphone2 = json->create_object();
+        json->add_to_object(jphone2, "type", json->create_string("fax"));
+        json->add_to_object(jphone2, "number", json->create_null());
+        json->add_to_array(jphone, jphone1);
+        json->add_to_array(jphone, jphone2);
+        json->add_to_object(jroot, "phoneNumber", jphone);
+
+        if(strncmp(pvalue2, "formatted", strlen("formatted")) == 0 && strlen("formatted") == strlen(pvalue2)){
+            resp = json->print(jroot);
+            response->body_write(dr, resp, strlen(resp));
+        }else if(strncmp(pvalue2, "unformatted", strlen("unformatted")) == 0 && strlen("unformatted") == strlen(pvalue2)){
+            resp = json->print_unformatted(jroot);
+            response->body_write(dr, resp, strlen(resp));
+        }else{
+            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+        }
+    }else if(strncmp(pvalue1, "parse", strlen("parse")) == 0 && strlen("parse") == strlen(pvalue1)){
+        jparse = json->parse(strparse);
+        if(strncmp(pvalue2, "formatted", strlen("formatted")) == 0 && strlen("formatted") == strlen(pvalue2)){
+            resp = json->print(jparse);
+            response->body_write(dr, resp, strlen(resp));
+        }else if(strncmp(pvalue2, "unformatted", strlen("unformatted")) == 0 && strlen("unformatted") == strlen(pvalue2)){
+            resp = json->print_unformatted(jparse);
+            response->body_write(dr, resp, strlen(resp));
+        }else{
+            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+        }
+        json->delete(jparse);
+    }else{
+        response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+    }
+        response->end(dr, cb_end);
+}
+
 void *cb_global_mem()
 {
     void *mem = malloc(16);
@@ -125,7 +212,8 @@ int duda_init(struct duda_api_objects *api)
 {
     duda_interface_t *if_system;
     duda_method_t    *method;
-
+    duda_param_t *param;
+    
     duda_service_init();
     duda_load_package(json, "json");
 
@@ -142,8 +230,19 @@ int duda_init(struct duda_api_objects *api)
     method = map->method_new("hello_world", "cb_hello_world", 0);
     map->interface_add_method(method, if_system);
 
-    /* URI: /hello/examples/json */
-    method = map->method_new("json", "cb_json", 0);
+    /* URI: /hello/examples/json_first */
+    method = map->method_new("json_first", "cb_json_first", 0);
+    map->interface_add_method(method, if_system);
+
+    /* URI: /hello/examples/json_second/<action>/<format>
+     * action: create/parse
+     * format: formatted/unformatted
+     */
+    method = map->method_new("json_second", "cb_json_second", 1);
+    param = map->param_new("action", strlen("create"));
+    map->method_add_param(param, method);
+    param = map->param_new("format", strlen("unformatted")); 
+    map->method_add_param(param, method);
     map->interface_add_method(method, if_system);
 
     /* URI: /hello/examples/sendfile */
diff --git a/plugins/duda/webservice.c b/plugins/duda/webservice.c
index daed0de..963c67e 100644
--- a/plugins/duda/webservice.c
+++ b/plugins/duda/webservice.c
@@ -70,7 +70,7 @@ duda_param_t *duda_param_new(char *uid, short int max_len)
 {
     duda_param_t *param;
 
-    param = mk_api->mem_alloc(sizeof(duda_method_t));
+    param = mk_api->mem_alloc(sizeof(duda_param_t));
     param->max_len = max_len;
 
     return param;
diff --git a/plugins/duda/webservice.h b/plugins/duda/webservice.h
index 4fc3eab..32c79fd 100644
--- a/plugins/duda/webservice.h
+++ b/plugins/duda/webservice.h
@@ -42,6 +42,7 @@ struct duda_api_map *map;
 struct duda_api_msg *msg;
 struct duda_api_response *response;
 struct duda_api_debug *debug;
+struct duda_api_params *params;
 struct duda_api_global *global;
 duda_package_t *pkg_temp;
 
@@ -58,6 +59,7 @@ duda_package_t *pkg_temp;
     msg      = api->msg;                                                \
     response = api->response;                                           \
     debug    = api->debug;                                              \
+    params   = api->params;                                             \
     global   = api->global;                                             \
     mk_list_init(&_duda_interfaces);                                    \
     mk_list_init(&_duda_global_dist);
_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to