/*
*
* Copyright (c) 2000 Scott Oaks and Henry Wong. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and
* without fee is hereby granted.
*
* This sample source code is provided for example only,
* on an unsupported, as-is basis.
*
* AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). AUTHOR
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.io.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;
import net.jini.discovery.*;
import com.sun.jini.lookup.*;
public class ConvertServiceImpl implements ConvertService, Serializable, Runnable {
// Make this transient, so on all clients it will be false (the default
// serialization value
transient boolean isServer;
String remoteHost; // Host where server socket is running
int remotePort; // Port of server socket
ConvertServiceImpl(String host, int port) {
remoteHost = host;
remotePort = port;
isServer = true;
}
public String convert(int i) throws RemoteException {
Socket sock;
DataOutputStream dos;
BufferedReader br;
String s;
try {
// Construct the socket to the remote server. Send it the
// integer and read the returned string. This should only
// be called by the client.
if (isServer)
throw new IllegalArgumentException("Can't call from server");
sock = new Socket(remoteHost, remotePort);
dos = new DataOutputStream(sock.getOutputStream());
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
dos.writeInt(i);
dos.flush();
s = br.readLine();
sock.close();
return s;
} catch (Exception e) {
throw new RemoteException("Convert failed", e);
}
}
public void run() {
try {
while (true) {
// On the server, start the server socket and process results
if (!isServer)
throw new IllegalArgumentException("Should only run on server");
ServerSocket ss = new ServerSocket(remotePort);
while (true) {
Socket s;
try {
s = ss.accept();
} catch (Exception e) {
// Resource error on the server; can't recover
return;
}
DataInputStream dis = new DataInputStream(s.getInputStream());
PrintWriter pw = new PrintWriter(s.getOutputStream());
Integer I = new Integer(dis.readInt());
pw.println(I);
pw.flush();
s.close();
}
} catch (Exception e) {
// Client disconnected; go get the next client
}
}
}
public static void main(String[] args) throws Exception {
System.setSecurityManager(new SecurityManager());
String[] groups = new String[] { "" };
// Create the instance of the service; the JoinManager will
// register it and renew its leases with the lookup service
ConvertServiceImpl csi = (ConvertServiceImpl) new ConvertServiceImpl(InetAddress.getLocalHost().getHostName(), 3333);
new Thread(csi).start();
JoinManager manager = new JoinManager(csi, null, groups,
null, null, null);
}
}
|