File: sockets.pl

package info (click to toggle)
swi-prolog 9.2.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 84,456 kB
  • sloc: ansic: 401,705; perl: 374,799; lisp: 9,080; cpp: 8,920; java: 5,525; sh: 3,282; javascript: 2,690; python: 2,655; ruby: 1,594; yacc: 845; makefile: 440; xml: 317; sed: 12; sql: 6
file content (174 lines) | stat: -rw-r--r-- 6,219 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2010-2012, 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(sicstus_sockets,
	  [ socket/2,			% +Domain, -Socket
	    socket_close/1,		% +Socket
	    socket_bind/2,		% +Socket, 'AF_INET'(+Host,+Port)
	    socket_connect/3,		% +Socket, 'AF_INET'(+Host,+Port), -Stream
	    socket_listen/2,		% +Socket, +Length
	    socket_accept/2,		% +Socket, -Stream
	    socket_accept/3,		% +Socket, -Client, -Stream
	    socket_select/5,		% +TermsSockets, -NewTermsStreams,
					% +TimeOut, +Streams, -ReadStreams
	    current_host/1,		% ?HostName
	    hostname_address/2		% ?HostName, ?HostAddress
	  ]).
:- use_module(library(socket)).
:- use_module(library(error)).
:- use_module(library(apply)).
:- use_module(library(pairs)).
:- use_module(library(lists)).

:- multifile sicstus:rename_module/2.

sicstus:rename_module(sockets, sicstus_sockets).

/** <module> SICStus 3-compatible library(sockets).

@tbd Our implementation does not support AF_UNIX sockets.
@see https://sicstus.sics.se/sicstus/docs/3.12.11/html/sicstus/Sockets.html
*/

socket(Domain, Socket) :-
	must_be(oneof(['AF_INET']), Domain),
	tcp_socket(Socket).

socket_close(Socket) :-
	tcp_close_socket(Socket).

socket_bind(Socket, Address) :-
	(   Address = 'AF_INET'(Host, Port)
	->  true
	;   type_error(socket_address, Address)
	),
	(   var(Host)
	->  gethostname(Host)
	;   true			% Warning?
	),
	tcp_bind(Socket, Port).

socket_connect(Socket, Address, StreamPair) :-
	(   Address = 'AF_INET'(Host, Port)
	->  true
	;   type_error(socket_address, Address)
	),
	tcp_connect(Socket, Host:Port),
	tcp_open_socket(Socket, Read, Write),
	stream_pair(StreamPair, Read, Write).

socket_listen(Socket, Length) :-
	tcp_listen(Socket, Length).

socket_accept(Socket, Client, StreamPair) :-
	tcp_accept(Socket, Socket2, Peer),
	peer_to_client(Peer, Client),
	tcp_open_socket(Socket2, Read, Write),
	stream_pair(StreamPair, Read, Write).

socket_accept(Socket, Stream) :-
	socket_accept(Socket, _Client, Stream).


peer_to_client(ip(A,B,C,D), Client) :-
	Parts = [A,B,C,D],
	ground(Parts), !,
	atomic_list_concat(Parts, '.', Client).
peer_to_client(ip(A,B,C,D), Client) :-
	atomic_list_concat(Parts, '.', Client),
	maplist(atom_number, Parts, Numbers),
	length(Numbers, 4), !,
	Numbers = [A,B,C,D].
peer_to_client(_, Client) :-
	domain_error(ip_address, Client).


%%	socket_select(+TermsSockets, -NewTermsStreams,
%%		      +TimeOut, +Streams, -ReadStreams) is det.
%
%	The  list  of  streams  in  Streams   is  checked  for  readable
%	characters. A stream can be any   stream  associated with an I/O
%	descriptor.  The  list  ReadStreams  returns  the  streams  with
%	readable data. socket_select/5 also waits for connections to the
%	sockets specified by TermsSockets.  This   argument  should be a
%	list of Term-Socket pairs, where Term, which can be any term, is
%	used  as  an  identifier.   NewTermsStreams    is   a   list  of
%	Term-connection(Client,Stream) pairs, where  Stream   is  a  new
%	stream open for communicating with a   process connecting to the
%	socket identified with Term, Client is   the client host address
%	(see socket_accept/3). If TimeOut is   instantiated  to off, the
%	predicate waits until something is available.  If TimeOut is S:U
%	the predicate waits at most S seconds and U microseconds. Both S
%	and U must be integers >=0. If   there is a timeout, ReadStreams
%	and NewTermsStreams are [].

socket_select(TermsSockets, NewTermsStreams, SicsTimeOut, Streams, ReadStreams) :-
	pairs_values(TermsSockets, Sockets),
	append(Sockets, Streams, AllStream),
	map_timeout(SicsTimeOut, TimeOut),
	wait_for_input(AllStream, ReadyStream, TimeOut),
	process_ready(ReadyStream, TermsSockets, NewTermsStreams, ReadStreams).

map_timeout(off, infinite) :- !.
map_timeout(S:U, Seconds) :- !,
	Seconds is S+U/1000000.
map_timeout(SicsTimeOut, _) :-
	type_error(sicstus_timeout, SicsTimeOut).

process_ready([], _, [], []).
process_ready([H|T], TermsSockets, NewTermsStreams, ReadStreams) :-
	memberchk(Term-H, TermsSockets), !,
	socket_accept(H, Client, Stream),
	NewTermsStreams = [Term-connection(Client,Stream)|NewTSTail],
	process_ready(T, TermsSockets, NewTSTail, ReadStreams).
process_ready([H|T], TermsSockets, NewTermsStreams, [H|ReadStreams]) :-
	process_ready(T, TermsSockets, NewTermsStreams, ReadStreams).


%%	current_host(-Host) is det.
%
%	True when Host is an atom that denotes the name of the host.
%
current_host(Host) :-
	gethostname(Host).

%%	hostname_address(+Host:atom, -Address:atom) is det.
%
%	True when Address is the IP address of Host.

hostname_address(Host, Address) :-
	nonvar(Host), !,
	tcp_host_to_address(Host, IP),
	peer_to_client(IP, Address).