author | wenzelm |
Fri, 09 Mar 2018 12:07:47 +0100 | |
changeset 67785 | ad96390ceb5d |
parent 67784 | 543e36ae489c |
child 67786 | be6d69595ca7 |
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 |
||
66353 | 10 |
import java.io.{BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, |
11 |
IOException} |
|
66350 | 12 |
import java.net.{Socket, ServerSocket, InetAddress} |
66347 | 13 |
|
14 |
||
15 |
object Server |
|
16 |
{ |
|
66927 | 17 |
/* protocol */ |
18 |
||
67785 | 19 |
val commands: Map[String, PartialFunction[(Server, JSON.T), JSON.T]] = |
66929 | 20 |
Map( |
67785 | 21 |
"echo" -> { case (_, t) => t }, |
22 |
"help" -> { case (_, JSON.empty) => commands.keySet.toList.sorted }, |
|
23 |
"shutdown" -> { case (server, JSON.empty) => server.shutdown(); JSON.empty }) |
|
66929 | 24 |
|
66927 | 25 |
object Reply extends Enumeration |
26 |
{ |
|
27 |
val OK, ERROR = Value |
|
28 |
} |
|
29 |
||
30 |
||
66347 | 31 |
/* per-user servers */ |
32 |
||
33 |
object Data |
|
34 |
{ |
|
66349 | 35 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db") |
66347 | 36 |
|
66857 | 37 |
val name = SQL.Column.string("name").make_primary_key |
66349 | 38 |
val port = SQL.Column.int("port") |
66347 | 39 |
val password = SQL.Column.string("password") |
66349 | 40 |
val table = SQL.Table("isabelle_servers", List(name, port, password)) |
66347 | 41 |
|
66349 | 42 |
sealed case class Entry(name: String, port: Int, password: String) |
66347 | 43 |
{ |
66348 | 44 |
def print: String = |
66349 | 45 |
"server " + quote(name) + " = 127.0.0.1:" + port + " (password " + quote(password) + ")" |
66353 | 46 |
|
67785 | 47 |
def connect(): Socket = |
48 |
new Socket(InetAddress.getByName("127.0.0.1"), port) |
|
49 |
||
50 |
def active(): Boolean = |
|
51 |
try { connect().close; true } |
|
66353 | 52 |
catch { case _: IOException => false } |
66347 | 53 |
} |
54 |
} |
|
55 |
||
56 |
def list(db: SQLite.Database): List[Data.Entry] = |
|
57 |
if (db.tables.contains(Data.table.name)) { |
|
58 |
db.using_statement(Data.table.select())(stmt => |
|
59 |
stmt.execute_query().iterator(res => |
|
60 |
Data.Entry( |
|
66349 | 61 |
res.string(Data.name), |
62 |
res.int(Data.port), |
|
63 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 64 |
} |
65 |
else Nil |
|
66 |
||
67 |
def find(db: SQLite.Database, name: String): Option[Data.Entry] = |
|
66353 | 68 |
list(db).find(entry => entry.name == name && entry.active) |
66347 | 69 |
|
67785 | 70 |
def start(name: String = "", port: Int = 0): (Data.Entry, Option[Server]) = |
66347 | 71 |
{ |
72 |
using(SQLite.open_database(Data.database))(db => |
|
73 |
db.transaction { |
|
74 |
find(db, name) match { |
|
66348 | 75 |
case Some(entry) => (entry, None) |
66347 | 76 |
case None => |
66352 | 77 |
val server = new Server(port) |
66347 | 78 |
val entry = Data.Entry(name, server.port, server.password) |
79 |
||
80 |
Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check |
|
81 |
db.create_table(Data.table) |
|
66353 | 82 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
66347 | 83 |
db.using_statement(Data.table.insert())(stmt => |
84 |
{ |
|
66349 | 85 |
stmt.string(1) = entry.name |
86 |
stmt.int(2) = entry.port |
|
66347 | 87 |
stmt.string(3) = entry.password |
88 |
stmt.execute() |
|
89 |
}) |
|
66348 | 90 |
|
67785 | 91 |
(entry, Some(server)) |
66347 | 92 |
} |
93 |
}) |
|
94 |
} |
|
95 |
||
96 |
def stop(name: String = ""): Boolean = |
|
97 |
{ |
|
98 |
using(SQLite.open_database(Data.database))(db => |
|
99 |
db.transaction { |
|
100 |
find(db, name) match { |
|
101 |
case Some(entry) => |
|
67785 | 102 |
using(entry.connect())(socket => |
103 |
{ |
|
104 |
using(socket.getOutputStream)(_.write(UTF8.bytes(entry.password + "\nshutdown"))) |
|
105 |
}) |
|
106 |
while(entry.active) { Thread.sleep(100) } |
|
66347 | 107 |
true |
67785 | 108 |
case None => false |
66347 | 109 |
} |
110 |
}) |
|
111 |
} |
|
112 |
||
113 |
||
114 |
/* Isabelle tool wrapper */ |
|
115 |
||
116 |
val isabelle_tool = |
|
117 |
Isabelle_Tool("server", "manage resident Isabelle servers", args => |
|
118 |
{ |
|
66348 | 119 |
var operation_list = false |
120 |
var name = "" |
|
121 |
var port = 0 |
|
66347 | 122 |
|
123 |
val getopts = |
|
124 |
Getopts(""" |
|
125 |
Usage: isabelle server [OPTIONS] |
|
126 |
||
127 |
Options are: |
|
66348 | 128 |
-L list servers |
129 |
-n NAME explicit server name |
|
130 |
-p PORT explicit server port |
|
66347 | 131 |
|
132 |
Manage resident Isabelle servers. |
|
133 |
""", |
|
66348 | 134 |
"L" -> (_ => operation_list = true), |
135 |
"n:" -> (arg => name = arg), |
|
136 |
"p:" -> (arg => port = Value.Int.parse(arg))) |
|
66347 | 137 |
|
138 |
val more_args = getopts(args) |
|
66348 | 139 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 140 |
|
66353 | 141 |
if (operation_list) { |
142 |
for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active) |
|
67178 | 143 |
Output.writeln(entry.print, stdout = true) |
66353 | 144 |
} |
66348 | 145 |
else { |
67785 | 146 |
val (entry, server) = start(name, port) |
67178 | 147 |
Output.writeln(entry.print, stdout = true) |
67785 | 148 |
server.foreach(_.join) |
66348 | 149 |
} |
66347 | 150 |
}) |
151 |
} |
|
152 |
||
66352 | 153 |
class Server private(_port: Int) |
66347 | 154 |
{ |
67785 | 155 |
server => |
156 |
||
66350 | 157 |
private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1")) |
158 |
def port: Int = server_socket.getLocalPort |
|
67785 | 159 |
def shutdown() { server_socket.close } |
66350 | 160 |
|
66352 | 161 |
val password: String = Library.UUID() |
66348 | 162 |
|
66350 | 163 |
private def handle_connection(socket: Socket) |
164 |
{ |
|
165 |
val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF8.charset)) |
|
166 |
val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream, UTF8.charset)) |
|
167 |
||
66927 | 168 |
def reply_line(msg: String) |
66350 | 169 |
{ |
66927 | 170 |
require(split_lines(msg).length <= 1) |
171 |
writer.write(msg) |
|
66350 | 172 |
writer.newLine() |
173 |
writer.flush() |
|
174 |
} |
|
175 |
||
66927 | 176 |
def reply(r: Server.Reply.Value, t: JSON.T) |
177 |
{ |
|
178 |
reply_line(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t)) |
|
179 |
} |
|
180 |
||
181 |
def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) } |
|
182 |
def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) } |
|
66929 | 183 |
def reply_error_message(message: String, more: (String, JSON.T)*): Unit = |
184 |
reply_error(Map("message" -> message) ++ more) |
|
185 |
||
66350 | 186 |
reader.readLine() match { |
187 |
case null => |
|
66927 | 188 |
case bad if bad != password => reply_error("Bad password -- connection closed") |
66350 | 189 |
case _ => |
190 |
var finished = false |
|
191 |
while (!finished) { |
|
192 |
reader.readLine() match { |
|
66921 | 193 |
case null => finished = true |
67784
543e36ae489c
tuned -- avoid regex matching on potentially large string;
wenzelm
parents:
67178
diff
changeset
|
194 |
case line => |
543e36ae489c
tuned -- avoid regex matching on potentially large string;
wenzelm
parents:
67178
diff
changeset
|
195 |
val cmd = line.takeWhile(c => Symbol.is_ascii_letter(c) || Symbol.is_ascii_letdig(c)) |
543e36ae489c
tuned -- avoid regex matching on potentially large string;
wenzelm
parents:
67178
diff
changeset
|
196 |
val input = line.substring(cmd.length).dropWhile(Symbol.is_ascii_blank(_)) |
66929 | 197 |
Server.commands.get(cmd) match { |
67784
543e36ae489c
tuned -- avoid regex matching on potentially large string;
wenzelm
parents:
67178
diff
changeset
|
198 |
case None => reply_error("Bad command " + quote(cmd)) |
66929 | 199 |
case Some(body) => |
200 |
proper_string(input) getOrElse "{}" match { |
|
201 |
case JSON.Format(arg) => |
|
67785 | 202 |
if (body.isDefinedAt((server, arg))) { |
203 |
try { reply_ok(body(server, arg)) } |
|
66929 | 204 |
catch { case ERROR(msg) => reply_error(msg) } |
205 |
} |
|
206 |
else { |
|
207 |
reply_error_message( |
|
208 |
"Bad argument for command", "command" -> cmd, "argument" -> arg) |
|
209 |
} |
|
210 |
case _ => |
|
211 |
reply_error_message( |
|
212 |
"Malformed command-line", "command" -> cmd, "input" -> input) |
|
213 |
} |
|
66921 | 214 |
} |
215 |
case _ => |
|
66350 | 216 |
} |
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
67785 | 221 |
private lazy val thread: Thread = |
66350 | 222 |
Standard_Thread.fork("server") { |
223 |
var finished = false |
|
224 |
while (!finished) { |
|
225 |
Exn.capture(server_socket.accept) match { |
|
226 |
case Exn.Res(socket) => |
|
227 |
Standard_Thread.fork("server_connection") |
|
228 |
{ try { handle_connection(socket) } finally { socket.close } } |
|
229 |
case Exn.Exn(_) => finished = true |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
67785 | 233 |
|
234 |
def join { thread.join; shutdown() } |
|
66347 | 235 |
} |