-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.pl
140 lines (115 loc) · 3.79 KB
/
interface.pl
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CWT-Prolog Server Interface %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(database).
% Basic header for plain text
header('text/plain', 'Content-type: text/plain~n~n').
%--------------------------------------------------------------------------------%
% Main Interface
%--------------------------------------------------------------------------------%
:- dynamic port/1.
:- http_handler(/, server_status, []).
:- http_handler('/login', login, []).
:- http_handler('/logout', logout, []).
:- http_handler('/ping', ping, []).
:- http_handler('/create_game', create_game, []).
:- http_handler('/join_game', join_game, []).
:- http_handler('/resign_game', resign_game, []).
%% start_server(+File:atom, +Port:between(1, 0xffff)) is semidet.
%
% Attach to the specified database file, and start the server on the specified
% port.
start_server(File, Port) :-
must_be(between(1, 0xffff), Port),
attach_db(File),
db_sync(gc),
asserta(port(Port)),
http_server(http_dispatch, [port(Port)]).
%% server_status(+Request) is det.
%
% Friendly message to let client know that the server is up.
server_status(_Request) :-
header('text/plain', Header),
format(Header),
format('Server is up.~n').
%% send_status(+Status:string) is det.
%
% Takes a response status and sends the information to the client.
send_status(Status) :-
header('text/plain', Header),
format(Header),
format('~s', [Status]).
%% disconnect is det.
%
% Shut down server on specified port and clean up information from top level.
disconnect :-
port(Port),
http_stop_server(Port, []),
retractall(port(_)).
%--------------------------------------------------------------------------------%
% Queries
%--------------------------------------------------------------------------------%
%% login(+Query:compound) is det.
%
% Attempt login and send status back to client.
login(Query) :-
http_parameters(Query, [name(User, [string])]),
login(user(User), Response),
send_status(Response).
%% login(+Query:compound) is det.
%
% Attempt logout and send status back to client.
logout(Query) :-
http_parameters(Query, [name(User, [string])]),
logout(user(User), Response),
send_status(Response).
%% ping(+Query:compound) is det.
%
% Receive ping from client with username.
ping(Query) :-
http_parameters(Query, [name(User, [string])]),
ping(user(User), Response),
send_status(Response).
%% create_game(+Query:compound) is det.
%
% Create a game if all internal restrictions are met for creation.
create_game(Query) :-
http_parameters(Query, [
user(User, [string]),
pos(Pos, [between(1, 4)]),
game(Game, [string]),
limit(Limit, [between(1, 4)]),
layout(Layout, [string])
]),
string_codes(Layout, Codes),
maplist(code_lower_char, Codes, Chars),
create_game(game(User, Game, limit(Limit), Chars), pos(Pos), Response),
send_status(Response).
code_lower_char(Code, Char) :-
to_lower(Code, Lower),
char_code(Char, Lower).
%% join_game(+Query:compound) is det.
%
% Allow a user to join a game if all internal restrictions are met for admission.
join_game(Query) :-
http_parameters(Query, [
user(User, [string]),
pos(Pos, [between(1, 4)]),
game(Game, [string])
]),
join_game(user(User), pos(Pos), Game, Response),
send_status(Response).
%% resign_game(+Query:compound) is det.
%
% Resign a user from a game.
resign_game(Query) :-
http_parameters(Query, [
user(User, [string]),
game(Game, [string]),
pos(Pos, [between(1, 4)])
]),
resign_game(user(User), Game, pos(Pos), Response),
send_status(Response).