---
plugins/duda/duda_api.c | 26 +++++++++
plugins/duda/duda_api.h | 6 ++
plugins/duda/example/hello.c | 117 +++++++++++++++++++++++++++++++++++++++---
plugins/duda/webservice.c | 3 +-
plugins/duda/webservice.h | 3 +-
5 files changed, 144 insertions(+), 11 deletions(-)
diff --git a/plugins/duda/duda_api.c b/plugins/duda/duda_api.c
index 28aece8..e237aa1 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->param_count = duda_param_count;
+ objs->params->param_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..79468b0 100644
--- a/plugins/duda/duda_api.h
+++ b/plugins/duda/duda_api.h
@@ -158,6 +158,11 @@ struct duda_api_debug {
void (*stacktrace) (void);
};
+/* PARAMS object: params->() */
+struct duda_api_params{
+ char *(*param_get) (duda_request_t *,short int);
+ short int (*param_count) (duda_request_t *);
+};
/* Global data (thread scope) */
struct duda_api_global {
@@ -179,6 +184,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 4df56fa..57c5181 100644
--- a/plugins/duda/example/hello.c
+++ b/plugins/duda/example/hello.c
@@ -3,8 +3,11 @@
#include "webservice.h"
#include "packages/json/json.h"
+#define INCORRECT_PARAMETERS "Incorrect Parameters\n"
#define FORMATTED "==Formatted JSON output==\n"
#define UNFORMATTED "\n==Unformatted JSON output==\n"
+#define PARSE "Parse"
+#define CREATE "Create"
DUDA_REGISTER("Service Example", "service");
@@ -21,7 +24,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 +61,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[]= " { \
@@ -79,7 +87,6 @@ void cb_json(duda_request_t *dr)
response->http_status(dr, 200);
response->http_header(dr, "Content-Type: text/plain", 24);
-
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));
@@ -113,6 +120,87 @@ void cb_json(duda_request_t *dr)
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->param_get(dr, pnumber);
+ pnumber = 1;
+ pvalue2 = params->param_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);
@@ -123,7 +211,9 @@ 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");
@@ -139,15 +229,26 @@ int duda_init(struct duda_api_objects *api)
/* URI: /hello/examples/hello_word */
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 */
method = map->method_new("sendfile", "cb_sendfile", 0);
map->interface_add_method(method, if_system);
-
+
/* Add interface to map */
duda_service_add_interface(if_system);
diff --git a/plugins/duda/webservice.c b/plugins/duda/webservice.c
index daed0de..76a842d 100644
--- a/plugins/duda/webservice.c
+++ b/plugins/duda/webservice.c
@@ -70,9 +70,8 @@ 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..6b5d6a2 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);
@@ -97,4 +99,3 @@ void duda_method_add_param(duda_param_t *param, duda_method_t *method);
struct duda_api_objects *duda_new_api_objects();
#endif
-
_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey