author | wenzelm |
Fri, 09 Mar 2018 15:24:19 +0100 | |
changeset 67799 | f801cb14a0b3 |
parent 67798 | d327558f776a |
child 67800 | fd30e767d900 |
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 |
|
35 |
} |
|
36 |
||
37 |
||
67786 | 38 |
/* socket connection */ |
39 |
||
40 |
object Connection |
|
41 |
{ |
|
42 |
def apply(socket: Socket): Connection = |
|
43 |
new Connection(socket) |
|
44 |
} |
|
45 |
||
67788 | 46 |
class Connection private(val socket: Socket) |
67786 | 47 |
{ |
48 |
override def toString: String = socket.toString |
|
49 |
||
50 |
def close() { socket.close } |
|
51 |
||
52 |
val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF8.charset)) |
|
53 |
val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream, UTF8.charset)) |
|
54 |
||
55 |
def read_line(): Option[String] = |
|
67793 | 56 |
Exn.capture { reader.readLine() } match { |
57 |
case Exn.Res(null) => None |
|
58 |
case Exn.Res(line) => Some(line) |
|
59 |
case Exn.Exn(_: SocketException) => None |
|
60 |
case Exn.Exn(exn) => throw exn |
|
67786 | 61 |
} |
62 |
||
63 |
def write_line(msg: String) |
|
64 |
{ |
|
65 |
require(split_lines(msg).length <= 1) |
|
66 |
writer.write(msg) |
|
67 |
writer.newLine() |
|
67797 | 68 |
try { writer.flush() } catch { case _: SocketException => } |
67786 | 69 |
} |
70 |
||
71 |
def reply(r: Server.Reply.Value, t: JSON.T) |
|
72 |
{ |
|
73 |
write_line(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t)) |
|
74 |
} |
|
75 |
||
76 |
def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) } |
|
77 |
def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) } |
|
78 |
def reply_error_message(message: String, more: (String, JSON.T)*): Unit = |
|
79 |
reply_error(Map("message" -> message) ++ more) |
|
80 |
} |
|
81 |
||
82 |
||
66347 | 83 |
/* per-user servers */ |
84 |
||
67787 | 85 |
def print(port: Int, password: String): String = |
86 |
"127.0.0.1:" + port + " (password " + quote(password) + ")" |
|
87 |
||
66347 | 88 |
object Data |
89 |
{ |
|
66349 | 90 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db") |
66347 | 91 |
|
66857 | 92 |
val name = SQL.Column.string("name").make_primary_key |
66349 | 93 |
val port = SQL.Column.int("port") |
66347 | 94 |
val password = SQL.Column.string("password") |
66349 | 95 |
val table = SQL.Table("isabelle_servers", List(name, port, password)) |
66347 | 96 |
|
66349 | 97 |
sealed case class Entry(name: String, port: Int, password: String) |
66347 | 98 |
{ |
67787 | 99 |
override def toString: String = |
100 |
"server " + quote(name) + " = " + print(port, password) |
|
66353 | 101 |
|
67786 | 102 |
def connection(): Connection = |
67797 | 103 |
{ |
104 |
val connection = Connection(new Socket(InetAddress.getByName("127.0.0.1"), port)) |
|
105 |
connection.write_line(password) |
|
106 |
connection |
|
107 |
} |
|
67785 | 108 |
|
109 |
def active(): Boolean = |
|
67797 | 110 |
try { |
111 |
using(connection())(connection => |
|
112 |
{ |
|
67798 | 113 |
connection.socket.setSoTimeout(2000) |
67797 | 114 |
connection.read_line() == Some(Reply.OK.toString) |
115 |
}) |
|
116 |
} |
|
117 |
catch { |
|
118 |
case _: IOException => false |
|
119 |
case _: SocketException => false |
|
120 |
case _: SocketTimeoutException => false |
|
121 |
} |
|
66347 | 122 |
} |
123 |
} |
|
124 |
||
125 |
def list(db: SQLite.Database): List[Data.Entry] = |
|
126 |
if (db.tables.contains(Data.table.name)) { |
|
127 |
db.using_statement(Data.table.select())(stmt => |
|
128 |
stmt.execute_query().iterator(res => |
|
129 |
Data.Entry( |
|
66349 | 130 |
res.string(Data.name), |
131 |
res.int(Data.port), |
|
132 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 133 |
} |
134 |
else Nil |
|
135 |
||
136 |
def find(db: SQLite.Database, name: String): Option[Data.Entry] = |
|
66353 | 137 |
list(db).find(entry => entry.name == name && entry.active) |
66347 | 138 |
|
67789 | 139 |
def init(name: String = "", port: Int = 0): (Data.Entry, Option[Server]) = |
66347 | 140 |
{ |
141 |
using(SQLite.open_database(Data.database))(db => |
|
67799 | 142 |
{ |
143 |
db.transaction { |
|
144 |
Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check |
|
145 |
db.create_table(Data.table) |
|
146 |
list(db).filterNot(_.active).foreach(entry => |
|
147 |
db.using_statement(Data.table.delete(Data.name.where_equal(entry.name)))(_.execute)) |
|
148 |
} |
|
149 |
db.transaction { |
|
150 |
find(db, name) match { |
|
151 |
case Some(entry) => (entry, None) |
|
152 |
case None => |
|
153 |
val server = new Server(port) |
|
154 |
val entry = Data.Entry(name, server.port, server.password) |
|
66347 | 155 |
|
67799 | 156 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
157 |
db.using_statement(Data.table.insert())(stmt => |
|
158 |
{ |
|
159 |
stmt.string(1) = entry.name |
|
160 |
stmt.int(2) = entry.port |
|
161 |
stmt.string(3) = entry.password |
|
162 |
stmt.execute() |
|
163 |
}) |
|
66348 | 164 |
|
67799 | 165 |
server.start |
166 |
(entry, Some(server)) |
|
167 |
} |
|
66347 | 168 |
} |
169 |
}) |
|
170 |
} |
|
171 |
||
67789 | 172 |
def exit(name: String = ""): Boolean = |
66347 | 173 |
{ |
174 |
using(SQLite.open_database(Data.database))(db => |
|
175 |
db.transaction { |
|
176 |
find(db, name) match { |
|
177 |
case Some(entry) => |
|
67797 | 178 |
using(entry.connection())(_.write_line("shutdown")) |
179 |
while(entry.active) { Thread.sleep(50) } |
|
66347 | 180 |
true |
67785 | 181 |
case None => false |
66347 | 182 |
} |
183 |
}) |
|
184 |
} |
|
185 |
||
186 |
||
187 |
/* Isabelle tool wrapper */ |
|
188 |
||
189 |
val isabelle_tool = |
|
190 |
Isabelle_Tool("server", "manage resident Isabelle servers", args => |
|
191 |
{ |
|
66348 | 192 |
var operation_list = false |
193 |
var name = "" |
|
194 |
var port = 0 |
|
66347 | 195 |
|
196 |
val getopts = |
|
197 |
Getopts(""" |
|
198 |
Usage: isabelle server [OPTIONS] |
|
199 |
||
200 |
Options are: |
|
66348 | 201 |
-L list servers |
202 |
-n NAME explicit server name |
|
203 |
-p PORT explicit server port |
|
66347 | 204 |
|
205 |
Manage resident Isabelle servers. |
|
206 |
""", |
|
66348 | 207 |
"L" -> (_ => operation_list = true), |
208 |
"n:" -> (arg => name = arg), |
|
209 |
"p:" -> (arg => port = Value.Int.parse(arg))) |
|
66347 | 210 |
|
211 |
val more_args = getopts(args) |
|
66348 | 212 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 213 |
|
66353 | 214 |
if (operation_list) { |
215 |
for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active) |
|
67787 | 216 |
Output.writeln(entry.toString, stdout = true) |
66353 | 217 |
} |
66348 | 218 |
else { |
67789 | 219 |
val (entry, server) = init(name, port) |
67787 | 220 |
Output.writeln(entry.toString, stdout = true) |
67785 | 221 |
server.foreach(_.join) |
66348 | 222 |
} |
66347 | 223 |
}) |
224 |
} |
|
225 |
||
66352 | 226 |
class Server private(_port: Int) |
66347 | 227 |
{ |
67785 | 228 |
server => |
229 |
||
66350 | 230 |
private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1")) |
67791 | 231 |
|
232 |
def close() { server_socket.close } |
|
233 |
||
66350 | 234 |
def port: Int = server_socket.getLocalPort |
67787 | 235 |
val password: String = Library.UUID() |
66350 | 236 |
|
67787 | 237 |
override def toString: String = Server.print(port, password) |
66348 | 238 |
|
67786 | 239 |
private def handle(connection: Server.Connection) |
66350 | 240 |
{ |
67786 | 241 |
connection.read_line() match { |
67796 | 242 |
case Some(line) if line == password => |
243 |
connection.reply_ok(JSON.empty) |
|
66350 | 244 |
var finished = false |
245 |
while (!finished) { |
|
67786 | 246 |
connection.read_line() match { |
247 |
case None => finished = true |
|
67795 | 248 |
case Some(line) if line != "" => |
67794 | 249 |
val (cmd, input) = Server.split_line(line) |
66929 | 250 |
Server.commands.get(cmd) match { |
67786 | 251 |
case None => connection.reply_error("Bad command " + quote(cmd)) |
66929 | 252 |
case Some(body) => |
67794 | 253 |
input match { |
66929 | 254 |
case JSON.Format(arg) => |
67785 | 255 |
if (body.isDefinedAt((server, arg))) { |
67786 | 256 |
try { connection.reply_ok(body(server, arg)) } |
257 |
catch { case ERROR(msg) => connection.reply_error(msg) } |
|
66929 | 258 |
} |
259 |
else { |
|
67786 | 260 |
connection.reply_error_message( |
66929 | 261 |
"Bad argument for command", "command" -> cmd, "argument" -> arg) |
262 |
} |
|
263 |
case _ => |
|
67786 | 264 |
connection.reply_error_message( |
66929 | 265 |
"Malformed command-line", "command" -> cmd, "input" -> input) |
266 |
} |
|
66921 | 267 |
} |
268 |
case _ => |
|
66350 | 269 |
} |
270 |
} |
|
67796 | 271 |
case _ => |
66350 | 272 |
} |
273 |
} |
|
274 |
||
67786 | 275 |
private lazy val server_thread: Thread = |
66350 | 276 |
Standard_Thread.fork("server") { |
277 |
var finished = false |
|
278 |
while (!finished) { |
|
279 |
Exn.capture(server_socket.accept) match { |
|
280 |
case Exn.Res(socket) => |
|
281 |
Standard_Thread.fork("server_connection") |
|
67786 | 282 |
{ using(Server.Connection(socket))(handle(_)) } |
66350 | 283 |
case Exn.Exn(_) => finished = true |
284 |
} |
|
285 |
} |
|
286 |
} |
|
67785 | 287 |
|
67790
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
288 |
def start { server_thread } |
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
289 |
|
67791 | 290 |
def join { server_thread.join; close() } |
66347 | 291 |
} |