author | wenzelm |
Sun, 24 Jan 2021 16:37:46 +0100 | |
changeset 73179 | f9c71ce29150 |
parent 73135 | 76bdfde8a579 |
child 73340 | 0ffcad1f6130 |
permissions | -rw-r--r-- |
66347 | 1 |
/* Title: Pure/Tools/server.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Resident Isabelle servers. |
|
67809 | 5 |
|
6 |
Message formats: |
|
7 |
- short message (single line): |
|
8 |
NAME ARGUMENT |
|
9 |
- long message (multiple lines): |
|
10 |
BYTE_LENGTH |
|
11 |
NAME ARGUMENT |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
12 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
13 |
Argument formats: |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
14 |
- Unit as empty string |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
15 |
- XML.Elem in YXML notation |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
16 |
- JSON.T in standard notation |
66347 | 17 |
*/ |
18 |
||
19 |
package isabelle |
|
20 |
||
21 |
||
67837 | 22 |
import java.io.{BufferedInputStream, BufferedOutputStream, InputStreamReader, OutputStreamWriter, |
23 |
IOException} |
|
67797 | 24 |
import java.net.{Socket, SocketException, SocketTimeoutException, ServerSocket, InetAddress} |
66347 | 25 |
|
26 |
||
27 |
object Server |
|
28 |
{ |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
29 |
/* message argument */ |
66927 | 30 |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
31 |
object Argument |
67794 | 32 |
{ |
67903 | 33 |
def is_name_char(c: Char): Boolean = |
34 |
Symbol.is_ascii_letter(c) || Symbol.is_ascii_digit(c) || c == '_' || c == '.' |
|
35 |
||
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
36 |
def split(msg: String): (String, String) = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
37 |
{ |
71601 | 38 |
val name = msg.takeWhile(is_name_char) |
39 |
val argument = msg.substring(name.length).dropWhile(Symbol.is_ascii_blank) |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
40 |
(name, argument) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
41 |
} |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
42 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
43 |
def print(arg: Any): String = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
44 |
arg match { |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
45 |
case () => "" |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
46 |
case t: XML.Elem => YXML.string_of_tree(t) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
47 |
case t: JSON.T => JSON.Format(t) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
48 |
} |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
49 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
50 |
def parse(argument: String): Any = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
51 |
if (argument == "") () |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
52 |
else if (YXML.detect_elem(argument)) YXML.parse_elem(argument) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
53 |
else JSON.parse(argument, strict = false) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
54 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
55 |
def unapply(argument: String): Option[Any] = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
56 |
try { Some(parse(argument)) } |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
57 |
catch { case ERROR(_) => None } |
67794 | 58 |
} |
59 |
||
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
60 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
61 |
/* input command */ |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
62 |
|
72163
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
63 |
type Command_Body = PartialFunction[(Context, Any), Any] |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
64 |
|
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
65 |
abstract class Command(val command_name: String) |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
66 |
{ |
72163
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
67 |
def command_body: Command_Body |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
68 |
override def toString: String = command_name |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
69 |
} |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
70 |
|
72163
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
71 |
class Commands(commands: Command*) extends Isabelle_System.Service |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
72 |
{ |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
73 |
def entries: List[Command] = commands.toList |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
74 |
} |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
75 |
|
72163
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
76 |
private lazy val command_table: Map[String, Command] = |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
77 |
(Map.empty[String, Command] /: Isabelle_System.make_services(classOf[Commands]).flatMap(_.entries))( |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
78 |
{ case (cmds, cmd) => |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
79 |
val name = cmd.command_name |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
80 |
if (cmds.isDefinedAt(name)) error("Duplicate Isabelle server command: " + quote(name)) |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
81 |
else cmds + (name -> cmd) |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
82 |
}) |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
83 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
84 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
85 |
/* output reply */ |
66929 | 86 |
|
67857 | 87 |
class Error(val message: String, val json: JSON.Object.T = JSON.Object.empty) |
88 |
extends RuntimeException(message) |
|
89 |
||
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
90 |
def json_error(exn: Throwable): JSON.Object.T = |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
91 |
exn match { |
67913
d58fa3ed236f
proper order of matches: Server.Error is an instance of Exn.ERROR;
wenzelm
parents:
67911
diff
changeset
|
92 |
case e: Error => Reply.error_message(e.message) ++ e.json |
67901 | 93 |
case ERROR(msg) => Reply.error_message(msg) |
94 |
case _ if Exn.is_interrupt(exn) => Reply.error_message(Exn.message(exn)) |
|
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
95 |
case _ => JSON.Object.empty |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
96 |
} |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
97 |
|
66927 | 98 |
object Reply extends Enumeration |
99 |
{ |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
100 |
val OK, ERROR, FINISHED, FAILED, NOTE = Value |
67800 | 101 |
|
67901 | 102 |
def message(msg: String, kind: String = ""): JSON.Object.T = |
67931
f7917c15b566
field "kind" is always present, with default "writeln";
wenzelm
parents:
67920
diff
changeset
|
103 |
JSON.Object(Markup.KIND -> proper_string(kind).getOrElse(Markup.WRITELN), "message" -> msg) |
67901 | 104 |
|
105 |
def error_message(msg: String): JSON.Object.T = |
|
67911
3cda747493d8
clarified markup according to common Command.Results;
wenzelm
parents:
67904
diff
changeset
|
106 |
message(msg, kind = Markup.ERROR) |
67857 | 107 |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
108 |
def unapply(msg: String): Option[(Reply.Value, Any)] = |
67800 | 109 |
{ |
67809 | 110 |
if (msg == "") None |
67800 | 111 |
else { |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
112 |
val (name, argument) = Argument.split(msg) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
113 |
for { |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
114 |
reply <- |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
115 |
try { Some(withName(name)) } |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
116 |
catch { case _: NoSuchElementException => None } |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
117 |
arg <- Argument.unapply(argument) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
118 |
} yield (reply, arg) |
67800 | 119 |
} |
120 |
} |
|
66927 | 121 |
} |
122 |
||
123 |
||
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
124 |
/* handler: port, password, thread */ |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
125 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
126 |
abstract class Handler(port0: Int) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
127 |
{ |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
128 |
val socket: ServerSocket = new ServerSocket(port0, 50, Server.localhost) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
129 |
def port: Int = socket.getLocalPort |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
130 |
val password: String = UUID.random_string() |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
131 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
132 |
override def toString: String = print(port, password) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
133 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
134 |
def handle(connection: Server.Connection): Unit |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
135 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
136 |
private lazy val thread: Thread = |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
137 |
Isabelle_Thread.fork(name = "server_handler") { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
138 |
var finished = false |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
139 |
while (!finished) { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
140 |
Exn.capture(socket.accept) match { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
141 |
case Exn.Res(client) => |
73179
f9c71ce29150
tuned name, e.g. relevant for Naproche-SAD debugging in Isabelle/jEdit;
wenzelm
parents:
73135
diff
changeset
|
142 |
Isabelle_Thread.fork(name = "client") { |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
143 |
using(Connection(client))(connection => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
144 |
if (connection.read_password(password)) handle(connection)) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
145 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
146 |
case Exn.Exn(_) => finished = true |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
147 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
148 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
149 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
150 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
151 |
def start { thread } |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
152 |
def join { thread.join } |
73133 | 153 |
def stop { socket.close; join } |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
154 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
155 |
|
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
156 |
|
67786 | 157 |
/* socket connection */ |
158 |
||
159 |
object Connection |
|
160 |
{ |
|
161 |
def apply(socket: Socket): Connection = |
|
162 |
new Connection(socket) |
|
163 |
} |
|
164 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69033
diff
changeset
|
165 |
class Connection private(socket: Socket) extends AutoCloseable |
67786 | 166 |
{ |
167 |
override def toString: String = socket.toString |
|
168 |
||
169 |
def close() { socket.close } |
|
170 |
||
67832 | 171 |
def set_timeout(t: Time) { socket.setSoTimeout(t.ms.toInt) } |
172 |
||
173 |
private val in = new BufferedInputStream(socket.getInputStream) |
|
174 |
private val out = new BufferedOutputStream(socket.getOutputStream) |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
175 |
private val out_lock: AnyRef = new Object |
67786 | 176 |
|
71713 | 177 |
def tty_loop(): TTY_Loop = |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
178 |
new TTY_Loop( |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
179 |
new OutputStreamWriter(out), |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
180 |
new InputStreamReader(in), |
71713 | 181 |
writer_lock = out_lock) |
67837 | 182 |
|
69464 | 183 |
def read_password(password: String): Boolean = |
184 |
try { Byte_Message.read_line(in).map(_.text) == Some(password) } |
|
185 |
catch { case _: IOException => false } |
|
186 |
||
67809 | 187 |
def read_message(): Option[String] = |
69451 | 188 |
try { Byte_Message.read_line_message(in).map(_.text) } |
189 |
catch { case _: IOException => None } |
|
67786 | 190 |
|
73133 | 191 |
def await_close(): Unit = |
73135 | 192 |
try { Byte_Message.read(in, 1); socket.close() } |
73133 | 193 |
catch { case _: IOException => } |
194 |
||
69448 | 195 |
def write_message(msg: String): Unit = |
196 |
out_lock.synchronized { Byte_Message.write_line_message(out, Bytes(UTF8.bytes(msg))) } |
|
67786 | 197 |
|
67859 | 198 |
def reply(r: Reply.Value, arg: Any) |
67786 | 199 |
{ |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
200 |
val argument = Argument.print(arg) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
201 |
write_message(if (argument == "") r.toString else r.toString + " " + argument) |
67786 | 202 |
} |
203 |
||
67859 | 204 |
def reply_ok(arg: Any) { reply(Reply.OK, arg) } |
205 |
def reply_error(arg: Any) { reply(Reply.ERROR, arg) } |
|
67857 | 206 |
def reply_error_message(message: String, more: JSON.Object.Entry*): Unit = |
67901 | 207 |
reply_error(Reply.error_message(message) ++ more) |
67801 | 208 |
|
67859 | 209 |
def notify(arg: Any) { reply(Reply.NOTE, arg) } |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
210 |
} |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
211 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
212 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
213 |
/* context with output channels */ |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
214 |
|
67878 | 215 |
class Context private[Server](val server: Server, connection: Connection) |
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69033
diff
changeset
|
216 |
extends AutoCloseable |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
217 |
{ |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
218 |
context => |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
219 |
|
72163
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
220 |
def command_list: List[String] = command_table.keys.toList.sorted |
f5722290a4d0
allow user-defined server commands via isabelle_scala_service;
wenzelm
parents:
71747
diff
changeset
|
221 |
|
67859 | 222 |
def reply(r: Reply.Value, arg: Any) { connection.reply(r, arg) } |
67840 | 223 |
def notify(arg: Any) { connection.notify(arg) } |
67857 | 224 |
def message(kind: String, msg: String, more: JSON.Object.Entry*): Unit = |
67901 | 225 |
notify(Reply.message(msg, kind = kind) ++ more) |
67857 | 226 |
def writeln(msg: String, more: JSON.Object.Entry*): Unit = message(Markup.WRITELN, msg, more:_*) |
227 |
def warning(msg: String, more: JSON.Object.Entry*): Unit = message(Markup.WARNING, msg, more:_*) |
|
228 |
def error_message(msg: String, more: JSON.Object.Entry*): Unit = |
|
67911
3cda747493d8
clarified markup according to common Command.Results;
wenzelm
parents:
67904
diff
changeset
|
229 |
message(Markup.ERROR, msg, more:_*) |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
230 |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
231 |
def progress(more: JSON.Object.Entry*): Connection_Progress = |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
232 |
new Connection_Progress(context, more:_*) |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
233 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
234 |
override def toString: String = connection.toString |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
235 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
236 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
237 |
/* asynchronous tasks */ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
238 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
239 |
private val _tasks = Synchronized(Set.empty[Task]) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
240 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
241 |
def make_task(body: Task => JSON.Object.T): Task = |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
242 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
243 |
val task = new Task(context, body) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
244 |
_tasks.change(_ + task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
245 |
task |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
246 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
247 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
248 |
def remove_task(task: Task): Unit = |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
249 |
_tasks.change(_ - task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
250 |
|
69458 | 251 |
def cancel_task(id: UUID.T): Unit = |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
252 |
_tasks.change(tasks => { tasks.find(task => task.id == id).foreach(_.cancel); tasks }) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
253 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
254 |
def close() |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
255 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
256 |
while(_tasks.change_result(tasks => { tasks.foreach(_.cancel); (tasks.nonEmpty, tasks) })) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
257 |
{ _tasks.value.foreach(_.join) } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
258 |
} |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
259 |
} |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
260 |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
261 |
class Connection_Progress private[Server](context: Context, more: JSON.Object.Entry*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
262 |
extends Progress |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
263 |
{ |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
264 |
override def echo(msg: String): Unit = context.writeln(msg, more:_*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
265 |
override def echo_warning(msg: String): Unit = context.warning(msg, more:_*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
266 |
override def echo_error_message(msg: String): Unit = context.error_message(msg, more:_*) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
267 |
|
68957 | 268 |
override def theory(theory: Progress.Theory) |
269 |
{ |
|
270 |
val entries: List[JSON.Object.Entry] = |
|
271 |
List("theory" -> theory.theory, "session" -> theory.session) ::: |
|
272 |
(theory.percentage match { case None => Nil case Some(p) => List("percentage" -> p) }) |
|
273 |
context.writeln(theory.message, entries ::: more.toList:_*) |
|
274 |
} |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
275 |
|
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
276 |
override def nodes_status(nodes_status: Document_Status.Nodes_Status) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
277 |
{ |
68903 | 278 |
val json = |
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
279 |
for ((name, node_status) <- nodes_status.present) |
73132 | 280 |
yield name.json + ("status" -> node_status.json) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
281 |
context.notify(JSON.Object(Markup.KIND -> Markup.NODES_STATUS, Markup.NODES_STATUS -> json)) |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
282 |
} |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
283 |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
284 |
override def toString: String = context.toString |
67786 | 285 |
} |
286 |
||
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
287 |
class Task private[Server](val context: Context, body: Task => JSON.Object.T) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
288 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
289 |
task => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
290 |
|
69458 | 291 |
val id: UUID.T = UUID.random() |
67885 | 292 |
val ident: JSON.Object.Entry = ("task" -> id.toString) |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
293 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
294 |
val progress: Connection_Progress = context.progress(ident) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
295 |
def cancel { progress.stop } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
296 |
|
71692 | 297 |
private lazy val thread = Isabelle_Thread.fork(name = "server_task") |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
298 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
299 |
Exn.capture { body(task) } match { |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
300 |
case Exn.Res(res) => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
301 |
context.reply(Reply.FINISHED, res + ident) |
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
302 |
case Exn.Exn(exn) => |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
303 |
val err = json_error(exn) |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
304 |
if (err.isEmpty) throw exn else context.reply(Reply.FAILED, err + ident) |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
305 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
306 |
progress.stop |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
307 |
context.remove_task(task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
308 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
309 |
def start { thread } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
310 |
def join { thread.join } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
311 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
312 |
|
67786 | 313 |
|
67807 | 314 |
/* server info */ |
315 |
||
69463 | 316 |
val localhost_name: String = "127.0.0.1" |
317 |
def localhost: InetAddress = InetAddress.getByName(localhost_name) |
|
318 |
||
319 |
def print_address(port: Int): String = localhost_name + ":" + port |
|
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
320 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
321 |
def print(port: Int, password: String): String = |
69461 | 322 |
print_address(port) + " (password " + quote(password) + ")" |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
323 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
324 |
object Info |
67807 | 325 |
{ |
69463 | 326 |
private val Pattern = |
327 |
("""server "([^"]*)" = \Q""" + localhost_name + """\E:(\d+) \(password "([^"]*)"\)""").r |
|
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
328 |
|
69463 | 329 |
def parse(s: String): Option[Info] = |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
330 |
s match { |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
331 |
case Pattern(name, Value.Int(port), password) => Some(Info(name, port, password)) |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
332 |
case _ => None |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
333 |
} |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
334 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
335 |
def apply(name: String, port: Int, password: String): Info = |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
336 |
new Info(name, port, password) |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
337 |
} |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
338 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
339 |
class Info private(val name: String, val port: Int, val password: String) |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
340 |
{ |
69461 | 341 |
def address: String = print_address(port) |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
342 |
|
67807 | 343 |
override def toString: String = |
67821
82fb12061069
more uniform output: this may be parsed by another program;
wenzelm
parents:
67820
diff
changeset
|
344 |
"server " + quote(name) + " = " + print(port, password) |
67807 | 345 |
|
346 |
def connection(): Connection = |
|
347 |
{ |
|
69463 | 348 |
val connection = Connection(new Socket(localhost, port)) |
67809 | 349 |
connection.write_message(password) |
67807 | 350 |
connection |
351 |
} |
|
352 |
||
353 |
def active(): Boolean = |
|
354 |
try { |
|
355 |
using(connection())(connection => |
|
356 |
{ |
|
67832 | 357 |
connection.set_timeout(Time.seconds(2.0)) |
67867 | 358 |
connection.read_message() match { |
359 |
case Some(Reply(Reply.OK, _)) => true |
|
360 |
case _ => false |
|
361 |
} |
|
67807 | 362 |
}) |
363 |
} |
|
364 |
catch { |
|
365 |
case _: IOException => false |
|
366 |
case _: SocketException => false |
|
367 |
case _: SocketTimeoutException => false |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
||
66347 | 372 |
/* per-user servers */ |
373 |
||
67822 | 374 |
val default_name = "isabelle" |
375 |
||
66347 | 376 |
object Data |
377 |
{ |
|
66349 | 378 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db") |
66347 | 379 |
|
66857 | 380 |
val name = SQL.Column.string("name").make_primary_key |
66349 | 381 |
val port = SQL.Column.int("port") |
66347 | 382 |
val password = SQL.Column.string("password") |
66349 | 383 |
val table = SQL.Table("isabelle_servers", List(name, port, password)) |
66347 | 384 |
} |
385 |
||
67807 | 386 |
def list(db: SQLite.Database): List[Info] = |
66347 | 387 |
if (db.tables.contains(Data.table.name)) { |
388 |
db.using_statement(Data.table.select())(stmt => |
|
389 |
stmt.execute_query().iterator(res => |
|
67807 | 390 |
Info( |
66349 | 391 |
res.string(Data.name), |
392 |
res.int(Data.port), |
|
393 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 394 |
} |
395 |
else Nil |
|
396 |
||
67807 | 397 |
def find(db: SQLite.Database, name: String): Option[Info] = |
398 |
list(db).find(server_info => server_info.name == name && server_info.active) |
|
66347 | 399 |
|
67870 | 400 |
def init( |
401 |
name: String = default_name, |
|
402 |
port: Int = 0, |
|
403 |
existing_server: Boolean = false, |
|
404 |
log: Logger = No_Logger): (Info, Option[Server]) = |
|
66347 | 405 |
{ |
406 |
using(SQLite.open_database(Data.database))(db => |
|
67799 | 407 |
{ |
408 |
db.transaction { |
|
71114 | 409 |
Isabelle_System.chmod("600", Data.database) |
67799 | 410 |
db.create_table(Data.table) |
67807 | 411 |
list(db).filterNot(_.active).foreach(server_info => |
412 |
db.using_statement(Data.table.delete(Data.name.where_equal(server_info.name)))( |
|
413 |
_.execute)) |
|
67799 | 414 |
} |
415 |
db.transaction { |
|
416 |
find(db, name) match { |
|
67807 | 417 |
case Some(server_info) => (server_info, None) |
67799 | 418 |
case None => |
67821
82fb12061069
more uniform output: this may be parsed by another program;
wenzelm
parents:
67820
diff
changeset
|
419 |
if (existing_server) error("Isabelle server " + quote(name) + " not running") |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
420 |
|
67870 | 421 |
val server = new Server(port, log) |
67807 | 422 |
val server_info = Info(name, server.port, server.password) |
66347 | 423 |
|
67799 | 424 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
425 |
db.using_statement(Data.table.insert())(stmt => |
|
426 |
{ |
|
67807 | 427 |
stmt.string(1) = server_info.name |
428 |
stmt.int(2) = server_info.port |
|
429 |
stmt.string(3) = server_info.password |
|
67799 | 430 |
stmt.execute() |
431 |
}) |
|
66348 | 432 |
|
67799 | 433 |
server.start |
67807 | 434 |
(server_info, Some(server)) |
67799 | 435 |
} |
66347 | 436 |
} |
437 |
}) |
|
438 |
} |
|
439 |
||
67822 | 440 |
def exit(name: String = default_name): Boolean = |
66347 | 441 |
{ |
442 |
using(SQLite.open_database(Data.database))(db => |
|
443 |
db.transaction { |
|
444 |
find(db, name) match { |
|
67807 | 445 |
case Some(server_info) => |
67809 | 446 |
using(server_info.connection())(_.write_message("shutdown")) |
71684 | 447 |
while(server_info.active) { Time.seconds(0.05).sleep } |
66347 | 448 |
true |
67785 | 449 |
case None => false |
66347 | 450 |
} |
451 |
}) |
|
452 |
} |
|
453 |
||
454 |
||
455 |
/* Isabelle tool wrapper */ |
|
456 |
||
457 |
val isabelle_tool = |
|
72763 | 458 |
Isabelle_Tool("server", "manage resident Isabelle servers", Scala_Project.here, args => |
66347 | 459 |
{ |
67806 | 460 |
var console = false |
67870 | 461 |
var log_file: Option[Path] = None |
66348 | 462 |
var operation_list = false |
67823 | 463 |
var operation_exit = false |
67822 | 464 |
var name = default_name |
66348 | 465 |
var port = 0 |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
466 |
var existing_server = false |
66347 | 467 |
|
468 |
val getopts = |
|
469 |
Getopts(""" |
|
470 |
Usage: isabelle server [OPTIONS] |
|
471 |
||
472 |
Options are: |
|
67870 | 473 |
-L FILE logging on FILE |
67875 | 474 |
-c console interaction with specified server |
67904 | 475 |
-l list servers (alternative operation) |
67822 | 476 |
-n NAME explicit server name (default: """ + default_name + """) |
66348 | 477 |
-p PORT explicit server port |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
478 |
-s assume existing server, no implicit startup |
67904 | 479 |
-x exit specified server (alternative operation) |
66347 | 480 |
|
481 |
Manage resident Isabelle servers. |
|
482 |
""", |
|
67870 | 483 |
"L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))), |
67875 | 484 |
"c" -> (_ => console = true), |
67870 | 485 |
"l" -> (_ => operation_list = true), |
66348 | 486 |
"n:" -> (arg => name = arg), |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
487 |
"p:" -> (arg => port = Value.Int.parse(arg)), |
67870 | 488 |
"s" -> (_ => existing_server = true), |
67876 | 489 |
"x" -> (_ => operation_exit = true)) |
66347 | 490 |
|
491 |
val more_args = getopts(args) |
|
66348 | 492 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 493 |
|
66353 | 494 |
if (operation_list) { |
67807 | 495 |
for { |
71714 | 496 |
server_info <- using(SQLite.open_database(Data.database))(list) |
67807 | 497 |
if server_info.active |
498 |
} Output.writeln(server_info.toString, stdout = true) |
|
66353 | 499 |
} |
67823 | 500 |
else if (operation_exit) { |
501 |
val ok = Server.exit(name) |
|
69033 | 502 |
sys.exit(if (ok) 0 else 2) |
67823 | 503 |
} |
66348 | 504 |
else { |
67870 | 505 |
val log = Logger.make(log_file) |
506 |
val (server_info, server) = |
|
507 |
init(name, port = port, existing_server = existing_server, log = log) |
|
67807 | 508 |
Output.writeln(server_info.toString, stdout = true) |
67834 | 509 |
if (console) { |
510 |
using(server_info.connection())(connection => connection.tty_loop().join) |
|
511 |
} |
|
67785 | 512 |
server.foreach(_.join) |
66348 | 513 |
} |
66347 | 514 |
}) |
515 |
} |
|
516 |
||
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
517 |
class Server private(port0: Int, val log: Logger) extends Server.Handler(port0) |
66347 | 518 |
{ |
67785 | 519 |
server => |
520 |
||
69458 | 521 |
private val _sessions = Synchronized(Map.empty[UUID.T, Headless.Session]) |
522 |
def err_session(id: UUID.T): Nothing = error("No session " + Library.single_quote(id.toString)) |
|
71383 | 523 |
def the_session(id: UUID.T): Headless.Session = _sessions.value.getOrElse(id, err_session(id)) |
69458 | 524 |
def add_session(entry: (UUID.T, Headless.Session)) { _sessions.change(_ + entry) } |
525 |
def remove_session(id: UUID.T): Headless.Session = |
|
67871
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
526 |
{ |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
527 |
_sessions.change_result(sessions => |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
528 |
sessions.get(id) match { |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
529 |
case Some(session) => (session, sessions - id) |
67883 | 530 |
case None => err_session(id) |
67871
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
531 |
}) |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
532 |
} |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
533 |
|
67902 | 534 |
def shutdown() |
535 |
{ |
|
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
536 |
server.socket.close |
67791 | 537 |
|
67902 | 538 |
val sessions = _sessions.change_result(sessions => (sessions, Map.empty)) |
539 |
for ((_, session) <- sessions) { |
|
540 |
try { |
|
541 |
val result = session.stop() |
|
71747 | 542 |
if (!result.ok) log("Session shutdown failed: " + result.print_rc) |
67902 | 543 |
} |
544 |
catch { case ERROR(msg) => log("Session shutdown failed: " + msg) } |
|
545 |
} |
|
546 |
} |
|
67791 | 547 |
|
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
548 |
override def join { super.join; shutdown() } |
66350 | 549 |
|
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
550 |
override def handle(connection: Server.Connection) |
66350 | 551 |
{ |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
552 |
using(new Server.Context(server, connection))(context => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
553 |
{ |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
554 |
connection.reply_ok( |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
555 |
JSON.Object( |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
556 |
"isabelle_id" -> Isabelle_System.isabelle_id(), |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
557 |
"isabelle_version" -> Distribution.version)) |
67867 | 558 |
|
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
559 |
var finished = false |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
560 |
while (!finished) { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
561 |
connection.read_message() match { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
562 |
case None => finished = true |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
563 |
case Some("") => context.notify("Command 'help' provides list of commands") |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
564 |
case Some(msg) => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
565 |
val (name, argument) = Server.Argument.split(msg) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
566 |
Server.command_table.get(name) match { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
567 |
case Some(cmd) => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
568 |
argument match { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
569 |
case Server.Argument(arg) => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
570 |
if (cmd.command_body.isDefinedAt((context, arg))) { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
571 |
Exn.capture { cmd.command_body((context, arg)) } match { |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
572 |
case Exn.Res(task: Server.Task) => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
573 |
connection.reply_ok(JSON.Object(task.ident)) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
574 |
task.start |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
575 |
case Exn.Res(res) => connection.reply_ok(res) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
576 |
case Exn.Exn(exn) => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
577 |
val err = Server.json_error(exn) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
578 |
if (err.isEmpty) throw exn else connection.reply_error(err) |
69464 | 579 |
} |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
580 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
581 |
else { |
69464 | 582 |
connection.reply_error_message( |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
583 |
"Bad argument for command " + Library.single_quote(name), |
69464 | 584 |
"argument" -> argument) |
73131
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
585 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
586 |
case _ => |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
587 |
connection.reply_error_message( |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
588 |
"Malformed argument for command " + Library.single_quote(name), |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
589 |
"argument" -> argument) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
590 |
} |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
591 |
case None => connection.reply_error("Bad command " + Library.single_quote(name)) |
ff6b5e468d5f
clarified signature: support more generic server implementations;
wenzelm
parents:
72763
diff
changeset
|
592 |
} |
69464 | 593 |
} |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
594 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
595 |
}) |
66350 | 596 |
} |
66347 | 597 |
} |