Class: Bones::RPC::Synchronous::Connection::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/synchronous/connection/reader.rb

Overview

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initialize(connection, socket, adapter, writer) ⇒ Reader

Returns a new instance of Reader.

Since:

  • 0.0.1



7
8
9
10
11
12
13
14
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 7

def initialize(connection, socket, adapter, writer)
  @connection = connection
  @socket = socket
  @adapter = adapter
  @writer = writer
  @alive = true
  @buffer = ""
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.1



16
17
18
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 16

def alive?
  !!@alive
end

#parse(data, proxy) ⇒ Object

Since:

  • 0.0.1



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 20

def parse(data, proxy)
  @buffer << data
  if @buffer.empty?
    read(proxy)
  else
    parser = Bones::RPC::Parser.new(@buffer, @adapter)
    begin
      loop { send parser.read, proxy }
    rescue EOFError
      @buffer.replace(parser.buffer.to_str)
    end
    return if @buffer.empty?
    read(proxy)
  end
end

#read(proxy) ⇒ Object

Since:

  • 0.0.1



36
37
38
39
40
41
42
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 36

def read(proxy)
  parse @socket.readpartial(4096), proxy
rescue EOFError, Errors::ConnectionFailure => e
  Loggable.warn("  BONES-RPC:", "#{@connection.node.address.resolved} Reader terminating: #{e.message}", "n/a")
  terminate
  raise e
end

#send(message, proxy) ⇒ Object

Since:

  • 0.0.1



44
45
46
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 44

def send(message, proxy)
  proxy.handle_message(message)
end

#terminateObject

Since:

  • 0.0.1



48
49
50
51
52
53
# File 'lib/bones/rpc/synchronous/connection/reader.rb', line 48

def terminate
  return if not alive?
  @alive = false
  @buffer.clear
  @writer.terminate
end