Class: Proxo::Proxomaton
- Inherits:
-
Object
- Object
- Proxo::Proxomaton
- Defined in:
- lib/proxo.rb
Overview
Your code goes here…
Constant Summary collapse
- VALID_LOG_LEVELS =
%i[unknown fatal error warn info].freeze
Instance Method Summary collapse
-
#initialize(input_port:, input_host: "127.0.0.1", output_host: nil, output_port: nil, verbose: false, logger: nil) ⇒ Proxomaton
constructor
A new instance of Proxomaton.
-
#on_the_way_back(&block) ⇒ Object
The given block will be called when data is received back from the “output” socket.
-
#on_the_way_there(&block) ⇒ Object
The given block will be called when data is received from the “input” socket.
- #start! ⇒ Object
Constructor Details
#initialize(input_port:, input_host: "127.0.0.1", output_host: nil, output_port: nil, verbose: false, logger: nil) ⇒ Proxomaton
Returns a new instance of Proxomaton.
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/proxo.rb', line 25 def initialize(input_port:, input_host: "127.0.0.1", output_host: nil, output_port: nil, verbose: false, logger: nil) # The host to listen to @input_host = input_host # The port to listen on @input_port = input_port.to_i # The host to publish to @output_host = output_host || input_host # The port to publish to (`nil` if you don't want to republish) @output_port = output_port.to_i unless output_port.nil? # Print all received data before running middleman, and some other info @verbose = !!verbose # Logger for printing (defaults to STDOUT) @logger = logger || Logger.new(STDOUT) @logger.level = Logger::INFO # If no middlemen are given, it'll just pass the data through unmodified @outbound_middleman = proc(&:itself) @inbound_middleman = proc(&:itself) end |
Instance Method Details
#on_the_way_back(&block) ⇒ Object
The given block will be called when data is received back from the “output” socket. The block takes one argument: the data received.
58 59 60 |
# File 'lib/proxo.rb', line 58 def on_the_way_back(&block) @inbound_middleman = block end |
#on_the_way_there(&block) ⇒ Object
The given block will be called when data is received from the “input” socket. The block takes one argument: the data received.
52 53 54 |
# File 'lib/proxo.rb', line 52 def on_the_way_there(&block) @outbound_middleman = block end |
#start! ⇒ Object
62 63 64 65 66 67 68 69 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 105 106 107 108 109 110 111 112 |
# File 'lib/proxo.rb', line 62 def start! Thread.abort_on_exception = true input_server = TCPServer.new(input_host, input_port) loop do Thread.start(input_server.accept) do |input_socket| output_socket = TCPSocket.new(output_host, output_port) loop do # Waiting for sockets to be ready... ready_sockets, * = IO.select([input_socket, output_socket], nil, nil) if ready_sockets && ready_sockets.include?(input_socket) # Waiting for output to be writable... _, write_sockets, * = IO.select(nil, [output_socket], nil, 0) if write_sockets && write_sockets.include?(output_socket) # Receiving data from input port... data = input_socket.gets break if data.nil? log("Received data from input port: #{data.chomp}") new_data = outbound_middleman.call(data) log("Writing transformed data to output port: #{new_data.chomp}") output_socket.write(new_data) end end next unless ready_sockets && ready_sockets.include?(output_socket) # Waiting for input to be writable... _, write_sockets, * = IO.select(nil, [input_socket], nil, 0) next unless write_sockets && write_sockets.include?(input_socket) # Receiving data from output port... data = output_socket.gets break if data.nil? log("Received data from output port: #{data.chomp}") new_data = inbound_middleman.call(data) log("Writing transformed data to input port: #{new_data.chomp}") input_socket.write(new_data) end input_socket.close output_socket.close end end end |