Class: Ruberl::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", port = 7050) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
# File 'lib/ruberl/base.rb', line 4

def initialize(host="localhost", port=7050)
  @host = host
  @port = port
  
  @recv_timeout = 3
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/ruberl/base.rb', line 3

def host
  @host
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/ruberl/base.rb', line 3

def port
  @port
end

#recv_timeoutObject

Returns the value of attribute recv_timeout.



3
4
5
# File 'lib/ruberl/base.rb', line 3

def recv_timeout
  @recv_timeout
end

Instance Method Details

#messenger_cast!(cmd) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruberl/base.rb', line 30

def messenger_cast!(cmd)
  with_udp do |sock|
    sock.send(cmd, 0, @host, @port)
    @resp = if select([sock], nil, nil, @recv_timeout)
      sock.recvfrom(65536)
    end
    @resp[0] = @resp[0][4..-1] if @resp
  end
  @resp ? @resp[0] : nil
end

#messenger_send!(msg = "get_current_load cpu") ⇒ Object



40
41
42
43
44
45
46
# File 'lib/ruberl/base.rb', line 40

def messenger_send!(msg="get_current_load cpu")
  with_socket do |sock|
    sock.send(msg, 0)
    @str = sock.recv(2000)      
  end
  @str
end

#with_socket(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ruberl/base.rb', line 10

def with_socket(&block)
  begin
    socket = TCPSocket.open(@host, @port)
    out = yield(socket)
    socket.close
    out
  rescue Exception => e
  end
end

#with_udp(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruberl/base.rb', line 19

def with_udp(&block)
  out,sock = nil,nil
  begin
    socket = UDPSocket.open
    out = yield(socket)
  rescue IOError, SystemCallError
  ensure
    socket.close if socket
  end
  out
end