Class: ProxyLocal::Client

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Protocols::ObjectProtocol
Defined in:
lib/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(options = {}) ⇒ Object



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

def self.run(options = {})
  trap "SIGCLD", "IGNORE"
  trap "INT" do
    puts
    EventMachine.run
    exit
  end

  EventMachine.run do
    EventMachine::connect(options[:server_host], options[:server_port], self) do |c|
      c.instance_eval do
        @options = options
        send_object(BERT::Tuple[:options, @options])
      end
    end
  end
end

Instance Method Details

#receive_object(message) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/client.rb', line 41

def receive_object(message)
  message = [message] unless message.is_a?(Array)

  case message[0]
  when :message
    puts message[1]
  when :halt
    EventMachine.stop_event_loop
  when :request
    _, uuid, request = message
    EventMachine::connect('127.0.0.1', @options[:local_port], ClientProxy) do |connection|
      connection.callback do |data|
        send_object(BERT::Tuple[:response, uuid, data])
      end
      connection.send_data(request)
    end
  end
end

#serializerObject



19
20
21
# File 'lib/client.rb', line 19

def serializer
  Serializer
end

#unbindObject



60
61
62
63
# File 'lib/client.rb', line 60

def unbind
  EventMachine.stop_event_loop
  puts "A connection has been terminated"
end