Dear friends,
The following is the code for a server program which uses Sun RPC.
package Server;
import java.io.IOException;
import java.net.InetAddress;
import org.acplt.oncrpc.OncRpcAuthStatus;
import org.acplt.oncrpc.OncRpcAuthType;
import org.acplt.oncrpc.OncRpcAuthenticationException;
import org.acplt.oncrpc.OncRpcException;
import org.acplt.oncrpc.OncRpcTcpClient;
import org.acplt.oncrpc.XdrBufferDecodingStream;
import org.acplt.oncrpc.XdrBufferEncodingStream;
import org.acplt.oncrpc.XdrString;
import org.acplt.oncrpc.XdrVoid;
import org.acplt.oncrpc.server.OncRpcCallInformation;
import org.acplt.oncrpc.server.OncRpcDispatchable;
import org.acplt.oncrpc.server.OncRpcServerAuthShort;
import org.acplt.oncrpc.server.OncRpcServerAuthUnix;
import org.acplt.oncrpc.server.OncRpcUdpServerTransport;
public class RpcServer implements OncRpcDispatchable {
public class ShutdownHookThread extends Thread{
private RpcServer svr;
public ShutdownHookThread(RpcServer svr) {
this.svr = svr;
}
public void run() {
System.out.println("Shutdown Hook Thread activated");
svr.shutdown();
}
}
//
// Flag to signal shut down of the server.
//
public Object leaveTheStage = new Object();
//
// Shorthand credential use counter.
//
public int shorthandCredCounter = 0;
public RpcServer()throws OncRpcException, IOException {
OncRpcUdpServerTransport udpTrans = new OncRpcUdpServerTransport(this, 5555, 0x49679, 1, 8192);
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(this));
udpTrans.register();
System.out.println("Server started.");
udpTrans.listen();
System.out.println("PORT " + udpTrans.getPort());
//
// Reality Check: open a connection to the TCP/IP server socket
// waiting for incoming connection, thus testing proper shutdown
// behavior later...
//
OncRpcTcpClient client = new OncRpcTcpClient(
InetAddress.getByName("127.0.0.1"), 0x49679, 1, 0);
//
// Now wait for the shutdown to become signaled... Note that
// a simple call to wait() without the outer loop would not be
// sufficient as we might get spurious wake-ups due to
// interruptions.
//
for ( ;; ) {
synchronized ( leaveTheStage ) {
try {
leaveTheStage.wait();
break;
} catch ( InterruptedException e ) {
}
}
}
System.out.println("Server shutting down...");
//
// Unregister UDP-based transport. Then close it. This will
// automatically bring down the thread which handles the
// UDP transport.
//
udpTrans.unregister();
udpTrans.close();
System.out.println("Server shut down.");
}
//
// Indicate that the server should be shut down. Sad enough that the
// Java Environment can not cope with signals. I know that they're not
// universally available -- but why shouldn't be there a class providing
// this functionality and in case the runtime environment does not support
// sending and receiving signals, it either throws an exception or gives
// some indication otherwise. It wouldn't be bad and we would be sure
// that the appropriate class is always available.
//
public void shutdown() {
if ( leaveTheStage != null ) {
System.out.println("Requesting server to shut down...");
synchronized ( leaveTheStage ) {
leaveTheStage.notify();
}
}
}
//
// Handle incomming calls...
//
public void dispatchOncRpcCall(OncRpcCallInformation call,
int program, int version, int procedure)
throws OncRpcException, IOException {
//
// Spit out some diagnosis messages...
//
System.out.println("Incomming call for program "
+ Integer.toHexString(program)
+ "; version " + version
+ "; procedure " + Integer.toHexString(procedure)
+ "; auth type " + call.callMessage.auth.getAuthenticationType());