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.
  53
  54/** <module> Event service
  55
  56Generic broadcasting service. Broadcasts are   made  using the predicate
  57broadcast(+Templ). All registered  `listeners'  will   have  their  goal
  58called. Success or failure of this is ignored. The listener can not bind
  59arguments.
  60
  61This library is particulary  useful  for   disconnecting  modules  in an
  62application. Modules can broadcast events  such as changes, anticipating
  63other modules need to react on   such  changes. For example, settings.pl
  64broadcasts changes to settings, allowing dependent   modules to react on
  65changes:
  66
  67==
  68:- listing(setting(changed(http:workers, New)),
  69           change_workers(New)).
  70
  71change_workers(New) :-
  72        setting(http:port, Port),
  73        http_workers(Port, New).
  74==
  75*/
  76
  77%!  listen(+Listener, +Templ, :Goal) is det.
  78%!  listen(+Templ, :Goal) is det.
  79%
  80%   Open a channel for listening for events of the given `Templ'.
  81
  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).
  88
  89
  90%!  unlisten(+Listener) is det.
  91%!  unlisten(+Listener, +Templ) is det.
  92%!  unlisten(+Listener, +Templ, :Goal) is det.
  93%
  94%   Destroy a channel. All arguments may  be variables, removing the
  95%   all matching listening channals.
  96
  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).
 106
 107
 108%!  listening(?Listener, ?Templ, ?Goal) is nondet.
 109%
 110%   returns currently open channels
 111
 112listening(Listener0, Templ, Module:Goal) :-
 113    canonical_listener(Listener0, Listener),
 114    listener(Templ, Listener, Module, Goal).
 115
 116
 117%!  broadcast(+Templ) is det.
 118%
 119%   Broadcast given event.
 120
 121broadcast(Templ) :-
 122    (   listener(Templ, _Listener, Module, Goal),
 123        (   Module:Goal
 124        ->  fail
 125        )
 126    ;   true
 127    ).
 128
 129
 130%!  broadcast_request(+Templ) is nondet.
 131%
 132%   Broadcast given event till accepted.  Succeeds then, fail if no
 133%   listener accepts the call.  Bindings made by the listener goal
 134%   are maintained.  May be used to make broadcast requests.
 135
 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)).
 155
 156%!  canonical_listener(+Raw, -Canonical)
 157%
 158%   Entry for later optimization.
 159
 160canonical_listener(Templ, Templ).