]>
git.saurik.com Git - redis.git/blob - tests/support/redis.tcl
1 # Tcl clinet library - used by test-redis.tcl script for now
2 # Copyright (C) 2009 Salvatore Sanfilippo
3 # Released under the BSD license like Redis itself
7 # set r [redis 127.0.0.1 6379]
10 # $r lrange mylist 0 -1
13 # Non blocking usage example:
15 # proc handlePong {r type reply} {
16 # puts "PONG $type '$reply'"
17 # if {$reply ne "PONG"} {
18 # $r ping [list handlePong]
24 # $r get fo [list handlePong]
28 package require
Tcl 8.5
29 package provide redis
0.1
31 namespace eval redis
{}
33 array set ::redis::fd {}
34 array set ::redis::blocking {}
35 array set ::redis::deferred {}
36 array set ::redis::callback {}
37 array set ::redis::state {} ;# State in non-blocking reply reading
38 array set ::redis::statestack {} ;# Stack of states, for nested mbulks
40 proc redis
{{server
127.0.0.1} {port
6379} {defer
0}} {
41 set fd
[socket $server $port]
42 fconfigure $fd -translation binary
43 set id
[incr ::redis::id]
44 set ::redis::fd($id) $fd
45 set ::redis::blocking($id) 1
46 set ::redis::deferred($id) $defer
47 ::redis::redis_reset_state $id
48 interp alias
{} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
51 proc ::redis::__dispatch__ {id method args
} {
52 set fd
$::redis::fd($id)
53 set blocking
$::redis::blocking($id)
54 set deferred
$::redis::deferred($id)
56 if {[llength $args] == 0} {
57 error "Please provide a callback in non-blocking mode"
59 set callback
[lindex $args end
]
60 set args
[lrange $args 0 end-1
]
62 if {[info command
::redis::__method__$method] eq
{}} {
63 set cmd
"*[expr {[llength $args]+1}]\r\n"
64 append cmd
"$[string length $method]\r\n$method\r\n"
66 append cmd
"$[string length $a]\r\n$a\r\n"
68 ::redis::redis_write $fd $cmd
73 ::redis::redis_read_reply $fd
75 # Every well formed reply read will pop an element from this
76 # list and use it as a callback. So pipelining is supported
77 # in non blocking mode.
78 lappend ::redis::callback($id) $callback
79 fileevent $fd readable
[list ::redis::redis_readable $fd $id]
83 uplevel 1 [list ::redis::__method__$method $id $fd] $args
87 proc ::redis::__method__blocking {id fd val
} {
88 set ::redis::blocking($id) $val
89 fconfigure $fd -blocking $val
92 proc ::redis::__method__read {id fd
} {
93 ::redis::redis_read_reply $fd
96 proc ::redis::__method__write {id fd buf
} {
97 ::redis::redis_write $fd $buf
100 proc ::redis::__method__flush {id fd
} {
104 proc ::redis::__method__close {id fd
} {
106 catch {unset ::redis::fd($id)}
107 catch {unset ::redis::blocking($id)}
108 catch {unset ::redis::state($id)}
109 catch {unset ::redis::statestack($id)}
110 catch {unset ::redis::callback($id)}
111 catch {interp alias
{} ::redis::redisHandle$id {}}
114 proc ::redis::__method__channel {id fd
} {
118 proc ::redis::redis_write {fd buf
} {
119 puts -nonewline $fd $buf
122 proc ::redis::redis_writenl {fd buf
} {
124 redis_write
$fd "\r\n"
128 proc ::redis::redis_readnl {fd len
} {
129 set buf
[read $fd $len]
130 read $fd 2 ; # discard CR LF
134 proc ::redis::redis_bulk_read {fd
} {
135 set count
[redis_read_line
$fd]
136 if {$count == -1} return {}
137 set buf
[redis_readnl
$fd $count]
141 proc ::redis::redis_multi_bulk_read fd
{
142 set count
[redis_read_line
$fd]
143 if {$count == -1} return {}
145 for {set i
0} {$i < $count} {incr i
} {
146 lappend l
[redis_read_reply
$fd]
151 proc ::redis::redis_read_line fd
{
152 string trim
[gets $fd]
155 proc ::redis::redis_read_reply fd
{
156 set type
[read $fd 1]
157 switch -exact -- $type {
159 + {redis_read_line
$fd}
160 - {return -code error [redis_read_line
$fd]}
161 $ {redis_bulk_read
$fd}
162 * {redis_multi_bulk_read
$fd}
163 default {return -code error "Bad protocol, $type as reply type byte"}
167 proc ::redis::redis_reset_state id
{
168 set ::redis::state($id) [dict create buf
{} mbulk
-1 bulk
-1 reply
{}]
169 set ::redis::statestack($id) {}
172 proc ::redis::redis_call_callback {id type reply
} {
173 set cb
[lindex $::redis::callback($id) 0]
174 set ::redis::callback($id) [lrange $::redis::callback($id) 1 end
]
175 uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
176 ::redis::redis_reset_state $id
179 # Read a reply in non-blocking mode.
180 proc ::redis::redis_readable {fd id
} {
182 redis_call_callback
$id eof {}
183 ::redis::__method__close $id $fd
186 if {[dict get
$::redis::state($id) bulk
] == -1} {
188 if {$line eq
{}} return ;# No complete line available, return
189 switch -exact -- [string index
$line 0] {
191 + {redis_call_callback
$id reply
[string range
$line 1 end-1
]}
192 - {redis_call_callback
$id err
[string range
$line 1 end-1
]}
194 dict
set ::redis::state($id) bulk
\
195 [expr [string range
$line 1 end-1
]+2]
196 if {[dict get
$::redis::state($id) bulk
] == 1} {
197 # We got a $-1, hack the state to play well with this.
198 dict
set ::redis::state($id) bulk
2
199 dict
set ::redis::state($id) buf
"\r\n"
200 ::redis::redis_readable $fd $id
204 dict
set ::redis::state($id) mbulk
[string range
$line 1 end-1
]
206 if {[dict get
$::redis::state($id) mbulk
] == -1} {
207 redis_call_callback
$id reply
{}
211 redis_call_callback
$id err
\
212 "Bad protocol, $type as reply type byte"
216 set totlen
[dict get
$::redis::state($id) bulk
]
217 set buflen
[string length
[dict get
$::redis::state($id) buf
]]
218 set toread
[expr {$totlen-$buflen}]
219 set data
[read $fd $toread]
220 set nread
[string length
$data]
221 dict
append ::redis::state($id) buf
$data
222 # Check if we read a complete bulk reply
223 if {[string length
[dict get
$::redis::state($id) buf
]] ==
224 [dict get
$::redis::state($id) bulk
]} {
225 if {[dict get
$::redis::state($id) mbulk
] == -1} {
226 redis_call_callback
$id reply
\
227 [string range
[dict get
$::redis::state($id) buf
] 0 end-2
]
229 dict with
::redis::state($id) {
230 lappend reply
[string range
$buf 0 end-2
]
234 if {[dict get
$::redis::state($id) mbulk
] == 0} {
235 redis_call_callback
$id reply
\
236 [dict get
$::redis::state($id) reply
]