Module: Sparrow::Server
- Includes:
- Miscel
- Defined in:
- lib/sparrow/server.rb
Defined Under Namespace
Classes: ClientError, InvalidBodyLength, NoMoreMessages, StatementInvalid
Constant Summary
collapse
- CR =
"\r\n"
- ERROR =
"ERROR"
- OK =
"OK"
- EOF =
"END"
- CLIENT_ERROR =
"CLIENT_ERROR"
- SERVER_ERROR =
"SERVER_ERROR"
- STORED =
"STORED"
- NOT_STORED =
"NOT_STORED"
- DELETED =
"DELETED"
- NOT_FOUND =
"NOT_FOUND"
- VALUE =
"VALUE"
- VERSION =
"VERSION"
- STATS =
"STATS"
- SET_REGEX =
/\ASET\s/i
- ADD_REGEX =
/\AADD\s/i
- REPLACE_REGEX =
/\AREPLACE\s/i
- DELETE_REGEX =
/\ADELETE\s/i
- GET_REGEX =
/\AGET\s/i
- QUIT_REGEX =
/\AQUIT/i
- FLUSH_ALL_REGEX =
/\AFLUSH_ALL/i
- VERSION_REGEX =
/\AVERSION/i
- STATS_REGEX =
/\ASTATS/i
Instance Method Summary
collapse
Methods included from Miscel
#base_dir, #log_path, #logger, #options, #options=, #pid_dir
Instance Method Details
#delete_command ⇒ Object
200
201
202
203
204
205
206
207
|
# File 'lib/sparrow/server.rb', line 200
def delete_command
if Sparrow::Queue.delete(!args[1])
logger.info "Deleting queue - #{args[1]}"
publish DELETED
else
publish NOT_FOUND
end
end
|
#flush_all_command ⇒ Object
228
229
230
231
232
|
# File 'lib/sparrow/server.rb', line 228
def flush_all_command
logger.info "Flushing all queues"
Sparrow::Queue.delete_all
publish OK
end
|
#get_command ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/sparrow/server.rb', line 175
def get_command
args.shift
raise ClientError if args.empty?
rsp = []
args.each do |queue|
logger.debug "Getting message from queue - #{queue}"
begin
msg = Sparrow::Queue.next_message(queue)
next unless msg
rescue NoMoreMessages
next
end
flag = msg[-1..-1]
msg = msg[0..-2]
rsp << [VALUE, queue, flag, msg.length].join(' ')
rsp << msg
self.get_count += 1
end
rsp << EOF
send_data(rsp.join(CR) + CR)
end
|
#post_init ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/sparrow/server.rb', line 62
def post_init
@current_queue = nil
@expecting_body = false
@expected_bytes = 0
@current_flag = nil
@buffer = ''
self.connections_made += 1
logger.debug "New client [#{client_ip}]"
end
|
#process_body ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/sparrow/server.rb', line 158
def process_body
if @data.length != @expected_bytes
raise InvalidBodyLength
end
@data << @current_flag
logger.debug "Adding message to queue - #{@current_queue}"
Sparrow::Queue.add_message(@current_queue, @data)
self.set_count += 1
@expected_bytes = 0
@current_queue = nil
@expecting_body = false
publish STORED
end
|
#process_message(ln) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/sparrow/server.rb', line 104
def process_message ln
@data = ln
if ln =~ SET_REGEX
set_command
elsif ln =~ ADD_REGEX
add_command
elsif ln =~ REPLACE_REGEX
replace_command
elsif ln =~ GET_REGEX
get_command
elsif ln =~ DELETE_REGEX
delete_command
elsif ln =~ QUIT_REGEX
quit_command
elsif ln =~ VERSION_REGEX
version_command
elsif ln =~ FLUSH_ALL_REGEX
flush_all_command
elsif ln =~ STATS_REGEX
stats_command
elsif @expecting_body
process_body
else
raise StatementInvalid
end
@data = nil
@split_args = nil
rescue ClientError => e
logger.error e
publish CLIENT_ERROR, e
publish ERROR
rescue => e
logger.error e
publish SERVER_ERROR, e
end
|
#process_whole_messages(data) ⇒ Object
process any whole messages in the buffer,
and return the new contents of the buffer
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/sparrow/server.rb', line 91
def process_whole_messages(data)
return data if data !~ /\r\n/i
messages = data.split(CR)
if data =~ /\r\n$/i
data = ''
else
data = messages.pop
end
messages.each {|message| process_message(message) }
return data
end
|
#publish(*args) ⇒ Object
141
142
143
|
# File 'lib/sparrow/server.rb', line 141
def publish *args
send_data args.join(' ') + CR
end
|
#quit_command ⇒ Object
240
241
242
243
|
# File 'lib/sparrow/server.rb', line 240
def quit_command
logger.debug "Closing connection"
close_connection
end
|
#receive_data(data) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/sparrow/server.rb', line 82
def receive_data(data)
logger.debug "Receiving data: #{data}"
self.bytes_read += 1
@buffer << data
@buffer = process_whole_messages(@buffer)
end
|
#send_data(data) ⇒ Object
77
78
79
80
|
# File 'lib/sparrow/server.rb', line 77
def send_data(data)
self.bytes_written += 1
super(data)
end
|
#set_command ⇒ Object
Also known as:
add_command, replace_command
148
149
150
151
152
153
154
|
# File 'lib/sparrow/server.rb', line 148
def set_command
@current_queue = args[1]
@current_flag = args[2] || 0
raise ClientError unless @current_queue
@expected_bytes = args[4].to_i || 0
@expecting_body = true
end
|
#stats_command ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/sparrow/server.rb', line 209
def stats_command
rsp = []
stats_hash = Sparrow::Queue.get_stats(args[1])
stats_hash.merge!({
:curr_connections => (self.connections_made - self.connections_lost),
:total_connections => self.connections_made,
:bytes_read => self.bytes_read,
:bytes_written => self.bytes_written,
:get_count => self.get_count,
:set_count => self.set_count
})
stats_hash.each do |key, value|
rsp << [STATS, key, value].join(' ')
end
rsp << EOF
send_data(rsp.join(CR) + CR)
end
|
#unbind ⇒ Object
72
73
74
75
|
# File 'lib/sparrow/server.rb', line 72
def unbind
self.connections_lost += 1
logger.debug "Lost client"
end
|
#version_command ⇒ Object
235
236
237
|
# File 'lib/sparrow/server.rb', line 235
def version_command
publish VERSION, Sparrow::Version
end
|