Class Client
You can directly create and connect a client by using the
Client(String host, int port, String login, String passwd)
constructor, or use a three-phase construction:
- empty constructor
- setting host, port, login, and password with setters
- call empty connect()
Objects retrieved by Client are attached (directly or indirectly) to it. If the connection is closed, attached objects must not be used anymore (GC).
Note: The jNut Client does not support any reconnection nor ping mechanism, so the calling application must know that the UPSD can time out the connection.
Note: Retrieved values are not valid along the time, they are valid at the precise moment they are retrieved.
- Author:
- Emilien Kia
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidIntend to authenticate with an already set login and password.voidauthenticate(String login, String passwd) Intend to authenticate with a specified login and password, overriding already defined ones (remembers them as class instance fields).voidconnect()Connection to UPSD with already specified parameters (including a call toauthenticate(String, String)if so configured).voidIntent to connect to an UPSD with specified parameters (remembers them as class instance fields) without authentication.voidIntent to connect and authenticate to an UPSD with specified parameters (remembers them as class instance fields).voidDisconnect.booleanEnable TRACKING mode on this connection if it is not already enabled.protected StringSend a GET query line, then read the reply and validate the response.protected StringSend a GET query line, then read the reply and validate the response.Intend to retrieve a device by its name.Device[]Returns the list of available devices from the NUT server.getHost()Get the host name or address to which the client is (or will be) connected.Get the tracking ID for last SET or INSTCMD.getLogin()Get the login with which the client is (or will be connected).Get the password with which the client is (or will be connected).intgetPort()Get the port to which the client is (or will be) connected.booleanTest if the client is connected to the UPSD.booleanGet the tracking activation status.protected String[]Send a LIST query line, then read replies and validate them.protected String[]Send a LIST query line, then read replies and validate them.protected String[]Send a LIST query line, then read replies and validate them.voidlogout()Log out.protected StringSend a query line, then read the response.protected StringSend a query line, then read the response.protected StringSend a query line, then read the response.protected StringSend a query line, then read the response.voidSet the host name (or address) to which the client will intend to connect to at the next connection.voidSet the login with which the client will intend to connect.voidSet the password with which the client will intend to connect.voidsetPort(int port) Set the port to which the client is (or will be) connected.voidsetSslConfig(SSLConfig sslConfig) Set the SSL configuration for the client; should do so before callingconnect(String, int, String, String).voidsetTracking(boolean tracking) Set the tracking activation status; requires that the client connection is already active (call this afterconnect(String, int, String, String)succeeds).booleanwaitTrackingResult(TrackingID id, int waitIntervalSec, int waitMaxCount) Wait for a tracking ID to complete.
-
Constructor Details
-
Client
public Client()Default constructor. -
Client
public Client(String host, int port, String login, String passwd) throws IOException, UnknownHostException, NutException Connection constructor. Construct the Client object and intend to connect. Throw an exception if we cannot connect.- Parameters:
host- Host to which connect.port- IP port.login- Login to use to connect to UPSD.passwd- Password to use to connect to UPSD.- Throws:
IOExceptionUnknownHostExceptionNutException
-
-
Method Details
-
getHost
Get the host name or address to which the client is (or will be) connected.- Returns:
- Host name or address.
-
setHost
Set the host name (or address) to which the client will intend to connect to at the next connection.- Parameters:
host- New host name or address.
-
getLogin
Get the login with which the client is (or will be connected).- Returns:
- The login.
-
setLogin
Set the login with which the client will intend to connect.- Parameters:
login- New login.
-
getPasswd
Get the password with which the client is (or will be connected).- Returns:
- The password.
-
setPasswd
Set the password with which the client will intend to connect.- Parameters:
passwd- New password.
-
getPort
public int getPort()Get the port to which the client is (or will be) connected.- Returns:
- Port number.
-
setPort
public void setPort(int port) Set the port to which the client is (or will be) connected.- Parameters:
port- Port number.
-
getSslConfig
-
setSslConfig
Set the SSL configuration for the client; should do so before callingconnect(String, int, String, String).- Parameters:
sslConfig- SSL configuration object.- See Also:
-
isTrackingEnabled
public boolean isTrackingEnabled()Get the tracking activation status.- Returns:
- Tracking activation status.
-
setTracking
Set the tracking activation status; requires that the client connection is already active (call this afterconnect(String, int, String, String)succeeds).- Parameters:
tracking- New tracking activation status.- Throws:
IOExceptionNutException
-
getLastTrackingId
Get the tracking ID for last SET or INSTCMD.- Returns:
- Tracking ID.
-
connect
public void connect(String host, int port, String login, String passwd) throws IOException, UnknownHostException, NutException Intent to connect and authenticate to an UPSD with specified parameters (remembers them as class instance fields).Throw an exception if we cannot connect.
- Parameters:
host- Host to which connect.port- IP port.login- Login to use to connect to UPSD.passwd- Password to use to connect to UPSD.- Throws:
IOExceptionUnknownHostExceptionNutException
-
connect
Intent to connect to an UPSD with specified parameters (remembers them as class instance fields) without authentication.Throw an exception if we cannot connect.
- Parameters:
host- Host to which connect.port- IP port.- Throws:
IOExceptionUnknownHostExceptionNutException
-
connect
Connection to UPSD with already specified parameters (including a call toauthenticate(String, String)if so configured). Throw an exception if we cannot connect. -
authenticate
Intend to authenticate with a specified login and password, overriding already defined ones (remembers them as class instance fields).NOTE: This is an operation different from
Device.login()which lets a program likeupsmonassume a special role on a specific device.- Parameters:
login-passwd-- Throws:
IOExceptionNutException
-
authenticate
Intend to authenticate with an already set login and password.NOTE: This is an operation different from
Device.login()which lets a program likeupsmonassume a special role on a specific device.- Throws:
IOExceptionNutException
-
isConnected
public boolean isConnected()Test if the client is connected to the UPSD. Note: it does not actively probe to detect if the connection has been closed by the server -- just reports if we had opened it or not.- Returns:
- True if connected.
- See Also:
-
disconnect
public void disconnect()Disconnect. -
logout
public void logout()Log out. -
query
Send a query line, then read the response. Helper aroundquery(String).- Parameters:
query- Query to send.subquery- Sub query to send.- Returns:
- The reply.
- Throws:
IOExceptionNutException
-
query
protected String query(String query, String subquery, String[] params) throws IOException, NutException Send a query line, then read the response. Helper aroundquery(String, String...).- Parameters:
query- Query to send.subquery- Sub query to send.params- Optional additional parameters.- Returns:
- The reply.
- Throws:
IOExceptionNutException
-
query
Send a query line, then read the response.- Parameters:
query- Query to send.params- Optional additional parameters.- Returns:
- The reply.
- Throws:
IOExceptionNutException
-
query
Send a query line, then read the response.- Parameters:
query- Query to send.- Returns:
- The reply.
- Throws:
IOExceptionNutException
-
get
Send a GET query line, then read the reply and validate the response.- Parameters:
subcmd- GET subcommand to send.param- Extra parameters- Returns:
- GET result return by UPSD, without the subcommand and param prefix.
- Throws:
IOExceptionNutException
-
get
Send a GET query line, then read the reply and validate the response.- Parameters:
subcmd- GET subcommand to send.params- Eventual extra parameters.- Returns:
- GET result return by UPSD, without the subcommand and param prefix.
- Throws:
IOExceptionNutException
-
list
Send a LIST query line, then read replies and validate them.- Parameters:
subcmd- LIST subcommand to send.- Returns:
- LIST results return by UPSD, without the subcommand and param prefix.
- Throws:
IOExceptionNutException
-
list
Send a LIST query line, then read replies and validate them.- Parameters:
subcmd- LIST subcommand to send.param- Extra parameters.- Returns:
- LIST results return by UPSD, without the subcommand and param prefix.
- Throws:
IOExceptionNutException
-
list
Send a LIST query line, then read replies and validate them.- Parameters:
subcmd- LIST subcommand to send.params- Eventual extra parameters.- Returns:
- LIST results return by UPSD, without the subcommand and param prefix.
- Throws:
IOExceptionNutException
-
enableTrackingModeOnce
Enable TRACKING mode on this connection if it is not already enabled.- Returns:
- True if tracking is enabled.
- Throws:
IOExceptionNutException
-
getTrackingResult
- Throws:
IOExceptionNutException
-
waitTrackingResult
public boolean waitTrackingResult(TrackingID id, int waitIntervalSec, int waitMaxCount) throws IOException, NutException Wait for a tracking ID to complete.- Parameters:
id- Tracking ID to wait for.waitIntervalSec- Interval between checks in seconds.waitMaxCount- Maximum number of checks.- Returns:
- True if the command succeeded, false if timed out or failed.
- Throws:
IOExceptionNutException
-
getDeviceList
Returns the list of available devices from the NUT server.- Returns:
- List of devices, empty if nothing, null if not connected or failed.
- Throws:
IOExceptionNutException
-
getDevice
Intend to retrieve a device by its name.- Parameters:
name- Name of the device to look at.- Returns:
- Device
- Throws:
IOExceptionNutException
-