com.adventnet.snmp.snmp2
Class SASClient

java.lang.Object
  |
  +--java.util.Observable
        |
        +--com.adventnet.snmp.snmp2.SASClient
All Implemented Interfaces:
java.lang.Runnable, java.io.Serializable

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
static int HTTP_PROTOCOL
           
static int TCP_PROTOCOL
           
 
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.
 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.
 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.
 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.
 java.sql.ResultSet queryDB(java.lang.String queryString)
          This method is for doing a database query.
 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.
static void setFileReadWrite(boolean writeFlag)
          Sets the fileWrite flag.
 void setTimeout(int to)
          For setting the timeOut value for a synchronous SASClient operation.
 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.
 
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

TCP_PROTOCOL

public static final int TCP_PROTOCOL

HTTP_PROTOCOL

public static final int HTTP_PROTOCOL
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.

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

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.

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.


Copyright (c)AdventNet Inc., 1996-2004