Class: MClient

Inherits:
Object
  • Object
show all
Defined in:
lib/minitcp.rb

Overview

Assure connection to server, extend socket connection by SocketReactive module.

MClient.run_one_shot(“localhost”,2200) do |socket| .. end.join

MClient.run_continous(“localhost”,2200,6000) do |socket| .. end.join

Class Method Summary collapse

Class Method Details

.run_continious(host, port, timer_interconnection_ms, &b) ⇒ Object

maintain a conntection to a TCP serveur, sleep timer_interconnection_ms millisecondes beetwen each reconnections



218
219
220
221
222
# File 'lib/minitcp.rb', line 218

def self.run_continious(host,port,timer_interconnection_ms,&b)
  Thread.new do
    loop { run_one_shot(host,port,&b).join ; sleep timer_interconnection_ms/1000.0 }
  end
end

.run_continous(host, port, timer_interconnection, &b) ⇒ Object



224
225
226
# File 'lib/minitcp.rb', line 224

def self.run_continous(host,port,timer_interconnection,&b)
  self.run_continious(host,port,timer_interconnection,&b)
end

.run_one_shot(host = "localhost", port = 80) ⇒ Object

Connecte to a TCP server, call block with client socket if connection sucess. enssure close connection after end of block



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/minitcp.rb', line 230

def self.run_one_shot(host="localhost",port=80)
  begin
    sleep(0.03) # give some time for server ready (for test...)
    socket = TCPSocket.new(host,port)
  rescue
    puts "not connected to #{host}:#{port}: " + $!.to_s
    return (Thread.new {})
  end
  SocketReactive::make_socket_reactive(socket)
  Thread.new do
    begin
      yield(socket)
    rescue Exception => e
      puts "#{e}\n  #{e.backtrace.join("\n  ")}"
    ensure
      socket.close() rescue nil
    end
  end
end