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