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)
}
}