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