% Kilroy application, copyright 1987, Eric Jul.

import NodeListElement, NodeList from "Builtins"
import OutStream from "Builtins"

const Kilroy == object Kilroy
  const     delayTime         == Time.create[3,0]

  process
    var             i:              Integer <- 0
    var             count:          Integer
    var             remoteNode:     Node
    var             startTime:      Time
    var             endTime:        Time
    var             diff:           Time
    var             myList:         NodeList
    var             myNode:         Node <- locate self
    var             localOutStream: OutStream
    var             nodeCount:      Integer

    stdout.PutString["Runner starting up on node "]
    stdout.PutInt[myNode$lnn, 2]
    stdout.PutString["\^J"]
    myList <- myNode.getActiveNodes

    nodeCount <- myList.upperbound +1
    stdout.PutString["Found "]
    stdout.PutInt[nodeCount, 2]
    stdout.PutString[" nodes active.\^J"]

    stdout.PutString["... waiting 3 seconds for screen IO to settle\^J"]
    myNode.Delay[delayTime]

    startTime <- myNode.getTimeOfDay

    loop
      exit when i >= nodeCount
      remoteNode    <- myList(i)$theNode
      move Klroy to remoteNode
      localOutStream <- Node.getStdout
      localOutStream.PutString["Kilroy was here\^J"]
      localOutStream.Flush
      localOutStream <- NIL
      i <- i + 1
    end loop

    move Killroy to myNode

    endTime <- myNode.getTimeOfDay
    diff <- endTime - startTime
    stdout.PutString["Back home - total time  " || diff.asString || "\^J"]
    stdout.PutString["DONE\^J"]
    stdout.close
    stdin.close
  end process
end Kilroy

