Asynchronous Communication through RMI

 

Description

 

This sample application makes use of CLI API to create either a dedicated or a multiplexed (non-dedicated) CLISession with devices. The application works on the top of RMI interface.

 

Format

 

java RmiCliAsyncApp [-s session(d/nd)] [-n RemotePort] [-cp CmdPrompt] [-lp LoginPrompt] [-pp PasswordPrompt] [-l LoginName] [-p Password ]

[-d Debug] [-rh rmiserver host] [-rp rmiserver port] RemoteHost Command

 

Options

 

[-s

-

Session, dedicated (d) or non-dedicated (nd). Default is dedicated.

[-n

-

Remote port. The default is the telnet port (23)

[-cp]

-

Command prompt. This is the prompt displayed by the device for each command. Default is $.

[-lp]

-

The Login prompt. This is the prompt that the device issues for getting the user name. Default is 'login:'.

[-pp]

-

The Password prompt. This is the prompt that is issued by the device for getting the password. Default is 'Password:'.

[-d]

-

To set debug as true or false.

[-l]

-

Login Name, One of the user names/Login names present in the remote host.

[-p]

-

Password for the user.

[-rh]

-

The remote host name on which the rmiregistry (CLIFactoryImpl) is running.

[-rp]

-

The remote port to which the rmiregistry is listening Default is 1099.

Remote host

M

The host to which the session has to be established.

Command

M

The actual command that has to be sent to the device.

 

Note:

1. M - Mandatory

2. Before connecting the device, start the rmiregistry and run com.adventnet.cli.rmi.CLIFactoryImpl

 

Initially, import the following packages:

 

com.adventnet.cli.*;

com.adventnet.cli.transport.*;

com.adventnet.cli.messageset.*;

com.java.io.*;

com.adventnet.cli.rmi.* ;

 

In asynchronous communication, you have to implement CLI Client interface so that it can receive messages in the callback method.

 

The main function consists of the following declaration.

 

public static void main(String args[]){

String usage = "java RmiCliSyncApp [-s session(d/nd)] [-n RemotePort]

[-cp CmdPrompt] [-lp LoginPrompt ] [-pp PasswordPrompt ] [-l LoginName] [-p Password ] [-d Debug ] [-rh rmiserver host] [-rp rmiserver port] RemoteHost Command";

String options[] = {"-s", "-n", "-cp", "-lp", "-pp", "-l", "-p", "-d", "-rh", "-rp"};

String values[] = { null, null, null, null, null, null, null, "None", null, null };

String cmdData = null;

String rmiHost = "localhost";

String rmiPort = "1099";

boolean enablePooling=false;

boolean isDebug = false;

 

ParseOptions class is used to check the arguments passed to the application. If there is any syntax error, the class would throw an exception else it will continue by opening a session. This is done to prevent wrong arguments passed to the device.

 

ParseOptions opt = new ParseOptions(args,options,values,usage);

if(opt.remArgs.length<2){

opt.usage_error();

 

Since Telnet is used as the transport provider,construct the ProtocolOptions for Telnet connections. Telnet protocol options contain the RemoteHost/RemotePort and other parameters to be set.

 

TelnetProtocolOptionsImpl tpoi = new TelnetProtocolOptionsImpl();

 

The following parameters are set to the Telnet protocol implementation class so that the transport provider can connect to the device for communication.

 

tpoi.setRemotePort(Integer.parseInt(values[1]));

tpoi.setPrompt(values[2]);

tpoi.setLoginPrompt(values[3]);

tpoi.setPasswdPrompt(values[4]);

tpoi.setLoginName(values[5]);

tpoi.setPassword(values[6]);

isDebug = true ;

rmiHost = values[8];

rmiPort = values[9];

tpoi.setRemoteHost(opt.remArgs[0]);

cmdData = opt.remArgs[1];

 

Create CLIMessage class that contains the CLI command. Instantiate this class with the message to be sent and pass it to send () in CLISession class.

 

CLIMessage climsg = new CLIMessage(" ");

 

Declare a session interface available with the com.adventnet.cli.rmi package for creating remote server objects from RMI client to the RMI server.

 

com.adventnet.cli.rmi.CLISession clisession = null;

RmiAsyncApp rmicliApp = new RmiAsyncApp();

 

To create a remote object from an RMI client, you have to look at the services in the RMI registry for which the corresponding server must have created a connection. If the remote server host and port matches, the client establishes connection and thereby uses the services provided by the remote server.

 

CLIFactory CLIFactory = (com.adventnet.cli.rmi.CLIFactory) java.rmi.Naming.lookup("rmi://"+opt.remArgs[4]+":"+rmiPort + "/AdventnetCLIFactory"); CLIsession = CLIFactory.createCLISession(tpoi,enablePooling); CLIsession.setMaxconnections(3);

 

Set the transport provider implementation before opening the session by using the session.setTransportProviderClassName(String ClassName). In case you create your own implementation, pass the corresponding filename as parameter.

 

clisession.setTransportProviderClassName

("com.adventnet.cli.transport.TelnetTransportImpl"); CLIsession.open();

 

You can register with CLISession for receiving the asynchronous response.

 

java.rmi.server.UnicastRemoteObject.exportObject(rmicliApp); CLIsession.addCLIClient(rmicliApp);

 

Now, send the CLI commands to the device from the CLI interface and get responses.

 

clisession.send(CLImessage);

 

The callback method must be used whenever the application sends messages in asynchronous mode. All messages will be received in the same callback method. To get the response for a particular CLI message, the parameter message ID can be used.

 

public boolean callback(com.adventnet.cli.rmi.CLISession session, CLIMessage msg,int msgId)

{

if(msg!=null)

{

System.out.println(msg.getData());

try{

session.close();

} catch ( Exception e ){}

System.exit(1);

} else {

System.out.println(" No response received ");

try{

session.close();

}catch(Exception e){}

System.exit(1);

}

return true;

}

 

The complete source code is available in <CLI Home>/examples/rmi directory.