src/Pure/PIDE/session.scala
author wenzelm
Wed Jan 14 17:24:55 2015 +0100 (2015-01-14 ago)
changeset 59367 6193bbbbe564
parent 59366 e94df7f6b608
child 60075 b079ee0e766c
permissions -rw-r--r--
more type-safe handler interface;
proper progress for Build.Handler;
wenzelm@56210
     1
/*  Title:      Pure/PIDE/session.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@57923
     3
    Options:    :folding=explicit:
wenzelm@36676
     4
wenzelm@56210
     5
PIDE editor session, potentially with running prover process.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@44733
    10
wenzelm@46771
    11
import scala.collection.immutable.Queue
wenzelm@34777
    12
wenzelm@34815
    13
wenzelm@34791
    14
object Session
wenzelm@34791
    15
{
wenzelm@56715
    16
  /* outlets */
wenzelm@56715
    17
wenzelm@56715
    18
  object Consumer
wenzelm@56715
    19
  {
wenzelm@56715
    20
    def apply[A](name: String)(consume: A => Unit): Consumer[A] =
wenzelm@56715
    21
      new Consumer[A](name, consume)
wenzelm@56715
    22
  }
wenzelm@56715
    23
  final class Consumer[-A] private(val name: String, val consume: A => Unit)
wenzelm@56715
    24
wenzelm@56715
    25
  class Outlet[A](dispatcher: Consumer_Thread[() => Unit])
wenzelm@56715
    26
  {
wenzelm@56715
    27
    private val consumers = Synchronized(List.empty[Consumer[A]])
wenzelm@56715
    28
wenzelm@56715
    29
    def += (c: Consumer[A]) { consumers.change(Library.update(c)) }
wenzelm@56715
    30
    def -= (c: Consumer[A]) { consumers.change(Library.remove(c)) }
wenzelm@56715
    31
wenzelm@56715
    32
    def post(a: A)
wenzelm@56715
    33
    {
wenzelm@56715
    34
      for (c <- consumers.value.iterator) {
wenzelm@56715
    35
        dispatcher.send(() =>
wenzelm@56715
    36
          try { c.consume(a) }
wenzelm@56715
    37
          catch {
wenzelm@56715
    38
            case exn: Throwable =>
wenzelm@56782
    39
              Output.error_message("Consumer failed: " + quote(c.name) + "\n" + Exn.message(exn))
wenzelm@56715
    40
          })
wenzelm@56715
    41
      }
wenzelm@56715
    42
    }
wenzelm@56715
    43
  }
wenzelm@56715
    44
wenzelm@56715
    45
wenzelm@56315
    46
  /* change */
wenzelm@56315
    47
wenzelm@56315
    48
  sealed case class Change(
wenzelm@56315
    49
    previous: Document.Version,
wenzelm@59077
    50
    syntax_changed: List[Document.Node.Name],
wenzelm@56316
    51
    deps_changed: Boolean,
wenzelm@56315
    52
    doc_edits: List[Document.Edit_Command],
wenzelm@56315
    53
    version: Document.Version)
wenzelm@56315
    54
wenzelm@57976
    55
  case object Change_Flush
wenzelm@57976
    56
wenzelm@56315
    57
wenzelm@34813
    58
  /* events */
wenzelm@34813
    59
wenzelm@43716
    60
  //{{{
wenzelm@50697
    61
  case class Statistics(props: Properties.T)
wenzelm@50117
    62
  case class Global_Options(options: Options)
wenzelm@44805
    63
  case object Caret_Focus
wenzelm@54521
    64
  case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52531
    65
  case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@59364
    66
  case class Build_Theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@47027
    67
  case class Commands_Changed(
wenzelm@47027
    68
    assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    69
wenzelm@39630
    70
  sealed abstract class Phase
wenzelm@39630
    71
  case object Inactive extends Phase
wenzelm@39701
    72
  case object Startup extends Phase  // transient
wenzelm@39701
    73
  case object Failed extends Phase
wenzelm@39630
    74
  case object Ready extends Phase
wenzelm@39701
    75
  case object Shutdown extends Phase  // transient
wenzelm@43716
    76
  //}}}
wenzelm@52111
    77
wenzelm@52111
    78
wenzelm@56775
    79
  /* syslog */
wenzelm@56775
    80
wenzelm@56775
    81
  private[Session] class Syslog(limit: Int)
wenzelm@56775
    82
  {
wenzelm@56775
    83
    private var queue = Queue.empty[XML.Elem]
wenzelm@56775
    84
    private var length = 0
wenzelm@56775
    85
wenzelm@56775
    86
    def += (msg: XML.Elem): Unit = synchronized {
wenzelm@56775
    87
      queue = queue.enqueue(msg)
wenzelm@56775
    88
      length += 1
wenzelm@56775
    89
      if (length > limit) queue = queue.dequeue._2
wenzelm@56775
    90
    }
wenzelm@56775
    91
wenzelm@56775
    92
    def content: String = synchronized {
wenzelm@56775
    93
      cat_lines(queue.iterator.map(XML.content)) +
wenzelm@56775
    94
      (if (length > limit) "\n(A total of " + length + " messages...)" else "")
wenzelm@56775
    95
    }
wenzelm@56775
    96
  }
wenzelm@56775
    97
wenzelm@56775
    98
wenzelm@52111
    99
  /* protocol handlers */
wenzelm@52111
   100
wenzelm@52111
   101
  abstract class Protocol_Handler
wenzelm@52111
   102
  {
wenzelm@56864
   103
    def start(prover: Prover): Unit = {}
wenzelm@52111
   104
    def stop(prover: Prover): Unit = {}
wenzelm@56385
   105
    val functions: Map[String, (Prover, Prover.Protocol_Output) => Boolean]
wenzelm@52111
   106
  }
wenzelm@52111
   107
wenzelm@59367
   108
  def bad_protocol_handler(exn: Throwable, name: String): Unit =
wenzelm@59367
   109
    Output.error_message(
wenzelm@59367
   110
      "Failed to initialize protocol handler: " + quote(name) + "\n" + Exn.message(exn))
wenzelm@59367
   111
wenzelm@59366
   112
  private class Protocol_Handlers(
wenzelm@52111
   113
    handlers: Map[String, Session.Protocol_Handler] = Map.empty,
wenzelm@56385
   114
    functions: Map[String, Prover.Protocol_Output => Boolean] = Map.empty)
wenzelm@52111
   115
  {
wenzelm@53054
   116
    def get(name: String): Option[Protocol_Handler] = handlers.get(name)
wenzelm@53054
   117
wenzelm@59367
   118
    def add(prover: Prover, handler: Protocol_Handler): Protocol_Handlers =
wenzelm@52111
   119
    {
wenzelm@59367
   120
      val name = handler.getClass.getName
wenzelm@59367
   121
wenzelm@52111
   122
      val (handlers1, functions1) =
wenzelm@52111
   123
        handlers.get(name) match {
wenzelm@52111
   124
          case Some(old_handler) =>
wenzelm@56782
   125
            Output.warning("Redefining protocol handler: " + name)
wenzelm@52111
   126
            old_handler.stop(prover)
wenzelm@52111
   127
            (handlers - name, functions -- old_handler.functions.keys)
wenzelm@52111
   128
          case None => (handlers, functions)
wenzelm@52111
   129
        }
wenzelm@52111
   130
wenzelm@52111
   131
      val (handlers2, functions2) =
wenzelm@52111
   132
        try {
wenzelm@59367
   133
          handler.start(prover)
wenzelm@56864
   134
wenzelm@52111
   135
          val new_functions =
wenzelm@59367
   136
            for ((a, f) <- handler.functions.toList) yield
wenzelm@56385
   137
              (a, (msg: Prover.Protocol_Output) => f(prover, msg))
wenzelm@52111
   138
wenzelm@52111
   139
          val dups = for ((a, _) <- new_functions if functions1.isDefinedAt(a)) yield a
wenzelm@59319
   140
          if (dups.nonEmpty) error("Duplicate protocol functions: " + commas_quote(dups))
wenzelm@52111
   141
wenzelm@59367
   142
          (handlers1 + (name -> handler), functions1 ++ new_functions)
wenzelm@52111
   143
        }
wenzelm@52111
   144
        catch {
wenzelm@52111
   145
          case exn: Throwable =>
wenzelm@59367
   146
            Session.bad_protocol_handler(exn, name)
wenzelm@52111
   147
            (handlers1, functions1)
wenzelm@52111
   148
        }
wenzelm@52111
   149
wenzelm@52111
   150
      new Protocol_Handlers(handlers2, functions2)
wenzelm@52111
   151
    }
wenzelm@52111
   152
wenzelm@56385
   153
    def invoke(msg: Prover.Protocol_Output): Boolean =
wenzelm@54442
   154
      msg.properties match {
wenzelm@52111
   155
        case Markup.Function(a) if functions.isDefinedAt(a) =>
wenzelm@54442
   156
          try { functions(a)(msg) }
wenzelm@52111
   157
          catch {
wenzelm@52111
   158
            case exn: Throwable =>
wenzelm@56782
   159
              Output.error_message(
wenzelm@56782
   160
                "Failed invocation of protocol function: " + quote(a) + "\n" + Exn.message(exn))
wenzelm@52111
   161
            false
wenzelm@52111
   162
          }
wenzelm@52111
   163
        case _ => false
wenzelm@52111
   164
      }
wenzelm@52113
   165
wenzelm@52113
   166
    def stop(prover: Prover): Protocol_Handlers =
wenzelm@52113
   167
    {
wenzelm@52113
   168
      for ((_, handler) <- handlers) handler.stop(prover)
wenzelm@52113
   169
      new Protocol_Handlers()
wenzelm@52113
   170
    }
wenzelm@52111
   171
  }
wenzelm@34791
   172
}
wenzelm@34791
   173
wenzelm@34777
   174
wenzelm@56208
   175
class Session(val resources: Resources)
wenzelm@34777
   176
{
wenzelm@47653
   177
  /* global flags */
wenzelm@47653
   178
wenzelm@47653
   179
  @volatile var timing: Boolean = false
wenzelm@47653
   180
  @volatile var verbose: Boolean = false
wenzelm@47653
   181
wenzelm@47653
   182
wenzelm@49288
   183
  /* tuning parameters */
wenzelm@49288
   184
wenzelm@49288
   185
  def output_delay: Time = Time.seconds(0.1)  // prover output (markup, common messages)
wenzelm@57867
   186
  def prune_delay: Time = Time.seconds(60.0)  // prune history (delete old versions)
wenzelm@49293
   187
  def prune_size: Int = 0  // size of retained history
wenzelm@49293
   188
  def syslog_limit: Int = 100
wenzelm@49524
   189
  def reparse_limit: Int = 0
wenzelm@37849
   190
wenzelm@37849
   191
wenzelm@56715
   192
  /* outlets */
wenzelm@56715
   193
wenzelm@56715
   194
  private val dispatcher =
wenzelm@56715
   195
    Consumer_Thread.fork[() => Unit]("Session.dispatcher", daemon = true) { case e => e(); true }
wenzelm@34809
   196
wenzelm@56715
   197
  val statistics = new Session.Outlet[Session.Statistics](dispatcher)
wenzelm@56715
   198
  val global_options = new Session.Outlet[Session.Global_Options](dispatcher)
wenzelm@56715
   199
  val caret_focus = new Session.Outlet[Session.Caret_Focus.type](dispatcher)
wenzelm@56715
   200
  val raw_edits = new Session.Outlet[Session.Raw_Edits](dispatcher)
wenzelm@56715
   201
  val commands_changed = new Session.Outlet[Session.Commands_Changed](dispatcher)
wenzelm@56715
   202
  val phase_changed = new Session.Outlet[Session.Phase](dispatcher)
wenzelm@56715
   203
  val syslog_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   204
  val raw_output_messages = new Session.Outlet[Prover.Output](dispatcher)
wenzelm@56715
   205
  val all_messages = new Session.Outlet[Prover.Message](dispatcher)  // potential bottle-neck
wenzelm@56715
   206
  val trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher)
wenzelm@34809
   207
wenzelm@34809
   208
wenzelm@56706
   209
wenzelm@56799
   210
  /** main protocol manager **/
wenzelm@45635
   211
wenzelm@56799
   212
  /* internal messages */
wenzelm@45635
   213
wenzelm@56799
   214
  private case class Start(name: String, args: List[String])
wenzelm@56799
   215
  private case object Stop
wenzelm@56799
   216
  private case class Cancel_Exec(exec_id: Document_ID.Exec)
wenzelm@56799
   217
  private case class Protocol_Command(name: String, args: List[String])
wenzelm@56799
   218
  private case class Update_Options(options: Options)
wenzelm@56799
   219
  private case object Prune_History
wenzelm@45635
   220
wenzelm@38841
   221
wenzelm@43651
   222
  /* global state */
wenzelm@43644
   223
wenzelm@56775
   224
  private val syslog = new Session.Syslog(syslog_limit)
wenzelm@56775
   225
  def syslog_content(): String = syslog.content
wenzelm@39626
   226
wenzelm@39630
   227
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   228
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   229
  {
wenzelm@39630
   230
    _phase = new_phase
wenzelm@56715
   231
    phase_changed.post(new_phase)
wenzelm@39630
   232
  }
wenzelm@43716
   233
  def phase = _phase
wenzelm@43553
   234
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   235
wenzelm@56690
   236
  private val global_state = Synchronized(Document.State.init)
wenzelm@56687
   237
  def current_state(): Document.State = global_state.value
wenzelm@46944
   238
wenzelm@59077
   239
  def recent_syntax(name: Document.Node.Name): Prover.Syntax =
wenzelm@59077
   240
    current_state().recent_finished.version.get_finished.nodes(name).syntax getOrElse
wenzelm@59077
   241
    resources.base_syntax
wenzelm@34816
   242
wenzelm@43651
   243
wenzelm@43651
   244
  /* theory files */
wenzelm@43651
   245
wenzelm@48707
   246
  def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
wenzelm@44442
   247
  {
wenzelm@48707
   248
    val header1 =
wenzelm@56208
   249
      if (resources.loaded_theories(name.theory))
wenzelm@54559
   250
        header.error("Cannot update finished theory " + quote(name.theory))
wenzelm@48707
   251
      else header
wenzelm@48707
   252
    (name, Document.Node.Deps(header1))
wenzelm@44442
   253
  }
wenzelm@44442
   254
wenzelm@43651
   255
wenzelm@56799
   256
  /* pipelined change parsing */
wenzelm@56799
   257
wenzelm@56799
   258
  private case class Text_Edits(
wenzelm@56799
   259
    previous: Future[Document.Version],
wenzelm@56799
   260
    doc_blobs: Document.Blobs,
wenzelm@56799
   261
    text_edits: List[Document.Edit_Text],
wenzelm@56799
   262
    version_result: Promise[Document.Version])
wenzelm@43651
   263
wenzelm@56799
   264
  private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
wenzelm@56799
   265
  {
wenzelm@56799
   266
    case Text_Edits(previous, doc_blobs, text_edits, version_result) =>
wenzelm@56799
   267
      val prev = previous.get_finished
wenzelm@56799
   268
      val change =
wenzelm@56799
   269
        Timing.timeit("parse_change", timing) {
wenzelm@56799
   270
          resources.parse_change(reparse_limit, prev, doc_blobs, text_edits)
wenzelm@56799
   271
        }
wenzelm@56799
   272
      version_result.fulfill(change.version)
wenzelm@56799
   273
      manager.send(change)
wenzelm@56799
   274
      true
wenzelm@56799
   275
  }
wenzelm@41534
   276
wenzelm@56706
   277
wenzelm@56719
   278
  /* buffered changes */
wenzelm@56719
   279
wenzelm@56719
   280
  private object change_buffer
wenzelm@56719
   281
  {
wenzelm@56719
   282
    private var assignment: Boolean = false
wenzelm@56719
   283
    private var nodes: Set[Document.Node.Name] = Set.empty
wenzelm@56719
   284
    private var commands: Set[Command] = Set.empty
wenzelm@56719
   285
wenzelm@56719
   286
    def flush(): Unit = synchronized {
wenzelm@59319
   287
      if (assignment || nodes.nonEmpty || commands.nonEmpty)
wenzelm@56719
   288
        commands_changed.post(Session.Commands_Changed(assignment, nodes, commands))
wenzelm@56719
   289
      assignment = false
wenzelm@56719
   290
      nodes = Set.empty
wenzelm@56719
   291
      commands = Set.empty
wenzelm@56719
   292
    }
wenzelm@56771
   293
    private val delay_flush = Simple_Thread.delay_first(output_delay) { flush() }
wenzelm@56719
   294
wenzelm@56719
   295
    def invoke(assign: Boolean, cmds: List[Command]): Unit = synchronized {
wenzelm@56719
   296
      assignment |= assign
wenzelm@56719
   297
      for (command <- cmds) {
wenzelm@56719
   298
        nodes += command.node_name
wenzelm@56719
   299
        commands += command
wenzelm@56719
   300
      }
wenzelm@56771
   301
      delay_flush.invoke()
wenzelm@56719
   302
    }
wenzelm@56719
   303
wenzelm@56719
   304
    def shutdown()
wenzelm@56719
   305
    {
wenzelm@56771
   306
      delay_flush.revoke()
wenzelm@56719
   307
      flush()
wenzelm@56719
   308
    }
wenzelm@56719
   309
  }
wenzelm@56719
   310
wenzelm@56719
   311
wenzelm@56706
   312
  /* postponed changes */
wenzelm@44733
   313
wenzelm@56706
   314
  private object postponed_changes
wenzelm@56706
   315
  {
wenzelm@56706
   316
    private var postponed: List[Session.Change] = Nil
wenzelm@56706
   317
wenzelm@56706
   318
    def store(change: Session.Change): Unit = synchronized { postponed ::= change }
wenzelm@56705
   319
wenzelm@57976
   320
    def flush(state: Document.State): List[Session.Change] = synchronized {
wenzelm@56706
   321
      val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous))
wenzelm@56706
   322
      postponed = unassigned
wenzelm@57976
   323
      assigned.reverse
wenzelm@56706
   324
    }
wenzelm@56706
   325
  }
wenzelm@44733
   326
wenzelm@56706
   327
wenzelm@56793
   328
  /* prover process */
wenzelm@56793
   329
wenzelm@56793
   330
  private object prover
wenzelm@56793
   331
  {
wenzelm@56793
   332
    private val variable = Synchronized(None: Option[Prover])
wenzelm@56793
   333
wenzelm@56793
   334
    def defined: Boolean = variable.value.isDefined
wenzelm@56793
   335
    def get: Prover = variable.value.get
wenzelm@56793
   336
    def set(p: Prover) { variable.change(_ => Some(p)) }
wenzelm@56793
   337
    def reset { variable.change(_ => None) }
wenzelm@56793
   338
    def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
wenzelm@56793
   339
  }
wenzelm@56793
   340
wenzelm@56793
   341
wenzelm@59366
   342
  /* protocol handlers */
wenzelm@59366
   343
wenzelm@59366
   344
  private val _protocol_handlers = Synchronized(new Session.Protocol_Handlers)
wenzelm@59366
   345
wenzelm@59366
   346
  def get_protocol_handler(name: String): Option[Session.Protocol_Handler] =
wenzelm@59366
   347
    _protocol_handlers.value.get(name)
wenzelm@59366
   348
wenzelm@59367
   349
  def add_protocol_handler(handler: Session.Protocol_Handler): Unit =
wenzelm@59367
   350
    _protocol_handlers.change(_.add(prover.get, handler))
wenzelm@59366
   351
wenzelm@59366
   352
wenzelm@56706
   353
  /* manager thread */
wenzelm@44733
   354
wenzelm@56771
   355
  private val delay_prune = Simple_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
wenzelm@56771
   356
wenzelm@56706
   357
  private val manager: Consumer_Thread[Any] =
wenzelm@56706
   358
  {
wenzelm@52649
   359
    /* raw edits */
wenzelm@52649
   360
wenzelm@54521
   361
    def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52649
   362
    //{{{
wenzelm@52649
   363
    {
wenzelm@56793
   364
      require(prover.defined)
wenzelm@56712
   365
wenzelm@52649
   366
      prover.get.discontinue_execution()
wenzelm@52649
   367
wenzelm@56687
   368
      val previous = global_state.value.history.tip.version
wenzelm@52649
   369
      val version = Future.promise[Document.Version]
wenzelm@56711
   370
      global_state.change(_.continue_history(previous, edits, version))
wenzelm@52649
   371
wenzelm@56715
   372
      raw_edits.post(Session.Raw_Edits(doc_blobs, edits))
wenzelm@56704
   373
      change_parser.send(Text_Edits(previous, doc_blobs, edits, version))
wenzelm@52649
   374
    }
wenzelm@52649
   375
    //}}}
wenzelm@52649
   376
wenzelm@52649
   377
wenzelm@43716
   378
    /* resulting changes */
wenzelm@34809
   379
wenzelm@56315
   380
    def handle_change(change: Session.Change)
wenzelm@38221
   381
    //{{{
wenzelm@34809
   382
    {
wenzelm@56793
   383
      require(prover.defined)
wenzelm@56712
   384
wenzelm@44383
   385
      def id_command(command: Command)
wenzelm@43720
   386
      {
wenzelm@54519
   387
        for {
wenzelm@57842
   388
          (name, digest) <- command.blobs_defined
wenzelm@56687
   389
          if !global_state.value.defined_blob(digest)
wenzelm@54519
   390
        } {
wenzelm@57842
   391
          change.version.nodes(name).get_blob match {
wenzelm@54519
   392
            case Some(blob) =>
wenzelm@56687
   393
              global_state.change(_.define_blob(digest))
wenzelm@56335
   394
              prover.get.define_blob(digest, blob.bytes)
wenzelm@55783
   395
            case None =>
wenzelm@57842
   396
              Output.error_message("Missing blob " + quote(name.toString))
wenzelm@54519
   397
          }
wenzelm@54519
   398
        }
wenzelm@54519
   399
wenzelm@56687
   400
        if (!global_state.value.defined_command(command.id)) {
wenzelm@56687
   401
          global_state.change(_.define_command(command))
wenzelm@44644
   402
          prover.get.define_command(command)
wenzelm@43720
   403
        }
wenzelm@43720
   404
      }
wenzelm@56315
   405
      change.doc_edits foreach {
wenzelm@44383
   406
        case (_, edit) =>
wenzelm@44383
   407
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   408
      }
wenzelm@43722
   409
wenzelm@56687
   410
      val assignment = global_state.value.the_assignment(change.previous).check_finished
wenzelm@56687
   411
      global_state.change(_.define_version(change.version, assignment))
wenzelm@56315
   412
      prover.get.update(change.previous.id, change.version.id, change.doc_edits)
wenzelm@56316
   413
      resources.commit(change)
wenzelm@34809
   414
    }
wenzelm@38221
   415
    //}}}
wenzelm@34809
   416
wenzelm@34809
   417
wenzelm@46772
   418
    /* prover output */
wenzelm@34809
   419
wenzelm@56385
   420
    def handle_output(output: Prover.Output)
wenzelm@38221
   421
    //{{{
wenzelm@34809
   422
    {
wenzelm@51662
   423
      def bad_output()
wenzelm@43716
   424
      {
wenzelm@43716
   425
        if (verbose)
wenzelm@56782
   426
          Output.warning("Ignoring bad prover output: " + output.message.toString)
wenzelm@43716
   427
      }
wenzelm@43716
   428
wenzelm@52531
   429
      def accumulate(state_id: Document_ID.Generic, message: XML.Elem)
wenzelm@51662
   430
      {
wenzelm@51662
   431
        try {
wenzelm@56687
   432
          val st = global_state.change_result(_.accumulate(state_id, message))
wenzelm@56719
   433
          change_buffer.invoke(false, List(st.command))
wenzelm@51662
   434
        }
wenzelm@51662
   435
        catch {
wenzelm@51662
   436
          case _: Document.State.Fail => bad_output()
wenzelm@51662
   437
        }
wenzelm@51662
   438
      }
wenzelm@51662
   439
wenzelm@54442
   440
      output match {
wenzelm@56385
   441
        case msg: Prover.Protocol_Output =>
wenzelm@59366
   442
          val handled = _protocol_handlers.value.invoke(msg)
wenzelm@54442
   443
          if (!handled) {
wenzelm@54442
   444
            msg.properties match {
wenzelm@56793
   445
              case Markup.Protocol_Handler(name) if prover.defined =>
wenzelm@59367
   446
                try {
wenzelm@59367
   447
                  val handler =
wenzelm@59367
   448
                    Class.forName(name).newInstance.asInstanceOf[Session.Protocol_Handler]
wenzelm@59367
   449
                  add_protocol_handler(handler)
wenzelm@59367
   450
                }
wenzelm@59367
   451
                catch { case exn: Throwable => Session.bad_protocol_handler(exn, name) }
wenzelm@54442
   452
wenzelm@56793
   453
              case Protocol.Command_Timing(state_id, timing) if prover.defined =>
wenzelm@54442
   454
                val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@54442
   455
                accumulate(state_id, prover.get.xml_cache.elem(message))
wenzelm@46122
   456
wenzelm@54442
   457
              case Markup.Assign_Update =>
wenzelm@54442
   458
                msg.text match {
wenzelm@54442
   459
                  case Protocol.Assign_Update(id, update) =>
wenzelm@54442
   460
                    try {
wenzelm@56687
   461
                      val cmds = global_state.change_result(_.assign(id, update))
wenzelm@56719
   462
                      change_buffer.invoke(true, cmds)
wenzelm@57976
   463
                      manager.send(Session.Change_Flush)
wenzelm@54442
   464
                    }
wenzelm@54442
   465
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   466
                  case _ => bad_output()
wenzelm@54442
   467
                }
wenzelm@56771
   468
                delay_prune.invoke()
wenzelm@46122
   469
wenzelm@54442
   470
              case Markup.Removed_Versions =>
wenzelm@54442
   471
                msg.text match {
wenzelm@54442
   472
                  case Protocol.Removed(removed) =>
wenzelm@54442
   473
                    try {
wenzelm@56687
   474
                      global_state.change(_.removed_versions(removed))
wenzelm@57976
   475
                      manager.send(Session.Change_Flush)
wenzelm@54442
   476
                    }
wenzelm@54442
   477
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   478
                  case _ => bad_output()
wenzelm@54442
   479
                }
wenzelm@54442
   480
wenzelm@54442
   481
              case Markup.ML_Statistics(props) =>
wenzelm@56715
   482
                statistics.post(Session.Statistics(props))
wenzelm@54442
   483
wenzelm@54442
   484
              case Markup.Task_Statistics(props) =>
wenzelm@54442
   485
                // FIXME
wenzelm@52111
   486
wenzelm@54442
   487
              case _ => bad_output()
wenzelm@54442
   488
            }
wenzelm@54442
   489
          }
wenzelm@54442
   490
        case _ =>
wenzelm@54442
   491
          output.properties match {
wenzelm@54442
   492
            case Position.Id(state_id) =>
wenzelm@54442
   493
              accumulate(state_id, output.message)
wenzelm@56210
   494
wenzelm@54442
   495
            case _ if output.is_init =>
wenzelm@54442
   496
              phase = Session.Ready
wenzelm@56210
   497
wenzelm@54442
   498
            case Markup.Return_Code(rc) if output.is_exit =>
wenzelm@54442
   499
              if (rc == 0) phase = Session.Inactive
wenzelm@54442
   500
              else phase = Session.Failed
wenzelm@56793
   501
              prover.reset
wenzelm@56210
   502
wenzelm@56715
   503
            case _ => raw_output_messages.post(output)
wenzelm@44661
   504
          }
wenzelm@52111
   505
        }
wenzelm@34809
   506
    }
wenzelm@38221
   507
    //}}}
wenzelm@34809
   508
wenzelm@34820
   509
wenzelm@56706
   510
    /* main thread */
wenzelm@56706
   511
wenzelm@56710
   512
    Consumer_Thread.fork[Any]("Session.manager", daemon = true)
wenzelm@56709
   513
    {
wenzelm@56709
   514
      case arg: Any =>
wenzelm@56709
   515
        //{{{
wenzelm@56709
   516
        arg match {
wenzelm@56733
   517
          case output: Prover.Output =>
wenzelm@56775
   518
            if (output.is_stdout || output.is_stderr)
wenzelm@56775
   519
              raw_output_messages.post(output)
wenzelm@56733
   520
            else handle_output(output)
wenzelm@56775
   521
wenzelm@56733
   522
            if (output.is_syslog) {
wenzelm@56775
   523
              syslog += output.message
wenzelm@56733
   524
              syslog_messages.post(output)
wenzelm@56733
   525
            }
wenzelm@56775
   526
wenzelm@56733
   527
            all_messages.post(output)
wenzelm@56733
   528
wenzelm@56733
   529
          case input: Prover.Input =>
wenzelm@56733
   530
            all_messages.post(input)
wenzelm@56733
   531
wenzelm@56793
   532
          case Start(name, args) if !prover.defined =>
wenzelm@56709
   533
            if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@56709
   534
              phase = Session.Startup
wenzelm@56793
   535
              prover.set(resources.start_prover(manager.send(_), name, args))
wenzelm@56709
   536
            }
wenzelm@56706
   537
wenzelm@56709
   538
          case Stop =>
wenzelm@56793
   539
            if (prover.defined && is_ready) {
wenzelm@59366
   540
              _protocol_handlers.change(_.stop(prover.get))
wenzelm@56772
   541
              global_state.change(_ => Document.State.init)
wenzelm@56709
   542
              phase = Session.Shutdown
wenzelm@56709
   543
              prover.get.terminate
wenzelm@56709
   544
            }
wenzelm@56706
   545
wenzelm@56771
   546
          case Prune_History =>
wenzelm@56793
   547
            if (prover.defined) {
wenzelm@57976
   548
              val old_versions = global_state.change_result(_.remove_versions(prune_size))
wenzelm@59319
   549
              if (old_versions.nonEmpty) prover.get.remove_versions(old_versions)
wenzelm@56771
   550
            }
wenzelm@56771
   551
wenzelm@56709
   552
          case Update_Options(options) =>
wenzelm@56793
   553
            if (prover.defined && is_ready) {
wenzelm@56709
   554
              prover.get.options(options)
wenzelm@56709
   555
              handle_raw_edits(Document.Blobs.empty, Nil)
wenzelm@56709
   556
            }
wenzelm@56715
   557
            global_options.post(Session.Global_Options(options))
wenzelm@34809
   558
wenzelm@56793
   559
          case Cancel_Exec(exec_id) if prover.defined =>
wenzelm@56709
   560
            prover.get.cancel_exec(exec_id)
wenzelm@56706
   561
wenzelm@56793
   562
          case Session.Raw_Edits(doc_blobs, edits) if prover.defined =>
wenzelm@56709
   563
            handle_raw_edits(doc_blobs, edits)
wenzelm@56709
   564
wenzelm@56793
   565
          case Session.Dialog_Result(id, serial, result) if prover.defined =>
wenzelm@56709
   566
            prover.get.dialog_result(serial, result)
wenzelm@56709
   567
            handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@56706
   568
wenzelm@59364
   569
          case Session.Build_Theories(id, master_dir, theories) if prover.defined =>
wenzelm@59364
   570
            prover.get.build_theories(id, master_dir, theories)
wenzelm@59362
   571
wenzelm@56793
   572
          case Protocol_Command(name, args) if prover.defined =>
wenzelm@56709
   573
            prover.get.protocol_command(name, args:_*)
wenzelm@56706
   574
wenzelm@56793
   575
          case change: Session.Change if prover.defined =>
wenzelm@57976
   576
            val state = global_state.value
wenzelm@57976
   577
            if (!state.removing_versions && state.is_assigned(change.previous))
wenzelm@56709
   578
              handle_change(change)
wenzelm@56709
   579
            else postponed_changes.store(change)
wenzelm@57651
   580
wenzelm@57976
   581
          case Session.Change_Flush if prover.defined =>
wenzelm@57976
   582
            val state = global_state.value
wenzelm@57976
   583
            if (!state.removing_versions)
wenzelm@57976
   584
              postponed_changes.flush(state).foreach(handle_change(_))
wenzelm@57976
   585
wenzelm@57651
   586
          case bad =>
wenzelm@57651
   587
            if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
wenzelm@56706
   588
        }
wenzelm@56709
   589
        true
wenzelm@56709
   590
        //}}}
wenzelm@56709
   591
    }
wenzelm@34777
   592
  }
wenzelm@34777
   593
wenzelm@34809
   594
wenzelm@56799
   595
  /* main operations */
wenzelm@56799
   596
wenzelm@56799
   597
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@56799
   598
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@56799
   599
    global_state.value.snapshot(name, pending_edits)
wenzelm@34809
   600
wenzelm@56387
   601
  def start(name: String, args: List[String])
wenzelm@56706
   602
  { manager.send(Start(name, args)) }
wenzelm@45076
   603
wenzelm@50117
   604
  def stop()
wenzelm@50117
   605
  {
wenzelm@56709
   606
    manager.send_wait(Stop)
wenzelm@56793
   607
    prover.await_reset()
wenzelm@56704
   608
    change_parser.shutdown()
wenzelm@56706
   609
    change_buffer.shutdown()
wenzelm@56771
   610
    delay_prune.revoke()
wenzelm@56706
   611
    manager.shutdown()
wenzelm@56715
   612
    dispatcher.shutdown()
wenzelm@50117
   613
  }
wenzelm@38841
   614
wenzelm@53054
   615
  def protocol_command(name: String, args: String*)
wenzelm@56706
   616
  { manager.send(Protocol_Command(name, args.toList)) }
wenzelm@53054
   617
wenzelm@56706
   618
  def cancel_exec(exec_id: Document_ID.Exec)
wenzelm@56706
   619
  { manager.send(Cancel_Exec(exec_id)) }
wenzelm@52931
   620
wenzelm@54521
   621
  def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@59319
   622
  { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
wenzelm@50498
   623
wenzelm@52084
   624
  def update_options(options: Options)
wenzelm@56706
   625
  { manager.send_wait(Update_Options(options)) }
wenzelm@52084
   626
wenzelm@58928
   627
  def init_options(options: Options)
wenzelm@58928
   628
  {
wenzelm@58928
   629
    update_options(options)
wenzelm@58928
   630
  }
wenzelm@58928
   631
wenzelm@52531
   632
  def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@56706
   633
  { manager.send(Session.Dialog_Result(id, serial, result)) }
wenzelm@59362
   634
wenzelm@59364
   635
  def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@59364
   636
  { manager.send(Session.Build_Theories(id, master_dir, theories)) }
wenzelm@34777
   637
}