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)  2006-2013, University of Amsterdam
    7                              VU University 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(broadcast,
   37          [ listen/3,           % Listener x Templ x Goal
   38            listen/2,           % Templ x Goal
   39            unlisten/1,         % Listener
   40            unlisten/2,         % Listener x Templ
   41            unlisten/3,         % Listener x Templ x Goal
   42            listening/3,        % Listener x Templ x Goal
   43            broadcast/1,        % Templ
   44            broadcast_request/1 % Templ
   45          ]).   46:- meta_predicate
   47    listen(+, 0),
   48    listen(+, +, 0),
   49    unlisten(+, +, 0).   50
   51:- dynamic
   52    listener/4.

Event service

Generic broadcasting service. Broadcasts are made using the predicate broadcast(+Templ). All registered `listeners' will have their goal called. Success or failure of this is ignored. The listener can not bind arguments.

This library is particularly useful for disconnecting modules in an application. Modules can broadcast events such as changes, anticipating other modules need to react on such changes. For example, settings.pl broadcasts changes to settings, allowing dependent modules to react on changes:

:- listen(setting(changed(http:workers, New)),
          change_workers(New)).

change_workers(New) :-
        setting(http:port, Port),
        http_workers(Port, New).

*/

 listen(+Listener, +Templ, :Goal) is det
 listen(+Templ, :Goal) is det
Open a channel for listening for events of the given `Templ'.
   82listen(Listener0, Templ, Module:Goal) :-
   83    canonical_listener(Listener0, Listener),
   84    assert_listener(Templ, Listener, Module, Goal).
   85
   86listen(Templ, Module:Goal) :-
   87    assert_listener(Templ, Module, Module, Goal).
 unlisten(+Listener) is det
 unlisten(+Listener, +Templ) is det
 unlisten(+Listener, +Templ, :Goal) is det
Destroy a channel. All arguments may be variables, removing the all matching listening channels.
   97unlisten(Listener0) :-
   98    canonical_listener(Listener0, Listener),
   99    retractall(listener(_, Listener, _, _)).
  100unlisten(Listener0, Templ) :-
  101    canonical_listener(Listener0, Listener),
  102    retractall(listener(Templ, Listener, _, _)).
  103unlisten(Listener0, Templ, Module:Goal) :-
  104    canonical_listener(Listener0, Listener),
  105    retract_listener(Templ, Listener, Module, Goal).
 listening(?Listener, ?Templ, ?Goal) is nondet
returns currently open channels
  112listening(Listener0, Templ, Module:Goal) :-
  113    canonical_listener(Listener0, Listener),
  114    listener(Templ, Listener, Module, Goal).
 broadcast(+Templ) is det
Broadcast given event.
  121broadcast(Templ) :-
  122    (   listener(Templ, _Listener, Module, Goal),
  123        (   Module:Goal
  124        ->  fail
  125        )
  126    ;   true
  127    ).
 broadcast_request(+Templ) is nondet
Broadcast given event till accepted. Succeeds then, fail if no listener accepts the call. Bindings made by the listener goal are maintained. May be used to make broadcast requests.
  136broadcast_request(Templ) :-
  137    listener(Templ, _Listener, Module, Goal),
  138    Module:Goal.
  139
  140
  141%       {assert,retract}_listener(+Templ, +Listener, +Module, +Goal)
  142%
  143%       Implemented as sub-predicate to ensure storage in this module.
  144%       Second registration is ignored.  Is this ok?  It avoids problems
  145%       using multiple registration of global listen channels.
  146
  147assert_listener(Templ, Listener, Module, TheGoal) :-
  148    listener(Templ, Listener, Module, TheGoal),
  149    !.
  150assert_listener(Templ, Listener, Module, TheGoal) :-
  151    asserta(listener(Templ, Listener, Module, TheGoal)).
  152
  153retract_listener(Templ, Listener, Module, TheGoal) :-
  154    retractall(listener(Templ, Listener, Module, TheGoal)).
 canonical_listener(+Raw, -Canonical)
Entry for later optimization.
  160canonical_listener(Templ, Templ)