Class: Istat::Client

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

Defined Under Namespace

Classes: NoServiceException, RegisterException, WrongPasswordException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, passwd, logger = nil) ⇒ Client

create a new istat client instance

Examples:

@client = Istat::Client.new("example.com", 5109, "00000")

Parameters:

  • host (String)

    the hostname of the remote istat server

  • port (String)

    the port of the istat server usually 5109

  • passwd (String)

    the passwd or code to access the server

  • logger (optional Logger) (defaults to: nil)

    a logger that will log all actions on the client



39
40
41
42
# File 'lib/istat/client.rb', line 39

def initialize(host, port, passwd, logger = nil)
  @host, @port, @passwd, @logger = host, port, passwd, logger
  @request_id = 1
end

Instance Attribute Details

#connection_frameObject

the frame that was send from the server after registration



29
30
31
# File 'lib/istat/client.rb', line 29

def connection_frame
  @connection_frame
end

Instance Method Details

#authenticate!Boolean

Note:

must be connected and registered to the remote machine

authenticate using the password, that was passed during initialization

Returns:

  • (Boolean)

    true on success



81
82
83
84
85
86
# File 'lib/istat/client.rb', line 81

def authenticate!
  @logger.info "Authenticate using password #{'*' * @passwd.size}" if @logger
  send Istat::Frames::AuthenticationRequest.new(@passwd)
  response = Istat::Frames::AuthenticationResponse.new(receive)
  response.ready?
end

#close!Object

closes the connection

Returns:

  • true on success



100
101
102
103
104
# File 'lib/istat/client.rb', line 100

def close!
  @logger.info "Close the connection" if @logger
  @socket.close
  true
end

#connect!Boolean

connect to the remote server

Returns:

  • (Boolean)

    true is the connection was successfull



72
73
74
75
76
# File 'lib/istat/client.rb', line 72

def connect!
  @logger.info "Connect to #{@host}:#{@port}" if @logger
  @socket = TCPSocket.new(@host, @port)
  true
end

#fetch(since = -1)) ⇒ Istat::Frames::MeasurementResponse

fetch data from the remote server

Examples:

@client = Istat::Client.new("example.com", 5109, "00000")
@client.start do |session|
  response = session.fetch
  response.load # => [0.54, 0.59, 0.65]
end

Parameters:

  • since (Integer|Time) (defaults to: -1))

    size of the requested history (-1 last)

Returns:



129
130
131
132
133
134
# File 'lib/istat/client.rb', line 129

def fetch(since = -1)
  @logger.info("Fetch measurements with request_id #{@request_id}") if @logger
  send Istat::Frames::MeasurementRequest.new(@request_id, since)
  @request_id += 1 # increment for next request
  Istat::Frames::MeasurementResponse.new(receive)
end

#fetch_allIstat::Frames::MeasurementResponse

fetch all data available on the remote server

Returns:

See Also:



139
140
141
# File 'lib/istat/client.rb', line 139

def fetch_all
  fetch(-2)
end

#online?Boolean

Note:

must be connected to the remote machine

checks if the host has the istat service active

Returns:

  • (Boolean)

    true on success



91
92
93
94
95
96
# File 'lib/istat/client.rb', line 91

def online?
  @logger.info "Test the connection" if @logger
  send Istat::Frames::ConnectionTestRequest.new
  response = Istat::Frames::ConnectionTestResponse.new(receive)
  response.success?
end

#register!(hostname = nil, duuid = nil) ⇒ Object

Note:

must be connected before this action should be called

register to the remote server using the source hostname and a uuid. If no values are passed, they will be fetched usind system methods. (Socket.gethostname)

Parameters:

  • hostname (optional, String) (defaults to: nil)

    the hostname for the registration (e.g. example.com)

  • duuid (optional, String) (defaults to: nil)

    the uuid for the registration



111
112
113
114
115
116
117
# File 'lib/istat/client.rb', line 111

def register!(hostname = nil, duuid = nil)
  hostname ||= Socket.gethostname
  duuid ||= Istat::Utils.uuid
  @logger.info "Register using hostname '#{hostname}' and duuid '#{duuid}'" if @logger
  send Istat::Frames::RegisterRequest.new(hostname, duuid)
  @connection_frame = Istat::Frames::RegisterResponse.new(receive)
end

#start {|Istat::Client| ... } ⇒ Object

starts a session on the remote machine and yields it to the passed block.

Examples:

@client = Istat::Client.new("example.com", 5109, "00000")
@client.start do |session|
  # work with the session
end

Yields:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/istat/client.rb', line 52

def start
  connect!
  if online?
    if register!
      if authenticate!
        yield(self)
      else
        raise WrongPasswordException.new("Wrong password/code!")
      end
    else
      raise RegisterException.new("Can't register to the service")
    end
  else
    raise NoServiceException.new("Service is not available")
  end
  close!
end