Advertisement
logicmoo

AbstractOutputSream

May 14th, 2015
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.79 KB | None | 0 0
  1. :-dynamic(wotp_server_port/1).
  2. wotp_server_port(16996).
  3.  
  4. wotp_lambda([Data],Call,Data):-Call.
  5.  
  6. % Redirect output stream and read_line_to_string to write to a predicate
  7. with_output_to_pred(Pred, Call):-
  8.    with_output_to_pred(read_line_to_string, Pred, Call).
  9.  
  10. % Redirect output stream to write to a predicate with ReaderPred line [read_line_to_string,get_char,..]
  11. with_output_to_pred(ReaderPred, Pred, Call):- thread_self(ID),
  12.    wotp_client(ReaderPred, wotp_lambda([Data],thread_signal(ID,call(Pred,Data))),Call).
  13.  
  14. wotp_client(ReaderPred,Pred, Call):-
  15.     wotp_create_server,
  16.     must(wotp_server_port(Port)),
  17.     tcp_socket(Socket),
  18.     tcp_connect(Socket, localhost:Port),
  19.     tcp_open_socket(Socket, StreamPair),
  20.     stream_pair(StreamPair, In, Out),
  21.     wotp_io_setup(In, Out),    
  22.     format(Out,'~N~q.~nq.~n',[ReaderPred,Pred]),
  23.     flush_output(Out),
  24.     setup_call_cleanup(tell(Out),Call,
  25.      ignore(((format(Out,'~N',[]),told,ignore(catch(close(StreamPair, [force(true)]),_,true)))))).
  26.  
  27.  
  28. wotp_create_server(Port):- thread_property(_T,alias(wotp_server)),!.
  29. wotp_create_server(Port):-
  30.         (wotp_server_port(Port) -> true ; asserta(wotp_server_port(Port))),
  31.         tcp_socket(ServerSocket),
  32.     tcp_setopt(ServerSocket, reuseaddr),
  33.     tcp_bind(ServerSocket, Port),
  34.     tcp_listen(ServerSocket, 5),
  35.     thread_create(wotp_server_loop(ServerSocket), _,[ alias(wotp_server)]).
  36.  
  37. wotp_server_loop(ServerSocket) :-
  38.     tcp_accept(ServerSocket, Client, _Peer),
  39.     tcp_open_socket(Client, StreamPair),
  40.         gensym(wotp_client_,Alias),
  41.     thread_create(wotp_service(Client, StreamPair),_,[alias(Alias),detached(true)]),
  42.     wotp_server_loop(ServerSocket).
  43.  
  44. wotp_service(Client, StreamPair):-
  45.          stream_pair(StreamPair, In, Out),
  46.          wotp_io_setup(In, Out),
  47.          read(In,ReaderPred),read(In,Pred), debug(wotp,'~q. => ~q. ~n',[ReaderPred,Pred]),
  48.          repeat,
  49.          call(In,Data), debug(wotp,'~q.~n',[call(In,Data)]),
  50.          ((Data \==end_of_file, Data \== -1 ) -> (once(call(Pred,Data)),fail) ;
  51.          ignore(catch(close(StreamPair, [force(true)]),_,true))).
  52.        
  53.  
  54. wotp_io_setup(In, Out):-          
  55.       set_stream(In, close_on_abort(false)),
  56.       set_stream(Out, close_on_abort(false)),
  57.       current_prolog_flag(encoding, Enc),
  58.       set_stream(In, encoding(Enc)),
  59.       set_stream(Out, encoding(Enc)),
  60.       set_stream(In, newline(detect)),
  61.       set_stream(Out, newline(dos)).
  62.  
  63. wotp_create_server:- (wotp_server_port(Port)->wotp_create_server(Port);wotp_create_server(16996)).
  64.  
  65. :- source_location(S,_),forall(source_file(H,S),ignore((  \+ (predicate_property(H,PP),member(PP,[(multifile),built_in])),  
  66.  functor(H,F,A),module_transparent(F/A),export(F/A)))).
  67.  
  68. :- wotp_create_server.
  69.  
  70. % Example  
  71. :- with_output_to_pred(say(dmiles), format('Hello World~n',[])).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement