Class: Grueserve::Server

Inherits:
Object
  • Object
show all
Includes:
Debuggable
Defined in:
lib/grueserve/server.rb

Defined Under Namespace

Modules: ClientSocket

Constant Summary

Constants included from Debuggable

Debuggable::LEVELS

Instance Method Summary collapse

Methods included from Debuggable

format, level, level=, #method_missing, output, #report, #report_for, #reset, #reset_for, source_part, #time

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



82
83
84
85
86
87
88
89
90
# File 'lib/grueserve/server.rb', line 82

def initialize(options = {})
  @port = options.delete(:port) || 25999
  raise "#{self.class}.new requires an :application argument." unless options.include?(:application)
  @application = options.delete(:application)
  @frequency = options.delete(:frequency) || 100
  raise "Unknown options to #{self.class}: #{options.inspect}" unless options.empty?
  @sockets = []
  @sockets_to_close = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Grueserve::Debuggable

Instance Method Details

#kill_socket(socket) ⇒ Object



131
132
133
134
135
# File 'lib/grueserve/server.rb', line 131

def kill_socket(socket)
  socket.client.shutdown
  @sockets.delete(socket)
  @sockets_to_close << socket
end

#start!Object



92
93
94
95
96
# File 'lib/grueserve/server.rb', line 92

def start!
  start_updating!
  start_listening!
  start_serving!
end

#start_listening!Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/grueserve/server.rb', line 137

def start_listening!
  @listener ||= nil
  @listener.kill if @listener.respond_to?(:kill)
  @listener = Thread.new do
    loop do
      begin
        to_close = @sockets_to_close.to_a
        while (socket = to_close.shift)
          socket.close unless socket.closed?
        end
        read_ready, write_read, errors = select(@sockets, nil, @sockets, 1)
        unless errors.nil?
          errors.each do |socket|
            log_warn("Going to drop #{socket} from #{@sockets.inspect} since it got an error")
            kill_socket(socket)
          end
        end
        unless read_ready.nil?
          read_ready.each do |socket|
            if socket.eof?
              log_info("Going to drop #{socket} from #{@sockets.inspect} since it is closed")
              kill_socket(socket)
            else
              begin
                socket.handle_input
              rescue ClientSocket::PolicyFileRequest => e
                log_info("Got policy file request from #{socket.inspect}")
                socket.client.handle_policy_request
              end
            end
          end
        end
      rescue Exception => e
        log_warn(e)
      end
    end
  end
  log_debug("Started listener")
end

#start_serving!Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/grueserve/server.rb', line 177

def start_serving!
  log_debug("Starting serving")
  @server ||= nil
  @server.close if @server.respond_to?(:close)
  @server = TCPServer.new(@port)
  loop do
    begin
      socket = @server.accept_nonblock
      socket.extend(ClientSocket)
      socket.init_client_socket(Client.new(:socket => socket, :application => @application), self)
      log_info("Received #{socket}, adding it to #{@sockets.inspect}")
      @sockets << socket
    rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR => e
      log_trace("Got TCPServer error: #{e}")
      IO.select([@server])
      retry
    rescue Exception => e
      log_warn(e)
    end      
  end
end

#start_updating!Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/grueserve/server.rb', line 98

def start_updating!
  @updater ||= nil
  @updater.kill if @updater.respond_to?(:kill)
  @updater = Thread.new do
    counter = 0
    timer = Time.now
    sleep_sum = 0
    loop do
      begin
        start_time = Time.now
        @application.map.update!
        if (diff = (1.0 / @frequency) - ((stop_time = Time.now).to_f - start_time.to_f)) > 0
          sleep_sum += diff
          sleep(diff)
        end
        counter += 1
        if Time.now - timer > 10
          avg_frequency = counter / (Time.now.to_f - timer.to_f)
          avg_sleep = sleep_sum / (Time.now.to_f - timer.to_f)
          log_info("Avg frequency: #{avg_frequency}, sleeping #{(avg_sleep * 100).to_i}% of the time")
          report
          reset
          timer = Time.now
          counter = 0
          sleep_sum = 0
        end
      rescue Exception => e
        log_warn(e)
      end
    end
  end
end