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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/client.rb', line 22

def self.run(options = {})
  @@logger = Logger.new(STDOUT)
  @@logger.level = options[:verbose] ? Logger::INFO : Logger::WARN

  @@logger.info("Run with options #{options.inspect}")

  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
        if @options[:tls]
          @@logger.info("Request TLS")
          send_object(:start_tls)
        else
          send_options
        end
      end
    end
  end
end

Instance Method Details

#first_line(data) ⇒ Object



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

def first_line(data)
  data.split(/\r?\n/, 2).first
end

#receive_object(message) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/client.rb', line 66

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

  case message[0]
  when :start_tls
    @@logger.info("Start TLS")
    start_tls
  when :message
    puts message[1]
  when :halt
    EventMachine.stop_event_loop
  when :request
    _, uuid, request = message
    @@logger.info(first_line(request))
    EventMachine.connect('127.0.0.1', @options[:local_port], ClientProxy) do |connection|
      connection.callback do |data|
        @@logger.info("#{first_line(data)} - #{first_line(request)}")
        send_object(BERT::Tuple[:response, uuid, data])
      end
      connection.send_data(request)
    end
  end
end

#send_optionsObject



54
55
56
# File 'lib/client.rb', line 54

def send_options
  send_object(BERT::Tuple[:options, @options])
end

#serializerObject



50
51
52
# File 'lib/client.rb', line 50

def serializer
  Serializer
end

#ssl_handshake_completedObject



58
59
60
# File 'lib/client.rb', line 58

def ssl_handshake_completed
  send_options
end

#unbindObject



90
91
92
93
# File 'lib/client.rb', line 90

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