Class: ProxyLocal::Client
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- ProxyLocal::Client
show all
- Includes:
- Protocol
- Defined in:
- lib/proxylocal/client.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Protocol
#receive_object, #send_object, #serializer
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
31
32
33
34
35
|
# File 'lib/proxylocal/client.rb', line 31
def initialize(options)
@options = options
@reconnect = options[:hosts] && options[:hosts].any?
end
|
Class Method Details
.connect(options) ⇒ Object
27
28
29
|
# File 'lib/proxylocal/client.rb', line 27
def self.connect(options)
EventMachine.connect(options[:server_host], options[:server_port], self, options)
end
|
.run(options = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/proxylocal/client.rb', line 8
def self.run(options = {})
@@logger = Logger.new(STDOUT)
@@logger.level = options[:verbose] ? Logger::INFO : Logger::WARN
@@logger.info("Run with options #{options.inspect}")
begin
trap 'SIGCLD', 'IGNORE'
trap 'INT' do
puts
EventMachine.stop
exit
end
rescue ArgumentError
end
EventMachine.run { connect(options) }
end
|
Instance Method Details
#post_init ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/proxylocal/client.rb', line 41
def post_init
@connections = {}
if @options[:tls]
@@logger.info('Request TLS')
send_object(:start_tls)
else
send_options
end
end
|
#receive_close(id) ⇒ Object
104
105
106
107
|
# File 'lib/proxylocal/client.rb', line 104
def receive_close(id)
connection = @connections.delete(id)
connection.close_connection_after_writing if connection
end
|
#receive_connection(id) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/proxylocal/client.rb', line 86
def receive_connection(id)
@@logger.info('New connection')
connection = EventMachine.connect('127.0.0.1', @options[:local_port], ClientProxy)
connection.on_data do |data|
send_object(:stream, id, data)
end
connection.on_unbind do
@@logger.info('Connection closed')
@connections.delete(id)
send_object(:close, id)
end
@connections[id] = connection
end
|
#receive_halt ⇒ Object
81
82
83
84
|
# File 'lib/proxylocal/client.rb', line 81
def receive_halt
@reconnect = false
EventMachine.stop_event_loop
end
|
#receive_message(message) ⇒ Object
77
78
79
|
# File 'lib/proxylocal/client.rb', line 77
def receive_message(message)
puts message
end
|
#receive_start_tls ⇒ Object
72
73
74
75
|
# File 'lib/proxylocal/client.rb', line 72
def receive_start_tls
@@logger.info('Start TLS')
start_tls
end
|
#receive_stream(id, data) ⇒ Object
100
101
102
|
# File 'lib/proxylocal/client.rb', line 100
def receive_stream(id, data)
@connections[id].send_data(data) if @connections[id]
end
|
#receive_unknown(object) ⇒ Object
68
69
70
|
# File 'lib/proxylocal/client.rb', line 68
def receive_unknown(object)
@@logger.info("Received #{object.inspect}")
end
|
#send_options ⇒ Object
37
38
39
|
# File 'lib/proxylocal/client.rb', line 37
def send_options
send_object(:options, @options)
end
|
#ssl_handshake_completed ⇒ Object
52
53
54
|
# File 'lib/proxylocal/client.rb', line 52
def ssl_handshake_completed
send_options
end
|
#unbind ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/proxylocal/client.rb', line 56
def unbind
if @reconnect
puts "Connection has been terminated. Trying to reconnect..."
EventMachine.add_timer 5 do
self.class.connect(@options)
end
else
EventMachine.stop_event_loop
puts "Connection has been terminated"
end
end
|