com.adventnet.snmp.snmp2
Class SASClient

java.lang.Object
  |
  +--java.util.Observable
        |
        +--com.adventnet.snmp.snmp2.SASClient

public class SASClient
extends java.util.Observable
implements java.io.Serializable, java.lang.Runnable

The SASClient class enables applet services for applets that are limited by the security sandbox that Java applets often run within. In addition to sending and receiving SNMP data, the services available through SASClient allow saving files, creating and deleting directories and files, etc., on the web server that loaded the applet, provided the SAS server is run on the server.

You can also do all these operations using HTTP instead of TCP/IP by setting the value of protocol in SnmpSession using setSASProtocol() to 2 or by using the constructor SASClient(applet,debug, protocol). For using HTTP protocol, you don't have to start the SAServer.

The SASClient class is needed on account of browser security which prevents Java applets from sending data to any host but the applet host when loaded over the network.

This class will not need to be used directly for SNMP operations as the use of SASClient class use will not be transparent to the user. The SNMP library will by default attempt to use SAS if available, or use HTTP if the protocol value is set to 2 but will otherwise use direct socket connections if SAS is unavailable. The file access methods of this class may be used if file operation are required by an applet. For example the applet may use the saveFile method or appendFile method to save data on the applet host.

If the protocol value is 1,which is the default value, this class implements a client to send/receive PDUs from any SNMP peer via a deamon (SAS) on the Applet Host, i.e. Web server. While using Java enabled browsers that do not permit socket connections to anything but the applet host ,your applet will automatically instantiate this class to communicate with an SNMP entity.

If the protocol value is 2,this class uses the servlet loaded with the web-server to send/receive requests.

Use the open method on your session instance to specify whether the SAS or HTTP function needs to be used by your applet. For each session, you will call the open method with your applet instance as an argument. If the connection to the SAS daemon fails(if the value of protocol specified is 1) or if the protocol is not set to 2, the session will try and open a direct local UDP port for SNMP operations.

Enable CALLBACKTHREAD to use the SASClient calls from callback. By default it is disabled.

See Also:
SnmpSession, Serialized Form

Field Summary
(package private)  java.applet.Applet app
           
(package private)  int CALL_TIMEOUT
           
(package private)  byte[] callData
           
(package private)  boolean closeSocket
           
(package private)  boolean connected
           
(package private)  byte[] data
           
(package private) static boolean fileWrite
           
(package private)  boolean gotResponse
          Some parameters to implement the clientCall
(package private)  boolean gotUserResponse
           
(package private) static int HTTP_ADDRESS_LOOKUP
           
(package private) static int HTTP_ALIVE
           
(package private) static int HTTP_APPEND
           
(package private) static int HTTP_ASYNC_RESPONSE
           
(package private) static int HTTP_CLIENT_CALL
           
(package private) static int HTTP_CLIENT_RESPONSE
           
(package private) static int HTTP_CLOSE
           
(package private) static int HTTP_CREATE_DIR
           
(package private) static int HTTP_DELETE_DIR
           
(package private) static int HTTP_DELETE_FILE
           
(package private) static int HTTP_ERROR
           
(package private) static int HTTP_GETCLIENTID
           
(package private) static int HTTP_NAME_LOOKUP
           
(package private) static int HTTP_OPEN
           
static int HTTP_PROTOCOL
           
(package private) static int HTTP_RECEIVED
           
(package private) static int HTTP_SAVE
           
(package private) static int HTTP_SEND
           
(package private) static int HTTP_TRAP_REQUEST
           
(package private)  int len
          Some necessary parameters used to pick up datagrams
(package private)  ServletCommunication msg
           
(package private) static java.lang.String ONE
           
(package private)  int port
          Some necessary parameters used to pick up datagrams
(package private)  java.util.Properties props
           
(package private)  int protocol
           
(package private)  TransportProvider provider
           
(package private)  java.lang.String remoteHost
           
(package private)  java.lang.String remoteHostName
           
(package private) static int reqID
           
(package private) static int SAS_ADDRESS_LOOKUP
           
(package private) static int SAS_ALIVE
           
(package private) static int SAS_APPEND
           
(package private) static int SAS_ASYNC_RESPONSE
           
(package private) static int SAS_CLIENT_CALL
           
(package private) static int SAS_CLIENT_RESPONSE
           
(package private) static int SAS_CLOSE
           
(package private) static int SAS_CREATE_DIR
           
(package private) static int SAS_DB_CLOSE
           
(package private) static int SAS_DB_CONNECT
           
(package private) static int SAS_DB_FAILURE
           
(package private) static int SAS_DB_QUERY
           
(package private) static int SAS_DB_SUCCESS
           
(package private) static int SAS_DB_UPDATE
           
(package private) static int SAS_DELETE_DIR
           
(package private) static int SAS_DELETE_FILE
           
(package private) static int SAS_ERROR
           
(package private) static int SAS_INVALID_TYPE
          The request types used to talk to the SNMP Applet Server
(package private) static int SAS_NAME_LOOKUP
           
(package private) static int SAS_OPEN
           
(package private) static int SAS_RECEIVED
           
(package private) static int SAS_SAVE
           
(package private) static int SAS_SEND
           
(package private) static int SAS_TRAP_REQUEST
           
(package private) static int SAS_VALID_TYPES
           
(package private)  SessionTransportProvider sock
          The socket used to communicate with the SNMP Applet Server
(package private) static java.lang.String STATUS
           
static int TCP_PROTOCOL
           
(package private) static int TIME
           
(package private) static java.lang.String TWO
           
(package private)  java.net.URL url
           
(package private)  int user_req_type
           
(package private)  byte[] userRcvData
           
(package private) static java.lang.String ZERO
           
 
Constructor Summary
SASClient(java.applet.Applet applet, boolean debug)
          By default this constructor will take the protocol value as 1 and look for the SASPort.html file in the applet html directory.
SASClient(java.applet.Applet applet, boolean debug, int protocol)
          If the protocol value is set to 1, this constructor will look for the SASPort.html file in the applet html directory.
 
Method Summary
 void appendFile(java.lang.String filename, byte[] data)
          The appendFile method for appending data to a file in "SASusers" sub-directory in which SASPort.html was created by the SAServer.
 byte[] clientCall(byte[] bytes)
          This method is used to build user defined functions around SAS.
(package private)  void close()
          Close the remote session
 void closeDB()
          This method is for closing the database connection which is opened through the method connectDB method.
 void connectDB(java.lang.String driver, java.lang.String url, java.lang.String user, java.lang.String pass)
          This method is for connecting with the database through SAS.
(package private)  int convertByteArrayToInt(byte[] bArr)
           
(package private)  int convertByteArrayToInt(byte[] bArr, int startIndex)
           
(package private)  byte[] convertIntToByteArray(int i)
           
 void createDir(java.lang.String directory)
          The createDir method is for creating a directory on the applet host.
 void deleteDir(java.lang.String directory)
          The deleteDir method for deleting a directory on the applet host.
 void deleteFile(java.lang.String filename)
          The deleteFile method is for deleting a file on the applet host.
(package private) static byte[] getBytes(java.lang.String s)
           
(package private) static java.lang.String getByteString(byte[] array, int offset, int length)
           
(package private)  int getClientID()
           
(package private)  byte[] getData(java.util.Vector vec)
           
 java.lang.String getHostAddress(java.lang.String hostname, int timeout)
          The ip address lookup method is for applet clients.
 java.lang.String getHostName(java.lang.String address, int timeout)
          The name lookup method is for applet clients.
 int getMaxSasTypes()
          This method returns the number of known SAS request types and users are requested to use a number greater than the returned value which will keep changing in the future.
 boolean isConnected()
          Indicates if the connection to the SNMP Applet Server has been successfully established.
 boolean isDBConnected()
          This method is for knowing whether the database connection has been established or not.
static boolean isFileReadWrite()
          Checks if the fileWrite flag is true.
 boolean isKnownSASType(int type)
          If the value passed is one of the known request types, it returns true.
(package private)  void notifyWaitingThreads()
           
(package private)  void open(int local_port)
          open the socket at applet host
(package private)  void open(int local_port, java.lang.String[] localAddresses)
           
 java.sql.ResultSet queryDB(java.lang.String queryString)
          This method is for doing a database query.
(package private)  void receive()
          Used to pick up PDUs from the remote port on the SAS server Blocks until a PDU has been received
 void reqTraps(int port)
          Method to request delivery of traps by SAServer arriving at that port.
 void run()
          Invokes receive() method.
 void saveFile(java.lang.String filename, byte[] data)
          The saveFile method for saving data to a file in "SASusers" sub-directory in which SASPort.html was created by the SAServer.
(package private)  void send(java.lang.String hostname, int port, byte[] data)
          The send method for sending PDUs
(package private)  void send(java.lang.String hostname, int port, byte[] data, long timeoutmillis)
           
static void setFileReadWrite(boolean writeFlag)
          Sets the fileWrite flag.
 void setTimeout(int to)
          For setting the timeOut value for a synchronous SASClient operation.
(package private)  void setTransportProvider(java.lang.String providerName)
           
 void start()
          Starts the receiver thread.
 void stop()
          Stops the receiver thread and closes socket connection with SAServer.
 int updateDB(java.lang.String updateString)
          This method is for updating the database with a new value.
 byte[] userSyncSend(int userType, byte[] bytes)
          Using this method the user can send his own request type.
(package private)  void waitUntilNotification()
           
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZERO

static final java.lang.String ZERO

ONE

static final java.lang.String ONE

TWO

static final java.lang.String TWO

STATUS

static final java.lang.String STATUS

TIME

static final int TIME

url

java.net.URL url

props

java.util.Properties props

msg

ServletCommunication msg

TCP_PROTOCOL

public static final int TCP_PROTOCOL

HTTP_PROTOCOL

public static final int HTTP_PROTOCOL

protocol

int protocol

app

java.applet.Applet app

reqID

static int reqID

fileWrite

static boolean fileWrite

connected

boolean connected

sock

SessionTransportProvider sock
The socket used to communicate with the SNMP Applet Server

SAS_INVALID_TYPE

static final int SAS_INVALID_TYPE
The request types used to talk to the SNMP Applet Server

SAS_OPEN

static final int SAS_OPEN

SAS_SEND

static final int SAS_SEND

SAS_RECEIVED

static final int SAS_RECEIVED

SAS_CLOSE

static final int SAS_CLOSE

SAS_SAVE

static final int SAS_SAVE

SAS_ERROR

static final int SAS_ERROR

SAS_APPEND

static final int SAS_APPEND

SAS_DELETE_FILE

static final int SAS_DELETE_FILE

SAS_CREATE_DIR

static final int SAS_CREATE_DIR

SAS_DELETE_DIR

static final int SAS_DELETE_DIR

SAS_CLIENT_CALL

static final int SAS_CLIENT_CALL

SAS_CLIENT_RESPONSE

static final int SAS_CLIENT_RESPONSE

SAS_ALIVE

static final int SAS_ALIVE

SAS_TRAP_REQUEST

static final int SAS_TRAP_REQUEST

SAS_NAME_LOOKUP

static final int SAS_NAME_LOOKUP

SAS_ADDRESS_LOOKUP

static final int SAS_ADDRESS_LOOKUP

SAS_ASYNC_RESPONSE

static final int SAS_ASYNC_RESPONSE

SAS_DB_CONNECT

static final int SAS_DB_CONNECT

SAS_DB_CLOSE

static final int SAS_DB_CLOSE

SAS_DB_QUERY

static final int SAS_DB_QUERY

SAS_DB_UPDATE

static final int SAS_DB_UPDATE

SAS_DB_FAILURE

static final int SAS_DB_FAILURE

SAS_DB_SUCCESS

static final int SAS_DB_SUCCESS

SAS_VALID_TYPES

static final int SAS_VALID_TYPES

HTTP_OPEN

static final int HTTP_OPEN

HTTP_SEND

static final int HTTP_SEND

HTTP_RECEIVED

static final int HTTP_RECEIVED

HTTP_CLOSE

static final int HTTP_CLOSE

HTTP_SAVE

static final int HTTP_SAVE

HTTP_ERROR

static final int HTTP_ERROR

HTTP_APPEND

static final int HTTP_APPEND

HTTP_DELETE_FILE

static final int HTTP_DELETE_FILE

HTTP_CREATE_DIR

static final int HTTP_CREATE_DIR

HTTP_DELETE_DIR

static final int HTTP_DELETE_DIR

HTTP_CLIENT_CALL

static final int HTTP_CLIENT_CALL

HTTP_CLIENT_RESPONSE

static final int HTTP_CLIENT_RESPONSE

HTTP_ALIVE

static final int HTTP_ALIVE

HTTP_TRAP_REQUEST

static final int HTTP_TRAP_REQUEST

HTTP_NAME_LOOKUP

static final int HTTP_NAME_LOOKUP

HTTP_ADDRESS_LOOKUP

static final int HTTP_ADDRESS_LOOKUP

HTTP_ASYNC_RESPONSE

static final int HTTP_ASYNC_RESPONSE

HTTP_GETCLIENTID

static final int HTTP_GETCLIENTID

port

int port
Some necessary parameters used to pick up datagrams

len

int len
Some necessary parameters used to pick up datagrams

data

byte[] data

remoteHost

java.lang.String remoteHost

remoteHostName

java.lang.String remoteHostName

closeSocket

boolean closeSocket

gotResponse

boolean gotResponse
Some parameters to implement the clientCall

callData

byte[] callData

CALL_TIMEOUT

int CALL_TIMEOUT

gotUserResponse

boolean gotUserResponse

userRcvData

byte[] userRcvData

user_req_type

int user_req_type

provider

TransportProvider provider
Constructor Detail

SASClient

public SASClient(java.applet.Applet applet,
                 boolean debug)
          throws SnmpException
By default this constructor will take the protocol value as 1 and look for the SASPort.html file in the applet html directory. If the SASPort.html file is created elsewhere then the directory where it is created, is specified in the applet html file, using an applet parameter SAS_PORT_DIR.
Parameters:
applet - applet refernce using this sasclient to talk to SAServer.
debug - Debug messages are printed, if true.
Throws:
SnmpException - is thrown whenever an error occurs.

SASClient

public SASClient(java.applet.Applet applet,
                 boolean debug,
                 int protocol)
          throws SnmpException
If the protocol value is set to 1, this constructor will look for the SASPort.html file in the applet html directory. If the SASPort.html file is created elsewhere then the directory where it is created is specified in the applet html file using an applet parameter SAS_PORT_DIR. If you want to change the default protocol value which is 1, use the method setSASProtocol(int) of SnmpSession before calling SnmpSession.open(applet).
Parameters:
applet - applet reference using this sasclient to talk to SAServer
debug - Debug messages are printed, if true.
Throws:
SnmpException - is thrown whenever an error occurs.
Method Detail

setTimeout

public void setTimeout(int to)
For setting the timeOut value for a synchronous SASClient operation.
Parameters:
to - the timeOut value in milliSeconds.

isFileReadWrite

public static boolean isFileReadWrite()
Checks if the fileWrite flag is true.

setFileReadWrite

public static void setFileReadWrite(boolean writeFlag)
Sets the fileWrite flag.

isConnected

public boolean isConnected()
Indicates if the connection to the SNMP Applet Server has been successfully established.
Returns:
true if the connection is established.

isKnownSASType

public boolean isKnownSASType(int type)
If the value passed is one of the known request types, it returns true.
Parameters:
type - requestType
Returns:
true if one of the known types

getMaxSasTypes

public int getMaxSasTypes()
This method returns the number of known SAS request types and users are requested to use a number greater than the returned value which will keep changing in the future. So it is advisable for the user to use a new request type by incrementing the value for your different requests.
Returns:
int The number of sas request types.

open

void open(int local_port)
open the socket at applet host
Parameters:
local_port - Local Port number

open

void open(int local_port,
          java.lang.String[] localAddresses)

reqTraps

public void reqTraps(int port)
              throws java.io.IOException,
                     TransportException
Method to request delivery of traps by SAServer arriving at that port.
Parameters:
port - the port at which SAServer to listen for traps and forward to this client. Multiple client applications can request for the same port.
Throws:
java.io.IOException - on error in writing to SAServer

send

void send(java.lang.String hostname,
          int port,
          byte[] data)
    throws java.io.IOException,
           TransportException
The send method for sending PDUs
Parameters:
hostname - the host to which the request is to be sent
port - the port to which the request is to be sent
data - the packet to be sent
Throws:
java.io.IOException - on error in writing to SAServer

send

void send(java.lang.String hostname,
          int port,
          byte[] data,
          long timeoutmillis)
    throws java.io.IOException,
           TransportException

saveFile

public void saveFile(java.lang.String filename,
                     byte[] data)
              throws TransportException,
                     java.io.IOException
The saveFile method for saving data to a file in "SASusers" sub-directory in which SASPort.html was created by the SAServer. Lacks error checking at this point.
Parameters:
filename - the file will be saved by this name at the SAServer.
data - the contents of the file.
Throws:
java.io.IOException - on error in writing to SAServer.

appendFile

public void appendFile(java.lang.String filename,
                       byte[] data)
                throws TransportException,
                       java.io.IOException
The appendFile method for appending data to a file in "SASusers" sub-directory in which SASPort.html was created by the SAServer. Lacks error checking at this point.
Parameters:
filename - the data will be appended to file by this name at the SAServer.
data - the contents to be appended to the file.
Throws:
java.io.IOException - on error in writing to SAServer.

connectDB

public void connectDB(java.lang.String driver,
                      java.lang.String url,
                      java.lang.String user,
                      java.lang.String pass)
               throws java.sql.SQLException,
                      java.lang.ClassNotFoundException
This method is for connecting with the database through SAS. This is a synchronous method which waits till the response comes or till it timesout.
Parameters:
driver - the driver class for database.
url - the url where the database exists
user - the username
pass - the password
Throws:
java.sql.SQLException - is thrown when unable to create a Connection.
java.lang.ClassNotFoundException - when the driver class specified is not present.

isDBConnected

public boolean isDBConnected()
                      throws SnmpException
This method is for knowing whether the database connection has been established or not.
Returns:
returns true if the database connection has been successfully established and false otherwise.
Throws:
SnmpException - if any error occurs.

closeDB

public void closeDB()
             throws java.sql.SQLException
This method is for closing the database connection which is opened through the method connectDB method.
Throws:
java.sql.SQLException - thrown when unable to close the database.

queryDB

public java.sql.ResultSet queryDB(java.lang.String queryString)
                           throws java.sql.SQLException
This method is for doing a database query.
Parameters:
queryString - the actual query string that is to be performed on the database.
Returns:
a ResultSet object for the query. This object's next() method will return false, if zero rows are afected by the query.
Throws:
java.sql.SQLException - thrown when a database access error occurs.

updateDB

public int updateDB(java.lang.String updateString)
             throws java.sql.SQLException
This method is for updating the database with a new value.
Parameters:
updateString - the sql query string that is to be performed on the database.
Returns:
either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing.
Throws:
java.sql.SQLException - if a database access error occurs.

start

public void start()
Starts the receiver thread. Use this for receiving non SNMP service responses when SnmpSession thread is not available.

run

public void run()
Invokes receive() method.
Specified by:
run in interface java.lang.Runnable

stop

public void stop()
Stops the receiver thread and closes socket connection with SAServer.

getByteString

static java.lang.String getByteString(byte[] array,
                                      int offset,
                                      int length)

getBytes

static byte[] getBytes(java.lang.String s)

receive

void receive()
       throws SnmpException,
              TransportException,
              java.io.IOException
Used to pick up PDUs from the remote port on the SAS server Blocks until a PDU has been received
Throws:
SnmpException - is thrown on receive errors.
java.io.IOException - is thrown on receive errors.

notifyWaitingThreads

void notifyWaitingThreads()

waitUntilNotification

void waitUntilNotification()

close

void close()
Close the remote session

clientCall

public byte[] clientCall(byte[] bytes)
This method is used to build user defined functions around SAS. SAS supports extension of server/client interaction, and the building of synchronous and asynchronous interactions. This is synchronous when data is returned immediately, and asynchronous when the observer interface is used. Note: Currently asynchrouns communication won't work.
Parameters:
bytes - The byte array data to be sent
Returns:
The response data

userSyncSend

public byte[] userSyncSend(int userType,
                           byte[] bytes)
Using this method the user can send his own request type.
Parameters:
userType - The request type to be sent to SAServer.
bytes - The byte array packet to be sent.
Returns:
The response packet.

getHostName

public java.lang.String getHostName(java.lang.String address,
                                    int timeout)
                             throws TransportException
The name lookup method is for applet clients. This will return the host name for the address. e.g., returns littleangel.com for '192.234.123.56'.
Parameters:
address - the ip address of the host.
timeout - the time in ms to wait for the SAServer to respond. Defaults to 2 Secs.
Returns:
null, on failure.
Throws:
TransportException - on error in writing to SAServer.

getHostAddress

public java.lang.String getHostAddress(java.lang.String hostname,
                                       int timeout)
                                throws TransportException
The ip address lookup method is for applet clients. This will return the address for the host name. e.g., returns 192.234.123.56 for littleangel.com
Parameters:
hostname - the host whose ipaddress needs to be looked up.
timeout - the time in ms to wait for the SAServer to respond. Defaults to 2 Secs.
Returns:
the host address, null if Host Not Found.
Throws:
TransportException - on error in writing to SAServer.

createDir

public void createDir(java.lang.String directory)
               throws TransportException,
                      java.io.IOException
The createDir method is for creating a directory on the applet host. Can specify a relative path to SASusers sub-directory in which SASPort.html was created by the SAServer.
Parameters:
directory - The name of the directory to be created
Throws:
java.io.IOException - on error in Creating Directory

deleteFile

public void deleteFile(java.lang.String filename)
                throws TransportException,
                       java.io.IOException
The deleteFile method is for deleting a file on the applet host.
Parameters:
filename - The name of the file to be deleted.
Throws:
java.io.IOException - on error in Deleting File .

deleteDir

public void deleteDir(java.lang.String directory)
               throws TransportException,
                      java.io.IOException
The deleteDir method for deleting a directory on the applet host. It will not delete a directory if that is not empty.
Parameters:
directory - The name for the directory to be deleted.
Throws:
java.io.IOException - on error in Deleting Directory.

setTransportProvider

void setTransportProvider(java.lang.String providerName)
                    throws java.lang.Exception

convertIntToByteArray

byte[] convertIntToByteArray(int i)

convertByteArrayToInt

int convertByteArrayToInt(byte[] bArr)
                    throws java.io.IOException

convertByteArrayToInt

int convertByteArrayToInt(byte[] bArr,
                          int startIndex)
                    throws java.io.IOException

getData

byte[] getData(java.util.Vector vec)

getClientID

int getClientID()