class Server

Public Class Methods

new() click to toggle source
# File lib/server.rb, line 74
def initialize
  print "Input Port of servrer: ".green
  @port = $stdin.gets.chomp
  @port = '5899' if blank? @port
  FileUtils.rm_rf("#{__dir__}/../tmp")
  Dir.mkdir("#{__dir__}/../tmp")
end

Public Instance Methods

mount() click to toggle source
# File lib/server.rb, line 82
def mount
  @server = HTTPServer.new(:Port => @port)
  @server.mount "/",          RootPath
  @server.mount "/lorem",     LoremPath
  @server.mount "/cookie",    CookiePath
  @server.mount "/file",      FilePath
  @server.mount('/file.txt',  WEBrick::HTTPServlet::DefaultFileHandler, "#{__dir__}/../files/file.txt")
  @server.mount '/exit',      ExitPath
end
start() click to toggle source
# File lib/server.rb, line 92
def start
  @usw = Usagewatch
  @new0 = @usw.bandrx
  @time0 = Time.now

  ['TERM', 'INT'].each do |signal|
    trap(signal){
      @server.shutdown
      statistics
    }
  end
  @server.start
end
statistics() click to toggle source
# File lib/server.rb, line 106
def statistics
  new1 = @usw.bandrx
  time1 = Time.now

  bytesreceived = new1[0].to_i - @new0[0].to_i
  bitsreceived = bytesreceived * 8
  bandwidth = (bitsreceived.to_f / 1024 / 1024).round(3)
  time = (time1 - @time0).round(3)
  sum_req_times = ExitPath.results.sum.round(3)
  package_loss = ExitPath.loss
  package_rec = ExitPath.rec
  average_req_times = begin
    if ExitPath.results.empty?
      0
    else
      (ExitPath.results.inject{ |sum, el| sum + el } / ExitPath.results.size).round(3)
    end
  end
  bandwidth_per_time = (bandwidth / time).round(3)

  puts
  puts '------------------------------------------------------------------'.green
  puts "~>  AMBIDEXTER's SUMMARY".yellow
  puts
  puts "#{time} seconds Server sesion time".yellow
  puts "#{bandwidth} Mbit Current Bandwidth Received".yellow
  puts "#{bandwidth_per_time} Mbit/s Average Bandwidth Received".yellow
  puts "#{sum_req_times} seconds Summary request time".yellow
  puts "#{average_req_times} seconds Average request time".yellow
  if package_loss > 0
    puts "#{package_loss} Packages lost".red
  end
  puts "#{package_rec} Packages received".yellow
  puts
                   puts "Made by Oleg Cherednichenko 2017, KNURE".rjust 66
  puts '------------------------------------------------------------------'.green
end