class Marta::Server::MartaServer

Server control and logic is in the class.

@note It is believed that no user will use it

Public Class Methods

new(port = SettingMaster.port) click to toggle source
# File lib/marta/server.rb, line 78
def initialize(port = SettingMaster.port)
  @port = port
  @@thread = Thread.new(port) do |port|
    begin
      the_server_start(port)
    rescue
      raise RuntimeError, "Could not start the server!"
      @@thread.kill
    end
  end
  MartaServer.server_check
end
port_check(port) click to toggle source
# File lib/marta/server.rb, line 117
def self.port_check(port)
  Timeout::timeout(1) do
    begin
      TCPSocket.new('127.0.0.1', port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      false
    end
  end
rescue Timeout::Error
  false
end
server_check() click to toggle source

Marta knows when server is not okay

# File lib/marta/server.rb, line 108
def self.server_check
  if !@@thread.alive?
    warn "Marta server was not working properly"
    @@thread.join
  else
    true
  end
end
thread() click to toggle source

Here we will store the thread where server is living

# File lib/marta/server.rb, line 92
def self.thread
  @@thread
end
wait_user_dialog_response(wait_time = 600) click to toggle source

Server can wait for while somebody will touch it

# File lib/marta/server.rb, line 140
def self.wait_user_dialog_response(wait_time = 600)
  ServerStore.has_answer = nil
  server_check
  start_time = Time.now
  while ServerStore.has_answer.nil? and (Time.now - start_time < wait_time)
    # No idea what Am I doing here...
  end
  ServerStore.has_answer
end

Public Instance Methods

server_kill() click to toggle source

We are killing the server sometimes

# File lib/marta/server.rb, line 131
def server_kill
  # So nasty. But WEBrick knows what to do.
  while @@thread.alive?
    @@thread.exit
  end
  @@thread.join
end
the_server_start(port) click to toggle source

Server is starting with mounts.

# File lib/marta/server.rb, line 97
def the_server_start(port)
  the_server = WEBrick::HTTPServer.new(Port: port,
             Logger: WEBrick::Log.new(File.open(File::NULL, 'w')),
             AccessLog: WEBrick::Log.new(File.open(File::NULL, 'w')))
  the_server.mount "/dialog", DialogServlet
  the_server.mount "/welcome", WelcomeServlet
  the_server.mount_proc('/q') {|req, resp| the_server.shutdown;  exit;}
  the_server.start
end