[CT414]: Add RMI sample code

This commit is contained in:
2025-01-21 10:13:14 +00:00
parent ba430091f2
commit 6a761c728c
20 changed files with 428 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,15 @@
import java.rmi.*;
public class CityClient
{
public static void main (String args[])
{
try {
String target = (args.length == 0) ? "Ireland" : args[0];
CityServer cities = (CityServer) Naming.lookup("//localhost/Capitals");
String capital = cities.getCapital(target);
System.out.println(capital);
}
catch (Exception e) {}
}
}

View File

@ -0,0 +1,8 @@
// Remote Object has a single method that is passed
// the name of a country and returns the capital city.
import java.rmi.*;
public interface CityServer extends Remote
{
String getCapital(String Country) throws RemoteException;
}

View File

@ -0,0 +1,54 @@
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class CityServerImpl implements CityServer
{
// constructor is required in RMI
CityServerImpl() throws RemoteException
{
super(); // call the parent constructor
}
// Remote method we are implementing!
public String getCapital(String country) throws RemoteException
{
System.out.println("Sending return string now - country requested: " + country);
if (country.toLowerCase().compareTo("usa") == 0)
return "Washington";
else if (country.toLowerCase().compareTo("ireland") == 0)
return "Dublin";
else if (country.toLowerCase().compareTo("france") == 0)
return "Paris";
return "Don't know that one!";
}
// main is required because the server is standalone
public static void main(String args[])
{
try
{
// First reset our Security manager - deprecated so no longer used
//if (System.getSecurityManager() == null) {
// System.setSecurityManager(new SecurityManager());
// System.out.println("Security manager set");
// }
// Create an instance of the local object
CityServer cityServer = new CityServerImpl();
System.out.println("Instance of City Server created");
CityServer stub = (CityServer) UnicastRemoteObject.exportObject(cityServer, 0);
// Put the server object into the Registry
// Naming.rebind("Capitals", stub);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("Capitals", stub);
System.out.println("Name rebind completed");
System.out.println("Server ready for requests!");
}
catch(Exception exc)
{
System.out.println("Error in main - " + exc.toString());
}
}
}

View File

@ -0,0 +1 @@
java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample CityClient

View File

@ -0,0 +1 @@
rmiregistry -J-Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/

View File

@ -0,0 +1 @@
java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/ CityServerImpl