src/Pure/Tools/server.scala
changeset 78397 c8df7abb8707
parent 78396 7853d9072d1b
child 78596 470d4f1e89d9
equal deleted inserted replaced
78396:7853d9072d1b 78397:c8df7abb8707
   364   val default_name = "isabelle"
   364   val default_name = "isabelle"
   365 
   365 
   366   object private_data extends SQL.Data() {
   366   object private_data extends SQL.Data() {
   367     val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
   367     val database = Path.explode("$ISABELLE_HOME_USER/servers.db")
   368 
   368 
   369     val name = SQL.Column.string("name").make_primary_key
   369     override lazy val tables = SQL.Tables(Base.table)
   370     val port = SQL.Column.int("port")
   370 
   371     val password = SQL.Column.string("password")
   371     object Base {
   372     val table = SQL.Table("isabelle_servers", List(name, port, password))
   372       val name = SQL.Column.string("name").make_primary_key
   373 
   373       val port = SQL.Column.int("port")
   374     override val tables = SQL.Tables(table)
   374       val password = SQL.Column.string("password")
   375   }
   375       val table = SQL.Table("isabelle_servers", List(name, port, password))
   376 
   376     }
   377   def list(db: SQLite.Database): List[Info] =
   377 
   378     if (db.exists_table(private_data.table)) {
   378     def list(db: SQLite.Database): List[Info] =
   379       db.execute_query_statement(private_data.table.select(),
   379       if (db.exists_table(Base.table)) {
   380         List.from[Info],
   380         db.execute_query_statement(Base.table.select(),
   381         { res =>
   381           List.from[Info],
   382           val name = res.string(private_data.name)
   382           { res =>
   383           val port = res.int(private_data.port)
   383             val name = res.string(Base.name)
   384           val password = res.string(private_data.password)
   384             val port = res.int(Base.port)
   385           Info(name, port, password)
   385             val password = res.string(Base.password)
   386         }
   386             Info(name, port, password)
   387       ).sortBy(_.name)
   387           }
   388     }
   388         ).sortBy(_.name)
   389     else Nil
   389       }
   390 
   390       else Nil
   391   def find(db: SQLite.Database, name: String): Option[Info] =
   391 
   392     list(db).find(server_info => server_info.name == name && server_info.active)
   392     def find(db: SQLite.Database, name: String): Option[Info] =
       
   393       list(db).find(server_info => server_info.name == name && server_info.active)
       
   394   }
   393 
   395 
   394   def init(
   396   def init(
   395     name: String = default_name,
   397     name: String = default_name,
   396     port: Int = 0,
   398     port: Int = 0,
   397     existing_server: Boolean = false,
   399     existing_server: Boolean = false,
   398     log: Logger = No_Logger
   400     log: Logger = No_Logger
   399   ): (Info, Option[Server]) = {
   401   ): (Info, Option[Server]) = {
   400     using(SQLite.open_database(private_data.database, restrict = true)) { db =>
   402     using(SQLite.open_database(private_data.database, restrict = true)) { db =>
   401       private_data.transaction_lock(db, create = true) {
   403       private_data.transaction_lock(db, create = true) {
   402         list(db).filterNot(_.active).foreach(server_info =>
   404         private_data.list(db).filterNot(_.active).foreach(server_info =>
   403           db.execute_statement(private_data.table.delete(sql = private_data.name.where_equal(server_info.name))))
   405           db.execute_statement(
       
   406             private_data.Base.table.delete(sql =
       
   407               private_data.Base.name.where_equal(server_info.name))))
   404       }
   408       }
   405       private_data.transaction_lock(db) {
   409       private_data.transaction_lock(db) {
   406         find(db, name) match {
   410         private_data.find(db, name) match {
   407           case Some(server_info) => (server_info, None)
   411           case Some(server_info) => (server_info, None)
   408           case None =>
   412           case None =>
   409             if (existing_server) error("Isabelle server " + quote(name) + " not running")
   413             if (existing_server) error("Isabelle server " + quote(name) + " not running")
   410 
   414 
   411             val server = new Server(port, log)
   415             val server = new Server(port, log)
   412             val server_info = Info(name, server.port, server.password)
   416             val server_info = Info(name, server.port, server.password)
   413 
   417 
   414             db.execute_statement(private_data.table.delete(sql = private_data.name.where_equal(name)))
   418             db.execute_statement(
   415             db.execute_statement(private_data.table.insert(), body =
   419               private_data.Base.table.delete(sql = private_data.Base.name.where_equal(name)))
       
   420             db.execute_statement(private_data.Base.table.insert(), body =
   416               { stmt =>
   421               { stmt =>
   417                 stmt.string(1) = server_info.name
   422                 stmt.string(1) = server_info.name
   418                 stmt.int(2) = server_info.port
   423                 stmt.int(2) = server_info.port
   419                 stmt.string(3) = server_info.password
   424                 stmt.string(3) = server_info.password
   420               })
   425               })
   427   }
   432   }
   428 
   433 
   429   def exit(name: String = default_name): Boolean = {
   434   def exit(name: String = default_name): Boolean = {
   430     using(SQLite.open_database(private_data.database)) { db =>
   435     using(SQLite.open_database(private_data.database)) { db =>
   431       private_data.transaction_lock(db) {
   436       private_data.transaction_lock(db) {
   432         find(db, name) match {
   437         private_data.find(db, name) match {
   433           case Some(server_info) =>
   438           case Some(server_info) =>
   434             using(server_info.connection())(_.write_line_message("shutdown"))
   439             using(server_info.connection())(_.write_line_message("shutdown"))
   435             while(server_info.active) { Time.seconds(0.05).sleep() }
   440             while(server_info.active) { Time.seconds(0.05).sleep() }
   436             true
   441             true
   437           case None => false
   442           case None => false
   479         val more_args = getopts(args)
   484         val more_args = getopts(args)
   480         if (more_args.nonEmpty) getopts.usage()
   485         if (more_args.nonEmpty) getopts.usage()
   481 
   486 
   482         if (operation_list) {
   487         if (operation_list) {
   483           for {
   488           for {
   484             server_info <- using(SQLite.open_database(private_data.database))(list)
   489             server_info <- using(SQLite.open_database(private_data.database))(private_data.list)
   485             if server_info.active
   490             if server_info.active
   486           } Output.writeln(server_info.toString, stdout = true)
   491           } Output.writeln(server_info.toString, stdout = true)
   487         }
   492         }
   488         else if (operation_exit) {
   493         else if (operation_exit) {
   489           val ok = Server.exit(name)
   494           val ok = Server.exit(name)