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