author | wenzelm |
Sat, 10 Mar 2018 14:20:27 +0100 | |
changeset 67812 | b123c9a007d0 |
parent 67811 | 33199d033505 |
child 67820 | e30d6368c7c8 |
permissions | -rw-r--r-- |
66347 | 1 |
/* Title: Pure/Tools/server.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Resident Isabelle servers. |
|
67809 | 5 |
|
6 |
Message formats: |
|
7 |
||
8 |
- short message (single line): |
|
9 |
NAME ARGUMENT |
|
10 |
||
11 |
- long message (multiple lines): |
|
12 |
BYTE_LENGTH |
|
13 |
NAME ARGUMENT |
|
66347 | 14 |
*/ |
15 |
||
16 |
package isabelle |
|
17 |
||
18 |
||
67806 | 19 |
import java.io.{BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter, |
20 |
InputStreamReader, OutputStreamWriter, IOException} |
|
67797 | 21 |
import java.net.{Socket, SocketException, SocketTimeoutException, ServerSocket, InetAddress} |
66347 | 22 |
|
23 |
||
24 |
object Server |
|
25 |
{ |
|
66927 | 26 |
/* protocol */ |
27 |
||
67809 | 28 |
def split_message(msg: String): (String, String) = |
67794 | 29 |
{ |
67809 | 30 |
val head = msg.takeWhile(c => Symbol.is_ascii_letter(c) || Symbol.is_ascii_letdig(c)) |
31 |
val rest = msg.substring(head.length).dropWhile(Symbol.is_ascii_blank(_)) |
|
67794 | 32 |
(head, proper_string(rest) getOrElse "{}") |
33 |
} |
|
34 |
||
67785 | 35 |
val commands: Map[String, PartialFunction[(Server, JSON.T), JSON.T]] = |
66929 | 36 |
Map( |
67785 | 37 |
"echo" -> { case (_, t) => t }, |
38 |
"help" -> { case (_, JSON.empty) => commands.keySet.toList.sorted }, |
|
67791 | 39 |
"shutdown" -> { case (server, JSON.empty) => server.close(); JSON.empty }) |
66929 | 40 |
|
66927 | 41 |
object Reply extends Enumeration |
42 |
{ |
|
67801 | 43 |
val OK, ERROR, NOTE = Value |
67800 | 44 |
|
67809 | 45 |
def unapply(msg: String): Option[(Reply.Value, JSON.T)] = |
67800 | 46 |
{ |
67809 | 47 |
if (msg == "") None |
67800 | 48 |
else { |
67809 | 49 |
val (reply, output) = split_message(msg) |
67800 | 50 |
try { Some((withName(reply), JSON.parse(output, strict = false))) } |
51 |
catch { |
|
52 |
case _: NoSuchElementException => None |
|
53 |
case Exn.ERROR(_) => None |
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
66927 | 57 |
} |
58 |
||
59 |
||
67786 | 60 |
/* socket connection */ |
61 |
||
62 |
object Connection |
|
63 |
{ |
|
64 |
def apply(socket: Socket): Connection = |
|
65 |
new Connection(socket) |
|
66 |
} |
|
67 |
||
67788 | 68 |
class Connection private(val socket: Socket) |
67786 | 69 |
{ |
70 |
override def toString: String = socket.toString |
|
71 |
||
72 |
def close() { socket.close } |
|
73 |
||
67805
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
67801
diff
changeset
|
74 |
val in = new BufferedInputStream(socket.getInputStream) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
67801
diff
changeset
|
75 |
val out = new BufferedOutputStream(socket.getOutputStream) |
67786 | 76 |
|
67809 | 77 |
def read_message(): Option[String] = |
78 |
try { |
|
79 |
Bytes.read_line(in).map(_.text) match { |
|
80 |
case Some(Value.Int(n)) => |
|
81 |
Bytes.read_block(in, n).map(bytes => Library.trim_line(bytes.text)) |
|
82 |
case res => res |
|
83 |
} |
|
84 |
} |
|
67805
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
67801
diff
changeset
|
85 |
catch { case _: SocketException => None } |
67786 | 86 |
|
67809 | 87 |
def write_message(msg: String) |
67786 | 88 |
{ |
67809 | 89 |
val b = UTF8.bytes(msg) |
90 |
if (b.length > 100 || b.contains(10)) { |
|
91 |
out.write(UTF8.bytes((b.length + 1).toString)) |
|
92 |
out.write(10) |
|
93 |
} |
|
94 |
out.write(b) |
|
67805
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
67801
diff
changeset
|
95 |
out.write(10) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
67801
diff
changeset
|
96 |
try { out.flush() } catch { case _: SocketException => } |
67786 | 97 |
} |
98 |
||
99 |
def reply(r: Server.Reply.Value, t: JSON.T) |
|
100 |
{ |
|
67809 | 101 |
write_message(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t)) |
67786 | 102 |
} |
103 |
||
104 |
def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) } |
|
105 |
def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) } |
|
106 |
def reply_error_message(message: String, more: (String, JSON.T)*): Unit = |
|
107 |
reply_error(Map("message" -> message) ++ more) |
|
67801 | 108 |
|
109 |
def notify(t: JSON.T) { reply(Server.Reply.NOTE, t) } |
|
110 |
def notify_message(message: String, more: (String, JSON.T)*): Unit = |
|
111 |
notify(Map("message" -> message) ++ more) |
|
67786 | 112 |
} |
113 |
||
114 |
||
67807 | 115 |
/* server info */ |
116 |
||
117 |
sealed case class Info(name: String, port: Int, password: String) |
|
118 |
{ |
|
119 |
override def toString: String = |
|
67812 | 120 |
"server " + print_name_space(name) + "= " + print(port, password) |
67807 | 121 |
|
122 |
def connection(): Connection = |
|
123 |
{ |
|
124 |
val connection = Connection(new Socket(InetAddress.getByName("127.0.0.1"), port)) |
|
67809 | 125 |
connection.write_message(password) |
67807 | 126 |
connection |
127 |
} |
|
128 |
||
129 |
def active(): Boolean = |
|
130 |
try { |
|
131 |
using(connection())(connection => |
|
132 |
{ |
|
133 |
connection.socket.setSoTimeout(2000) |
|
67809 | 134 |
connection.read_message() == Some(Reply.OK.toString) |
67807 | 135 |
}) |
136 |
} |
|
137 |
catch { |
|
138 |
case _: IOException => false |
|
139 |
case _: SocketException => false |
|
140 |
case _: SocketTimeoutException => false |
|
141 |
} |
|
142 |
||
143 |
def console() |
|
144 |
{ |
|
145 |
using(connection())(connection => |
|
146 |
{ |
|
147 |
val tty_loop = |
|
148 |
new TTY_Loop( |
|
149 |
new BufferedWriter(new OutputStreamWriter(connection.socket.getOutputStream)), |
|
150 |
new BufferedReader(new InputStreamReader(connection.socket.getInputStream))) |
|
151 |
tty_loop.join |
|
152 |
}) |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
||
66347 | 157 |
/* per-user servers */ |
158 |
||
67812 | 159 |
def print_name_space(name: String): String = |
160 |
if (name == "") "" else quote(name) + " " |
|
161 |
||
67787 | 162 |
def print(port: Int, password: String): String = |
163 |
"127.0.0.1:" + port + " (password " + quote(password) + ")" |
|
164 |
||
66347 | 165 |
object Data |
166 |
{ |
|
66349 | 167 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db") |
66347 | 168 |
|
66857 | 169 |
val name = SQL.Column.string("name").make_primary_key |
66349 | 170 |
val port = SQL.Column.int("port") |
66347 | 171 |
val password = SQL.Column.string("password") |
66349 | 172 |
val table = SQL.Table("isabelle_servers", List(name, port, password)) |
66347 | 173 |
} |
174 |
||
67807 | 175 |
def list(db: SQLite.Database): List[Info] = |
66347 | 176 |
if (db.tables.contains(Data.table.name)) { |
177 |
db.using_statement(Data.table.select())(stmt => |
|
178 |
stmt.execute_query().iterator(res => |
|
67807 | 179 |
Info( |
66349 | 180 |
res.string(Data.name), |
181 |
res.int(Data.port), |
|
182 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 183 |
} |
184 |
else Nil |
|
185 |
||
67807 | 186 |
def find(db: SQLite.Database, name: String): Option[Info] = |
187 |
list(db).find(server_info => server_info.name == name && server_info.active) |
|
66347 | 188 |
|
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
189 |
def init(name: String = "", port: Int = 0, existing_server: Boolean = false) |
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
190 |
: (Info, Option[Server]) = |
66347 | 191 |
{ |
192 |
using(SQLite.open_database(Data.database))(db => |
|
67799 | 193 |
{ |
194 |
db.transaction { |
|
195 |
Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check |
|
196 |
db.create_table(Data.table) |
|
67807 | 197 |
list(db).filterNot(_.active).foreach(server_info => |
198 |
db.using_statement(Data.table.delete(Data.name.where_equal(server_info.name)))( |
|
199 |
_.execute)) |
|
67799 | 200 |
} |
201 |
db.transaction { |
|
202 |
find(db, name) match { |
|
67807 | 203 |
case Some(server_info) => (server_info, None) |
67799 | 204 |
case None => |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
205 |
if (existing_server) { |
67812 | 206 |
error("Isabelle server " + print_name_space(name) + "not running") |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
207 |
} |
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
208 |
|
67799 | 209 |
val server = new Server(port) |
67807 | 210 |
val server_info = Info(name, server.port, server.password) |
66347 | 211 |
|
67799 | 212 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
213 |
db.using_statement(Data.table.insert())(stmt => |
|
214 |
{ |
|
67807 | 215 |
stmt.string(1) = server_info.name |
216 |
stmt.int(2) = server_info.port |
|
217 |
stmt.string(3) = server_info.password |
|
67799 | 218 |
stmt.execute() |
219 |
}) |
|
66348 | 220 |
|
67799 | 221 |
server.start |
67807 | 222 |
(server_info, Some(server)) |
67799 | 223 |
} |
66347 | 224 |
} |
225 |
}) |
|
226 |
} |
|
227 |
||
67789 | 228 |
def exit(name: String = ""): Boolean = |
66347 | 229 |
{ |
230 |
using(SQLite.open_database(Data.database))(db => |
|
231 |
db.transaction { |
|
232 |
find(db, name) match { |
|
67807 | 233 |
case Some(server_info) => |
67809 | 234 |
using(server_info.connection())(_.write_message("shutdown")) |
67807 | 235 |
while(server_info.active) { Thread.sleep(50) } |
66347 | 236 |
true |
67785 | 237 |
case None => false |
66347 | 238 |
} |
239 |
}) |
|
240 |
} |
|
241 |
||
242 |
||
243 |
/* Isabelle tool wrapper */ |
|
244 |
||
245 |
val isabelle_tool = |
|
246 |
Isabelle_Tool("server", "manage resident Isabelle servers", args => |
|
247 |
{ |
|
67806 | 248 |
var console = false |
66348 | 249 |
var operation_list = false |
250 |
var name = "" |
|
251 |
var port = 0 |
|
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
252 |
var existing_server = false |
66347 | 253 |
|
254 |
val getopts = |
|
255 |
Getopts(""" |
|
256 |
Usage: isabelle server [OPTIONS] |
|
257 |
||
258 |
Options are: |
|
67806 | 259 |
-C console interaction with specified server |
260 |
-L list servers only |
|
66348 | 261 |
-n NAME explicit server name |
262 |
-p PORT explicit server port |
|
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
263 |
-s assume existing server, no implicit startup |
66347 | 264 |
|
265 |
Manage resident Isabelle servers. |
|
266 |
""", |
|
67806 | 267 |
"C" -> (_ => console = true), |
66348 | 268 |
"L" -> (_ => operation_list = true), |
269 |
"n:" -> (arg => name = arg), |
|
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
270 |
"p:" -> (arg => port = Value.Int.parse(arg)), |
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
271 |
"s" -> (_ => existing_server = true)) |
66347 | 272 |
|
273 |
val more_args = getopts(args) |
|
66348 | 274 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 275 |
|
66353 | 276 |
if (operation_list) { |
67807 | 277 |
for { |
278 |
server_info <- using(SQLite.open_database(Data.database))(list(_)) |
|
279 |
if server_info.active |
|
280 |
} Output.writeln(server_info.toString, stdout = true) |
|
66353 | 281 |
} |
66348 | 282 |
else { |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
283 |
val (server_info, server) = init(name, port = port, existing_server = existing_server) |
67807 | 284 |
Output.writeln(server_info.toString, stdout = true) |
285 |
if (console) server_info.console() |
|
67785 | 286 |
server.foreach(_.join) |
66348 | 287 |
} |
66347 | 288 |
}) |
289 |
} |
|
290 |
||
66352 | 291 |
class Server private(_port: Int) |
66347 | 292 |
{ |
67785 | 293 |
server => |
294 |
||
66350 | 295 |
private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1")) |
67791 | 296 |
|
297 |
def close() { server_socket.close } |
|
298 |
||
66350 | 299 |
def port: Int = server_socket.getLocalPort |
67787 | 300 |
val password: String = Library.UUID() |
66350 | 301 |
|
67787 | 302 |
override def toString: String = Server.print(port, password) |
66348 | 303 |
|
67786 | 304 |
private def handle(connection: Server.Connection) |
66350 | 305 |
{ |
67809 | 306 |
connection.read_message() match { |
307 |
case Some(msg) if msg == password => |
|
67796 | 308 |
connection.reply_ok(JSON.empty) |
66350 | 309 |
var finished = false |
310 |
while (!finished) { |
|
67809 | 311 |
connection.read_message() match { |
67786 | 312 |
case None => finished = true |
67801 | 313 |
case Some("") => |
314 |
connection.notify_message("Command 'help' provides list of commands") |
|
67809 | 315 |
case Some(msg) => |
316 |
val (cmd, input) = Server.split_message(msg) |
|
66929 | 317 |
Server.commands.get(cmd) match { |
67786 | 318 |
case None => connection.reply_error("Bad command " + quote(cmd)) |
66929 | 319 |
case Some(body) => |
67794 | 320 |
input match { |
66929 | 321 |
case JSON.Format(arg) => |
67785 | 322 |
if (body.isDefinedAt((server, arg))) { |
67786 | 323 |
try { connection.reply_ok(body(server, arg)) } |
324 |
catch { case ERROR(msg) => connection.reply_error(msg) } |
|
66929 | 325 |
} |
326 |
else { |
|
67786 | 327 |
connection.reply_error_message( |
66929 | 328 |
"Bad argument for command", "command" -> cmd, "argument" -> arg) |
329 |
} |
|
330 |
case _ => |
|
67786 | 331 |
connection.reply_error_message( |
67809 | 332 |
"Malformed message", "command" -> cmd, "input" -> input) |
66929 | 333 |
} |
66921 | 334 |
} |
66350 | 335 |
} |
336 |
} |
|
67796 | 337 |
case _ => |
66350 | 338 |
} |
339 |
} |
|
340 |
||
67786 | 341 |
private lazy val server_thread: Thread = |
66350 | 342 |
Standard_Thread.fork("server") { |
343 |
var finished = false |
|
344 |
while (!finished) { |
|
345 |
Exn.capture(server_socket.accept) match { |
|
346 |
case Exn.Res(socket) => |
|
347 |
Standard_Thread.fork("server_connection") |
|
67786 | 348 |
{ using(Server.Connection(socket))(handle(_)) } |
66350 | 349 |
case Exn.Exn(_) => finished = true |
350 |
} |
|
351 |
} |
|
352 |
} |
|
67785 | 353 |
|
67790
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
354 |
def start { server_thread } |
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
355 |
|
67791 | 356 |
def join { server_thread.join; close() } |
66347 | 357 |
} |