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