0

I'm connecting to a vendor API using 2 TCP connections. On one connection I make synchronous requests; the second connection is used for the API to push out responses.

I'm currently using the ganymed-ssh-2 library to establish the two connections with the server. I perform a handshake when making the connections: on establishing the sync connection, I receive a token, which I use to authenticate on the async channel. I'm currently simply printing out all messages received on the async channel once I'm authenticated.

What steps should I take to keep making requests on the sync channel (simply using stdin at the moment), and continuing to print the responses from the async channel on stdout? I'm not sure whether I should be using an Actor (I've struggled to find an example of an Actor which can read from a input stream and parse messages correspondingly), or whether there's some other Scala-esque construct I should be using.

class SyncConnection {
  def connect(): String = {
    // Establish connection    
    ...    
    val out = new PrintStream(outputStream)
    val in = new BufferedSource(inputStream).getLines()

    // Make login request, receive token
    out.println("loginRequest")
    out.flush()

    val token = in.next()

    token
  }
}

class AsyncConnection {
  def connect(token: String) {
    // Establish connection    
    ...    
    val in = new BufferedSource(inputStream).getLines()
    val out = new PrintStream(outputStream)

    // Authenticate using token
    out.println(token)
    out.flush()

    // Print all messages received on input stream
    for (line <- in) println(line)    
  }
}
4

1 回答 1

0

I ended up using a combination or regular Scala actors and Akka actors to accomplish this task. The actors can easily pass messages to one another to perform the handshake:

  • sync requestor actor makes login request
  • sync listener receives token needed to establish async connection
  • sync listener forwards token to async requestor
  • async requestor makes authentication request
  • async listener receives confirmation of successful authentication

I hooked 2 regular actors up to the input streams of the two connections:

class InputStreamReaderActor(lines: Iterator[String], listener: ActorRef) extends Actor {
  def act() {
    for (line <- lines) {
      listener ! line
    }
  }
}

This would read any incoming messages on the two connections, and immediately forward the response to a listener actor. These listener actors looked like the following:

class SyncListenerActor() extends Actor {
  def receive: Receive = {
    // respond to incoming messages
  }
}

And to make requests on the connections, I simply hooked the output streams of each connection to an Akka actor:

class SyncRequestorActor(out: PrintStream) extends Actor {
  def receive: Receive = {
    //    
  }

  def sendRequest(request: String) {
    println("Making request: " + request)
    out.println(request)
    out.flush()
  }
}
于 2012-07-16T15:02:18.110 回答