org.networkupstools.jnut
Class Client

java.lang.Object
  extended by org.networkupstools.jnut.Client

public class Client
extends java.lang.Object

A jNut client is start point to dialog to UPSD. It can connect to an UPSD then retrieve its device list. It support authentication by login/password.

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:

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 the UPSD can timeout 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
Client()
          Default constructor.
Client(java.lang.String host, int port, java.lang.String login, java.lang.String passwd)
          Connection constructor.
 
Method Summary
 void authenticate()
          Intend to authenticate with alread set login and password.
 void authenticate(java.lang.String login, java.lang.String passwd)
          Intend to authenticate with specified login and password, overriding already defined ones.
 void connect()
          Connection to UPSD with already specified parameters.
 void connect(java.lang.String host, int port)
          Intent to connect to an UPSD with specified parameters without authentication.
 void connect(java.lang.String host, int port, java.lang.String login, java.lang.String passwd)
          Intent to connect and authenticate to an UPSD with specified parameters.
 void disconnect()
          Disconnect.
protected  java.lang.String get(java.lang.String subcmd, java.lang.String param)
          Send a GET query line then read the reply and validate the response.
protected  java.lang.String get(java.lang.String subcmd, java.lang.String[] params)
          Send a GET query line then read the reply and validate the response.
 Device getDevice(java.lang.String name)
          Intend to retrieve a device by its name.
 Device[] getDeviceList()
          Returns the list of available devices from the NUT server.
 java.lang.String getHost()
          Get the host name or address to which client is (or will be) connected.
 java.lang.String getLogin()
          Get the login with which the client is (or will be connected).
 java.lang.String getPasswd()
          Get the password with which the client is (or will be connected).
 int getPort()
          Get the port to which client is (or will be) connected.
 boolean isConnected()
          Test if the client is connected to the UPSD.
protected  java.lang.String[] list(java.lang.String subcmd)
          Send a LIST query line then read replies and validate them.
protected  java.lang.String[] list(java.lang.String subcmd, java.lang.String param)
          Send a LIST query line then read replies and validate them.
protected  java.lang.String[] list(java.lang.String subcmd, java.lang.String[] params)
          Send a LIST query line then read replies and validate them.
 void logout()
          Log out.
protected  java.lang.String query(java.lang.String query)
          Send a query line then read the response.
protected  java.lang.String query(java.lang.String query, java.lang.String subquery)
          Send a query line then read the response.
protected  java.lang.String query(java.lang.String query, java.lang.String[] params)
          Send a query line then read the response.
protected  java.lang.String query(java.lang.String query, java.lang.String subquery, java.lang.String[] params)
          Send a query line then read the response.
 void setHost(java.lang.String host)
          Set the host name (or address) to which the client will intend to connect to at next connection.
 void setLogin(java.lang.String login)
          Set the login with which the client will intend to connect.
 void setPasswd(java.lang.String passwd)
          Set the password with which the client will intend to connect.
 void setPort(int port)
          Set the port to which client is (or will be) connected.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Client

public Client()
Default constructor.


Client

public Client(java.lang.String host,
              int port,
              java.lang.String login,
              java.lang.String passwd)
       throws java.io.IOException,
              java.net.UnknownHostException,
              NutException
Connection constructor. Construct the Client object and intend to connect. Throw an exception if 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:
java.io.IOException
java.net.UnknownHostException
NutException
Method Detail

getHost

public java.lang.String getHost()
Get the host name or address to which client is (or will be) connected.

Returns:
Host name or address.

setHost

public void setHost(java.lang.String host)
Set the host name (or address) to which the client will intend to connect to at next connection.

Parameters:
host - New host name or address.

getLogin

public java.lang.String getLogin()
Get the login with which the client is (or will be connected).

Returns:
The login.

setLogin

public void setLogin(java.lang.String login)
Set the login with which the client will intend to connect.

Parameters:
login - New login.

getPasswd

public java.lang.String getPasswd()
Get the password with which the client is (or will be connected).

Returns:
The password.

setPasswd

public void setPasswd(java.lang.String passwd)
Set the password with which the client will intend to connect.

Parameters:
passwd - New password.

getPort

public int getPort()
Get the port to which client is (or will be) connected.

Returns:
Port number.

setPort

public void setPort(int port)
Set the port to which client is (or will be) connected.

Parameters:
port - Port number.

connect

public void connect(java.lang.String host,
                    int port,
                    java.lang.String login,
                    java.lang.String passwd)
             throws java.io.IOException,
                    java.net.UnknownHostException,
                    NutException
Intent to connect and authenticate to an UPSD with specified parameters. Throw an exception if 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:
java.io.IOException
java.net.UnknownHostException
NutException

connect

public void connect(java.lang.String host,
                    int port)
             throws java.io.IOException,
                    java.net.UnknownHostException,
                    NutException
Intent to connect to an UPSD with specified parameters without authentication. Throw an exception if cannot connect.

Parameters:
host - Host to which connect.
port - IP port.
Throws:
java.io.IOException
java.net.UnknownHostException
NutException

connect

public void connect()
             throws java.io.IOException,
                    java.net.UnknownHostException,
                    NutException
Connection to UPSD with already specified parameters. Throw an exception if cannot connect.

Throws:
java.io.IOException
java.net.UnknownHostException
NutException

authenticate

public void authenticate(java.lang.String login,
                         java.lang.String passwd)
                  throws java.io.IOException,
                         NutException
Intend to authenticate with specified login and password, overriding already defined ones.

Parameters:
login -
passwd -
Throws:
java.io.IOException
NutException

authenticate

public void authenticate()
                  throws java.io.IOException,
                         NutException
Intend to authenticate with alread set login and password.

Throws:
java.io.IOException
NutException

isConnected

public boolean isConnected()
Test if the client is connected to the UPSD. Note: it does not detect if the connection have been closed by server.

Returns:
True if connected.

disconnect

public void disconnect()
Disconnect.


logout

public void logout()
Log out.


query

protected java.lang.String query(java.lang.String query,
                                 java.lang.String subquery)
                          throws java.io.IOException,
                                 NutException
Send a query line then read the response. Helper around query(String).

Parameters:
query - Query to send.
subquery - Sub query to send.
Returns:
The reply.
Throws:
java.io.IOException
NutException

query

protected java.lang.String query(java.lang.String query,
                                 java.lang.String subquery,
                                 java.lang.String[] params)
                          throws java.io.IOException,
                                 NutException
Send a query line then read the response. Helper around query(String, String ...).

Parameters:
query - Query to send.
subquery - Sub query to send.
params - Optionnal additionnal parameters.
Returns:
The reply.
Throws:
java.io.IOException
NutException

query

protected java.lang.String query(java.lang.String query,
                                 java.lang.String[] params)
                          throws java.io.IOException,
                                 NutException
Send a query line then read the response.

Parameters:
query - Query to send.
params - Optionnal additionnal parameters.
Returns:
The reply.
Throws:
java.io.IOException
NutException

query

protected java.lang.String query(java.lang.String query)
                          throws java.io.IOException,
                                 NutException
Send a query line then read the response.

Parameters:
query - Query to send.
Returns:
The reply.
Throws:
java.io.IOException
NutException

get

protected java.lang.String get(java.lang.String subcmd,
                               java.lang.String param)
                        throws java.io.IOException,
                               NutException
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:
java.io.IOException
NutException

get

protected java.lang.String get(java.lang.String subcmd,
                               java.lang.String[] params)
                        throws java.io.IOException,
                               NutException
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:
java.io.IOException
NutException

list

protected java.lang.String[] list(java.lang.String subcmd)
                           throws java.io.IOException,
                                  NutException
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:
java.io.IOException
NutException

list

protected java.lang.String[] list(java.lang.String subcmd,
                                  java.lang.String param)
                           throws java.io.IOException,
                                  NutException
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:
java.io.IOException
NutException

list

protected java.lang.String[] list(java.lang.String subcmd,
                                  java.lang.String[] params)
                           throws java.io.IOException,
                                  NutException
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:
java.io.IOException
NutException

getDeviceList

public Device[] getDeviceList()
                       throws java.io.IOException,
                              NutException
Returns the list of available devices from the NUT server.

Returns:
List of devices, empty if nothing, null if not connected or failed.
Throws:
java.io.IOException
NutException

getDevice

public Device getDevice(java.lang.String name)
                 throws java.io.IOException,
                        NutException
Intend to retrieve a device by its name.

Parameters:
name - Name of the device to look at.
Returns:
Device
Throws:
java.io.IOException
NutException


Copyright © 2011. All Rights Reserved.