samer@0
|
1 :- module(testosc, [osc/1]).
|
samer@0
|
2
|
samer@0
|
3 :- use_module(library(plosc)).
|
samer@0
|
4
|
samer@0
|
5 :- dynamic server/2.
|
samer@0
|
6
|
samer@0
|
7 ack(_,_) :- writeln(ack_received).
|
samer@0
|
8
|
samer@0
|
9 echo(P,A) :- writeln(msg(P,A)).
|
samer@0
|
10 echo(Sender,Time,P,A) :-
|
samer@0
|
11 format('from ~w at ~w: ~w.\n',[Sender,Time,msg(P,A)]),
|
samer@0
|
12 ( Time=osc_immed -> osc_send(Sender,'/ack',[])
|
samer@0
|
13 ; osc_time_ts(T0,Time) -> osc_send(Sender,'/ack',[],T0+1)
|
samer@0
|
14 ).
|
samer@0
|
15
|
samer@0
|
16 forward(_,[string(Host),int(Port),string(Msg)|Args]) :-
|
samer@0
|
17 osc_mk_address(Host,Port,Addr),
|
samer@0
|
18 osc_send(Addr,Msg,Args).
|
samer@0
|
19
|
samer@0
|
20 sched_at(_,[double(Delay),string(Host),int(Port),string(Msg)|Args]) :-
|
samer@0
|
21 get_time(Now), Time is Now+Delay,
|
samer@0
|
22 osc_mk_address(Host,Port,Addr),
|
samer@0
|
23 osc_send(Addr,Msg,Args,Time).
|
samer@0
|
24
|
samer@0
|
25 osc(init(Port)) :- osc_mk_server(Port,S),
|
samer@0
|
26 osc_mk_address(localhost,Port,P),
|
samer@0
|
27 osc_add_handler_x(S, '/echox', any, echo),
|
samer@0
|
28 osc_add_handler(S, '/echo', any, echo),
|
samer@0
|
29 osc_add_handler(S, '/fwd', any, forward),
|
samer@0
|
30 osc_add_handler(S, '/after', any, sched_in),
|
samer@0
|
31 osc_add_handler(S, '/ack', any, ack),
|
samer@0
|
32 assert(server(S,P)).
|
samer@0
|
33
|
samer@0
|
34 osc(start) :- server(S,_), osc_start_server(S), at_halt(osc(stop)).
|
samer@0
|
35 osc(stop) :- server(S,_), osc_stop_server(S).
|
samer@0
|
36 osc(run) :- server(S,_), osc_run_server(S).
|
samer@0
|
37
|
samer@0
|
38 osc(send(M,A)) :- server(_,P), osc_send(P,M,A).
|
samer@0
|
39 osc(send(M,A,T)) :- server(_,P), osc_send(P,M,A,T).
|
samer@0
|
40 osc(send_from(M,A,T)) :- server(S,P), osc_send_from(S,P,M,A,T).
|
samer@0
|
41
|
samer@0
|
42 run(Port) :- osc(init(Port)),
|
samer@0
|
43 nl,
|
samer@0
|
44 writeln('Commands:'), nl,
|
samer@0
|
45 writeln(' osc(start) to start the server in a new thread.'),
|
samer@0
|
46 writeln(' osc(stop) to stop the server thread.'),
|
samer@0
|
47 writeln(' osc(run) run the server synchronously in the current thread.'),
|
samer@0
|
48 writeln(' osc(send(Path,Args)) send message with Path and Args.'),
|
samer@0
|
49 writeln(' osc(send(Path,Args,Time)) send timestamped message with Path and Args.'),
|
samer@0
|
50 nl,
|
samer@0
|
51 writeln('OSC messages:'), nl,
|
samer@0
|
52 writeln(' /echo <<args>>'),
|
samer@0
|
53 writeln(' write messages and arguments.'),
|
samer@0
|
54 writeln(' /echox <<args>>'),
|
samer@0
|
55 writeln(' write messages and arguments with sender and timestamp.'),
|
samer@0
|
56 writeln(' /fwd s<host> i<port> s<path> <<args>>'),
|
samer@0
|
57 writeln(' forward message to given server.'),
|
samer@0
|
58 writeln(' /after d<delay> s<host> i<port> s<path> <<args>>'),
|
samer@0
|
59 writeln(' forward message after delay.'),
|
samer@0
|
60 writeln(' /plosc/stop'),
|
samer@0
|
61 writeln(' stop the synchronous server.'),
|
samer@0
|
62 nl.
|