src/Pure/PIDE/session.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64827 4ef1eb75f1cd
child 65206 ff8c3c29a924
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@61590
    27
    private val consumers = Synchronized[List[Consumer[A]]](Nil)
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@62115
   186
  def prune_delay: Time = Time.seconds(15.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 trace_events = new Session.Outlet[Simplifier_Trace.Event.type](dispatcher)
wenzelm@60900
   206
  val debugger_updates = new Session.Outlet[Debugger.Update.type](dispatcher)
wenzelm@34809
   207
wenzelm@60749
   208
  val all_messages = new Session.Outlet[Prover.Message](dispatcher)  // potential bottle-neck!
wenzelm@34809
   209
wenzelm@56706
   210
wenzelm@56799
   211
  /** main protocol manager **/
wenzelm@45635
   212
wenzelm@56799
   213
  /* internal messages */
wenzelm@45635
   214
wenzelm@62556
   215
  private case class Start(start_prover: Prover.Receiver => Prover)
wenzelm@56799
   216
  private case object Stop
wenzelm@56799
   217
  private case class Cancel_Exec(exec_id: Document_ID.Exec)
wenzelm@56799
   218
  private case class Protocol_Command(name: String, args: List[String])
wenzelm@56799
   219
  private case class Update_Options(options: Options)
wenzelm@56799
   220
  private case object Prune_History
wenzelm@45635
   221
wenzelm@38841
   222
wenzelm@43651
   223
  /* global state */
wenzelm@43644
   224
wenzelm@56775
   225
  private val syslog = new Session.Syslog(syslog_limit)
wenzelm@56775
   226
  def syslog_content(): String = syslog.content
wenzelm@39626
   227
wenzelm@39630
   228
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   229
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   230
  {
wenzelm@39630
   231
    _phase = new_phase
wenzelm@56715
   232
    phase_changed.post(new_phase)
wenzelm@39630
   233
  }
wenzelm@43716
   234
  def phase = _phase
wenzelm@43553
   235
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   236
wenzelm@56690
   237
  private val global_state = Synchronized(Document.State.init)
wenzelm@56687
   238
  def current_state(): Document.State = global_state.value
wenzelm@46944
   239
wenzelm@63584
   240
  def recent_syntax(name: Document.Node.Name): Outer_Syntax =
wenzelm@60934
   241
    global_state.value.recent_finished.version.get_finished.nodes(name).syntax getOrElse
wenzelm@64854
   242
    resources.base.syntax
wenzelm@34816
   243
wenzelm@43651
   244
wenzelm@56799
   245
  /* pipelined change parsing */
wenzelm@56799
   246
wenzelm@56799
   247
  private case class Text_Edits(
wenzelm@56799
   248
    previous: Future[Document.Version],
wenzelm@56799
   249
    doc_blobs: Document.Blobs,
wenzelm@56799
   250
    text_edits: List[Document.Edit_Text],
wenzelm@56799
   251
    version_result: Promise[Document.Version])
wenzelm@43651
   252
wenzelm@56799
   253
  private val change_parser = Consumer_Thread.fork[Text_Edits]("change_parser", daemon = true)
wenzelm@56799
   254
  {
wenzelm@56799
   255
    case Text_Edits(previous, doc_blobs, text_edits, version_result) =>
wenzelm@56799
   256
      val prev = previous.get_finished
wenzelm@56799
   257
      val change =
wenzelm@56799
   258
        Timing.timeit("parse_change", timing) {
wenzelm@56799
   259
          resources.parse_change(reparse_limit, prev, doc_blobs, text_edits)
wenzelm@56799
   260
        }
wenzelm@56799
   261
      version_result.fulfill(change.version)
wenzelm@56799
   262
      manager.send(change)
wenzelm@56799
   263
      true
wenzelm@56799
   264
  }
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@59319
   276
      if (assignment || nodes.nonEmpty || commands.nonEmpty)
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@61556
   282
    private val delay_flush = Standard_Thread.delay_first(output_delay) { flush() }
wenzelm@56719
   283
wenzelm@56719
   284
    def invoke(assign: Boolean, cmds: List[Command]): Unit = synchronized {
wenzelm@56719
   285
      assignment |= assign
wenzelm@56719
   286
      for (command <- cmds) {
wenzelm@56719
   287
        nodes += command.node_name
wenzelm@64803
   288
        command.blobs_names.foreach(nodes += _)
wenzelm@56719
   289
        commands += command
wenzelm@56719
   290
      }
wenzelm@56771
   291
      delay_flush.invoke()
wenzelm@56719
   292
    }
wenzelm@56719
   293
wenzelm@56719
   294
    def shutdown()
wenzelm@56719
   295
    {
wenzelm@56771
   296
      delay_flush.revoke()
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@57976
   310
    def flush(state: Document.State): List[Session.Change] = synchronized {
wenzelm@56706
   311
      val (assigned, unassigned) = postponed.partition(change => state.is_assigned(change.previous))
wenzelm@56706
   312
      postponed = unassigned
wenzelm@57976
   313
      assigned.reverse
wenzelm@56706
   314
    }
wenzelm@56706
   315
  }
wenzelm@44733
   316
wenzelm@56706
   317
wenzelm@56793
   318
  /* prover process */
wenzelm@56793
   319
wenzelm@56793
   320
  private object prover
wenzelm@56793
   321
  {
wenzelm@61590
   322
    private val variable = Synchronized[Option[Prover]](None)
wenzelm@56793
   323
wenzelm@56793
   324
    def defined: Boolean = variable.value.isDefined
wenzelm@56793
   325
    def get: Prover = variable.value.get
wenzelm@56793
   326
    def set(p: Prover) { variable.change(_ => Some(p)) }
wenzelm@56793
   327
    def reset { variable.change(_ => None) }
wenzelm@56793
   328
    def await_reset() { variable.guarded_access({ case None => Some((), None) case _ => None }) }
wenzelm@56793
   329
  }
wenzelm@56793
   330
wenzelm@56793
   331
wenzelm@59366
   332
  /* protocol handlers */
wenzelm@59366
   333
wenzelm@59366
   334
  private val _protocol_handlers = Synchronized(new Session.Protocol_Handlers)
wenzelm@59366
   335
wenzelm@59366
   336
  def get_protocol_handler(name: String): Option[Session.Protocol_Handler] =
wenzelm@59366
   337
    _protocol_handlers.value.get(name)
wenzelm@59366
   338
wenzelm@59367
   339
  def add_protocol_handler(handler: Session.Protocol_Handler): Unit =
wenzelm@59367
   340
    _protocol_handlers.change(_.add(prover.get, handler))
wenzelm@59366
   341
wenzelm@59366
   342
wenzelm@56706
   343
  /* manager thread */
wenzelm@44733
   344
wenzelm@62115
   345
  private val delay_prune =
wenzelm@62115
   346
    Standard_Thread.delay_first(prune_delay) { manager.send(Prune_History) }
wenzelm@56771
   347
wenzelm@56706
   348
  private val manager: Consumer_Thread[Any] =
wenzelm@56706
   349
  {
wenzelm@52649
   350
    /* raw edits */
wenzelm@52649
   351
wenzelm@54521
   352
    def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@52649
   353
    //{{{
wenzelm@52649
   354
    {
wenzelm@56793
   355
      require(prover.defined)
wenzelm@56712
   356
wenzelm@52649
   357
      prover.get.discontinue_execution()
wenzelm@52649
   358
wenzelm@56687
   359
      val previous = global_state.value.history.tip.version
wenzelm@52649
   360
      val version = Future.promise[Document.Version]
wenzelm@56711
   361
      global_state.change(_.continue_history(previous, edits, version))
wenzelm@52649
   362
wenzelm@56715
   363
      raw_edits.post(Session.Raw_Edits(doc_blobs, edits))
wenzelm@56704
   364
      change_parser.send(Text_Edits(previous, doc_blobs, edits, version))
wenzelm@52649
   365
    }
wenzelm@52649
   366
    //}}}
wenzelm@52649
   367
wenzelm@52649
   368
wenzelm@43716
   369
    /* resulting changes */
wenzelm@34809
   370
wenzelm@56315
   371
    def handle_change(change: Session.Change)
wenzelm@38221
   372
    //{{{
wenzelm@34809
   373
    {
wenzelm@56793
   374
      require(prover.defined)
wenzelm@56712
   375
wenzelm@44383
   376
      def id_command(command: Command)
wenzelm@43720
   377
      {
wenzelm@54519
   378
        for {
wenzelm@57842
   379
          (name, digest) <- command.blobs_defined
wenzelm@56687
   380
          if !global_state.value.defined_blob(digest)
wenzelm@54519
   381
        } {
wenzelm@57842
   382
          change.version.nodes(name).get_blob match {
wenzelm@54519
   383
            case Some(blob) =>
wenzelm@56687
   384
              global_state.change(_.define_blob(digest))
wenzelm@56335
   385
              prover.get.define_blob(digest, blob.bytes)
wenzelm@55783
   386
            case None =>
wenzelm@57842
   387
              Output.error_message("Missing blob " + quote(name.toString))
wenzelm@54519
   388
          }
wenzelm@54519
   389
        }
wenzelm@54519
   390
wenzelm@56687
   391
        if (!global_state.value.defined_command(command.id)) {
wenzelm@56687
   392
          global_state.change(_.define_command(command))
wenzelm@44644
   393
          prover.get.define_command(command)
wenzelm@43720
   394
        }
wenzelm@43720
   395
      }
wenzelm@56315
   396
      change.doc_edits foreach {
wenzelm@44383
   397
        case (_, edit) =>
wenzelm@44383
   398
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   399
      }
wenzelm@43722
   400
wenzelm@56687
   401
      val assignment = global_state.value.the_assignment(change.previous).check_finished
wenzelm@56687
   402
      global_state.change(_.define_version(change.version, assignment))
wenzelm@56315
   403
      prover.get.update(change.previous.id, change.version.id, change.doc_edits)
wenzelm@56316
   404
      resources.commit(change)
wenzelm@34809
   405
    }
wenzelm@38221
   406
    //}}}
wenzelm@34809
   407
wenzelm@34809
   408
wenzelm@46772
   409
    /* prover output */
wenzelm@34809
   410
wenzelm@56385
   411
    def handle_output(output: Prover.Output)
wenzelm@38221
   412
    //{{{
wenzelm@34809
   413
    {
wenzelm@51662
   414
      def bad_output()
wenzelm@43716
   415
      {
wenzelm@43716
   416
        if (verbose)
wenzelm@56782
   417
          Output.warning("Ignoring bad prover output: " + output.message.toString)
wenzelm@43716
   418
      }
wenzelm@43716
   419
wenzelm@52531
   420
      def accumulate(state_id: Document_ID.Generic, message: XML.Elem)
wenzelm@51662
   421
      {
wenzelm@51662
   422
        try {
wenzelm@56687
   423
          val st = global_state.change_result(_.accumulate(state_id, message))
wenzelm@56719
   424
          change_buffer.invoke(false, List(st.command))
wenzelm@51662
   425
        }
wenzelm@51662
   426
        catch {
wenzelm@51662
   427
          case _: Document.State.Fail => bad_output()
wenzelm@51662
   428
        }
wenzelm@51662
   429
      }
wenzelm@51662
   430
wenzelm@54442
   431
      output match {
wenzelm@56385
   432
        case msg: Prover.Protocol_Output =>
wenzelm@59366
   433
          val handled = _protocol_handlers.value.invoke(msg)
wenzelm@54442
   434
          if (!handled) {
wenzelm@54442
   435
            msg.properties match {
wenzelm@56793
   436
              case Markup.Protocol_Handler(name) if prover.defined =>
wenzelm@59367
   437
                try {
wenzelm@59367
   438
                  val handler =
wenzelm@59367
   439
                    Class.forName(name).newInstance.asInstanceOf[Session.Protocol_Handler]
wenzelm@59367
   440
                  add_protocol_handler(handler)
wenzelm@59367
   441
                }
wenzelm@59367
   442
                catch { case exn: Throwable => Session.bad_protocol_handler(exn, name) }
wenzelm@54442
   443
wenzelm@56793
   444
              case Protocol.Command_Timing(state_id, timing) if prover.defined =>
wenzelm@54442
   445
                val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@54442
   446
                accumulate(state_id, prover.get.xml_cache.elem(message))
wenzelm@46122
   447
wenzelm@54442
   448
              case Markup.Assign_Update =>
wenzelm@54442
   449
                msg.text match {
wenzelm@54442
   450
                  case Protocol.Assign_Update(id, update) =>
wenzelm@54442
   451
                    try {
wenzelm@56687
   452
                      val cmds = global_state.change_result(_.assign(id, update))
wenzelm@56719
   453
                      change_buffer.invoke(true, cmds)
wenzelm@57976
   454
                      manager.send(Session.Change_Flush)
wenzelm@54442
   455
                    }
wenzelm@54442
   456
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   457
                  case _ => bad_output()
wenzelm@54442
   458
                }
wenzelm@56771
   459
                delay_prune.invoke()
wenzelm@46122
   460
wenzelm@54442
   461
              case Markup.Removed_Versions =>
wenzelm@54442
   462
                msg.text match {
wenzelm@54442
   463
                  case Protocol.Removed(removed) =>
wenzelm@54442
   464
                    try {
wenzelm@56687
   465
                      global_state.change(_.removed_versions(removed))
wenzelm@57976
   466
                      manager.send(Session.Change_Flush)
wenzelm@54442
   467
                    }
wenzelm@54442
   468
                    catch { case _: Document.State.Fail => bad_output() }
wenzelm@54442
   469
                  case _ => bad_output()
wenzelm@54442
   470
                }
wenzelm@54442
   471
wenzelm@54442
   472
              case Markup.ML_Statistics(props) =>
wenzelm@56715
   473
                statistics.post(Session.Statistics(props))
wenzelm@54442
   474
wenzelm@54442
   475
              case Markup.Task_Statistics(props) =>
wenzelm@54442
   476
                // FIXME
wenzelm@52111
   477
wenzelm@54442
   478
              case _ => bad_output()
wenzelm@54442
   479
            }
wenzelm@54442
   480
          }
wenzelm@54442
   481
        case _ =>
wenzelm@54442
   482
          output.properties match {
wenzelm@54442
   483
            case Position.Id(state_id) =>
wenzelm@54442
   484
              accumulate(state_id, output.message)
wenzelm@56210
   485
wenzelm@54442
   486
            case _ if output.is_init =>
wenzelm@54442
   487
              phase = Session.Ready
wenzelm@56210
   488
wenzelm@54442
   489
            case Markup.Return_Code(rc) if output.is_exit =>
wenzelm@54442
   490
              if (rc == 0) phase = Session.Inactive
wenzelm@54442
   491
              else phase = Session.Failed
wenzelm@56793
   492
              prover.reset
wenzelm@56210
   493
wenzelm@61376
   494
            case _ =>
wenzelm@61376
   495
              raw_output_messages.post(output)
wenzelm@44661
   496
          }
wenzelm@52111
   497
        }
wenzelm@34809
   498
    }
wenzelm@38221
   499
    //}}}
wenzelm@34809
   500
wenzelm@34820
   501
wenzelm@56706
   502
    /* main thread */
wenzelm@56706
   503
wenzelm@56710
   504
    Consumer_Thread.fork[Any]("Session.manager", daemon = true)
wenzelm@56709
   505
    {
wenzelm@56709
   506
      case arg: Any =>
wenzelm@56709
   507
        //{{{
wenzelm@56709
   508
        arg match {
wenzelm@56733
   509
          case output: Prover.Output =>
wenzelm@56775
   510
            if (output.is_stdout || output.is_stderr)
wenzelm@56775
   511
              raw_output_messages.post(output)
wenzelm@56733
   512
            else handle_output(output)
wenzelm@56775
   513
wenzelm@56733
   514
            if (output.is_syslog) {
wenzelm@56775
   515
              syslog += output.message
wenzelm@56733
   516
              syslog_messages.post(output)
wenzelm@56733
   517
            }
wenzelm@56775
   518
wenzelm@56733
   519
            all_messages.post(output)
wenzelm@56733
   520
wenzelm@56733
   521
          case input: Prover.Input =>
wenzelm@56733
   522
            all_messages.post(input)
wenzelm@56733
   523
wenzelm@62556
   524
          case Start(start_prover) if !prover.defined =>
wenzelm@56709
   525
            if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@56709
   526
              phase = Session.Startup
wenzelm@62556
   527
              prover.set(start_prover(manager.send(_)))
wenzelm@56709
   528
            }
wenzelm@56706
   529
wenzelm@56709
   530
          case Stop =>
wenzelm@56793
   531
            if (prover.defined && is_ready) {
wenzelm@59366
   532
              _protocol_handlers.change(_.stop(prover.get))
wenzelm@56772
   533
              global_state.change(_ => Document.State.init)
wenzelm@56709
   534
              phase = Session.Shutdown
wenzelm@56709
   535
              prover.get.terminate
wenzelm@56709
   536
            }
wenzelm@56706
   537
wenzelm@56771
   538
          case Prune_History =>
wenzelm@56793
   539
            if (prover.defined) {
wenzelm@57976
   540
              val old_versions = global_state.change_result(_.remove_versions(prune_size))
wenzelm@59319
   541
              if (old_versions.nonEmpty) prover.get.remove_versions(old_versions)
wenzelm@56771
   542
            }
wenzelm@56771
   543
wenzelm@56709
   544
          case Update_Options(options) =>
wenzelm@56793
   545
            if (prover.defined && is_ready) {
wenzelm@56709
   546
              prover.get.options(options)
wenzelm@56709
   547
              handle_raw_edits(Document.Blobs.empty, Nil)
wenzelm@56709
   548
            }
wenzelm@56715
   549
            global_options.post(Session.Global_Options(options))
wenzelm@34809
   550
wenzelm@56793
   551
          case Cancel_Exec(exec_id) if prover.defined =>
wenzelm@56709
   552
            prover.get.cancel_exec(exec_id)
wenzelm@56706
   553
wenzelm@56793
   554
          case Session.Raw_Edits(doc_blobs, edits) if prover.defined =>
wenzelm@56709
   555
            handle_raw_edits(doc_blobs, edits)
wenzelm@56709
   556
wenzelm@56793
   557
          case Session.Dialog_Result(id, serial, result) if prover.defined =>
wenzelm@56709
   558
            prover.get.dialog_result(serial, result)
wenzelm@56709
   559
            handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@56706
   560
wenzelm@59364
   561
          case Session.Build_Theories(id, master_dir, theories) if prover.defined =>
wenzelm@59364
   562
            prover.get.build_theories(id, master_dir, theories)
wenzelm@59362
   563
wenzelm@56793
   564
          case Protocol_Command(name, args) if prover.defined =>
wenzelm@56709
   565
            prover.get.protocol_command(name, args:_*)
wenzelm@56706
   566
wenzelm@56793
   567
          case change: Session.Change if prover.defined =>
wenzelm@57976
   568
            val state = global_state.value
wenzelm@57976
   569
            if (!state.removing_versions && state.is_assigned(change.previous))
wenzelm@56709
   570
              handle_change(change)
wenzelm@56709
   571
            else postponed_changes.store(change)
wenzelm@57651
   572
wenzelm@57976
   573
          case Session.Change_Flush if prover.defined =>
wenzelm@57976
   574
            val state = global_state.value
wenzelm@57976
   575
            if (!state.removing_versions)
wenzelm@57976
   576
              postponed_changes.flush(state).foreach(handle_change(_))
wenzelm@57976
   577
wenzelm@57651
   578
          case bad =>
wenzelm@57651
   579
            if (verbose) Output.warning("Ignoring bad message: " + bad.toString)
wenzelm@56706
   580
        }
wenzelm@56709
   581
        true
wenzelm@56709
   582
        //}}}
wenzelm@56709
   583
    }
wenzelm@34777
   584
  }
wenzelm@34777
   585
wenzelm@34809
   586
wenzelm@56799
   587
  /* main operations */
wenzelm@56799
   588
wenzelm@56799
   589
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@56799
   590
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@56799
   591
    global_state.value.snapshot(name, pending_edits)
wenzelm@34809
   592
wenzelm@62556
   593
  def start(start_prover: Prover.Receiver => Prover)
wenzelm@62556
   594
  { manager.send(Start(start_prover)) }
wenzelm@45076
   595
wenzelm@50117
   596
  def stop()
wenzelm@50117
   597
  {
wenzelm@62050
   598
    delay_prune.revoke()
wenzelm@56709
   599
    manager.send_wait(Stop)
wenzelm@56793
   600
    prover.await_reset()
wenzelm@56704
   601
    change_parser.shutdown()
wenzelm@56706
   602
    change_buffer.shutdown()
wenzelm@56706
   603
    manager.shutdown()
wenzelm@56715
   604
    dispatcher.shutdown()
wenzelm@50117
   605
  }
wenzelm@38841
   606
wenzelm@53054
   607
  def protocol_command(name: String, args: String*)
wenzelm@56706
   608
  { manager.send(Protocol_Command(name, args.toList)) }
wenzelm@53054
   609
wenzelm@56706
   610
  def cancel_exec(exec_id: Document_ID.Exec)
wenzelm@56706
   611
  { manager.send(Cancel_Exec(exec_id)) }
wenzelm@52931
   612
wenzelm@54521
   613
  def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
wenzelm@59319
   614
  { if (edits.nonEmpty) manager.send_wait(Session.Raw_Edits(doc_blobs, edits)) }
wenzelm@50498
   615
wenzelm@52084
   616
  def update_options(options: Options)
wenzelm@56706
   617
  { manager.send_wait(Update_Options(options)) }
wenzelm@52084
   618
wenzelm@52531
   619
  def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
wenzelm@56706
   620
  { manager.send(Session.Dialog_Result(id, serial, result)) }
wenzelm@59362
   621
wenzelm@59364
   622
  def build_theories(id: String, master_dir: Path, theories: List[(Options, List[Path])])
wenzelm@59364
   623
  { manager.send(Session.Build_Theories(id, master_dir, theories)) }
wenzelm@34777
   624
}