X 1 0 S e r v e r C l i e n t
J e s s e  P e t e r s o n



X10ServerClient is an implementation of a program that interacts with an X10Server running locally or on a remote machine. The X10ServerClient provides a command line interface to submit commands to the X10Server.

X10ServerClient can be started from the command line by issuing the following command:

java X10ServerClient hostname [port]

Where:

 
hostname The name or IP address of the host running the X10Server
port TCP port that the X10Server is listening for client connections on. If not specified, the default value of 4377 is used.
Note: The jar file x10.jar must be in the CLASSPATH when running X10ServerClient.

The command line interface provided by X10ServerClient will accept all of the commands supported by the X10Server. See the X10Server documentation available in the "docs" directory for more information on the available commands and their syntax.

When started, the program will prompt for command entry by displaying a '$' prompt. Enter your commands followed by the "Enter" key (or your systems equivalent). The command will be transmitted to the X10Server and processed. If an error occurs, it will be displayed. You should then be prompted with a '$' indicating the client readiness to accept more input. When done, you may type "QUIT" or press "Ctrl-C" (or your systems equivalent) to terminate the program.

Example

To turn a lamp on with house code 'B' and device code '3', the following command would be executed.

$(B03)(BON)
To turn the lamp off, the following command would be executed.
$(B03)(BOF)


Code

The source code for X10ServerClient.java can be found in the samples/X10ServerClient directory.

Within the main method of X10ServerClient, the optional TCP port parameter is read. If no port is specified, the default value is used. A new instance of the X10ServerClient is created with the specified host or IP address taken from the first parameter and the TCP port number. After creating the object, the service method is called.

When the X10ServerClient's constructor is called, an X10ServerSocketObjectPool is created. The pool is created with the host and port supplied in the X10ServerClient's constructor. It is used later in the service method. The pool will manage connecting to the X10Server, allowing the client to focus on its job of providing a command line interface and not worry about the network housekeeping.

The service method is mainly one continual loop. The command line prompt, which is the character '$', is initially displayed. The program will wait for a command line to be entered. If the command is the word "QUIT", the program terminates. Otherwise, a socket is retrieved from the pool. A check to make sure that the socket is not null is required. The returned object from the pool may be null if the pool is unable to grant your request. If the returned object is not null, it is cast to a Socket object. This safe because by definition, the X10ServerSocketObjectPool will create a class of the Socket object. The input stream and output stream are retrieved from the socket. The output stream is where commands are sent to the X10Server by writing the commands out on the stream. The input stream is where the client can read the error messages, if any, which are sent back from the X10Server. The client handles the response by continually reading from the input stream until the prompt character '$' is read. If only the prompt character is read, this signifies that there were no errors. If more data than just the prompt character '$' is read, this indicates an error. If there is an error, is printed to System.err. Then, the prompting process is started all over.

If there is even an IOException while trying to prompt, the socket is returned to the pool as broken. Otherwise, when the program terminates through a "QUIT" command, the socket is returned to the pool ready to be used again.



Copyright © 1999 Jesse Peterson