Class: Marta::Server::MartaServer

Inherits:
Object
  • Object
show all
Includes:
OptionsAndPaths
Defined in:
lib/marta/server.rb

Overview

Note:

It is believed that no user will use it

Server control and logic is in the class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = SettingMaster.port) ⇒ MartaServer

Returns a new instance of MartaServer.



78
79
80
81
82
83
84
85
86
87
88
89
# 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

Class Method Details

.port_check(port) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# 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_checkObject

Marta knows when server is not okay



108
109
110
111
112
113
114
115
# 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

.threadObject

Here we will store the thread where server is living



92
93
94
# File 'lib/marta/server.rb', line 92

def self.thread
  @@thread
end

.wait_user_dialog_response(wait_time = 600) ⇒ Object

Server can wait for while somebody will touch it



140
141
142
143
144
145
146
147
148
# 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

Instance Method Details

#server_killObject

We are killing the server sometimes



131
132
133
134
135
136
137
# 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) ⇒ Object

Server is starting with mounts.



97
98
99
100
101
102
103
104
105
# 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