Class: Serf::Client::IO

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::IO, Celluloid::Logger
Defined in:
lib/serf/client/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket, handler) ⇒ IO

Returns a new instance of IO.



10
11
12
13
14
15
16
# File 'lib/serf/client/io.rb', line 10

def initialize socket, handler
  @socket = socket
  @handler = handler
  @up = MessagePack::Unpacker.new(@socket)
  async.write
  async.read
end

Instance Method Details

#readObject



18
19
20
21
22
23
24
# File 'lib/serf/client/io.rb', line 18

def read
  loop do
    debug 'IO#read'
    msg = @up.read
    @handler.mailbox << msg
  end
end

#writeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/serf/client/io.rb', line 26

def write
  loop do
    debug 'IO#write'
    header, param = receive
    debug "writing #{header}"

    buff = MessagePack::Buffer.new
    buff << header.to_msgpack
    if param
      buff << param.to_msgpack
    end

    @socket.write buff.to_str
  end
end