« Return to Thread: Run Io coroutines in background simultaneous to using Io> prompt in foreground?

Re: Run Io coroutines in background simultaneous to using Io> prompt in foreground?

by dennisf486 :: Rate this Message:

Reply to Author | View in Thread

Success at last!  If anyone's interest in doing this, here's the script, it's not hard to do after all.  I fix some bugs in TerminalServer.io related to it calling methods that don't exist and expecting "String" when it should be "Sequence", so this is also a better TerminalServer.

// Filename:  CoIo.io
//
// To use this script:
//
// In one terminal, type:
// io CoIo.io
//
// In a second terminal type:
// telnet 127.0.0.1 8460
//
// 1. Observe that the first terminal prints 1, 2, 3, etc.
// 2. In the 2nd terminal, observe that typing "n print"
// prints the number currently in the first terminal.
// 3. In the 2nd terminal, type "n = 1000", and notice
// that now the 1st terminal is now counting from 1000!!

n := 1

doBackground := method(
        loop("N is " print; n println; n = n + 1; wait(1))
)

@@doBackground

Terminal := Object clone
Terminal handleSocketFromServer := method(aSocket, aServer,
  while(aSocket isOpen,
    aSocket write("\nIo> ")
    if(aSocket read,
      e := try(
        result := Lobby doString(aSocket readBuffer asString)
        aSocket write(result asString)
      )
      e catch (Exception,
        aSocket write("exception: ", e coroutine backtraceString)
      )
    )
    aSocket readBuffer empty
  )
)

write("starting io terminal server\n")
server := Server clone setPort(8460)
server handleSocket := method(aSocket,
  Terminal clone @handleSocketFromServer(aSocket, self)
)
server start

 « Return to Thread: Run Io coroutines in background simultaneous to using Io> prompt in foreground?