1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    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(broker_rt,
   36          [ add_server/2,
   37            del_server/1,
   38            bind_client/1,
   39            bind_proxy/1,
   40            remote_call/2
   41          ]).   42
   43:- use_module(library(http/websocket)).   44:- use_module(library(interface)).   45:- init_expansors.   46
   47%  Note: remote call of meta predicate should be banned, because the client
   48%  context module could be inexistent in the server.
   49
   50/*
   51:- meta_predicate
   52    remote_call(0 ).
   53
   54remote_call(MCall) :-
   55    predicate_property(MCall, implementation_module(M)),
   56    remote_call(Call, M).
   57*/
   58
   59:- multifile
   60    '$broker'/1.   61
   62:- dynamic
   63    module_server/2.   64
   65add_server(Module, URL) :-
   66    '$broker'(Module),
   67    assertz(module_server(Module, URL)).
   68
   69del_server(Module) :-
   70    '$broker'(Module),
   71    retractall(module_server(Module, _)).
   72
   73bind_client(Module) :-
   74    atom_concat(Module, '_intf', ModuleIntf),
   75    atom_concat(Module, '_remt', ModuleRemt),
   76    bind_interface(ModuleIntf, ModuleRemt).
   77
   78bind_proxy(Module) :-
   79    atom_concat(Module, '_intf', ModuleIntf),
   80    atom_concat(Module, '_prxy', ModuleRemt),
   81    bind_interface(ModuleIntf, ModuleRemt).
   82
   83remote_call(Call, M) :-
   84    term_variables(Call, Vars),
   85    remote_call(Call, M, Vars).
   86
   87remote_call(Call, Module, Vars) :-
   88    ( module_server(Module, URL),
   89      directory_file_path(URL, Module, Path),
   90      http_open_websocket(Path, WS, [])
   91      /* let this fail
   92      catch(...
   93            error(socket_error(_, _), _),
   94            fail)
   95      */
   96    ->setup_call_cleanup(
   97          ws_send(WS, prolog(b(Call, Vars))),
   98          remote_call_loop(WS, Vars),
   99          ws_close(WS, 1000, "bye"))
  100    /* % fallback to local call (if available)
  101    ; predicate_property(Module:Call, defined),
  102      Module:Call
  103    */
  104    ).
  105
  106remote_call_loop(WS, Vars) :-
  107    repeat,
  108      ws_receive(WS, Reply, [format(prolog)]),
  109      ( Reply.opcode = close
  110      ->!,
  111        fail
  112      ; e(Vars, Ex) = Reply.data
  113      ->throw(Ex)
  114      ; b(Vars) = Reply.data
  115      ->!
  116      ; n(Vars) = Reply.data
  117      ; ws_send(WS, prolog(next)),
  118        fail
  119      )