[CT414]: Add RMI sample code
This commit is contained in:
BIN
year4/semester2/CT414/materials/code/01-rmisample/.DS_Store
vendored
Normal file
BIN
year4/semester2/CT414/materials/code/01-rmisample/.DS_Store
vendored
Normal file
Binary file not shown.
@ -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) {}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
1
year4/semester2/CT414/materials/code/01-rmisample/start-client.sh
Executable file
1
year4/semester2/CT414/materials/code/01-rmisample/start-client.sh
Executable file
@ -0,0 +1 @@
|
||||
java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample CityClient
|
1
year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh
Executable file
1
year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh
Executable file
@ -0,0 +1 @@
|
||||
rmiregistry -J-Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/
|
1
year4/semester2/CT414/materials/code/01-rmisample/start-server.sh
Executable file
1
year4/semester2/CT414/materials/code/01-rmisample/start-server.sh
Executable file
@ -0,0 +1 @@
|
||||
java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/ CityServerImpl
|
Reference in New Issue
Block a user