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