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 |
|
|
19 |
object Reply extends Enumeration
|
|
20 |
{
|
|
21 |
val OK, ERROR = Value
|
|
22 |
}
|
|
23 |
|
|
24 |
|
66347
|
25 |
/* per-user servers */
|
|
26 |
|
|
27 |
object Data
|
|
28 |
{
|
66349
|
29 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
|
66347
|
30 |
|
66857
|
31 |
val name = SQL.Column.string("name").make_primary_key
|
66349
|
32 |
val port = SQL.Column.int("port")
|
66347
|
33 |
val password = SQL.Column.string("password")
|
66349
|
34 |
val table = SQL.Table("isabelle_servers", List(name, port, password))
|
66347
|
35 |
|
66349
|
36 |
sealed case class Entry(name: String, port: Int, password: String)
|
66347
|
37 |
{
|
66348
|
38 |
def print: String =
|
66349
|
39 |
"server " + quote(name) + " = 127.0.0.1:" + port + " (password " + quote(password) + ")"
|
66353
|
40 |
|
|
41 |
def active: Boolean =
|
|
42 |
try { (new Socket(InetAddress.getByName("127.0.0.1"), port)).close; true }
|
|
43 |
catch { case _: IOException => false }
|
66347
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
def list(db: SQLite.Database): List[Data.Entry] =
|
|
48 |
if (db.tables.contains(Data.table.name)) {
|
|
49 |
db.using_statement(Data.table.select())(stmt =>
|
|
50 |
stmt.execute_query().iterator(res =>
|
|
51 |
Data.Entry(
|
66349
|
52 |
res.string(Data.name),
|
|
53 |
res.int(Data.port),
|
|
54 |
res.string(Data.password))).toList.sortBy(_.name))
|
66347
|
55 |
}
|
|
56 |
else Nil
|
|
57 |
|
|
58 |
def find(db: SQLite.Database, name: String): Option[Data.Entry] =
|
66353
|
59 |
list(db).find(entry => entry.name == name && entry.active)
|
66347
|
60 |
|
66352
|
61 |
def start(name: String = "", port: Int = 0): (Data.Entry, Option[Thread]) =
|
66347
|
62 |
{
|
|
63 |
using(SQLite.open_database(Data.database))(db =>
|
|
64 |
db.transaction {
|
|
65 |
find(db, name) match {
|
66348
|
66 |
case Some(entry) => (entry, None)
|
66347
|
67 |
case None =>
|
66352
|
68 |
val server = new Server(port)
|
66347
|
69 |
val entry = Data.Entry(name, server.port, server.password)
|
|
70 |
|
|
71 |
Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check
|
|
72 |
db.create_table(Data.table)
|
66353
|
73 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute)
|
66347
|
74 |
db.using_statement(Data.table.insert())(stmt =>
|
|
75 |
{
|
66349
|
76 |
stmt.string(1) = entry.name
|
|
77 |
stmt.int(2) = entry.port
|
66347
|
78 |
stmt.string(3) = entry.password
|
|
79 |
stmt.execute()
|
|
80 |
})
|
66348
|
81 |
|
|
82 |
(entry, Some(server.thread))
|
66347
|
83 |
}
|
|
84 |
})
|
|
85 |
}
|
|
86 |
|
|
87 |
def stop(name: String = ""): Boolean =
|
|
88 |
{
|
|
89 |
using(SQLite.open_database(Data.database))(db =>
|
|
90 |
db.transaction {
|
|
91 |
find(db, name) match {
|
|
92 |
case Some(entry) =>
|
|
93 |
// FIXME shutdown server
|
66349
|
94 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute)
|
66347
|
95 |
true
|
|
96 |
case None =>
|
|
97 |
false
|
|
98 |
}
|
|
99 |
})
|
|
100 |
}
|
|
101 |
|
|
102 |
|
|
103 |
/* Isabelle tool wrapper */
|
|
104 |
|
|
105 |
val isabelle_tool =
|
|
106 |
Isabelle_Tool("server", "manage resident Isabelle servers", args =>
|
|
107 |
{
|
66348
|
108 |
var operation_list = false
|
|
109 |
var name = ""
|
|
110 |
var port = 0
|
66347
|
111 |
|
|
112 |
val getopts =
|
|
113 |
Getopts("""
|
|
114 |
Usage: isabelle server [OPTIONS]
|
|
115 |
|
|
116 |
Options are:
|
66348
|
117 |
-L list servers
|
|
118 |
-n NAME explicit server name
|
|
119 |
-p PORT explicit server port
|
66347
|
120 |
|
|
121 |
Manage resident Isabelle servers.
|
|
122 |
""",
|
66348
|
123 |
"L" -> (_ => operation_list = true),
|
|
124 |
"n:" -> (arg => name = arg),
|
|
125 |
"p:" -> (arg => port = Value.Int.parse(arg)))
|
66347
|
126 |
|
|
127 |
val more_args = getopts(args)
|
66348
|
128 |
if (more_args.nonEmpty) getopts.usage()
|
66347
|
129 |
|
66353
|
130 |
if (operation_list) {
|
|
131 |
for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active)
|
|
132 |
Console.println(entry.print)
|
|
133 |
}
|
66348
|
134 |
else {
|
|
135 |
val (entry, thread) = start(name, port)
|
|
136 |
Console.println(entry.print)
|
|
137 |
thread.foreach(_.join)
|
|
138 |
}
|
66347
|
139 |
})
|
|
140 |
}
|
|
141 |
|
66352
|
142 |
class Server private(_port: Int)
|
66347
|
143 |
{
|
66350
|
144 |
private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1"))
|
|
145 |
def port: Int = server_socket.getLocalPort
|
|
146 |
def close { server_socket.close }
|
|
147 |
|
66352
|
148 |
val password: String = Library.UUID()
|
66348
|
149 |
|
66350
|
150 |
private def handle_connection(socket: Socket)
|
|
151 |
{
|
|
152 |
val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF8.charset))
|
|
153 |
val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream, UTF8.charset))
|
|
154 |
|
66921
|
155 |
val Blank_Line = """^\s*$""".r
|
|
156 |
val Command_Line = """^(\S+)\s*(.*)$""".r
|
|
157 |
|
66927
|
158 |
def reply_line(msg: String)
|
66350
|
159 |
{
|
66927
|
160 |
require(split_lines(msg).length <= 1)
|
|
161 |
writer.write(msg)
|
66350
|
162 |
writer.newLine()
|
|
163 |
writer.flush()
|
|
164 |
}
|
|
165 |
|
66927
|
166 |
def reply(r: Server.Reply.Value, t: JSON.T)
|
|
167 |
{
|
|
168 |
reply_line(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t))
|
|
169 |
}
|
|
170 |
|
|
171 |
def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) }
|
|
172 |
def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) }
|
66921
|
173 |
|
66350
|
174 |
reader.readLine() match {
|
|
175 |
case null =>
|
66927
|
176 |
case bad if bad != password => reply_error("Bad password -- connection closed")
|
66350
|
177 |
case _ =>
|
|
178 |
var finished = false
|
|
179 |
while (!finished) {
|
|
180 |
reader.readLine() match {
|
66921
|
181 |
case null => finished = true
|
|
182 |
case Blank_Line() =>
|
|
183 |
case Command_Line(cmd, arg) =>
|
|
184 |
proper_string(arg) getOrElse "{}" match {
|
|
185 |
case JSON.Format(json) =>
|
66927
|
186 |
reply_ok(Map("command" -> cmd, "argument" -> json))
|
66921
|
187 |
case _ =>
|
66927
|
188 |
reply_error(
|
|
189 |
Map("message" -> "Malformed command", "command" -> cmd, "input" -> arg))
|
66921
|
190 |
}
|
|
191 |
case _ =>
|
66350
|
192 |
}
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
lazy val thread: Thread =
|
|
198 |
Standard_Thread.fork("server") {
|
|
199 |
var finished = false
|
|
200 |
while (!finished) {
|
|
201 |
Exn.capture(server_socket.accept) match {
|
|
202 |
case Exn.Res(socket) =>
|
|
203 |
Standard_Thread.fork("server_connection")
|
|
204 |
{ try { handle_connection(socket) } finally { socket.close } }
|
|
205 |
case Exn.Exn(_) => finished = true
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
66347
|
209 |
}
|