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