Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Tammo Jan Dijkema
dt_ctrl
Commits
b932dc78
Commit
b932dc78
authored
Nov 12, 2013
by
Jeroen Vreeken
Browse files
Add command handling to websocket
Make websocket javascript generic, not longer trace only
parent
d680bba1
Changes
8
Hide whitespace changes
Inline
Side-by-side
console/console/mod_websocket_dt/Makefile
View file @
b932dc78
...
@@ -2,7 +2,11 @@
...
@@ -2,7 +2,11 @@
LIBSRCS
=
mod_websocket_dt.c
LIBSRCS
=
mod_websocket_dt.c
LIBOBJS
=
$(LIBSRCS:.c=.lo)
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
LDFLAGS
+=
-L
../../../common/trace
-ltrace
all
:
mod_websocket_dt.so_install
all
:
mod_websocket_dt.so_install
...
...
console/console/mod_websocket_dt/mod_websocket_dt.c
View file @
b932dc78
...
@@ -21,10 +21,17 @@
...
@@ -21,10 +21,17 @@
#include
<stdio.h>
#include
<stdio.h>
#include
<pthread.h>
#include
<pthread.h>
#include
<string.h>
#include
<string.h>
#include
<sys/ioctl.h>
#include
<unistd.h>
#include
"trace.h"
#include
"trace.h"
#include
"websocket_plugin.h"
#include
"websocket_plugin.h"
#include
"tcp_connect.h"
#include
"dt_port_numbers.h"
#include
"dt_host.h"
struct
plugin_private
{
struct
plugin_private
{
const
WebSocketServer
*
server
;
const
WebSocketServer
*
server
;
...
@@ -173,6 +180,47 @@ static void start_trace(struct plugin_private *priv, int freq, char *variable)
...
@@ -173,6 +180,47 @@ static void start_trace(struct plugin_private *priv, int freq, char *variable)
priv
->
nr_threads
++
;
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
,
static
size_t
on_message
(
void
*
plugin_private
,
const
WebSocketServer
*
server
,
const
WebSocketServer
*
server
,
const
int
type
,
const
int
type
,
...
@@ -182,25 +230,33 @@ static size_t on_message(void *plugin_private,
...
@@ -182,25 +230,33 @@ static size_t on_message(void *plugin_private,
struct
plugin_private
*
priv
=
plugin_private
;
struct
plugin_private
*
priv
=
plugin_private
;
size_t
ret
=
0
;
size_t
ret
=
0
;
char
text
[
buffer_size
+
1
];
char
text
[
buffer_size
+
1
];
char
*
cmd
;
if
(
type
!=
MESSAGE_TYPE_TEXT
)
if
(
type
!=
MESSAGE_TYPE_TEXT
)
return
0
;
return
0
;
memcpy
(
text
,
buffer
,
buffer_size
);
memcpy
(
text
,
buffer
,
buffer_size
);
text
[
buffer_size
]
=
0
;
text
[
buffer_size
]
=
0
;
sscanf
(
text
,
"%as "
,
&
cmd
);
if
(
!
strncmp
((
char
*
)
buffer
,
"trace "
,
6
))
{
if
(
!
strcmp
(
cmd
,
"trace"
))
{
char
*
cmd
;
int
freq
;
int
freq
;
char
*
variable
;
char
*
variable
;
sscanf
(
text
,
"%
a
s %d %as"
,
&
cmd
,
&
freq
,
&
variable
);
sscanf
(
text
,
"%
*
s %d %as"
,
&
freq
,
&
variable
);
start_trace
(
priv
,
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
;
return
ret
;
}
}
...
...
console/htdocs/command.js
View file @
b932dc78
...
@@ -19,45 +19,11 @@
...
@@ -19,45 +19,11 @@
*/
*/
function
command_cb_default
(
line
)
{
/* default callback */
}
function
command
(
url
,
ident
)
function
command
(
url
,
ident
)
{
{
/* Callbacks */
this
.
send
=
function
send
(
switches
)
this
.
callback
=
command_cb_default
;
this
.
xrequest
=
0
;
this
.
xrequest_prev
=
0
;
this
.
handle_xml
=
function
(
event
)
{
{
this
.
callback
(
this
,
event
.
target
.
responseText
);
dt_websocket_send
(
"
command
"
+
ident
+
"
"
+
switches
);
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
;
}
}
}
}
console/htdocs/dt_websocket.js
0 → 100644
View file @
b932dc78
/*
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 */
console/htdocs/index.html
View file @
b932dc78
...
@@ -759,6 +759,9 @@ eval(load("manual_command.js"));
...
@@ -759,6 +759,9 @@ eval(load("manual_command.js"));
eval
(
load
(
"
dt_view.js
"
));
eval
(
load
(
"
dt_view.js
"
));
eval
(
load
(
"
utils.js
"
));
eval
(
load
(
"
utils.js
"
));
eval
(
load
(
"
offset.js
"
));
eval
(
load
(
"
offset.js
"
));
eval
(
load
(
"
dt_websocket.js
"
));
new
dt_websocket
(
"
ws://localhost/dt
"
);
/*
/*
'constants'
'constants'
...
...
console/htdocs/j2000_status.js
View file @
b932dc78
...
@@ -66,84 +66,3 @@ function j2000_status(url, ident)
...
@@ -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
);
}
}
}
console/htdocs/mech.html
View file @
b932dc78
...
@@ -232,6 +232,9 @@
...
@@ -232,6 +232,9 @@
<script
type=
"text/javascript"
>
<script
type=
"text/javascript"
>
eval
(
load
(
"
dt_ui.js
"
));
eval
(
load
(
"
dt_ui.js
"
));
eval
(
load
(
"
dt_websocket.js
"
));
new
dt_websocket
(
"
ws://localhost/dt
"
);
var
trace_url
=
"
trace.cgi
"
;
var
trace_url
=
"
trace.cgi
"
;
...
...
console/htdocs/trace.js
View file @
b932dc78
...
@@ -17,6 +17,14 @@
...
@@ -17,6 +17,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
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
)
function
trace_cb_default
(
obj
,
time
,
value
,
unit
)
{
{
...
@@ -39,56 +47,37 @@ var trace_priv_traceurl = "";
...
@@ -39,56 +47,37 @@ var trace_priv_traceurl = "";
var
trace_priv_freq
=
1
;
var
trace_priv_freq
=
1
;
var
trace_priv_waittime
=
1000
;
var
trace_priv_waittime
=
1000
;
function
trace_
start
(
url
)
function
trace_
onopen
(
obj
)
{
{
this
.
ws
=
new
WebSocket
(
url
);
for
(
i
=
0
;
i
<
trace_priv_nr
;
i
++
)
{
dt_websocket_send
(
"
trace
"
+
this
.
ws
.
onopen
=
function
()
trace_priv_list
[
i
].
freq
+
{
"
"
+
for
(
i
=
0
;
i
<
trace_priv_nr
;
i
++
)
{
trace_priv_list
[
i
].
tracename
);
trace_socket
.
ws
.
send
(
"
trace
"
+
trace_priv_list
[
i
].
freq
+
"
"
+
trace_priv_list
[
i
].
tracename
);
}
trace_connected
=
true
;
}
}
trace_connected
=
true
;
}
this
.
ws
.
onclose
=
function
(){
dt_websocket_onopen_add
(
"
trace
"
,
trace_onopen
,
null
);
//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
;
}
}
};
this
.
ws
.
onmessage
=
function
(
msg
)
function
trace_onmessage
(
obj
,
args
)
{
{
var
args
=
msg
.
data
.
split
(
"
"
);
for
(
i
=
0
;
i
<
trace_priv_nr
;
i
++
)
{
if
(
trace_priv_list
[
i
].
tracename
==
args
[
1
])
{
for
(
i
=
0
;
i
<
trace_priv_nr
;
i
++
)
{
trace_priv_list
[
i
].
callback
(
if
(
trace_priv_list
[
i
].
tracename
==
args
[
1
])
{
trace_priv_list
[
i
],
trace_priv_list
[
i
].
callback
(
args
[
2
],
trace_priv_list
[
i
],
args
[
3
],
args
[
2
],
""
);
args
[
3
],
return
;
""
);
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
()
function
trace
()
{
{
...
@@ -117,7 +106,7 @@ function trace ()
...
@@ -117,7 +106,7 @@ function trace ()
trace_this
.
freq
=
freq
;
trace_this
.
freq
=
freq
;
if
(
trace_connected
)
{
if
(
trace_connected
)
{
trace_
socket
.
ws
.
send
(
"
trace
"
+
dt_web
socket
_
send
(
"
trace
"
+
freq
+
freq
+
"
"
+
"
"
+
tracename
);
tracename
);
...
@@ -125,3 +114,4 @@ function trace ()
...
@@ -125,3 +114,4 @@ function trace ()
}
}
}
}
}
/* trace_loaded */
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment