1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2002-2018, University of Amsterdam 7 CWI, Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(stream_pool, 37 [ add_stream_to_pool/2, % +Stream, :Goal 38 delete_stream_from_pool/1, % +Stream 39 close_stream_pool/0, 40 dispatch_stream_pool/1, % +TimeOut 41 stream_pool_main_loop/0 42 ]). 43:- autoload(library(debug),[debug/3]). 44 45:- meta_predicate 46 add_stream_to_pool( , ). 47 48:- volatile 49 pool/2. % sockets don't survive a saved-state 50:- dynamic 51 pool/2. % Stream, Action 52 53/** <module> Input multiplexing 54 55This libary allows a single thread to monitor multiple streams and call 56a goal if input is available on a stream. 57 58@bug Note that if the processing predicate blocks other input channals 59are not processed. This may happen, for example, if a read/2 call blocks 60due to incomplete input. 61*/ 62 63%! add_stream_to_pool(+Stream :Goal) 64% 65% Call Goal whenever there is input on Stream. 66 67add_stream_to_pool(Stream, Action) :- 68 strip_module(Action, Module, Plain), 69 register_stream(Stream, Module:Plain). 70 71register_stream(Stream, Goal) :- 72 assert(pool(Stream, Goal)). 73 74%! delete_stream_from_pool(+Stream) 75% 76% Retract stream from the pool 77 78delete_stream_from_pool(Stream) :- 79 retractall(pool(Stream, _)). 80 81%! close_stream_pool 82% 83% Close all streams in the pool. This causes stream_pool_main_loop/0 84% to terminate. 85 86close_stream_pool :- 87 forall(retract(pool(Stream, _)), 88 close(Stream, [force(true)])). 89 90%! dispatch_stream_pool(+TimeOut) 91% 92% Wait for input on one or more streams and handle that. Wait for at 93% most TimeOut seconds (0 means infinite). 94 95dispatch_stream_pool(Timeout) :- 96 findall(S, pool(S, _), Pool), 97 debug(tcp, 'Select ~p ...', [Pool]), 98 catch(wait_for_input(Pool, Ready, Timeout), E, true), 99 debug(tcp, ' --> ~p (E=~p)', [Ready, E]), 100 ( var(E) 101 -> actions(Ready) 102 ; E = error(existence_error(stream, Stream), _) 103 -> delete_stream_from_pool(Stream) 104 ). 105 106actions([]). 107actions([H|T]) :- 108 action(H), 109 actions(T). 110 111action(Stream) :- 112 pool(Stream, Action), 113 ( catch(Action, E, true) 114 -> ( var(E) 115 -> true 116 ; print_message(error, E) 117 ) 118 ; print_message(warning, 119 goal_failed(Action, stream_pool)) 120 ). 121 122%! stream_pool_main_loop 123% 124% Keep handling input from the streams in the pool until they have all 125% died away. 126 127stream_pool_main_loop :- 128 pool(_, _), 129 !, 130 ( current_prolog_flag(windows, true) 131 -> dispatch_stream_pool(1) % so we can break out easily 132 ; dispatch_stream_pool(0) 133 ), 134 stream_pool_main_loop. 135stream_pool_main_loop