Commit b932dc78 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Add command handling to websocket

Make websocket javascript generic, not longer trace only
parent d680bba1
......@@ -2,7 +2,11 @@
LIBSRCS=mod_websocket_dt.c
LIBOBJS=$(LIBSRCS:.c=.lo)
CFLAGS+=-Wall -I../../../include -I../../../common/trace
CFLAGS+=-Wall \
-I../../../include \
-I../../../common/trace \
-I../../../common/utils \
-I../../../common/include
LDFLAGS+=-L../../../common/trace -ltrace
all: mod_websocket_dt.so_install
......
......@@ -21,10 +21,17 @@
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "trace.h"
#include "websocket_plugin.h"
#include "tcp_connect.h"
#include "dt_port_numbers.h"
#include "dt_host.h"
struct plugin_private {
const WebSocketServer *server;
......@@ -173,6 +180,47 @@ static void start_trace(struct plugin_private *priv, int freq, char *variable)
priv->nr_threads++;
}
static void do_command(char *ident, char *command)
{
char *host;
int port = -1;
int fd;
host = dt_host_console();
if (!strcmp(ident, "sat"))
port = CONSOLE_SAT_CMD_PORT;
else if (!strcmp(ident, "sun"))
port = CONSOLE_SUN_CMD_PORT;
else if (!strcmp(ident, "moon"))
port = CONSOLE_MOON_CMD_PORT;
else if (!strcmp(ident, "auth"))
port = CONSOLE_ACTIVE_PORT;
else if (!strcmp(ident, "j2000"))
port = CONSOLE_J2000_CMD_PORT;
else if (!strcmp(ident, "offset"))
port = CONSOLE_OFFSET_PORT;
else if (!strcmp(ident, "azel"))
port = CONSOLE_AZEL_PORT;
else if (!strcmp(ident, "manual"))
port = CONSOLE_MANUAL_CMD_PORT;
else if (!strcmp(ident, "dt_model"))
port = CONSOLE_DT_MODEL_CMD_PORT;
fd = tcp_connect(host, port);
if (fd >= 0) {
ioctl(fd, FIONBIO, &(int){ 0 });
write(fd, command, strlen(command));
write(fd,"\n", 1);
fsync(fd);
close(fd);
}
}
static size_t on_message(void *plugin_private,
const WebSocketServer *server,
const int type,
......@@ -182,25 +230,33 @@ static size_t on_message(void *plugin_private,
struct plugin_private *priv = plugin_private;
size_t ret = 0;
char text[buffer_size + 1];
char *cmd;
if (type != MESSAGE_TYPE_TEXT)
return 0;
memcpy(text, buffer, buffer_size);
text[buffer_size] = 0;
sscanf(text, "%as ", &cmd);
if (!strncmp((char *)buffer, "trace ", 6)) {
char *cmd;
if (!strcmp(cmd, "trace")) {
int freq;
char *variable;
sscanf(text, "%as %d %as", &cmd, &freq, &variable);
sscanf(text, "%*s %d %as", &freq, &variable);
start_trace(priv, freq, variable);
} else if (!strcmp(cmd, "command")) {
char *ident;
char *command;
free(cmd);
sscanf(text, "%*s %as", &ident);
command = text + strlen(cmd) + strlen(ident) + 2;
do_command(ident, command);
}
free(cmd);
return ret;
}
......
......@@ -19,45 +19,11 @@
*/
function command_cb_default (line)
{
/* default callback */
}
function command (url, ident)
{
/* Callbacks */
this.callback = command_cb_default;
this.xrequest = 0;
this.xrequest_prev = 0;
this.handle_xml = function (event)
this.send = function send (switches)
{
this.callback(this, event.target.responseText);
this.data_wd = 1;
}
this.url=url;
this.ident=ident;
this.send = send;
function send (switches)
{
if (this.xrequest_prev) {
this.xrequest.abort();
delete this.xrequest;
}
this.url = url;
this.xrequest = new XMLHttpRequest();
this.xrequest.multipart = true;
this.xrequest.open("POST", url, true);
this.xrequest.onload = this.handle_xml;
this.xrequest.send(ident + "\n" + switches);
this.xrequest_prev = 1;
dt_websocket_send("command " + ident + " " + switches);
}
}
/*
Javascript for websocket communication
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var dt_websocket_loaded;
if (!dt_websocket_loaded) {
dt_websocket_loaded = true;
var dt_socket;
var dt_socket_url;
var dt_socket_open;
var dt_socket_onopen = new Array();
var dt_socket_onmessage = new Array();
function dt_websocket(url)
{
if (dt_socket) {
return;
}
dt_socket_url = url;
dt_socket = this;
this.ws = new WebSocket(url);
this.ws.onopen = function()
{
for (i = 0; i < dt_socket_onopen.length; i++) {
dt_socket_onopen[i].func(dt_socket_onopen[i].obj);
}
dt_socket_open = true;
}
this.ws.onclose = function(){
//try to reconnect in 3 seconds
setTimeout(function(){dt_socket = new dt_websocket(dt_socket_url)}, 3000);
}
this.ws.onmessage = function(msg)
{
var args = msg.data.split(" ");
for (i = 0; i < dt_socket_onmessage.length; i++) {
if(dt_socket_onmessage[i].name == args[0]) {
dt_socket_onmessage[i].func(
dt_socket_onmessage[i].obj,
args);
}
}
}
}
function dt_websocket_send(msg)
{
dt_socket.ws.send(msg);
}
function dt_websocket_onopen_add(name, func, obj)
{
for (i = 0; i < dt_socket_onopen.length; i++) {
if (dt_socket_onopen[i].name == name) {
dt_socket_onopen[i].func = func;
dt_socket_onopen[i].obj = obj;
return;
}
}
dt_socket_onopen[dt_socket_onopen.length] = new function() {
this.name = name;
this.func = func;
this.obj = obj;
}
if (dt_socket_open)
func(obj);
}
function dt_websocket_onmessage_add(name, func, obj)
{
for (i = 0; i < dt_socket_onmessage.length; i++) {
if (dt_socket_onmessage[i].name == name) {
dt_socket_onmessage[i].func = func;
dt_socket_onmessage[i].obj = obj;
return;
}
}
dt_socket_onmessage[dt_socket_onmessage.length] = new function() {
this.name = name;
this.func = func;
this.obj = obj;
}
}
} /* dt_websocket loaded */
......@@ -759,6 +759,9 @@ eval(load("manual_command.js"));
eval(load("dt_view.js"));
eval(load("utils.js"));
eval(load("offset.js"));
eval(load("dt_websocket.js"));
new dt_websocket("ws://localhost/dt");
/*
'constants'
......
......@@ -66,84 +66,3 @@ function j2000_status(url, ident)
}
}
function j2000_status_old (url, ident)
{
var j2000_status_this = this;
/* Callbacks */
this.callback = j2000_status_cb_default;
this.xrequest = 0;
this.xrequest_prev = 0;
this.timeouto = 0;
this.timeout = 10 * 1000;
this.data_wd = 0;
this.handle_xml = function j2000_status_handle_xml(event)
{
var set_ra;
var set_dec;
var now_ra;
var now_dec;
var now_ut;
var switches;
set_ra = j2000_status_this.xrequest.responseText.split(' ')[0];
set_dec = j2000_status_this.xrequest.responseText.split(' ')[1];
now_ra = j2000_status_this.xrequest.responseText.split(' ')[2];
now_dec = j2000_status_this.xrequest.responseText.split(' ')[3];
now_ut = j2000_status_this.xrequest.responseText.split(' ')[4];
switches = j2000_status_this.xrequest.responseText.split(' ')[5];
j2000_status_this.callback(set_ra, set_dec,
now_ra, now_dec, now_ut,
switches);
j2000_status_this.data_wd = 1;
j2000_status_this.xmit();
}
this.url = url;
this.ident = ident;
this.open = function j2000_status_open()
{
return;
if (j2000_status_this.xrequest_prev) {
j2000_status_this.xrequest.abort();
}
j2000_status_this.url = url;
this.xmit();
j2000_status_this.timeouto =
setTimeout(j2000_status_this.checktime,
j2000_status_this.timeout, j2000_status_this);
}
this.xmit = function j2000_status_xmit()
{
if (j2000_status_this.xrequest_prev) {
delete j2000_status_this.xrequest;
}
j2000_status_this.xrequest = new XMLHttpRequest();
j2000_status_this.xrequest.multipart = false;
j2000_status_this.xrequest.open("POST", j2000_status_this.url, true);
j2000_status_this.xrequest.onload = this.handle_xml;
j2000_status_this.xrequest.send(this.ident);
j2000_status_this.xrequest_prev = 1;
}
this.checktime = function j2000_status_checktime(object)
{
if (!object.data_wd) {
/* no data for a while... reset trace */
object.open(object.url);
} else {
object.data_wd = 0;
setTimeout(object.checktime, object.timeout, object);
}
}
}
......@@ -232,6 +232,9 @@
<script type="text/javascript">
eval(load("dt_ui.js"));
eval(load("dt_websocket.js"));
new dt_websocket("ws://localhost/dt");
var trace_url = "trace.cgi";
......
......@@ -17,6 +17,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var trace_loaded;
if (!trace_loaded) {
trace_loaded = true;
eval(load("dt_websocket.js"));
var trace_connected;
function trace_cb_default (obj, time, value, unit)
{
......@@ -39,56 +47,37 @@ var trace_priv_traceurl = "";
var trace_priv_freq = 1;
var trace_priv_waittime = 1000;
function trace_start(url)
function trace_onopen(obj)
{
this.ws = new WebSocket(url);
this.ws.onopen = function()
{
for (i = 0; i < trace_priv_nr; i++) {
trace_socket.ws.send("trace " +
trace_priv_list[i].freq +
" " +
trace_priv_list[i].tracename);
}
trace_connected = true;
for (i = 0; i < trace_priv_nr; i++) {
dt_websocket_send("trace " +
trace_priv_list[i].freq +
" " +
trace_priv_list[i].tracename);
}
trace_connected = true;
}
this.ws.onclose = function(){
//try to reconnect in 5 seconds
setTimeout(function(){trace_socket = new trace_start(url)}, 5000);
for (i = 0; i < trace_priv_nr; i++) {
if (trace_priv_list[i].tracename == args[1]) {
trace_priv_list[i].timeout_callback(
trace_priv_list[i]);
return;
}
}
};
dt_websocket_onopen_add("trace", trace_onopen, null);
this.ws.onmessage = function(msg)
{
var args = msg.data.split(" ");
for (i = 0; i < trace_priv_nr; i++) {
if (trace_priv_list[i].tracename == args[1]) {
trace_priv_list[i].callback(
trace_priv_list[i],
args[2],
args[3],
"");
return;
}
function trace_onmessage(obj, args)
{
for (i = 0; i < trace_priv_nr; i++) {
if (trace_priv_list[i].tracename == args[1]) {
trace_priv_list[i].callback(
trace_priv_list[i],
args[2],
args[3],
"");
return;
}
alert("No trace for '" + msg.data + "' found");
}
alert("No trace for '" + msg.data + "' found");
}
dt_websocket_onmessage_add("trace", trace_onmessage, null);
var trace_connected = false;
var trace_socket = new trace_start("ws://localhost/dt");
function trace ()
{
......@@ -117,7 +106,7 @@ function trace ()
trace_this.freq = freq;
if (trace_connected) {
trace_socket.ws.send("trace " +
dt_websocket_send("trace " +
freq +
" " +
tracename);
......@@ -125,3 +114,4 @@ function trace ()
}
}
} /* trace_loaded */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment