Class: Server

Inherits:
Application show all
Defined in:
lib/server.rb

Defined Under Namespace

Classes: CookiePath, ExitPath, FilePath, LoremPath, RootPath

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



74
75
76
77
78
79
80
# 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

Instance Method Details

#mountObject



82
83
84
85
86
87
88
89
90
# 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

#startObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#statisticsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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