wip manage controller

This commit is contained in:
2018-04-29 23:14:44 +02:00
parent 2f178a3e6b
commit e65a08ed1f
9 changed files with 143 additions and 39 deletions

View File

@@ -33,7 +33,8 @@
"${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/x86_64-buildroot-linux-gnu/include/c++/5.4.0/x86_64-buildroot-linux-gnu", "${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/x86_64-buildroot-linux-gnu/include/c++/5.4.0/x86_64-buildroot-linux-gnu",
"${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/lib/gcc/x86_64-buildroot-linux-gnu/5.4.0/include", "${workspaceRoot}/_builds/x86_64_domo-debug/cross/opt/ext-toolchain/lib/gcc/x86_64-buildroot-linux-gnu/5.4.0/include",
"${workspaceFolder}/src/lib/libubus-cpp/include", "${workspaceFolder}/src/lib/libubus-cpp/include",
"${workspaceFolder}/src/prog/domod/include" "${workspaceFolder}/src/prog/domod/include",
"${workspaceFolder}/src/plugins/uhttpd/uhttpd-rest-api"
], ],
"defines": [], "defines": [],
"intelliSenseMode": "clang-x64", "intelliSenseMode": "clang-x64",

View File

@@ -91,7 +91,7 @@ void EtagRestConnection::invoke(struct ubus_context *a_ctx)
m_ctx = a_ctx; m_ctx = a_ctx;
the_method = m_controller->get_method(m_client->request.method); //TODO the_method = m_controller->get_method(m_client->request.method);
parse_parameter(); parse_parameter();

View File

@@ -93,14 +93,11 @@ void RestConnection::invoke(struct ubus_context *a_ctx)
{ {
uint32_t the_id = 0; uint32_t the_id = 0;
UBusCall the_cmd; UBusCall the_cmd;
std::string the_method;
m_ctx = a_ctx; m_ctx = a_ctx;
the_method = m_controller->get_method(m_client->request.method);
// printf("RestConnection::invoke(object: %s)....\n", m_controller->get_path().c_str()); // printf("RestConnection::invoke(object: %s)....\n", m_controller->get_path().c_str());
if (!ubus_lookup_id(a_ctx, m_controller->get_path().c_str(), &the_id)) if (!ubus_lookup_id(a_ctx, m_controller->get_ubus().get_path().c_str(), &the_id))
{ {
json_object *the_parameter_doc; json_object *the_parameter_doc;
@@ -110,7 +107,8 @@ void RestConnection::invoke(struct ubus_context *a_ctx)
the_parameter_doc = parse_form_encoded_data(); the_parameter_doc = parse_form_encoded_data();
// printf("RestConnection::invoke launch async call (%d)....\n", the_id); // printf("RestConnection::invoke launch async call (%d)....\n", the_id);
the_cmd.exec_async(a_ctx, the_id, the_method, json_object_to_json_string(the_parameter_doc), this); the_cmd.exec_async(a_ctx, the_id, m_controller->get_ubus().get_method().c_str(),
json_object_to_json_string(the_parameter_doc), this);
json_object_put(the_parameter_doc); json_object_put(the_parameter_doc);
} }

View File

@@ -30,7 +30,6 @@
#include <uhttpd/uhttpd.h> #include <uhttpd/uhttpd.h>
#define kControllerModelKey "model" #define kControllerModelKey "model"
#define kControllerEndpointKey "endpoint" #define kControllerEndpointKey "endpoint"
#define kControllerUbusKey "ubus" #define kControllerUbusKey "ubus"
@@ -52,7 +51,8 @@ RestController::RestController(void)
* *
* @brief constructor of the rest controller object. * @brief constructor of the rest controller object.
*/ */
RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path) RestController::RestController(const std::string &a_path, const std::string &a_method_get, const std::string &a_method_put, int a_timeout, bool a_raw_response) : WebController(a_path),
mf_raw_response(false)
#if 0 #if 0
m_method_get(a_method_get), m_method_get(a_method_get),
m_method_put(a_method_put), m_method_put(a_method_put),
@@ -81,7 +81,6 @@ WebConnection *RestController::new_connection(struct uhttpd_ops *an_ops, struct
return new RestConnection(an_ops, a_client, this, a_parameters); return new RestConnection(an_ops, a_client, this, a_parameters);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn from_json * @fn from_json
* *
@@ -128,21 +127,18 @@ bool RestController::from_json(struct json_object *a_node)
// path // path
if (json_object_object_get_ex(the_ubus_node, kControllerPathKey, &the_value_node)) if (json_object_object_get_ex(the_ubus_node, kControllerPathKey, &the_value_node))
{ {
m_ubus.m_path = json_object_get_string(the_value_node); m_ubus.set_path(json_object_get_string(the_value_node));
} }
//method //method
if (json_object_object_get_ex(the_ubus_node, kControllerMethodKey, &the_value_node)) if (json_object_object_get_ex(the_ubus_node, kControllerMethodKey, &the_value_node))
{ {
m_ubus.m_method = json_object_get_string(the_value_node); m_ubus.set_method(json_object_get_string(the_value_node));
} }
} }
return true; return true;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn find * @fn find
* *
@@ -150,10 +146,44 @@ bool RestController::from_json(struct json_object *a_node)
*/ */
bool RestController::find(const std::string &an_url) bool RestController::find(const std::string &an_url)
{ {
std::vector<std::string>::iterator the_it;
printf("find : %s\n", an_url.c_str());
for (the_it = m_endpoint.begin(); the_it != m_endpoint.end(); ++the_it)
{
int the_error;
the_error = (*the_it).compare(an_url);
printf("compare <%s> et <%s> %d\n", (*the_it).c_str(), an_url.c_str(), the_error);
if (the_error == 0)
return true; return true;
} }
return false;
}
/*! ----------------------------------------------------------------------------
* @fn get_ubus
*
* @brief return the ubus reference for this controller.
*/
const Ubus &RestController::get_ubus(void) const
{
return m_ubus;
}
/*! ----------------------------------------------------------------------------
* @fn is_raw_response
*
* @brief return the true if the controller didn't need the api response template.
*/
bool RestController::is_raw_response(void)
{
return mf_raw_response;
}
#if 0
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method_get * @fn get_method_get
* *
@@ -184,15 +214,6 @@ uint16_t RestController::get_timeout(void)
return 0; //m_timeout; return 0; //m_timeout;
} }
/*! ----------------------------------------------------------------------------
* @fn is_raw_response
*
* @brief return the true if the controller didn't need the awox api response template.
*/
bool RestController::is_raw_response(void)
{
return false; //mf_raw_response;
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_method * @fn get_method
@@ -218,3 +239,4 @@ std::string RestController::get_method(uint8_t a_method)
#endif #endif
return ""; return "";
} }
#endif

View File

@@ -58,18 +58,22 @@ class RestController : public WebController
bool find(const std::string &an_url); bool find(const std::string &an_url);
const Ubus &get_ubus(void) const;
bool is_raw_response(void);
/*
std::string get_method_get(void); std::string get_method_get(void);
std::string get_method_put(void); std::string get_method_put(void);
uint16_t get_timeout(void); uint16_t get_timeout(void);
bool is_raw_response(void);
std::string get_method(uint8_t a_method); std::string get_method(uint8_t a_method);
*/
protected: protected:
uint8_t m_method; uint8_t m_method;
std::vector<std::string> m_endpoint; std::vector<std::string> m_endpoint;
Ubus m_ubus; Ubus m_ubus;
bool mf_raw_response;
/* /*
std::string m_name; std::string m_name;
@@ -77,7 +81,6 @@ class RestController : public WebController
std::string m_method_get; std::string m_method_get;
std::string m_method_put; std::string m_method_put;
uint16_t m_timeout; uint16_t m_timeout;
bool mf_raw_response;
*/ */
}; };

View File

@@ -32,3 +32,43 @@
Ubus::Ubus(void) Ubus::Ubus(void)
{ {
} }
/*! ----------------------------------------------------------------------------
* @fn get_path
*
* @brief return the path.
*/
const std::string &Ubus::get_path(void) const
{
return m_path;
}
/*! ----------------------------------------------------------------------------
* @fn set_path
*
* @brief set the path.
*/
void Ubus::set_path(const char *a_path)
{
m_path = a_path;
}
/*! ----------------------------------------------------------------------------
* @fn get_method
*
* @brief return the method
*/
const std::string &Ubus::get_method(void) const
{
return m_method;
}
/*! ----------------------------------------------------------------------------
* @fn set_method
*
* @brief set the method
*/
void Ubus::set_method(const char *a_method)
{
m_method = a_method;
}

View File

@@ -34,6 +34,13 @@ class Ubus
public: public:
Ubus(void); Ubus(void);
const std::string &get_path(void) const;
void set_path(const char *a_path);
const std::string &get_method(void) const;
void set_method(const char *a_method);
private:
std::string m_path; std::string m_path;
std::string m_method; std::string m_method;
}; };

View File

@@ -328,16 +328,17 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
the_url = an_url; the_url = an_url;
} }
#warning todo the_controller = get_controller(the_url);
// the_controller = m_controller_array[the_url];
if (the_controller == NULL) if (the_controller == NULL)
{ {
printf ("not found :( \n");
std::string the_msg; std::string the_msg;
the_msg = fmt::format(kJsonControlerNotFound, the_url); the_msg = fmt::format(kJsonControlerNotFound, the_url);
send_error(a_cl, 404, "Not Found", the_msg); send_error(a_cl, 404, "Not Found", the_msg);
return; return;
} }
#warning TODO check if controller is available for this method.
// We found the controller. // We found the controller.
// printf("method: %d\n", a_cl->request.method); // printf("method: %d\n", a_cl->request.method);
struct dispatch *d = &a_cl->dispatch; struct dispatch *d = &a_cl->dispatch;
@@ -362,11 +363,11 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url,
break; break;
default: default:
// printf("Invalid Request\n"); printf("Invalid Request\n");
send_error(a_cl, 400, "Bad Request", "Invalid Request"); send_error(a_cl, 400, "Bad Request", "Invalid Request");
} }
// printf("uhttp_server_handle_request -done.\n"); printf("uhttp_server_handle_request -done.\n");
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
@@ -486,7 +487,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node)
if (the_rest_controller->from_json(the_ctrl_node)) if (the_rest_controller->from_json(the_ctrl_node))
{ {
//new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response) //new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response)
add_controller(the_path, the_rest_controller); add_controller("", the_rest_controller);
} }
} }
} }
@@ -662,12 +663,21 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
if (a_controller == NULL) if (a_controller == NULL)
return -1; return -1;
if (an_uri.empty())
{
m_controllers.push_back(a_controller);
}
else {
a_controller->set_name(an_uri); a_controller->set_name(an_uri);
// TODO
}
//m_controller_array[an_uri] = a_controller; //m_controller_array[an_uri] = a_controller;
#if 0
printf("add: an_uri:%s\n", an_uri.c_str()); printf("add: an_uri:%s\n", an_uri.c_str());
#if 0
// Keep a list of the API root. to check if a controller is managed by this plugin or not. // Keep a list of the API root. to check if a controller is managed by this plugin or not.
the_pos = an_uri.find_first_of("/", 1); the_pos = an_uri.find_first_of("/", 1);
if (the_pos != std::string::npos) if (the_pos != std::string::npos)
@@ -683,6 +693,28 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont
return 0; return 0;
} }
/*! ----------------------------------------------------------------------------
* @fn send_error
*
* @brief Send an error message to the client.
*/
WebController *UhttpServer::get_controller(const std::string &an_url)
{
std::vector<WebController *>::iterator the_it;
WebController *the_controller = NULL;
for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it)
{
if ((*the_it)->find(an_url))
{
printf ("return the controller.\n");
return (*the_it);
}
}
return the_controller;
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn send_error * @fn send_error
* *

View File

@@ -75,6 +75,7 @@ class UhttpServer
std::string &a_get_method, std::string &a_set_method, bool &a_raw_response); std::string &a_get_method, std::string &a_set_method, bool &a_raw_response);
struct json_object *load(const std::string &a_file); struct json_object *load(const std::string &a_file);
int add_controller(const std::string &an_uri, WebController *a_controller); int add_controller(const std::string &an_uri, WebController *a_controller);
WebController *get_controller(const std::string &an_url);
void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg); void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg);
private: private: