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