11 trait SocketOperations {
13 // Response codes from the Redis server
14 // they tell you what's coming next from the server.
16 val OK: String = "+OK"
17 val SINGLE: String = "+"
18 val BULK: String = "$"
19 val MULTI: String = "*"
26 var socket: Socket = null
27 var out: OutputStream = null
28 var in: BufferedReader = null
30 def getOutputStream: OutputStream = out
31 def getInputStream: BufferedReader = in
32 def getSocket: Socket = socket
34 def connected = { getSocket != null }
35 def reconnect = { disconnect && connect; }
37 // Connects the socket, and sets the input and output streams.
38 def connect: Boolean = {
40 socket = new Socket(host, port)
41 out = getSocket.getOutputStream
42 in = new BufferedReader(new InputStreamReader(getSocket.getInputStream));
45 case _ => clear_fd; false;
49 // Disconnects the socket.
50 def disconnect: Boolean = {
68 // Reads the server responses as Scala types.
69 def readString: String = readResponse.toString // Reads the server response as an Int
70 def readInt: Int = Integer.parseInt(readResponse.toString) // Reads the server response as an Int
71 def readList: List[String] = listReply(readResponse.toString) // Reads the server response as a List
72 def readSet: Set[String] = setReply(readResponse.toString) // Reads the server response as a String
73 def readBoolean: Boolean = readResponse match {
79 // Read from Input Stream.
80 def readline: String = {
82 getInputStream.readLine()
88 // Gets the type of response the server is going to send.
92 (res(0).toString(), res)
98 // Reads the response from the server based on the response code.
101 val responseType = readtype
103 responseType._1 match {
104 case ERR => reconnect; // RECONNECT
105 case SINGLE => lineReply(responseType._2)
106 case BULK => bulkReply(responseType._2)
107 case MULTI => responseType._2
108 case INT => integerReply(responseType._2)
109 case _ => reconnect; // RECONNECT
112 case e: Exception => false
116 def integerReply(response: String): Int = Integer.parseInt(response.split(":")(1).toString)
118 def lineReply(response: String): String = response
120 def listReply(response: String): List[String] = {
121 val total = Integer.parseInt(response.split('*')(1))
122 var list: List[String] = List()
123 for(i <- 1 to total){
124 list = (list ::: List(bulkReply(readtype._2)))
129 def bulkReply(response: String) = {
130 if(response(1).toString() != ERR){
131 var length: Int = Integer.parseInt(response.split('$')(1).split("\r\n")(0))
132 var line, res: String = ""
135 length -= (line.length+2)
137 if(length > 0) res += "\r\n"
143 def setReply(response: String): Set[String] = {
144 val total = Integer.parseInt(response.split('*')(1))
145 var set: Set[String] = Set()
146 for(i <- 1 to total){
147 set += bulkReply(readtype._2)
152 // Wraper for the socket write operation.
153 def write_to_socket(data: String)(op: OutputStream => Unit) = op(getOutputStream)
155 // Writes data to a socket using the specified block.
156 def write(data: String) = {
157 if(!connected) connect;
158 write_to_socket(data){
161 getSocket.write(data.getBytes)