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