Class: Istat::Client
- Inherits:
-
Object
- Object
- Istat::Client
- Defined in:
- lib/istat/client.rb
Defined Under Namespace
Classes: NoServiceException, RegisterException, WrongPasswordException
Instance Attribute Summary collapse
-
#connection_frame ⇒ Object
the frame that was send from the server after registration.
Instance Method Summary collapse
-
#authenticate! ⇒ Boolean
authenticate using the password, that was passed during initialization.
-
#close! ⇒ Object
closes the connection.
-
#connect! ⇒ Boolean
connect to the remote server.
-
#fetch(since = -1)) ⇒ Istat::Frames::MeasurementResponse
fetch data from the remote server.
-
#fetch_all ⇒ Istat::Frames::MeasurementResponse
fetch all data available on the remote server.
-
#initialize(host, port, passwd, logger = nil) ⇒ Client
constructor
create a new istat client instance.
-
#online? ⇒ Boolean
checks if the host has the istat service active.
-
#register!(hostname = nil, duuid = nil) ⇒ Object
register to the remote server using the source hostname and a uuid.
-
#start {|Istat::Client| ... } ⇒ Object
starts a session on the remote machine and yields it to the passed block.
Constructor Details
#initialize(host, port, passwd, logger = nil) ⇒ Client
create a new istat client instance
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_frame ⇒ Object
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
must be connected and registered to the remote machine
authenticate using the password, that was passed during initialization
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
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
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
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_all ⇒ Istat::Frames::MeasurementResponse
fetch all data available on the remote server
139 140 141 |
# File 'lib/istat/client.rb', line 139 def fetch_all fetch(-2) end |
#online? ⇒ Boolean
must be connected to the remote machine
checks if the host has the istat service active
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
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)
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.
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 |