src/Pure/Tools/server.scala
author wenzelm
Fri, 09 Mar 2018 14:30:13 +0100
changeset 67795 f8037c7e4659
parent 67794 82c5ca89ac61
child 67796 bb2bbabf3d37
permissions -rw-r--r--
ignore empty lines;
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.
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
     5
*/
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
     6
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
     7
package isabelle
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
     8
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
     9
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
    10
import java.io.{BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
    11
  IOException}
67793
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    12
import java.net.{Socket, SocketException, ServerSocket, InetAddress}
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    13
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    14
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    15
object Server
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    16
{
66927
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    17
  /* protocol */
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    18
67794
wenzelm
parents: 67793
diff changeset
    19
  def split_line(line: String): (String, String) =
wenzelm
parents: 67793
diff changeset
    20
  {
wenzelm
parents: 67793
diff changeset
    21
    val head = line.takeWhile(c => Symbol.is_ascii_letter(c) || Symbol.is_ascii_letdig(c))
wenzelm
parents: 67793
diff changeset
    22
    val rest = line.substring(head.length).dropWhile(Symbol.is_ascii_blank(_))
wenzelm
parents: 67793
diff changeset
    23
    (head, proper_string(rest) getOrElse "{}")
wenzelm
parents: 67793
diff changeset
    24
  }
wenzelm
parents: 67793
diff changeset
    25
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
    26
  val commands: Map[String, PartialFunction[(Server, JSON.T), JSON.T]] =
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
    27
    Map(
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
    28
      "echo" -> { case (_, t) => t },
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
    29
      "help" -> { case (_, JSON.empty) => commands.keySet.toList.sorted },
67791
acecef5fad58 tuned signature;
wenzelm
parents: 67790
diff changeset
    30
      "shutdown" -> { case (server, JSON.empty) => server.close(); JSON.empty })
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
    31
66927
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    32
  object Reply extends Enumeration
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    33
  {
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    34
    val OK, ERROR = Value
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    35
  }
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    36
153d7b68e8f8 more formal messages;
wenzelm
parents: 66921
diff changeset
    37
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    38
  /* socket connection */
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    39
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    40
  object Connection
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    41
  {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    42
    def apply(socket: Socket): Connection =
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    43
      new Connection(socket)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    44
  }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    45
67788
34e76587dc94 tuned signature;
wenzelm
parents: 67787
diff changeset
    46
  class Connection private(val socket: Socket)
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    47
  {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    48
    override def toString: String = socket.toString
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    49
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    50
    def close() { socket.close }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    51
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    52
    val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF8.charset))
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    53
    val writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream, UTF8.charset))
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    54
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    55
    def read_line(): Option[String] =
67793
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    56
      Exn.capture { reader.readLine() } match {
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    57
        case Exn.Res(null) => None
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    58
        case Exn.Res(line) => Some(line)
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    59
        case Exn.Exn(_: SocketException) => None
d0eeaeab48cc more robust read_line after shutdown;
wenzelm
parents: 67792
diff changeset
    60
        case Exn.Exn(exn) => throw exn
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    61
      }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    62
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    63
    def write_line(msg: String)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    64
    {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    65
      require(split_lines(msg).length <= 1)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    66
      writer.write(msg)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    67
      writer.newLine()
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    68
      writer.flush()
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    69
    }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    70
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    71
    def reply(r: Server.Reply.Value, t: JSON.T)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    72
    {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    73
      write_line(if (t == JSON.empty) r.toString else r.toString + " " + JSON.Format(t))
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    74
    }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    75
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    76
    def reply_ok(t: JSON.T) { reply(Server.Reply.OK, t) }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    77
    def reply_error(t: JSON.T) { reply(Server.Reply.ERROR, t) }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    78
    def reply_error_message(message: String, more: (String, JSON.T)*): Unit =
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    79
      reply_error(Map("message" -> message) ++ more)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    80
  }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    81
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
    82
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    83
  /* per-user servers */
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    84
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
    85
  def print(port: Int, password: String): String =
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
    86
    "127.0.0.1:" + port + " (password " + quote(password) + ")"
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
    87
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    88
  object Data
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    89
  {
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
    90
    val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    91
66857
f8f42289c4df tuned signature;
wenzelm
parents: 66353
diff changeset
    92
    val name = SQL.Column.string("name").make_primary_key
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
    93
    val port = SQL.Column.int("port")
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    94
    val password = SQL.Column.string("password")
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
    95
    val table = SQL.Table("isabelle_servers", List(name, port, password))
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    96
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
    97
    sealed case class Entry(name: String, port: Int, password: String)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
    98
    {
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
    99
      override def toString: String =
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
   100
        "server " + quote(name) + " = " + print(port, password)
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   101
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   102
      def connection(): Connection =
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   103
        Connection(new Socket(InetAddress.getByName("127.0.0.1"), port))
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   104
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   105
      def active(): Boolean =
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   106
        try { connection().close; true }
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   107
        catch { case _: IOException => false }
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   108
    }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   109
  }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   110
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   111
  def list(db: SQLite.Database): List[Data.Entry] =
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   112
    if (db.tables.contains(Data.table.name)) {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   113
      db.using_statement(Data.table.select())(stmt =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   114
        stmt.execute_query().iterator(res =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   115
          Data.Entry(
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
   116
            res.string(Data.name),
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
   117
            res.int(Data.port),
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
   118
            res.string(Data.password))).toList.sortBy(_.name))
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   119
    }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   120
    else Nil
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   121
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   122
  def find(db: SQLite.Database, name: String): Option[Data.Entry] =
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   123
    list(db).find(entry => entry.name == name && entry.active)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   124
67789
77a65c9a849a tuned signature;
wenzelm
parents: 67788
diff changeset
   125
  def init(name: String = "", port: Int = 0): (Data.Entry, Option[Server]) =
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   126
  {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   127
    using(SQLite.open_database(Data.database))(db =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   128
      db.transaction {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   129
        find(db, name) match {
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   130
          case Some(entry) => (entry, None)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   131
          case None =>
66352
7ed911242266 clarified signature;
wenzelm
parents: 66351
diff changeset
   132
            val server = new Server(port)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   133
            val entry = Data.Entry(name, server.port, server.password)
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   134
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   135
            Isabelle_System.bash("chmod 600 " + File.bash_path(Data.database)).check
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   136
            db.create_table(Data.table)
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   137
            db.using_statement(Data.table.delete(Data.name.where_equal(name)))(_.execute)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   138
            db.using_statement(Data.table.insert())(stmt =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   139
            {
66349
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
   140
              stmt.string(1) = entry.name
66b843e4cff5 clarified database names;
wenzelm
parents: 66348
diff changeset
   141
              stmt.int(2) = entry.port
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   142
              stmt.string(3) = entry.password
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   143
              stmt.execute()
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   144
            })
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   145
67790
1babcc248be0 clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents: 67789
diff changeset
   146
            server.start
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   147
            (entry, Some(server))
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   148
        }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   149
      })
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   150
  }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   151
67789
77a65c9a849a tuned signature;
wenzelm
parents: 67788
diff changeset
   152
  def exit(name: String = ""): Boolean =
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   153
  {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   154
    using(SQLite.open_database(Data.database))(db =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   155
      db.transaction {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   156
        find(db, name) match {
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   157
          case Some(entry) =>
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   158
            using(entry.connection())(connection =>
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   159
              {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   160
                connection.write_line(entry.password)
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   161
                connection.write_line("shutdown")
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   162
              })
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   163
            while(entry.active) { Thread.sleep(100) }
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   164
            true
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   165
          case None => false
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   166
        }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   167
      })
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   168
  }
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   169
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   170
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   171
  /* Isabelle tool wrapper */
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   172
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   173
  val isabelle_tool =
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   174
    Isabelle_Tool("server", "manage resident Isabelle servers", args =>
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   175
    {
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   176
      var operation_list = false
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   177
      var name = ""
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   178
      var port = 0
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   179
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   180
      val getopts =
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   181
        Getopts("""
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   182
Usage: isabelle server [OPTIONS]
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   183
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   184
  Options are:
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   185
    -L           list servers
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   186
    -n NAME      explicit server name
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   187
    -p PORT      explicit server port
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   188
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   189
  Manage resident Isabelle servers.
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   190
""",
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   191
          "L" -> (_ => operation_list = true),
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   192
          "n:" -> (arg => name = arg),
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   193
          "p:" -> (arg => port = Value.Int.parse(arg)))
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   194
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   195
      val more_args = getopts(args)
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   196
      if (more_args.nonEmpty) getopts.usage()
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   197
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   198
      if (operation_list) {
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   199
        for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active)
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
   200
          Output.writeln(entry.toString, stdout = true)
66353
6e114edae18b proper check for active server;
wenzelm
parents: 66352
diff changeset
   201
      }
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   202
      else {
67789
77a65c9a849a tuned signature;
wenzelm
parents: 67788
diff changeset
   203
        val (entry, server) = init(name, port)
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
   204
        Output.writeln(entry.toString, stdout = true)
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   205
        server.foreach(_.join)
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   206
      }
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   207
    })
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   208
}
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   209
66352
7ed911242266 clarified signature;
wenzelm
parents: 66351
diff changeset
   210
class Server private(_port: Int)
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   211
{
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   212
  server =>
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   213
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   214
  private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1"))
67791
acecef5fad58 tuned signature;
wenzelm
parents: 67790
diff changeset
   215
acecef5fad58 tuned signature;
wenzelm
parents: 67790
diff changeset
   216
  def close() { server_socket.close }
acecef5fad58 tuned signature;
wenzelm
parents: 67790
diff changeset
   217
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   218
  def port: Int = server_socket.getLocalPort
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
   219
  val password: String = Library.UUID()
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   220
67787
8335d88195c4 clarified toString operations;
wenzelm
parents: 67786
diff changeset
   221
  override def toString: String = Server.print(port, password)
66348
a426e826e84c more options;
wenzelm
parents: 66347
diff changeset
   222
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   223
  private def handle(connection: Server.Connection)
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   224
  {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   225
    connection.read_line() match {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   226
      case None =>
67792
73c7a55972b4 more implicit wire protocol;
wenzelm
parents: 67791
diff changeset
   227
      case Some(line) if line != password => connection.reply_error("Bad protocol")
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   228
      case _ =>
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   229
        var finished = false
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   230
        while (!finished) {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   231
          connection.read_line() match {
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   232
            case None => finished = true
67795
f8037c7e4659 ignore empty lines;
wenzelm
parents: 67794
diff changeset
   233
            case Some(line) if line != "" =>
67794
wenzelm
parents: 67793
diff changeset
   234
              val (cmd, input) = Server.split_line(line)
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   235
              Server.commands.get(cmd) match {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   236
                case None => connection.reply_error("Bad command " + quote(cmd))
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   237
                case Some(body) =>
67794
wenzelm
parents: 67793
diff changeset
   238
                  input match {
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   239
                    case JSON.Format(arg) =>
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   240
                      if (body.isDefinedAt((server, arg))) {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   241
                        try { connection.reply_ok(body(server, arg)) }
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   242
                        catch { case ERROR(msg) => connection.reply_error(msg) }
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   243
                      }
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   244
                      else {
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   245
                        connection.reply_error_message(
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   246
                          "Bad argument for command", "command" -> cmd, "argument" -> arg)
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   247
                      }
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   248
                    case _ =>
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   249
                      connection.reply_error_message(
66929
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   250
                        "Malformed command-line", "command" -> cmd, "input" -> input)
c19b17b72777 some concrete commands;
wenzelm
parents: 66927
diff changeset
   251
                  }
66921
3d3bd0718ef2 clarified command language;
wenzelm
parents: 66857
diff changeset
   252
              }
3d3bd0718ef2 clarified command language;
wenzelm
parents: 66857
diff changeset
   253
            case _ =>
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   254
          }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   255
        }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   256
    }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   257
  }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   258
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   259
  private lazy val server_thread: Thread =
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   260
    Standard_Thread.fork("server") {
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   261
      var finished = false
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   262
      while (!finished) {
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   263
        Exn.capture(server_socket.accept) match {
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   264
          case Exn.Res(socket) =>
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   265
            Standard_Thread.fork("server_connection")
67786
be6d69595ca7 clarified socket connection;
wenzelm
parents: 67785
diff changeset
   266
              { using(Server.Connection(socket))(handle(_)) }
66350
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   267
          case Exn.Exn(_) => finished = true
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   268
        }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   269
      }
66331026a2fc handle server connections;
wenzelm
parents: 66349
diff changeset
   270
    }
67785
ad96390ceb5d server commands may access Server;
wenzelm
parents: 67784
diff changeset
   271
67790
1babcc248be0 clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents: 67789
diff changeset
   272
  def start { server_thread }
1babcc248be0 clarified server start, notably for invocation within regular Isabelle/Scala process;
wenzelm
parents: 67789
diff changeset
   273
67791
acecef5fad58 tuned signature;
wenzelm
parents: 67790
diff changeset
   274
  def join { server_thread.join; close() }
66347
23eaab37e4a8 support for resident Isabelle servers;
wenzelm
parents:
diff changeset
   275
}