Two TCP servers run concurrently, one controlling the other

The complete source of networkTypes_tcp_two_servers.ek9, compiled by the EK9 compiler. Shown on networkTypes.html.

#!ek9
defines module site.networkTypes.tcpTwoServers

  defines function
    serverFunction() as abstract
      <- response as String?

    serviceConnections()
      ->
        tcpServer as TCP
        handler as TCPHandler
      <-
        response as String: `TCP Server on ${tcpServer} completed and shutdown`

      if not tcpServer.accept(handler)
        response: `Was not able to service requests gracefully on ${tcpServer}`

    createShutdownHandler
      ->
        controlServer as TCP
        processingServer as TCP
        shutdownCommand as String
      <-
        rtn as TCPHandler: (controlServer, processingServer, shutdownCommand) is TCPHandler as function
          while input?
            command <- input.next()
            if command == shutdownCommand
              output.println("Shutting down")
              processingServer.stop()
              controlServer.stop()
            else
              output.println(`Ignoring [${command}]`)

    createHandler()
      <- rtn as TCPHandler: () is TCPHandler as function
          cat input | map toUpperCase > output

    toUpperCase()
      -> input as String
      <- output as String: input.upperCase()

    shutdownFunction()
      -> tcpClient as TCP
      <- response as String: `Connection failed ${tcpClient}`

      if tcpClient?
        try
          -> connection <- tcpClient.connect()
          if connection?
            connection.output().println("SHUTDOWN")
            response: `Received response [${connection.input().next()} from ${tcpClient}]`
          else
            response: `Failed to connect on [${tcpClient}]`
      else
        response: `Failed to get TCP client running [${tcpClient}]`

  defines program
    TCPServer2()
      ->
        processingPort as Integer
        controlPort as Integer
        shutdownCommand as String

      stdout <- Stdout()

      //Alternative layout as there are a number of properties being set.
      //Both servers are named, so both are try-with-resources resources, closed when the try ends.
      try
        ->
          processingServer <- TCP(
            NetworkProperties(
              port: processingPort,
              timeout: 250ms,
              backlog: 2,
              maxConcurrent: 4,
              localOnly: false)
            )
          controlServer <- TCP(NetworkProperties(port: controlPort, localOnly: true))

        if controlServer? and processingServer?

          shutdownControl <- (controlServer, processingServer, shutdownCommand) extends serverFunction
            response: serviceConnections(
              tcpServer: controlServer,
              handler: createShutdownHandler(controlServer, processingServer, shutdownCommand)
            )

          processing <- (processingServer) is serverFunction
            response: serviceConnections(
              tcpServer: processingServer,
              handler: createHandler()
            )

          //Now run both of those serverFunctions concurrently.
          cat [shutdownControl, processing] | async > stdout
          //Once the serverFunctions both finish this program will finally exit.
        else
          Stderr().println(`Failed to start servers, control: ${controlServer} processing: ${processingServer}`)

Download networkTypes_tcp_two_servers.ek9