author | wenzelm |
Thu, 14 Nov 2019 11:35:02 +0100 | |
changeset 71114 | 6cfec8029831 |
parent 69818 | 60d0ee8f2ddb |
child 71383 | 8313dca6dee9 |
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 |
{ |
67903 | 38 |
val name = msg.takeWhile(is_name_char(_)) |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
39 |
val argument = msg.substring(name.length).dropWhile(Symbol.is_ascii_blank(_)) |
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 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
63 |
object Command |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
64 |
{ |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
65 |
type T = PartialFunction[(Context, Any), Any] |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
66 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
67 |
private val table: Map[String, T] = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
68 |
Map( |
67848
dd83610333de
added server command "session_build": similar to JEdit_Resources.session_build;
wenzelm
parents:
67840
diff
changeset
|
69 |
"help" -> { case (_, ()) => table.keySet.toList.sorted }, |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
70 |
"echo" -> { case (_, t) => t }, |
67902 | 71 |
"shutdown" -> { case (context, ()) => context.server.shutdown() }, |
67920 | 72 |
"cancel" -> |
73 |
{ case (context, Server_Commands.Cancel(args)) => context.cancel_task(args.task) }, |
|
67848
dd83610333de
added server command "session_build": similar to JEdit_Resources.session_build;
wenzelm
parents:
67840
diff
changeset
|
74 |
"session_build" -> |
dd83610333de
added server command "session_build": similar to JEdit_Resources.session_build;
wenzelm
parents:
67840
diff
changeset
|
75 |
{ case (context, Server_Commands.Session_Build(args)) => |
67861 | 76 |
context.make_task(task => |
67886 | 77 |
Server_Commands.Session_Build.command(args, progress = task.progress)._1) |
67869 | 78 |
}, |
79 |
"session_start" -> |
|
80 |
{ case (context, Server_Commands.Session_Start(args)) => |
|
81 |
context.make_task(task => |
|
82 |
{ |
|
67871
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
83 |
val (res, entry) = |
67878 | 84 |
Server_Commands.Session_Start.command( |
67886 | 85 |
args, progress = task.progress, log = context.server.log) |
67878 | 86 |
context.server.add_session(entry) |
67869 | 87 |
res |
88 |
}) |
|
67871
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
89 |
}, |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
90 |
"session_stop" -> |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
91 |
{ case (context, Server_Commands.Session_Stop(id)) => |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
92 |
context.make_task(_ => |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
93 |
{ |
67878 | 94 |
val session = context.server.remove_session(id) |
67871
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
95 |
Server_Commands.Session_Stop.command(session)._1 |
195ff117894c
store session: per Server/Context, not Connection;
wenzelm
parents:
67870
diff
changeset
|
96 |
}) |
67883 | 97 |
}, |
98 |
"use_theories" -> |
|
99 |
{ case (context, Server_Commands.Use_Theories(args)) => |
|
100 |
context.make_task(task => |
|
101 |
{ |
|
102 |
val session = context.server.the_session(args.session_id) |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
103 |
Server_Commands.Use_Theories.command( |
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
104 |
args, session, id = task.id, progress = task.progress)._1 |
68530 | 105 |
}) |
67941 | 106 |
}, |
107 |
"purge_theories" -> |
|
108 |
{ case (context, Server_Commands.Purge_Theories(args)) => |
|
109 |
val session = context.server.the_session(args.session_id) |
|
110 |
Server_Commands.Purge_Theories.command(args, session)._1 |
|
67848
dd83610333de
added server command "session_build": similar to JEdit_Resources.session_build;
wenzelm
parents:
67840
diff
changeset
|
111 |
}) |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
112 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
113 |
def unapply(name: String): Option[T] = table.get(name) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
114 |
} |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
115 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
116 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
117 |
/* output reply */ |
66929 | 118 |
|
67857 | 119 |
class Error(val message: String, val json: JSON.Object.T = JSON.Object.empty) |
120 |
extends RuntimeException(message) |
|
121 |
||
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
122 |
def json_error(exn: Throwable): JSON.Object.T = |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
123 |
exn match { |
67913
d58fa3ed236f
proper order of matches: Server.Error is an instance of Exn.ERROR;
wenzelm
parents:
67911
diff
changeset
|
124 |
case e: Error => Reply.error_message(e.message) ++ e.json |
67901 | 125 |
case ERROR(msg) => Reply.error_message(msg) |
126 |
case _ if Exn.is_interrupt(exn) => Reply.error_message(Exn.message(exn)) |
|
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
127 |
case _ => JSON.Object.empty |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
128 |
} |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
129 |
|
66927 | 130 |
object Reply extends Enumeration |
131 |
{ |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
132 |
val OK, ERROR, FINISHED, FAILED, NOTE = Value |
67800 | 133 |
|
67901 | 134 |
def message(msg: String, kind: String = ""): JSON.Object.T = |
67931
f7917c15b566
field "kind" is always present, with default "writeln";
wenzelm
parents:
67920
diff
changeset
|
135 |
JSON.Object(Markup.KIND -> proper_string(kind).getOrElse(Markup.WRITELN), "message" -> msg) |
67901 | 136 |
|
137 |
def error_message(msg: String): JSON.Object.T = |
|
67911
3cda747493d8
clarified markup according to common Command.Results;
wenzelm
parents:
67904
diff
changeset
|
138 |
message(msg, kind = Markup.ERROR) |
67857 | 139 |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
140 |
def unapply(msg: String): Option[(Reply.Value, Any)] = |
67800 | 141 |
{ |
67809 | 142 |
if (msg == "") None |
67800 | 143 |
else { |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
144 |
val (name, argument) = Argument.split(msg) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
145 |
for { |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
146 |
reply <- |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
147 |
try { Some(withName(name)) } |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
148 |
catch { case _: NoSuchElementException => None } |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
149 |
arg <- Argument.unapply(argument) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
150 |
} yield (reply, arg) |
67800 | 151 |
} |
152 |
} |
|
66927 | 153 |
} |
154 |
||
155 |
||
67786 | 156 |
/* socket connection */ |
157 |
||
158 |
object Connection |
|
159 |
{ |
|
160 |
def apply(socket: Socket): Connection = |
|
161 |
new Connection(socket) |
|
162 |
} |
|
163 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69033
diff
changeset
|
164 |
class Connection private(socket: Socket) extends AutoCloseable |
67786 | 165 |
{ |
166 |
override def toString: String = socket.toString |
|
167 |
||
168 |
def close() { socket.close } |
|
169 |
||
67832 | 170 |
def set_timeout(t: Time) { socket.setSoTimeout(t.ms.toInt) } |
171 |
||
172 |
private val in = new BufferedInputStream(socket.getInputStream) |
|
173 |
private val out = new BufferedOutputStream(socket.getOutputStream) |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
174 |
private val out_lock: AnyRef = new Object |
67786 | 175 |
|
67837 | 176 |
def tty_loop(interrupt: Option[() => Unit] = None): TTY_Loop = |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
177 |
new TTY_Loop( |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
178 |
new OutputStreamWriter(out), |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
179 |
new InputStreamReader(in), |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
180 |
writer_lock = out_lock, |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
181 |
interrupt = interrupt) |
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 |
|
69448 | 191 |
def write_message(msg: String): Unit = |
192 |
out_lock.synchronized { Byte_Message.write_line_message(out, Bytes(UTF8.bytes(msg))) } |
|
67786 | 193 |
|
67859 | 194 |
def reply(r: Reply.Value, arg: Any) |
67786 | 195 |
{ |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
196 |
val argument = Argument.print(arg) |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
197 |
write_message(if (argument == "") r.toString else r.toString + " " + argument) |
67786 | 198 |
} |
199 |
||
67859 | 200 |
def reply_ok(arg: Any) { reply(Reply.OK, arg) } |
201 |
def reply_error(arg: Any) { reply(Reply.ERROR, arg) } |
|
67857 | 202 |
def reply_error_message(message: String, more: JSON.Object.Entry*): Unit = |
67901 | 203 |
reply_error(Reply.error_message(message) ++ more) |
67801 | 204 |
|
67859 | 205 |
def notify(arg: Any) { reply(Reply.NOTE, arg) } |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
206 |
} |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
207 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
208 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
209 |
/* context with output channels */ |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
210 |
|
67878 | 211 |
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
|
212 |
extends AutoCloseable |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
213 |
{ |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
214 |
context => |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
215 |
|
67859 | 216 |
def reply(r: Reply.Value, arg: Any) { connection.reply(r, arg) } |
67840 | 217 |
def notify(arg: Any) { connection.notify(arg) } |
67857 | 218 |
def message(kind: String, msg: String, more: JSON.Object.Entry*): Unit = |
67901 | 219 |
notify(Reply.message(msg, kind = kind) ++ more) |
67857 | 220 |
def writeln(msg: String, more: JSON.Object.Entry*): Unit = message(Markup.WRITELN, msg, more:_*) |
221 |
def warning(msg: String, more: JSON.Object.Entry*): Unit = message(Markup.WARNING, msg, more:_*) |
|
222 |
def error_message(msg: String, more: JSON.Object.Entry*): Unit = |
|
67911
3cda747493d8
clarified markup according to common Command.Results;
wenzelm
parents:
67904
diff
changeset
|
223 |
message(Markup.ERROR, msg, more:_*) |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
224 |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
225 |
def progress(more: JSON.Object.Entry*): Connection_Progress = |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
226 |
new Connection_Progress(context, more:_*) |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
227 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
228 |
override def toString: String = connection.toString |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
229 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
230 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
231 |
/* asynchronous tasks */ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
232 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
233 |
private val _tasks = Synchronized(Set.empty[Task]) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
234 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
235 |
def make_task(body: Task => JSON.Object.T): Task = |
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 |
val task = new Task(context, body) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
238 |
_tasks.change(_ + task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
239 |
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 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
242 |
def remove_task(task: Task): Unit = |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
243 |
_tasks.change(_ - task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
244 |
|
69458 | 245 |
def cancel_task(id: UUID.T): Unit = |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
246 |
_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
|
247 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
248 |
def close() |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
249 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
250 |
while(_tasks.change_result(tasks => { tasks.foreach(_.cancel); (tasks.nonEmpty, tasks) })) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
251 |
{ _tasks.value.foreach(_.join) } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
252 |
} |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
253 |
} |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
254 |
|
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
255 |
class Connection_Progress private[Server](context: Context, more: JSON.Object.Entry*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
256 |
extends Progress |
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
257 |
{ |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
258 |
override def echo(msg: String): Unit = context.writeln(msg, more:_*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
259 |
override def echo_warning(msg: String): Unit = context.warning(msg, more:_*) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
260 |
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
|
261 |
|
68957 | 262 |
override def theory(theory: Progress.Theory) |
263 |
{ |
|
264 |
val entries: List[JSON.Object.Entry] = |
|
265 |
List("theory" -> theory.theory, "session" -> theory.session) ::: |
|
266 |
(theory.percentage match { case None => Nil case Some(p) => List("percentage" -> p) }) |
|
267 |
context.writeln(theory.message, entries ::: more.toList:_*) |
|
268 |
} |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
269 |
|
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
270 |
override def nodes_status(nodes_status: Document_Status.Nodes_Status) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
271 |
{ |
68903 | 272 |
val json = |
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
273 |
for ((name, node_status) <- nodes_status.present) |
68903 | 274 |
yield name.json + ("status" -> nodes_status(name).json) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
275 |
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
|
276 |
} |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68530
diff
changeset
|
277 |
|
67839
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
278 |
@volatile private var is_stopped = false |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
279 |
override def stopped: Boolean = is_stopped |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
280 |
def stop { is_stopped = true } |
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
281 |
|
0c2ed45ece20
explicit Server.Context with output channels (concurrent write);
wenzelm
parents:
67838
diff
changeset
|
282 |
override def toString: String = context.toString |
67786 | 283 |
} |
284 |
||
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
285 |
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
|
286 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
287 |
task => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
288 |
|
69458 | 289 |
val id: UUID.T = UUID.random() |
67885 | 290 |
val ident: JSON.Object.Entry = ("task" -> id.toString) |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
291 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
292 |
val progress: Connection_Progress = context.progress(ident) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
293 |
def cancel { progress.stop } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
294 |
|
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
295 |
private lazy val thread = Standard_Thread.fork("server_task") |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
296 |
{ |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
297 |
Exn.capture { body(task) } match { |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
298 |
case Exn.Res(res) => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
299 |
context.reply(Reply.FINISHED, res + ident) |
67891
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
300 |
case Exn.Exn(exn) => |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
301 |
val err = json_error(exn) |
4f383cd54f69
clarified exception handling: include interrupts;
wenzelm
parents:
67886
diff
changeset
|
302 |
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
|
303 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
304 |
progress.stop |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
305 |
context.remove_task(task) |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
306 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
307 |
def start { thread } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
308 |
def join { thread.join } |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
309 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
310 |
|
67786 | 311 |
|
67807 | 312 |
/* server info */ |
313 |
||
69463 | 314 |
val localhost_name: String = "127.0.0.1" |
315 |
def localhost: InetAddress = InetAddress.getByName(localhost_name) |
|
316 |
||
317 |
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
|
318 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
319 |
def print(port: Int, password: String): String = |
69461 | 320 |
print_address(port) + " (password " + quote(password) + ")" |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
321 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
322 |
object Info |
67807 | 323 |
{ |
69463 | 324 |
private val Pattern = |
325 |
("""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
|
326 |
|
69463 | 327 |
def parse(s: String): Option[Info] = |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
328 |
s match { |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
329 |
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
|
330 |
case _ => None |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
331 |
} |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
332 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
333 |
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
|
334 |
new Info(name, port, password) |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
335 |
} |
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
336 |
|
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
337 |
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
|
338 |
{ |
69461 | 339 |
def address: String = print_address(port) |
69460
5ffe7e17f770
clarified signature, e.g. for re-use by other servers;
wenzelm
parents:
69458
diff
changeset
|
340 |
|
67807 | 341 |
override def toString: String = |
67821
82fb12061069
more uniform output: this may be parsed by another program;
wenzelm
parents:
67820
diff
changeset
|
342 |
"server " + quote(name) + " = " + print(port, password) |
67807 | 343 |
|
344 |
def connection(): Connection = |
|
345 |
{ |
|
69463 | 346 |
val connection = Connection(new Socket(localhost, port)) |
67809 | 347 |
connection.write_message(password) |
67807 | 348 |
connection |
349 |
} |
|
350 |
||
351 |
def active(): Boolean = |
|
352 |
try { |
|
353 |
using(connection())(connection => |
|
354 |
{ |
|
67832 | 355 |
connection.set_timeout(Time.seconds(2.0)) |
67867 | 356 |
connection.read_message() match { |
357 |
case Some(Reply(Reply.OK, _)) => true |
|
358 |
case _ => false |
|
359 |
} |
|
67807 | 360 |
}) |
361 |
} |
|
362 |
catch { |
|
363 |
case _: IOException => false |
|
364 |
case _: SocketException => false |
|
365 |
case _: SocketTimeoutException => false |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
||
66347 | 370 |
/* per-user servers */ |
371 |
||
67822 | 372 |
val default_name = "isabelle" |
373 |
||
66347 | 374 |
object Data |
375 |
{ |
|
66349 | 376 |
val database = Path.explode("$ISABELLE_HOME_USER/servers.db") |
66347 | 377 |
|
66857 | 378 |
val name = SQL.Column.string("name").make_primary_key |
66349 | 379 |
val port = SQL.Column.int("port") |
66347 | 380 |
val password = SQL.Column.string("password") |
66349 | 381 |
val table = SQL.Table("isabelle_servers", List(name, port, password)) |
66347 | 382 |
} |
383 |
||
67807 | 384 |
def list(db: SQLite.Database): List[Info] = |
66347 | 385 |
if (db.tables.contains(Data.table.name)) { |
386 |
db.using_statement(Data.table.select())(stmt => |
|
387 |
stmt.execute_query().iterator(res => |
|
67807 | 388 |
Info( |
66349 | 389 |
res.string(Data.name), |
390 |
res.int(Data.port), |
|
391 |
res.string(Data.password))).toList.sortBy(_.name)) |
|
66347 | 392 |
} |
393 |
else Nil |
|
394 |
||
67807 | 395 |
def find(db: SQLite.Database, name: String): Option[Info] = |
396 |
list(db).find(server_info => server_info.name == name && server_info.active) |
|
66347 | 397 |
|
67870 | 398 |
def init( |
399 |
name: String = default_name, |
|
400 |
port: Int = 0, |
|
401 |
existing_server: Boolean = false, |
|
402 |
log: Logger = No_Logger): (Info, Option[Server]) = |
|
66347 | 403 |
{ |
404 |
using(SQLite.open_database(Data.database))(db => |
|
67799 | 405 |
{ |
406 |
db.transaction { |
|
71114 | 407 |
Isabelle_System.chmod("600", Data.database) |
67799 | 408 |
db.create_table(Data.table) |
67807 | 409 |
list(db).filterNot(_.active).foreach(server_info => |
410 |
db.using_statement(Data.table.delete(Data.name.where_equal(server_info.name)))( |
|
411 |
_.execute)) |
|
67799 | 412 |
} |
413 |
db.transaction { |
|
414 |
find(db, name) match { |
|
67807 | 415 |
case Some(server_info) => (server_info, None) |
67799 | 416 |
case None => |
67821
82fb12061069
more uniform output: this may be parsed by another program;
wenzelm
parents:
67820
diff
changeset
|
417 |
if (existing_server) error("Isabelle server " + quote(name) + " not running") |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
418 |
|
67870 | 419 |
val server = new Server(port, log) |
67807 | 420 |
val server_info = Info(name, server.port, server.password) |
66347 | 421 |
|
67799 | 422 |
db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute) |
423 |
db.using_statement(Data.table.insert())(stmt => |
|
424 |
{ |
|
67807 | 425 |
stmt.string(1) = server_info.name |
426 |
stmt.int(2) = server_info.port |
|
427 |
stmt.string(3) = server_info.password |
|
67799 | 428 |
stmt.execute() |
429 |
}) |
|
66348 | 430 |
|
67799 | 431 |
server.start |
67807 | 432 |
(server_info, Some(server)) |
67799 | 433 |
} |
66347 | 434 |
} |
435 |
}) |
|
436 |
} |
|
437 |
||
67822 | 438 |
def exit(name: String = default_name): Boolean = |
66347 | 439 |
{ |
440 |
using(SQLite.open_database(Data.database))(db => |
|
441 |
db.transaction { |
|
442 |
find(db, name) match { |
|
67807 | 443 |
case Some(server_info) => |
67809 | 444 |
using(server_info.connection())(_.write_message("shutdown")) |
67807 | 445 |
while(server_info.active) { Thread.sleep(50) } |
66347 | 446 |
true |
67785 | 447 |
case None => false |
66347 | 448 |
} |
449 |
}) |
|
450 |
} |
|
451 |
||
452 |
||
453 |
/* Isabelle tool wrapper */ |
|
454 |
||
455 |
val isabelle_tool = |
|
456 |
Isabelle_Tool("server", "manage resident Isabelle servers", args => |
|
457 |
{ |
|
67806 | 458 |
var console = false |
67870 | 459 |
var log_file: Option[Path] = None |
66348 | 460 |
var operation_list = false |
67823 | 461 |
var operation_exit = false |
67822 | 462 |
var name = default_name |
66348 | 463 |
var port = 0 |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
464 |
var existing_server = false |
66347 | 465 |
|
466 |
val getopts = |
|
467 |
Getopts(""" |
|
468 |
Usage: isabelle server [OPTIONS] |
|
469 |
||
470 |
Options are: |
|
67870 | 471 |
-L FILE logging on FILE |
67875 | 472 |
-c console interaction with specified server |
67904 | 473 |
-l list servers (alternative operation) |
67822 | 474 |
-n NAME explicit server name (default: """ + default_name + """) |
66348 | 475 |
-p PORT explicit server port |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
476 |
-s assume existing server, no implicit startup |
67904 | 477 |
-x exit specified server (alternative operation) |
66347 | 478 |
|
479 |
Manage resident Isabelle servers. |
|
480 |
""", |
|
67870 | 481 |
"L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))), |
67875 | 482 |
"c" -> (_ => console = true), |
67870 | 483 |
"l" -> (_ => operation_list = true), |
66348 | 484 |
"n:" -> (arg => name = arg), |
67811
33199d033505
more options: client without implicit server startup;
wenzelm
parents:
67809
diff
changeset
|
485 |
"p:" -> (arg => port = Value.Int.parse(arg)), |
67870 | 486 |
"s" -> (_ => existing_server = true), |
67876 | 487 |
"x" -> (_ => operation_exit = true)) |
66347 | 488 |
|
489 |
val more_args = getopts(args) |
|
66348 | 490 |
if (more_args.nonEmpty) getopts.usage() |
66347 | 491 |
|
66353 | 492 |
if (operation_list) { |
67807 | 493 |
for { |
494 |
server_info <- using(SQLite.open_database(Data.database))(list(_)) |
|
495 |
if server_info.active |
|
496 |
} Output.writeln(server_info.toString, stdout = true) |
|
66353 | 497 |
} |
67823 | 498 |
else if (operation_exit) { |
499 |
val ok = Server.exit(name) |
|
69033 | 500 |
sys.exit(if (ok) 0 else 2) |
67823 | 501 |
} |
66348 | 502 |
else { |
67870 | 503 |
val log = Logger.make(log_file) |
504 |
val (server_info, server) = |
|
505 |
init(name, port = port, existing_server = existing_server, log = log) |
|
67807 | 506 |
Output.writeln(server_info.toString, stdout = true) |
67834 | 507 |
if (console) { |
508 |
using(server_info.connection())(connection => connection.tty_loop().join) |
|
509 |
} |
|
67785 | 510 |
server.foreach(_.join) |
66348 | 511 |
} |
66347 | 512 |
}) |
513 |
} |
|
514 |
||
67870 | 515 |
class Server private(_port: Int, val log: Logger) |
66347 | 516 |
{ |
67785 | 517 |
server => |
518 |
||
69463 | 519 |
private val server_socket = new ServerSocket(_port, 50, Server.localhost) |
67902 | 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)) |
|
523 |
def the_session(id: UUID.T): Headless.Session = _sessions.value.get(id) getOrElse err_session(id) |
|
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 |
{ |
|
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() |
|
542 |
if (!result.ok) log("Session shutdown failed: return code " + result.rc) |
|
543 |
} |
|
544 |
catch { case ERROR(msg) => log("Session shutdown failed: " + msg) } |
|
545 |
} |
|
546 |
} |
|
67791 | 547 |
|
66350 | 548 |
def port: Int = server_socket.getLocalPort |
69458 | 549 |
val password: String = UUID.random_string() |
66350 | 550 |
|
67787 | 551 |
override def toString: String = Server.print(port, password) |
66348 | 552 |
|
67786 | 553 |
private def handle(connection: Server.Connection) |
66350 | 554 |
{ |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
555 |
using(new Server.Context(server, connection))(context => |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
556 |
{ |
69464 | 557 |
if (connection.read_password(password)) { |
558 |
connection.reply_ok( |
|
559 |
JSON.Object( |
|
560 |
"isabelle_id" -> Isabelle_System.isabelle_id(), |
|
561 |
"isabelle_version" -> Distribution.version)) |
|
67867 | 562 |
|
69464 | 563 |
var finished = false |
564 |
while (!finished) { |
|
565 |
connection.read_message() match { |
|
566 |
case None => finished = true |
|
567 |
case Some("") => context.notify("Command 'help' provides list of commands") |
|
568 |
case Some(msg) => |
|
569 |
val (name, argument) = Server.Argument.split(msg) |
|
570 |
name match { |
|
571 |
case Server.Command(cmd) => |
|
572 |
argument match { |
|
573 |
case Server.Argument(arg) => |
|
574 |
if (cmd.isDefinedAt((context, arg))) { |
|
575 |
Exn.capture { cmd((context, arg)) } match { |
|
576 |
case Exn.Res(task: Server.Task) => |
|
577 |
connection.reply_ok(JSON.Object(task.ident)) |
|
578 |
task.start |
|
579 |
case Exn.Res(res) => connection.reply_ok(res) |
|
580 |
case Exn.Exn(exn) => |
|
581 |
val err = Server.json_error(exn) |
|
582 |
if (err.isEmpty) throw exn else connection.reply_error(err) |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
583 |
} |
69464 | 584 |
} |
585 |
else { |
|
67786 | 586 |
connection.reply_error_message( |
69464 | 587 |
"Bad argument for command " + Library.single_quote(name), |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67812
diff
changeset
|
588 |
"argument" -> argument) |
69464 | 589 |
} |
590 |
case _ => |
|
591 |
connection.reply_error_message( |
|
592 |
"Malformed argument for command " + Library.single_quote(name), |
|
593 |
"argument" -> argument) |
|
594 |
} |
|
595 |
case _ => connection.reply_error("Bad command " + Library.single_quote(name)) |
|
596 |
} |
|
66350 | 597 |
} |
69464 | 598 |
} |
67860
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
599 |
} |
5a6c483269f3
support for asynchronous tasks, with "cancel" command;
wenzelm
parents:
67859
diff
changeset
|
600 |
}) |
66350 | 601 |
} |
602 |
||
67786 | 603 |
private lazy val server_thread: Thread = |
66350 | 604 |
Standard_Thread.fork("server") { |
605 |
var finished = false |
|
606 |
while (!finished) { |
|
607 |
Exn.capture(server_socket.accept) match { |
|
608 |
case Exn.Res(socket) => |
|
609 |
Standard_Thread.fork("server_connection") |
|
67786 | 610 |
{ using(Server.Connection(socket))(handle(_)) } |
66350 | 611 |
case Exn.Exn(_) => finished = true |
612 |
} |
|
613 |
} |
|
614 |
} |
|
67785 | 615 |
|
67790
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
616 |
def start { server_thread } |
1babcc248be0
clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents:
67789
diff
changeset
|
617 |
|
67902 | 618 |
def join { server_thread.join; shutdown() } |
66347 | 619 |
} |