Class: ProxyLocal::Client
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- ProxyLocal::Client
- Includes:
- EventMachine::Protocols::ObjectProtocol
- Defined in:
- lib/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #first_line(data) ⇒ Object
- #post_init ⇒ Object
- #receive_object(message) ⇒ Object
- #send_options ⇒ Object
- #serializer ⇒ Object
- #ssl_handshake_completed ⇒ Object
- #unbind ⇒ Object
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( = {}) @@logger = Logger.new(STDOUT) @@logger.level = [:verbose] ? Logger::INFO : Logger::WARN @@logger.info("Run with options #{.inspect}") trap "SIGCLD", "IGNORE" trap "INT" do puts EventMachine.run exit end EventMachine.run do EventMachine.connect([:server_host], [:server_port], self) do |c| c.instance_eval do @options = if @options[:tls] @@logger.info("Request TLS") send_object(:start_tls) else 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_init ⇒ Object
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() = [] unless .is_a?(Array) case [0] when :start_tls @@logger.info("Start TLS") start_tls when :message puts [1] when :halt EventMachine.stop_event_loop when :connection _, id = @@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 = @connections[id].send_data(data) if @connections[id] when :close _, id = connection = @connections.delete(id) connection.close_connection_after_writing if connection else @@logger.info("Received #{.inspect}") end end |
#send_options ⇒ Object
54 55 56 |
# File 'lib/client.rb', line 54 def send_object(BERT::Tuple[:options, @options]) end |
#serializer ⇒ Object
50 51 52 |
# File 'lib/client.rb', line 50 def serializer Serializer end |
#ssl_handshake_completed ⇒ Object
62 63 64 |
# File 'lib/client.rb', line 62 def ssl_handshake_completed end |
#unbind ⇒ Object
106 107 108 109 |
# File 'lib/client.rb', line 106 def unbind EventMachine.stop_event_loop puts "A connection has been terminated" end |