
remotecons package.
You can create and initialize new console server with instance of this class.
Look below for sample code. The simple code for initializing remote console server is given in main() method. If you want to start console server first without your own command handler and on default server port your code may looks like:
// Don't forget to 'import remotecons.RemoteServer;'
RemoteServer rs = new RemoteServer();
// rs.setAs_daemon(false); // Uncomment this line if it is only code in your app.
// rs.setServer_port(2702); // Uncomment this line to set different port number
try {
rs.initialize(null);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} // end of try-catch
Now if you run your application it will be listening on default port: 1612 and waiting for incoming connection requests. If you just created simple java class containing only above code in main() method your application will exit immediately. To prevent this uncomment line setting daemon mode to false.<br/> You can run telnet and try to connect to it with command: $ telnet localhost 1612Be aware of putting correct port number! Output should looks like following:
Trying 10.1.0.153... Connected to ahe.nutech.com.pl. Escape character is '^]'. 10.1.0.153 <<It means that remote console is ready for receiving your commands. Don't waste your time searching how finish connection. :-) Use 'quit' command to disconnect from remote console. And note that there is another similar command 'exit' which causes killing JVM where our remote console works.<br/> Your first command should be 'help'. You will get information about all available commands for user which are supported by all loaded command handlers.<br/> Output of 'help' command looks like following:
remotecons.wttools.ConnectionServer$InternalCommands >>
echo - switch 'echo' mode (for windows telnet is really needed)
quit - close connection to server
who - list all active connections
close n - close active connection number 'n'
help [command] - display this message info
ls [dir]* - list content of given directory
show filename - display content of given file
remotecons.wttools.CommandHandlerImpl >>
time - display current time on server
gc - run System.gc() command on remote system
mem [total|free] - display amount of total/free memory on remote system
exit [n] - call System.exit(n) on remote system
get [params]+ - complex command require additional parameters,
call 'help get' for more info. (Not implemented yet.)
set [params]+ - complex command require additional parameters,
call 'help set' for more info. (Not implemented yet.)
exec [params]+ - run Runtime.exec(String[] params) command on remote system
10.1.0.153 <<
You can see two command handlers loaded 'remotecons.wttools.ConnectionServer$InternalCommands' and 'remotecons.wttools.CommandHandlerImpl' each with its own set of commands.
Although there are several commands available in buildin handlers, remote console can be really useful only if you can use your own command handlers for accessing your own objects at running time.<br/> To do this look in sample code below. The simplest possible command handler must look like following:
// Don't forget to 'import remotecons.ifc.CommandHandlerIfc;'
public class SampleCommandHandler implements CommandHandlerIfc {
public String handleCommand(String comm) {
if (comm.trim().equals("hello")) {
return "Hello world!
";
} // end of if (comm.trim().equals("hello"))
return null;
}
public String help() {
return"
hello - Returns 'Hello world!' string as a result.
";
}
public String help(String comm) {
return "";
}
public CommandHandlerIfc getInstance() {
return new SampleCommandHandler();
}
}
To add your new created command handler your above code initializing remote console must be changed to following:
// Don't forget to 'import remotecons.RemoteServer;'
// Don't forget to 'import your new SampleCommandHandler;' // NEW LINE !!!
// Don't forget to 'import java.util.LinkedList;' // NEW LINE !!!
RemoteServer rs = new RemoteServer();
// rs.setAs_daemon(false); // Uncomment this line if it is only code in your app.
// rs.setServer_port(2702); // Uncomment this line to set different port number
LinkedList ll = new LinkedList(); // NEW LINE !!!
ll.add(new SampleCommandHandler()); // NEW LINE !!!
try {
rs.initialize(ll); // CHANGED LINE !!!
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} // end of try-catch
Try now to run your application and telnet to it. After running help command you should now get following output:
remotecons.wttools.ConnectionServer$InternalCommands >>
echo - switch 'echo' mode (for windows telnet is realy needed)
quit - close connection to server
who - list all active connections
close n - close active connection number 'n'
help [command] - display this message info
ls [dir]* - list content of given directory
show filename - display content of given file
remotecons.wttools.CommandHandlerImpl >>
time - display current time on server
gc - run System.gc() command on remote system
mem [total|free] - display amount of total/free memory on remote system
exit [n] - call System.exit(n) on remote system
get [params]+ - complex command require additional parameters,
call 'help get' for more info. (Not implemented yet.)
set [params]+ - complex command require additional parameters,
call 'help set' for more info. (Not implemented yet.)
exec [params]+ - run Runtime.exec(String[] params) command on remote system
SampleCommandHandler >>
hello - Returns 'Hello world!' string as a result.
10.1.0.153 <<
As you can see your command handler was added to all command handler lists and your supported command is now visible. Try to run your command.<br/> You should get result string: 10.1.0.153 << hello Hello world! 10.1.0.153 << quit Bye Connection closed by foreign host.I hope this is all you should know to use this library. I hope it will be useful.
Created: Thu Jan 10 15:07:40 2002
Definition at line 184 of file RemoteServer.java.
Public Methods | |
| void | initialize (List command_handlers) throws IOException |
Method initialize initializes SocketListener on default or earlier set port number and with given external command handlers. | |
| void | initialize (int port_no, List command_handlers) throws IOException |
Method initialize initializes SocketListener on given port number and with given command handlers. | |
| void | asyncMessage (String msg) |
asyncMessage method was added for support asynchronously sending messages from server to all connected clients. | |
| void | disable () |
Method disable turns off socket listener but not terminates current connections performed with remote users. | |
| int | getServer_port () |
| Gets the value of server_port. | |
| void | setServer_port (int argServer_port) |
| Sets the value of server_port. | |
| boolean | isAs_daemon () |
| Gets the value of as_daemon. | |
| void | setAs_daemon (boolean argAs_daemon) |
| Sets the value of as_daemon. | |
Static Public Methods | |
| void | main (String[] args) |
Method main is here only for test purpose and sample code only. | |
Protected Attributes | |
| int | server_port = Constants.DEFAULT_SERVER_PORT |
Variable server_port contains value of socket server port number to listen to. | |
| boolean | as_daemon = true |
Variable as_daemon works as switch for running remote console threads as daemons or as primary threads. | |
| SocketListener | s_listener = null |
Variable s_listener keeps instance of SocketListener object which is responsible for listening on server socket and handling new connection requests from users. | |
|
|
Please beware of using this feature.
Your application may not know how many remote consoles are active and in what states are they. But if you use this method you will send asynchronously message to connected client. It is application responsibility to not send Definition at line 265 of file RemoteServer.java. |
|
|
Method
Too terminate all remote connections you must first connect to remote console, list all connections with Definition at line 282 of file RemoteServer.java. |
|
|
Gets the value of server_port.
Definition at line 326 of file RemoteServer.java. |
|
||||||||||||
|
Method
Definition at line 242 of file RemoteServer.java. |
|
|
Method
Definition at line 223 of file RemoteServer.java. |
|
|
Gets the value of as_daemon.
Definition at line 345 of file RemoteServer.java. |
|
|
Method It is not necessary but may be good reference for creating your own code for starting remote console in your software.
Definition at line 293 of file RemoteServer.java. |
|
|
Sets the value of as_daemon.
Definition at line 354 of file RemoteServer.java. |
|
|
Sets the value of server_port.
Definition at line 335 of file RemoteServer.java. |
|
|
Variable If you run remote console threads as a primary threads you application will not finish its work untill all threads terminate. If they are daemon threads your application will finish its work if there are no more other threads. Definition at line 202 of file RemoteServer.java. |
|
|
Variable
Definition at line 209 of file RemoteServer.java. |
|
|
Variable
You must set this variable before initializing socket listener. To do it use
Definition at line 194 of file RemoteServer.java. |
1.3-rc2