Class: AvroMachine::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/avro_machine/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
# File 'lib/avro_machine/connection.rb', line 10

def initialize(*args)
  @input = ""
  super
end

Class Method Details

.responderObject

Raises:

  • (NotImplemented)


6
7
8
# File 'lib/avro_machine/connection.rb', line 6

def self.responder
  raise NotImplemented, "need to implement a reponder class"
end

Instance Method Details

#handle_avro(input) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/avro_machine/connection.rb', line 25

def handle_avro(input)
  reader = Avro::IPC::FramedReader.new(StringIO.new(input))
  str = reader.read_framed_message

  # handle the request
  responder = self.class.responder.new
  resp = responder.respond(str)

  # format the response
  writer = Avro::IPC::FramedWriter.new(StringIO.new(""))
  writer.write_framed_message(resp)
  send_data(writer.to_s)
end

#receive_data(data) ⇒ Object

buffer data - we can receive incomplete request over the tcp port



16
17
18
19
20
21
22
23
# File 'lib/avro_machine/connection.rb', line 16

def receive_data(data)
  @input << data

  # an avro request is terminated by a 0 byte
  return unless @input[-4..-1].unpack('N')[0] == 0

  handle_avro(@input)
end