3.2 KiB
##Extending the HTTP-API
The HTTP-API is implemented as a collection of httpMethodHandlers
. An httpMethodHandler
is a pointer to a function that is expected to respond to an HTTP GET request, using the Cesanta Mongoose framework. An httpMethodHandler
is defined (in trick/WebServer.hh
) as follows:
typedef void (*httpMethodHandler)(struct mg_connection*, struct http_message*);
Documentation for the Cesanta Mongoose Networking Library can be found at: https://cesanta.com/docs/overview/intro.html
Example HTTP-API Extension
Suppose you want your web server to send you a JSON message:
{ "greeting" : "Hello Trick Sim Developer!" }
when you invoke the URL: http://localhost:8888/api/http/hello
.
Creating an httpMethodHandler
.
The following two files will be our implementation of an httpMethodHandler
. We'll put these in some models directory httpMethods/
.
handle_HTTP_GET_hello.h
#ifndef HANDLE_HTTP_GET_HELLO
#define HANDLE_HTTP_GET_HELLO
#ifndef SWIG
void handle_HTTP_GET_hello(struct mg_connection *nc, struct http_message *hm);
#endif
#endif
handle_HTTP_GET_hello.c
#include "mongoose/mongoose.h"
void handle_HTTP_GET_hello(struct mg_connection *nc, struct http_message *hm) {
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
const char* json_text =
"{ \"greeting\" : \"Hello Trick Sim Developer!\" }";
mg_printf_http_chunk(nc, "%s", json_text);
mg_send_http_chunk(nc, "", 0);
}
Installing our httpMethodHandler
.
We'll do this from our S_define file:
-
Add
(httpMethods/handle_HTTP_GET_hello.c)
to theLIBRARY DEPENDENCIES
. -
Include our header file:
##include "httpMethods/handle_HTTP_GET_hello.h"
-
In
create_connections()
add :
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
A Complete S_define
/***********************TRICK HEADER*************************
PURPOSE:
(Cannon Numeric)
LIBRARY DEPENDENCIES:
(
(cannon/gravity/src/cannon_init.c)
(cannon/gravity/src/cannon_numeric.c)
(httpMethods/handle_HTTP_GET_hello.c)
)
*************************************************************/
#include "sim_objects/default_trick_sys.sm"
#include "sim_objects/WebServer.sm"
##include "cannon/gravity/include/cannon_numeric.h"
##include "httpMethods/handle_HTTP_GET_hello.h"
class CannonSimObject : public Trick::SimObject {
public:
CANNON cannon ;
int foo;
CannonSimObject() {
("default_data") cannon_default_data( &cannon ) ;
("initialization") cannon_init( &cannon ) ;
("derivative") cannon_deriv( &cannon ) ;
("integration") trick_ret = cannon_integ( &cannon ) ;
("dynamic_event") cannon_impact( &cannon) ;
}
} ;
CannonSimObject dyn ;
IntegLoop dyn_integloop (0.01) dyn;
void create_connections() {
dyn_integloop.getIntegrator(Runge_Kutta_4, 5);
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
}