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



66
67
68
# File 'lib/client.rb', line 66

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

#post_initObject



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

def post_init
  @connections = {}
end

#receive_object(message) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/client.rb', line 70

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 :connection
    _, id = message
    @@logger.info("New connection")
    connection = EventMachine.connect('127.0.0.1', @options[:local_port], ClientProxy)
    connection.on_data do |data|
      send_object(BERT::Tuple[:stream, id, data])
    end
    connection.on_unbind do
      @@logger.info("Connection closed")
      @connections.delete(id)
      send_object(BERT::Tuple[:close, id])
    end
    @connections[id] = connection
  when :stream
    _, id, data = message
    @connections[id].send_data(data) if @connections[id]
  when :close
    _, id = message
    connection = @connections.delete(id)
    connection.close_connection_after_writing if connection
  else
    @@logger.info("Received #{message.inspect}")
  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



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

def ssl_handshake_completed
  send_options
end

#unbindObject



106
107
108
109
# File 'lib/client.rb', line 106

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