Class: Ruby_q

Inherits:
Object
  • Object
show all
Defined in:
lib/r_message_queue.rb

Overview

need public facing api

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname = '0.0.0.0', port = 65530) ⇒ Ruby_q

Returns a new instance of Ruby_q.



13
14
15
16
17
18
# File 'lib/r_message_queue.rb', line 13

def initialize(hostname = '0.0.0.0', port = 65530)
  @hostname = hostname
  @port = port
  @router = Message_queue_router.new
  self.start
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/r_message_queue.rb', line 11

def port
  @port
end

Instance Method Details

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/r_message_queue.rb', line 20

def start()
  puts Time.now.getutc.to_s + " - Starting server on " + @hostname + " " + @port.to_s
  server = TCPServer.open(port)
  loop {
    Thread.start(server.accept) do |client|
      puts 'Connection from ' + client.addr.inspect
      while 1 do
        rx = client.gets
        if rx.nil?
          Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' closing connection.'
          break
        end
        puts Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' sent: ' + rx
        client_return = @router.route(rx.gsub(/\r/," ").gsub(/\n/," "), client)
        puts "Client return: " + client_return
        client.puts client_return
      end
      #client.close
    end
  }
end