SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use "collections"
  2. use "random"
  3. use "time"
  4.  
  5. actor Main
  6.   new create(env: Env) =>
  7.     let timers = Timers
  8.     let rm = ResourceManager(env.out)
  9.     Printer(env.out, timers, "p1", rm)
  10.     Printer(env.out, timers, "p2", rm)
  11.     User(env.out, rm, "bob")
  12.     User(env.out, rm, "alice")
  13.     env.out.print("helo " + Rand.int[U8](12).string())
  14.  
  15. actor User
  16.   let _out: OutStream
  17.   let _name: String val
  18.   let _rm: ResourceManager tag
  19.   var _ndoc: U8
  20.   new create(out: OutStream, rm: ResourceManager tag, name': String) =>
  21.     _out = out
  22.     _name = name'
  23.     _rm = rm
  24.     // [1,4) docs to print
  25.     _ndoc = Rand.int[U8](4) + 1
  26.     var count: U8 = 0
  27.     while count < _ndoc do
  28.       _rm.print(PrintRequest(this, "doc #" + _ndoc.string()))
  29.       count = count + 1
  30.     end
  31.  
  32.   be receive_print(doc: String) =>
  33.     _out.print(_name + " received doc #" + _ndoc.string())
  34.     _ndoc = _ndoc - 1
  35.     if _ndoc == 0 then
  36.       _out.print(_name + " received all documents")
  37.     end
  38.  
  39. class TimerReceiver is TimerNotify
  40.   let _printer: Printer
  41.   let _job: U64
  42.   new iso create(printer: Printer tag, job: U64) =>
  43.     _printer = printer
  44.     _job = job
  45.   fun ref apply(timer: Timer, count: U64): Bool =>
  46.     _printer.ready(_job)
  47.     false
  48.  
  49. actor Printer
  50.   let _name: String val
  51.   let _out: OutStream
  52.   let _timers: Timers
  53.   let _pn: PrintNotifier tag
  54.   new create(out: OutStream, timers: Timers, name: String, pn: PrintNotifier tag) =>
  55.     _out = out
  56.     _timers = timers
  57.     _name = name
  58.     _pn = pn
  59.     pn.printer_online(this)
  60.  
  61.   be print(job: U64) =>
  62.     _out.print("Printer '" + _name + "': printing job #" + job.string())
  63.     _pn.print_started(this, job)
  64.     // flip state after some time
  65.     // tick between 50-100ms
  66.     let timer = Timer(TimerReceiver(this, job), (Rand.int[U64](50) + 50) * 1_000_000)
  67.     _timers(consume timer)
  68.  
  69.   be ready(job: U64) =>
  70.     _out.print("Printer '" + _name + "': finished printing job#" + job.string())
  71.     _pn.print_completed(this, job)
  72.  
  73.  
  74. primitive Unknown
  75. primitive Ready
  76. primitive Busy
  77. type PrinterState is (Unknown | Ready | Busy)
  78.  
  79. interface PrintNotifier
  80.   be printer_online(printer: Printer)
  81.   be print_started(printer: Printer, job: U64)
  82.   be print_completed(printer: Printer, job: U64)
  83.  
  84. class PrintRequest
  85.   let user: User
  86.   let doc: String
  87.   new val create(user': User, doc': String) =>
  88.     user = user'
  89.     doc = doc'
  90.  
  91. actor ResourceManager
  92.   let _out: OutStream
  93.   let _printers: MapIs[Printer, PrinterState] = _printers.create()
  94.   var _njobs: U64 = 0
  95.   let _jobPR: Map[U64, PrintRequest val] = _jobPR.create()
  96.  
  97.   new create(out: OutStream) =>
  98.     _out = out
  99.  
  100.   be print(pr: PrintRequest val) =>
  101.     _njobs = _njobs + 1
  102.  
  103.     var free_printer_found = false
  104.     for (printer, state) in _printers.pairs() do
  105.       match state
  106.       | Ready =>
  107.         _jobPR(_njobs) = pr
  108.         _printers(printer) = Busy
  109.         printer.print(_njobs)
  110.         free_printer_found = true
  111.         break
  112.       end
  113.     end
  114.  
  115.     if not free_printer_found then
  116.       // TODO add to queue
  117.       _out.print("no free printers found")
  118.     end
  119.  
  120.   be printer_online(printer: Printer) =>
  121.     _printers(printer) = Ready
  122.  
  123.   be print_started(printer: Printer, job: U64) =>
  124.     _printers(printer) = Busy
  125.  
  126.   be print_completed(printer: Printer, job: U64) =>
  127.     _printers(printer) = Ready
  128.     try
  129.       (_, let pr) = _jobPR.remove(job)?
  130.       pr.user.receive_print(pr.doc)
  131.     else
  132.       _out.print("job #" + job.string() + " not found!?")
  133.     end
  134.     // remove job and notify user
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top