Class: Geppeto::Connection::Server
- Inherits:
-
Object
- Object
- Geppeto::Connection::Server
- Includes:
- Celluloid::IO
- Defined in:
- lib/connections/server.rb
Instance Attribute Summary collapse
-
#history ⇒ Object
Returns the value of attribute history.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #format_cmd(scout, command) ⇒ Object
- #handle_connection(socket) ⇒ Object
-
#initialize(host = "0.0.0.0", port = 22756, logger = nil) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #send(scout, command) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(host = "0.0.0.0", port = 22756, logger = nil) ⇒ Server
Returns a new instance of Server.
13 14 15 16 17 18 19 |
# File 'lib/connections/server.rb', line 13 def initialize(host="0.0.0.0", port = 22756, logger = nil) @history = Revolver.new(10) @logger = logger.nil? ? ::Logger.new(STDOUT) : logger @logger.info("Starting Pinoccio Server on #{host}:#{port}") @server = TCPServer.new(host, port) async.run end |
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
11 12 13 |
# File 'lib/connections/server.rb', line 11 def history @history end |
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/connections/server.rb', line 10 def logger @logger end |
Instance Method Details
#format_cmd(scout, command) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/connections/server.rb', line 27 def format_cmd(scout, command) cmd = { :to => scout.to_i, :command => command.to_s, :id => 1, :timeout => 10000, # TODO: Make this configurable. :type => "command", } cmd.to_json + "\n" end |
#handle_connection(socket) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/connections/server.rb', line 51 def handle_connection(socket) _, port, host = socket.peeraddr @logger.info("Connected: #{host}:#{port}") chunk = socket.gets data = JSON.parse(chunk) token = data["token"] @socket = socket @logger.debug("Connected token: #{token}") loop { chunk = @socket.gets.chomp! data = JSON.parse(chunk) @history << data @logger.debug("Received: #{data}") } rescue @logger.info("Disconnected: #{host}:#{port}") @socket.close @logger.debug("Removed token: #{token}") @socket = nil end |
#run ⇒ Object
47 48 49 |
# File 'lib/connections/server.rb', line 47 def run loop { async.handle_connection @server.accept } end |
#send(scout, command) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/connections/server.rb', line 38 def send(scout, command) @logger.debug("Scout #{scout}: #{command}") if @socket.nil? @logger.warn("No socket connected") else @socket.write format_cmd(scout, command) end end |
#shutdown ⇒ Object
21 22 23 24 25 |
# File 'lib/connections/server.rb', line 21 def shutdown @server.close if @server rescue IOError @logger.warn("Pinoccio Server shutting down") end |