author | wenzelm |
Fri, 09 Mar 2018 15:36:27 +0100 | |
changeset 67800 | fd30e767d900 |
parent 67799 | f801cb14a0b3 |
child 67801 | 8f5f5fbe291b |
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} |
|
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 |
{ |
|
34 |
val OK, ERROR = 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 |
||
65 |
val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF8.charset)) |
|
66 |
val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream, UTF8.charset)) |
|
67 |
||
68 |
def read_line(): Option[String] = |
|
67793 | 69 |
Exn.capture { reader.readLine() } match { |
70 |
case Exn.Res(null) => None |
|
71 |
case Exn.Res(line) => Some(line) |
|
72 |
case Exn.Exn(_: SocketException) => None |
|
73 |
case Exn.Exn(exn) => throw exn |
|
67786 | 74 |
} |
75 |
||
76 |
def write_line(msg: String) |
|
77 |
{ |
|
78 |
require(split_lines(msg).length <= 1) |
|
79 |
writer.write(msg) |
|
80 |
writer.newLine() |
|
67797 | 81 |
try { writer.flush() } catch { case _: SocketException => } |
67786 | 82 |
} |
83 |
||
84 |
def reply(r: Server.Reply.Value, t: JSON.T) |
|
85 |
{ |
|
86 |
write_line(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t)) |
|
87 |
} |
|
88 |
||
89 |
def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) } |
|
90 |
def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) } |
|
91 |
def reply_error_message(message: String, more: (String, JSON.T)*): Unit = |
|
92 |
reply_error(Map("message" -> message) ++ more) |
|
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 |
} |
|
66347 | 135 |
} |
136 |
} |
|
137 |
||
138 |
def list(db: SQLite.Database): List[Data.Entry] = |
|
139 |
if (db.tables.contains(Data.table.name)) { |
|
140 |
db.using_statement(Data.table.select())(stmt => |
|
141 |
stmt.execute_query().iterator(res => |
|
142 |
Data.Entry( |
|
66349 | 143 |
res.string(Data.name), |
144 |
res.int(Data.port), |
|
145 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 146 |
} |
147 |
else Nil |
|
148 |
||
149 |
def find(db: SQLite.Database, name: String): Option[Data.Entry] = |
|
66353 | 150 |
list(db).find(entry => entry.name == name && entry.active) |
66347 | 151 |
|
67789 | 152 |
def init(name: String = "", port: Int = 0): (Data.Entry, Option[Server]) = |
66347 | 153 |
{ |
154 |
using(SQLite.open_database(Data.database))(db => |
|
67799 | 155 |
{ |
156 |
db.transaction { |
|
157 |
Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check |
|
158 |
db.create_table(Data.table) |
|
159 |
list(db).filterNot(_.active).foreach(entry => |
|
160 |
db.using_statement(Data.table.delete(Data.name.where_equal(entry.name)))(_.execute)) |
|
161 |
} |
|
162 |
db.transaction { |
|
163 |
find(db, name) match { |
|
164 |
case Some(entry) => (entry, None) |
|
165 |
case None => |
|
166 |
val server = new Server(port) |
|
167 |
val entry = Data.Entry(name, server.port, server.password) |
|
66347 | 168 |
|
67799 | 169 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
170 |
db.using_statement(Data.table.insert())(stmt => |
|
171 |
{ |
|
172 |
stmt.string(1) = entry.name |
|
173 |
stmt.int(2) = entry.port |
|
174 |
stmt.string(3) = entry.password |
|
175 |
stmt.execute() |
|
176 |
}) |
|
66348 | 177 |
|
67799 | 178 |
server.start |
179 |
(entry, Some(server)) |
|
180 |
} |
|
66347 | 181 |
} |
182 |
}) |
|
183 |
} |
|
184 |
||
67789 | 185 |
def exit(name: String = ""): Boolean = |
66347 | 186 |
{ |
187 |
using(SQLite.open_database(Data.database))(db => |
|
188 |
db.transaction { |
|
189 |
find(db, name) match { |
|
190 |
case Some(entry) => |
|
67797 | 191 |
using(entry.connection())(_.write_line("shutdown")) |
192 |
while(entry.active) { Thread.sleep(50) } |
|
66347 | 193 |
true |
67785 | 194 |
case None => false |
66347 | 195 |
} |
196 |
}) |
|
197 |
} |
|
198 |
||
199 |
||
200 |
/* Isabelle tool wrapper */ |
|
201 |
||
202 |
val isabelle_tool = |
|
203 |
Isabelle_Tool("server", "manage resident Isabelle servers", args => |
|
204 |
{ |
|
66348 | 205 |
var operation_list = false |
206 |
var name = "" |
|
207 |
var port = 0 |
|
66347 | 208 |
|
209 |
val getopts = |
|
210 |
Getopts(""" |
|
211 |
Usage: isabelle server [OPTIONS] |
|
212 |
||
213 |
Options are: |
|
66348 | 214 |
-L list servers |
215 |
-n NAME explicit server name |
|
216 |
-p PORT explicit server port |
|
66347 | 217 |
|
218 |
Manage resident Isabelle servers. |
|
219 |
""", |
|
66348 | 220 |
"L" -> (_ => operation_list = true), |
221 |
"n:" -> (arg => name = arg), |
|
222 |
"p:" -> (arg => port = Value.Int.parse(arg))) |
|
66347 | 223 |
|
224 |
val more_args = getopts(args) |
|
66348 | 225 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 226 |
|
66353 | 227 |
if (operation_list) { |
228 |
for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active) |
|
67787 | 229 |
Output.writeln(entry.toString, stdout = true) |
66353 | 230 |
} |
66348 | 231 |
else { |
67789 | 232 |
val (entry, server) = init(name, port) |
67787 | 233 |
Output.writeln(entry.toString, stdout = true) |
67785 | 234 |
server.foreach(_.join) |
66348 | 235 |
} |
66347 | 236 |
}) |
237 |
} |
|
238 |
||
66352 | 239 |
class Server private(_port: Int) |
66347 | 240 |
{ |
67785 | 241 |
server => |
242 |
||
66350 | 243 |
private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1")) |
67791 | 244 |
|
245 |
def close() { server_socket.close } |
|
246 |
||
66350 | 247 |
def port: Int = server_socket.getLocalPort |
67787 | 248 |
val password: String = Library.UUID() |
66350 | 249 |
|
67787 | 250 |
override def toString: String = Server.print(port, password) |
66348 | 251 |
|
67786 | 252 |
private def handle(connection: Server.Connection) |
66350 | 253 |
{ |
67786 | 254 |
connection.read_line() match { |
67796 | 255 |
case Some(line) if line == password => |
256 |
connection.reply_ok(JSON.empty) |
|
66350 | 257 |
var finished = false |
258 |
while (!finished) { |
|
67786 | 259 |
connection.read_line() match { |
260 |
case None => finished = true |
|
67795 | 261 |
case Some(line) if line != "" => |
67794 | 262 |
val (cmd, input) = Server.split_line(line) |
66929 | 263 |
Server.commands.get(cmd) match { |
67786 | 264 |
case None => connection.reply_error("Bad command " + quote(cmd)) |
66929 | 265 |
case Some(body) => |
67794 | 266 |
input match { |
66929 | 267 |
case JSON.Format(arg) => |
67785 | 268 |
if (body.isDefinedAt((server, arg))) { |
67786 | 269 |
try { connection.reply_ok(body(server, arg)) } |
270 |
catch { case ERROR(msg) => connection.reply_error(msg) } |
|
66929 | 271 |
} |
272 |
else { |
|
67786 | 273 |
connection.reply_error_message( |
66929 | 274 |
"Bad argument for command", "command" -> cmd, "argument" -> arg) |
275 |
} |
|
276 |
case _ => |
|
67786 | 277 |
connection.reply_error_message( |
66929 | 278 |
"Malformed command-line", "command" -> cmd, "input" -> input) |
279 |
} |
|
66921 | 280 |
} |
281 |
case _ => |
|
66350 | 282 |
} |
283 |
} |
|
67796 | 284 |
case _ => |
66350 | 285 |
} |
286 |
} |
|
287 |
||
67786 | 288 |
private lazy val server_thread: Thread = |
66350 | 289 |
Standard_Thread.fork("server") { |
290 |
var finished = false |
|
291 |
while (!finished) { |
|
292 |
Exn.capture(server_socket.accept) match { |
|
293 |
case Exn.Res(socket) => |
|
294 |
Standard_Thread.fork("server_connection") |
|
67786 | 295 |
{ using(Server.Connection(socket))(handle(_)) } |
66350 | 296 |
case Exn.Exn(_) => finished = true |
297 |
} |
|
298 |
} |
|
299 |
} |
|
67785 | 300 |
|
67790
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
301 |
def start { server_thread } |
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
302 |
|
67791 | 303 |
def join { server_thread.join; close() } |
66347 | 304 |
} |