Skip to content
Snippets Groups Projects
Commit b2e23fd3 authored by clohr's avatar clohr
Browse files

Préparation de la v0.5r2

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/C/trunk@1593 b32b6428-25c9-4566-ad07-03861ab6144f
parent 50b4e3e9
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,6 @@ xaaws.o: xaaws.c xaal.h xaaws.h db.h options.h proto-http.h proto-xaal.h \
options.o: options.c options.h xaal.h
proto-http.o: proto-http.c proto-http.h xaaws.h xaal.h db.h xaagent.h \
options.h
proto-xaal.o: proto-xaal.c proto-xaal.h xaal.h
proto-xaal.o: proto-xaal.c proto-xaal.h db.h xaal.h xaaws.h
xaagent.o: xaagent.c xaal.h xaagent.h options.h db.h
db.o: db.c db.h xaal.h
/* xaaws - xAAL web interface
* Part of the 'majordom' software
* (c) 2017 Christophe Lohr <christophe.lohr@imt-atlantique.fr>
......@@ -16,16 +17,69 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "proto-xaal.h"
#include <xaal.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <libwebsockets.h>
#include <sys/queue.h>
#include <json-c/json.h>
#include <sys/queue.h>
#include "proto-xaal.h"
#include "db.h"
#include "xaaws.h"
int callback_xaal_proto(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
int callback_xaal_proto(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len) {
return -1;
}
int callback_xaal_dump(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
struct per_session_data__xaal_dump *pss = (struct per_session_data__xaal_dump *)user;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED: {
req_t *req = (req_t *) malloc( sizeof(req_t) );
req->wsi = wsi;
TAILQ_INSERT_TAIL(&(db.msgreqs), req, entries);
pss->janswer = NULL;
lws_callback_on_writable(wsi);
break; }
case LWS_CALLBACK_SERVER_WRITEABLE:
if (pss->janswer) {
if ( serve_ws_stream(wsi, pss->janswer) ) {
json_object_put(pss->janswer);
pss->janswer = NULL;
break;
} else {
lwsl_err("Can't feed WebSocket\n");
return -1;
}
}
break;
default:
break;
}
return 0;
}
bool serve_ws_stream(struct lws *wsi, struct json_object *jobj) {
const char *json = json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOZERO);
char buf[ LWS_PRE + strlen(json) + 1 ];
char *p = &buf[LWS_PRE];
int n, m;
n = sprintf((char *)p, "%s", json);
m = lws_write(wsi, (unsigned char *)p, n, LWS_WRITE_TEXT);
return (m >= n);
}
......@@ -20,9 +20,19 @@
#define _PROTOXAAL_
#include <libwebsockets.h>
#include <json-c/json.h>
struct per_session_data__xaal_dump {
struct json_object *janswer;
};
int callback_xaal_proto(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
int callback_xaal_dump(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
bool serve_ws_stream(struct lws *wsi, struct json_object *jobj);
#endif
try{
var socket = new WebSocket( "ws://" + document.domain + ':' + location.port, "xaal-dump" );
if (!!window.EventSource) {
var source = new EventSource('/json/dump');
source.onmessage = function(event) {
socket.onmessage = function(event) {
var theDiv = document.getElementById('dump');
var newNode = document.createElement('pre');
newNode.innerHTML = Date() + '<br>' + JSON.stringify(JSON.parse(event.data), null, 2);
......@@ -12,10 +10,10 @@ if (!!window.EventSource) {
};
source.onerror = function(event) {
socket.onerror = function(event) {
console.log(event.message);
};
}
else {
} catch(e) {
theDiv.innerHTML = 'Not supported';
}
if (!!window.EventSource) {
var source = new EventSource('/json/dump');
source.onmessage = function(event) {
var theDiv = document.getElementById('dump');
var newNode = document.createElement('pre');
newNode.innerHTML = Date() + '<br>' + JSON.stringify(JSON.parse(event.data), null, 2);
// theDiv.appendChild(newNode);
theDiv.insertBefore(newNode, theDiv.firstChild);
};
source.onerror = function(event) {
console.log(event.message);
};
}
else {
theDiv.innerHTML = 'Not supported';
}
......@@ -42,6 +42,7 @@
#include "xaagent.h"
#include "db.h"
int max_poll_elements;
struct lws_pollfd *pollfds;
int *fd_lookup;
......@@ -61,6 +62,7 @@ xAAL_devinfo_t me;
/* list of supported protocols and callbacks */
static struct lws_protocols protocols[] = {
{ "http-only", callback_http, sizeof(struct per_session_data__http), 0 },
{ "xaal-dump", callback_xaal_dump, sizeof(struct per_session_data__xaal_dump), 0 },
{ NULL, NULL, 0, 0 }
};
......@@ -157,7 +159,7 @@ int main(int argc, char **argv) {
info.max_http_header_pool = 32;
info.options = LWS_SERVER_OPTION_VALIDATE_UTF8;
info.extensions = exts;
info.timeout_secs = 300;
info.timeout_secs = 600;
context = lws_create_context(&info);
if (context == NULL) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment