mirror of
https://github.com/nasa/trick.git
synced 2024-12-19 05:07:54 +00:00
Implemented test ws and http endpoints
This commit is contained in:
parent
b7b7a6465d
commit
c0e77aeb80
@ -5,13 +5,16 @@ LIBRARY DEPENDENCIES:
|
||||
(
|
||||
(cannon/gravity/src/cannon_init.c)
|
||||
(cannon/gravity/src/cannon_numeric.c)
|
||||
(httpMethods/handle_HTTP_GET_hello.c)
|
||||
(httpMethods/TimeSession.cpp)
|
||||
)
|
||||
*************************************************************/
|
||||
|
||||
#include "sim_objects/default_trick_sys.sm"
|
||||
// #include "sim_objects/WebServer.sm"
|
||||
#include "sim_objects/CivetServer.sm"
|
||||
##include "cannon/gravity/include/cannon_numeric.h"
|
||||
##include "httpMethods/handle_HTTP_GET_hello.h"
|
||||
##include "httpMethods/TimeSession.hh"
|
||||
|
||||
class CannonSimObject : public Trick::SimObject {
|
||||
|
||||
@ -32,4 +35,6 @@ IntegLoop dyn_integloop (0.01) dyn;
|
||||
|
||||
void create_connections() {
|
||||
dyn_integloop.getIntegrator(Runge_Kutta_4, 5);
|
||||
}
|
||||
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
|
||||
web.server.installWebSocketSessionMaker( "Time", &makeTimeSession );
|
||||
}
|
36
trick_sims/Cannon/SIM_cannon_numeric/www/apps/time.html
Normal file
36
trick_sims/Cannon/SIM_cannon_numeric/www/apps/time.html
Normal file
@ -0,0 +1,36 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WS Example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="output"></div>
|
||||
<script type="text/javascript">
|
||||
|
||||
function log(s) {
|
||||
var p = document.createElement("p");
|
||||
p.style.wordWrap = "break-word";
|
||||
p.textContent = s;
|
||||
output.appendChild(p);
|
||||
}
|
||||
|
||||
var ws = new WebSocket('ws://localhost:8888/api/ws/Time');
|
||||
|
||||
// WebSocket Event Handlers
|
||||
ws.onopen = function(e) {
|
||||
ws.send("GMT");
|
||||
};
|
||||
ws.onmessage = function(e) {
|
||||
log(e.data);
|
||||
};
|
||||
ws.onerror = function(e) {
|
||||
console.log("WebSocket Error: " , e);
|
||||
handleErrors(e);
|
||||
};
|
||||
ws.onclose = function(e) {
|
||||
console.log("Connection closed", e);
|
||||
};
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
#include "civet/CivetServer.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);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#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
|
55
trick_sims/Cannon/models/httpMethods/TimeSession.cpp
Normal file
55
trick_sims/Cannon/models/httpMethods/TimeSession.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include "TimeSession.hh"
|
||||
#include <cstring>
|
||||
|
||||
// CONSTRUCTOR
|
||||
TimeSession::TimeSession( struct mg_connection *nc ) : WebSocketSession(nc) {
|
||||
time(&now);
|
||||
}
|
||||
|
||||
// DESTRUCTOR
|
||||
TimeSession::~TimeSession() {}
|
||||
|
||||
void TimeSession::marshallData() {
|
||||
time(&now);
|
||||
}
|
||||
|
||||
void TimeSession::sendMessage() {
|
||||
|
||||
char message[1024];
|
||||
struct tm *theTime;
|
||||
if (zone == TimeSession::LOCAL) {
|
||||
theTime = localtime(&now);
|
||||
} else {
|
||||
theTime = gmtime(&now);
|
||||
}
|
||||
int hours = theTime->tm_hour;
|
||||
int minutes = theTime->tm_min;
|
||||
int seconds = theTime->tm_sec;
|
||||
int day = theTime->tm_mday;
|
||||
int month = theTime->tm_mon + 1;
|
||||
int year = theTime->tm_year + 1900;
|
||||
|
||||
sprintf(message, "Time: %02d:%02d:%02d Date: %02d/%02d/%d\n", hours, minutes, seconds, month, day, year);
|
||||
mg_websocket_write(connection, MG_WEBSOCKET_OPCODE_TEXT, message, strlen(message));
|
||||
}
|
||||
|
||||
int TimeSession::handleMessage(std::string client_msg) {
|
||||
|
||||
if (client_msg.compare("GMT") == 0) {
|
||||
zone = TimeSession::GMT;
|
||||
} else if (client_msg.compare("LOCAL") == 0) {
|
||||
zone = TimeSession::LOCAL;
|
||||
} else {
|
||||
std::cerr << "ERROR: Unknown command \"" << client_msg << "\"." << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// WebSocketSessionMaker function for a TimeSession.
|
||||
WebSocketSession* makeTimeSession( struct mg_connection *nc ) {
|
||||
std::cerr << "DEBUG: Creating new TimeSession." << std::endl;
|
||||
return new TimeSession(nc);
|
||||
}
|
25
trick_sims/Cannon/models/httpMethods/TimeSession.hh
Normal file
25
trick_sims/Cannon/models/httpMethods/TimeSession.hh
Normal file
@ -0,0 +1,25 @@
|
||||
/*************************************************************************
|
||||
PURPOSE: (Represent the state of a variable server websocket connection.)
|
||||
**************************************************************************/
|
||||
#ifndef TIMESESSION_HH
|
||||
#define TIMESESSION_HH
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "time.h"
|
||||
#include "trick/WebSocketSession.hh"
|
||||
|
||||
class TimeSession : public WebSocketSession {
|
||||
public:
|
||||
enum Zone { GMT, LOCAL};
|
||||
TimeSession(struct mg_connection *nc);
|
||||
~TimeSession();
|
||||
void marshallData();
|
||||
void sendMessage();
|
||||
int handleMessage(std::string);
|
||||
private:
|
||||
time_t now;
|
||||
Zone zone;
|
||||
};
|
||||
|
||||
WebSocketSession* makeTimeSession( struct mg_connection *nc );
|
||||
#endif
|
10
trick_sims/Cannon/models/httpMethods/handle_HTTP_GET_hello.c
Normal file
10
trick_sims/Cannon/models/httpMethods/handle_HTTP_GET_hello.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include "civet/CivetInclude.h"
|
||||
#include <string.h>
|
||||
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, void *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_send_chunk(nc, json_text, strlen(json_text));
|
||||
mg_send_chunk(nc, "", 0);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#ifndef HANDLE_HTTP_GET_HELLO
|
||||
#define HANDLE_HTTP_GET_HELLO
|
||||
|
||||
#ifndef SWIG
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, void *hm);
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user