Class: BloomFilter::Server
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- BloomFilter::Server
- Defined in:
- lib/bloom_filter/server.rb
Constant Summary collapse
- PACK_N =
"N"
Class Method Summary collapse
Instance Method Summary collapse
- #post_init ⇒ Object
- #process_request(request) ⇒ Object
- #receive_data(data) ⇒ Object
- #write_response(response) ⇒ Object
Class Method Details
.filter ⇒ Object
17 18 19 |
# File 'lib/bloom_filter/server.rb', line 17 def self.filter @filter end |
.filter=(filter) ⇒ Object
21 22 23 |
# File 'lib/bloom_filter/server.rb', line 21 def self.filter=(filter) @filter = filter end |
Instance Method Details
#post_init ⇒ Object
25 26 27 28 |
# File 'lib/bloom_filter/server.rb', line 25 def post_init @buffer = nil @size = nil end |
#process_request(request) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bloom_filter/server.rb', line 54 def process_request(request) request_type, request_body = request[0,1], request[1..-1] case request_type.to_i when Protocol::ADD self.class.filter.add(request_body) write_response(true) when Protocol::ADD_MANY # TODO: support specifying a delimiter request_body.split(Protocol::DEFAULT_SEPARATOR).collect { |el| self.class.filter.add(el) } when Protocol::INCLUDE write_response(self.class.filter.include?(request_body)) when Protocol::INCLUDE_MANY # TODO: support specifying a delimiter write_response(request_body.split(Protocol::DEFAULT_SEPARATOR).collect { |el| self.class.filter.include?(el) }) when Protocol::DUMP begin File.open(request_body, "w") do |f| f.write(self.class.filter.dump) end write_response(true) rescue Exception => e write_response(false) end when Protocol::LOAD begin File.open(request_body, "r") do |file| self.class.filter.replace(file) end write_response(true) rescue Exception => e write_response(false) end end end |
#receive_data(data) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bloom_filter/server.rb', line 30 def receive_data(data) @buffer ||= "" @buffer << data if !@size && @buffer.size > 4 @size, @buffer = @buffer[0,4].unpack(PACK_N).first, @buffer[4..-1] end if @size && @buffer.size >= @size if @buffer.size > @size request, remainder = @buffer[0, @size], @buffer[@size..-1] @buffer = nil @size = nil process_request(request) receive_data(remainder) else process_request(@buffer) @buffer = nil @size = nil end end end |
#write_response(response) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/bloom_filter/server.rb', line 89 def write_response(response) response = Array(response) vector = BitVector.new(response.size) response.each_with_index { |t, index| vector[index] = t } str = vector.to_s send_data([str.size].pack(PACK_N) + str) end |