1/* $Id: streampool.pl,v 1.1.1.1 2005/03/04 07:57:51 stavros Exp $ 2 3 Part of SWI-Prolog 4 5 Author: Jan Wielemaker 6 E-mail: jan@swi.psy.uva.nl 7 WWW: http://www.swi-prolog.org 8 Copyright (C): 1985-2002, University of Amsterdam 9 10 This program is free software; you can redistribute it and/or 11 modify it under the terms of the GNU General Public License 12 as published by the Free Software Foundation; either version 2 13 of the License, or (at your option) any later version. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public 21 License along with this library; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 24 As a special exception, if you link this library with other files, 25 compiled with a Free Software compiler, to produce an executable, this 26 library does not by itself cause the resulting executable to be covered 27 by the GNU General Public License. This exception does not however 28 invalidate any other reasons why the executable file might be covered by 29 the GNU General Public License. 30*/ 31 32:- module(stream_pool, 33 [ add_stream_to_pool/2, % +Stream, :Goal 34 delete_stream_from_pool/1, % +Stream 35 close_stream_pool/0, 36 dispatch_stream_pool/1, % +TimeOut 37 stream_pool_main_loop/0 38 ]). 39:- use_module(library(quintus)). 40 41:- meta_predicate 42 add_stream_to_pool( , ). 43 44:- dynamic 45 pool/2. % Stream, Action 46 47% add_stream_to_pool(+Stream :Goal) 48% 49% Call Goal whenever there is input on Stream. 50 51add_stream_to_pool(Stream, Action) :- 52 '$strip_module'(Action, Module, Plain), 53 register_stream(Stream, Module:Plain). 54 55register_stream(Stream, Goal) :- 56 assert(pool(Stream, Goal)). 57 58% delete_stream_from_pool(+Stream) 59% 60% Retract stream from the pool 61 62delete_stream_from_pool(Stream) :- 63 retractall(pool(Stream, _)). 64 65% close_stream_pool 66 67close_stream_pool :- 68 ( retract(pool(Stream, _)), 69 close(Stream, [force(true)]), 70 fail 71 ; true 72 ). 73 74% dispatch_stream_pool(+TimeOut) 75% 76% Wait for input on one or more streams and handle that. Wait for 77% at most TimeOut seconds (0 means infinite). 78 79dispatch_stream_pool(Timeout) :- 80 findall(S, pool(S, _), Pool), 81 catch(wait_for_input(Pool, Ready, Timeout), E, true), 82 ( var(E) 83 -> actions(Ready) 84 ; E = error(existence_error(stream, Stream), _) 85 -> delete_stream_from_pool(Stream) 86 ). 87 88actions([]). 89actions([H|T]) :- 90 action(H), 91 actions(T). 92 93action(Stream) :- 94 pool(Stream, Action), 95 ( catch(Action, E, true) 96 -> ( var(E) 97 -> true 98 ; print_message(error, E) 99 ) 100 ; print_message(warning, format('Action failed: ~p', [Action])) 101 ). 102 103% stream_pool_main_loop 104% 105% Keep handling input from the streams in the pool until they have 106% all died away. 107 108%initialize_stream_pool(Id) :- 109% pipe(Read, Write), 110% assert(interface_streams(Read, Write)), 111% thread_create(stream_pool_main_loop, Id, [alias(sigio), 112% detached(true)]). 113 114stream_pool_main_loop :- 115 pool(_, _), !, 116 dispatch_stream_pool(0), 117 stream_pool_main_loop. 118stream_pool_main_loop. 119 120 /******************************* 121 * MESSAGES * 122 *******************************/ 123 124:- multifile 125 prolog:message/3. 126 127prologmessage(connect(IP)) --> 128 [ 'New connection from ' ], 129 ip(IP). 130prologmessage(close(IP)) --> 131 [ 'Disconnect from ' ], 132 ip(IP). 133 134ip(ip(A,B,C,D)) --> 135 [ '~w.~w.~w.~w'-[A,B,C,D] ]