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