File: prolog_server.pl

package info (click to toggle)
swi-prolog 8.0.2+dfsg-3+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (139 lines) | stat: -rw-r--r-- 4,893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker & Steve Prior
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2004-2011, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(prolog_server,
          [ prolog_server/2             % +Port, +Options
          ]).

:- use_module(library(socket)).

%!  prolog_server(?Port, +Options)
%
%   Create a TCP/IP based server  on  the   given  Port,  so you can
%   telnet into Prolog and run an  interactive session. This library
%   is intended to provide access for   debugging  and management of
%   embedded servers.
%
%   Currently defined options are:
%
%           * allow(IP)
%           Allow access from IP, a term of the format ip(A,B,C,D).
%           Multiple of such terms can exist and access is granted
%           if the peer IP address unifies to one of them.  If no
%           allow option is provided access is only granted from
%           ip(127,0,0,1) (localhost).
%
%   For example:
%
%           ==
%           ?- prolog_server(4000, []).
%
%           % telnet localhost 4000
%           Welcome to the SWI-Prolog server on thread 3
%
%           1 ?-
%           ==
%
%   @bug As the connection does not involve a terminal, command history
%   and completion are not provided. Neither are interrupts
%   (Control-C).  To terminate the Prolog shell one must enter the
%   command "end_of_file."


prolog_server(Port, Options) :-
    tcp_socket(ServerSocket),
    tcp_setopt(ServerSocket, reuseaddr),
    tcp_bind(ServerSocket, Port),
    tcp_listen(ServerSocket, 5),
    thread_create(server_loop(ServerSocket, Options), _,
                  [ alias(prolog_server)
                  ]).

server_loop(ServerSocket, Options) :-
    tcp_accept(ServerSocket, Slave, Peer),
    tcp_open_socket(Slave, InStream, OutStream),
    set_stream(InStream, close_on_abort(false)),
    set_stream(OutStream, close_on_abort(false)),
    tcp_host_to_address(Host, Peer),
    (   Postfix = []
    ;   between(2, 1000, Num),
        Postfix = [-, Num]
    ),
    atomic_list_concat(['client@', Host | Postfix], Alias),
    catch(thread_create(
              service_client(InStream, OutStream, Peer, Options),
              _,
              [ alias(Alias)
              ]),
          error(permission_error(create, thread, Alias), _),
          fail),
    !,
    server_loop(ServerSocket, Options).

service_client(InStream, OutStream, Peer, Options) :-
    allow(Peer, Options),
    !,
    thread_self(Id),
    set_prolog_IO(InStream, OutStream, OutStream),
    set_stream(InStream, tty(true)),
    set_prolog_flag(tty_control, false),
    current_prolog_flag(encoding, Enc),
    set_stream(user_input, encoding(Enc)),
    set_stream(user_output, encoding(Enc)),
    set_stream(user_error, encoding(Enc)),
    set_stream(user_input, newline(detect)),
    set_stream(user_output, newline(dos)),
    set_stream(user_error, newline(dos)),
    format(user_error,
           'Welcome to the SWI-Prolog server on thread ~w~n~n',
           [Id]),
    call_cleanup(prolog,
                 ( close(InStream),
                   close(OutStream),
                   thread_detach(Id))).
service_client(InStream, OutStream, _, _):-
    thread_self(Id),
    format(OutStream, 'Go away!!~n', []),
    close(InStream),
    close(OutStream),
    thread_detach(Id).


allow(Peer, Options) :-
    (   member(allow(Allow), Options)
    *-> Peer = Allow,
        !
    ;   Peer = ip(127,0,0,1)
    ).