src/Pure/Tools/server.scala
changeset 67787 8335d88195c4
parent 67786 be6d69595ca7
child 67788 34e76587dc94
equal deleted inserted replaced
67786:be6d69595ca7 67787:8335d88195c4
    71   }
    71   }
    72 
    72 
    73 
    73 
    74   /* per-user servers */
    74   /* per-user servers */
    75 
    75 
       
    76   def print(port: Int, password: String): String =
       
    77     "127.0.0.1:" + port + " (password " + quote(password) + ")"
       
    78 
    76   object Data
    79   object Data
    77   {
    80   {
    78     val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
    81     val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
    79 
    82 
    80     val name = SQL.Column.string("name").make_primary_key
    83     val name = SQL.Column.string("name").make_primary_key
    82     val password = SQL.Column.string("password")
    85     val password = SQL.Column.string("password")
    83     val table = SQL.Table("isabelle_servers", List(name, port, password))
    86     val table = SQL.Table("isabelle_servers", List(name, port, password))
    84 
    87 
    85     sealed case class Entry(name: String, port: Int, password: String)
    88     sealed case class Entry(name: String, port: Int, password: String)
    86     {
    89     {
    87       def print: String =
    90       override def toString: String =
    88         "server " + quote(name) + " = 127.0.0.1:" + port + " (password " + quote(password) + ")"
    91         "server " + quote(name) + " = " + print(port, password)
    89 
    92 
    90       def connection(): Connection =
    93       def connection(): Connection =
    91         Connection(new Socket(InetAddress.getByName("127.0.0.1"), port))
    94         Connection(new Socket(InetAddress.getByName("127.0.0.1"), port))
    92 
    95 
    93       def active(): Boolean =
    96       def active(): Boolean =
   182       val more_args = getopts(args)
   185       val more_args = getopts(args)
   183       if (more_args.nonEmpty) getopts.usage()
   186       if (more_args.nonEmpty) getopts.usage()
   184 
   187 
   185       if (operation_list) {
   188       if (operation_list) {
   186         for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active)
   189         for (entry <- using(SQLite.open_database(Data.database))(list(_)) if entry.active)
   187           Output.writeln(entry.print, stdout = true)
   190           Output.writeln(entry.toString, stdout = true)
   188       }
   191       }
   189       else {
   192       else {
   190         val (entry, server) = start(name, port)
   193         val (entry, server) = start(name, port)
   191         Output.writeln(entry.print, stdout = true)
   194         Output.writeln(entry.toString, stdout = true)
   192         server.foreach(_.join)
   195         server.foreach(_.join)
   193       }
   196       }
   194     })
   197     })
   195 }
   198 }
   196 
   199 
   198 {
   201 {
   199   server =>
   202   server =>
   200 
   203 
   201   private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1"))
   204   private val server_socket = new ServerSocket(_port, 50, InetAddress.getByName("127.0.0.1"))
   202   def port: Int = server_socket.getLocalPort
   205   def port: Int = server_socket.getLocalPort
   203 
       
   204   val password: String = Library.UUID()
   206   val password: String = Library.UUID()
       
   207 
       
   208   override def toString: String = Server.print(port, password)
   205 
   209 
   206   private def handle(connection: Server.Connection)
   210   private def handle(connection: Server.Connection)
   207   {
   211   {
   208     connection.read_line() match {
   212     connection.read_line() match {
   209       case None =>
   213       case None =>