samer@0: :- module(testosc, [osc/1]). samer@0: samer@0: :- use_module(library(plosc)). samer@0: samer@0: :- dynamic server/2. samer@0: samer@0: ack(_,_) :- writeln(ack_received). samer@0: samer@0: echo(P,A) :- writeln(msg(P,A)). samer@0: echo(Sender,Time,P,A) :- samer@0: format('from ~w at ~w: ~w.\n',[Sender,Time,msg(P,A)]), samer@0: ( Time=osc_immed -> osc_send(Sender,'/ack',[]) samer@0: ; osc_time_ts(T0,Time) -> osc_send(Sender,'/ack',[],T0+1) samer@0: ). samer@0: samer@0: forward(_,[string(Host),int(Port),string(Msg)|Args]) :- samer@0: osc_mk_address(Host,Port,Addr), samer@0: osc_send(Addr,Msg,Args). samer@0: samer@0: sched_at(_,[double(Delay),string(Host),int(Port),string(Msg)|Args]) :- samer@0: get_time(Now), Time is Now+Delay, samer@0: osc_mk_address(Host,Port,Addr), samer@0: osc_send(Addr,Msg,Args,Time). samer@0: samer@0: osc(init(Port)) :- osc_mk_server(Port,S), samer@0: osc_mk_address(localhost,Port,P), samer@0: osc_add_handler_x(S, '/echox', any, echo), samer@0: osc_add_handler(S, '/echo', any, echo), samer@0: osc_add_handler(S, '/fwd', any, forward), samer@0: osc_add_handler(S, '/after', any, sched_in), samer@0: osc_add_handler(S, '/ack', any, ack), samer@0: assert(server(S,P)). samer@0: samer@0: osc(start) :- server(S,_), osc_start_server(S), at_halt(osc(stop)). samer@0: osc(stop) :- server(S,_), osc_stop_server(S). samer@0: osc(run) :- server(S,_), osc_run_server(S). samer@0: samer@0: osc(send(M,A)) :- server(_,P), osc_send(P,M,A). samer@0: osc(send(M,A,T)) :- server(_,P), osc_send(P,M,A,T). samer@0: osc(send_from(M,A,T)) :- server(S,P), osc_send_from(S,P,M,A,T). samer@0: samer@0: run(Port) :- osc(init(Port)), samer@0: nl, samer@0: writeln('Commands:'), nl, samer@0: writeln(' osc(start) to start the server in a new thread.'), samer@0: writeln(' osc(stop) to stop the server thread.'), samer@0: writeln(' osc(run) run the server synchronously in the current thread.'), samer@0: writeln(' osc(send(Path,Args)) send message with Path and Args.'), samer@0: writeln(' osc(send(Path,Args,Time)) send timestamped message with Path and Args.'), samer@0: nl, samer@0: writeln('OSC messages:'), nl, samer@0: writeln(' /echo <>'), samer@0: writeln(' write messages and arguments.'), samer@0: writeln(' /echox <>'), samer@0: writeln(' write messages and arguments with sender and timestamp.'), samer@0: writeln(' /fwd s i s <>'), samer@0: writeln(' forward message to given server.'), samer@0: writeln(' /after d s i s <>'), samer@0: writeln(' forward message after delay.'), samer@0: writeln(' /plosc/stop'), samer@0: writeln(' stop the synchronous server.'), samer@0: nl.