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