Skip to content

Instantly share code, notes, and snippets.

@codedmart
Created March 24, 2015 14:01
Show Gist options
  • Save codedmart/aef9ff697cd1c282960a to your computer and use it in GitHub Desktop.
Save codedmart/aef9ff697cd1c282960a to your computer and use it in GitHub Desktop.
package com.scalaRethinkdb
import java.io.{InputStream, OutputStream, IOException}
import java.net.{InetSocketAddress, Socket}
import java.util.concurrent.atomic.AtomicInteger
import com.scalaRethinkdb.utils.{pack, unpack}
object r {
val DEFAULT_HOST = "localhost"
val DEFAULT_PORT_DRIVER = 28015
def connect(host: String = DEFAULT_HOST, port: Int = DEFAULT_PORT_DRIVER, db: String = null): Connection = {
val address = new InetSocketAddress(host, port)
db match {
case null => new Connection(address)
case _ => new Connection(address) // TODO: handle db here
}
}
}
class Connection(private val address: InetSocketAddress) extends AutoCloseable {
private var socket: Socket = _
private var in: InputStream = _
private var out: OutputStream = _
private val token: AtomicInteger = new AtomicInteger()
def nextToken = token.incrementAndGet
reconnect()
def reconnect(): Connection = {
if (isOpen) close()
try {
socket = new Socket(address.getAddress, address.getPort)
socket.setKeepAlive(true)
socket.setTcpNoDelay(true)
in = socket.getInputStream()
out = socket.getOutputStream()
}
catch {
case e: IOException => {
val message = "Could not connect to %s.".format(address)
println(message)
}
}
val version = VersionDummy.V0_3.toWire
val protocol = VersionDummy.JSON.toWire
var handshake = pack(version) ++ pack(0) ++ "".getBytes() ++ pack(protocol)
out.write(handshake)
out.flush()
val responseArr = read()
val response: String = new String(responseArr, "US-ASCII")
print(response)
print(responseArr)
if ("SUCCESS" != response) println("Error establishing handshake") // This is called
println("SUCCESS" == response) // This is false
this
}
def close(): Unit = {
if (!isOpen) println("Connection is closed.")
in.close()
out.close()
socket.close()
}
def isOpen: Boolean = {
socket != null && !socket.isClosed && socket.isConnected
}
private def read(): Array[Byte] = {
def readBuffer(response: Array[Byte]): Array[Byte] = {
val buffer = in.read()
buffer match {
case 0 => response
case _ => {
println(buffer) // This print 83, 85, 67, 67, 69, 83, 83 which is what I expect
readBuffer(response ++ pack(buffer))
}
}
}
readBuffer(Array[Byte]())
}
}
package com.scalaRethinkdb
import java.nio.{ByteOrder, ByteBuffer}
object utils {
/*
* Packs given integer value into a little-endian byte array.
*/
def pack(value: Int): Array[Byte] = {
ByteBuffer.allocate(4)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(value)
.array()
}
/*
* Unpacks given little-endian byte array into an integer value.
*/
def unpack(value: Array[Byte]): Int = {
ByteBuffer.wrap(value)
.order(ByteOrder.LITTLE_ENDIAN)
.getInt()
}
}
package com.scalaRethinkdb
object VersionDummy {
trait Version extends Wire
case object V0_3 extends Version {
val toWire = 0x5f75e83e
val fromWire = "V0_3" // TODO
}
case object V0_4 extends Version {
val toWire = 0x400c2d20
val fromWire = "V0_4" // TODO
}
trait Protocol extends Wire
case object JSON extends Protocol {
val toWire = 0x7e6970c7
val fromWire = "JSON" // TODO
}
}
package com.scalaRethinkdb
trait Wire {
val toWire: Int
val fromWire: String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment