started task 7
This commit is contained in:
BIN
week3_TinsaeGhilay/Task 4/out/client/Client.class
Normal file
BIN
week3_TinsaeGhilay/Task 4/out/client/Client.class
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 4/out/compute/Compute.class
Normal file
BIN
week3_TinsaeGhilay/Task 4/out/compute/Compute.class
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 4/out/server/Server.class
Normal file
BIN
week3_TinsaeGhilay/Task 4/out/server/Server.class
Normal file
Binary file not shown.
44
week3_TinsaeGhilay/Task 4/src/client/Client.java
Normal file
44
week3_TinsaeGhilay/Task 4/src/client/Client.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package client;
|
||||
|
||||
// Client/Client.java class represents the client that has to relay on the Hello engine
|
||||
// to perform tasks remotely.
|
||||
// it needs the Hello interface from Hello package
|
||||
import compute.Compute;
|
||||
|
||||
// it also needs those to access the registry,
|
||||
// so it can get remote object reference of the server
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Client {
|
||||
private Client() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// normally, the address of the server is passed here on the command line as an
|
||||
// argument
|
||||
// we get that address from args[0] if provided, else we use null for localhost
|
||||
String host = (args.length < 1) ? null : args[0];
|
||||
|
||||
try (Scanner scanner = new Scanner(System.in);) {
|
||||
|
||||
// get the registry from host(server)
|
||||
Registry registry = LocateRegistry.getRegistry(host);
|
||||
|
||||
// look up the remote object by name in the registry
|
||||
// stub is the a reference to the remote object
|
||||
Compute stub = (Compute) registry.lookup(Compute.SERVICE_NAME);
|
||||
boolean isAuthenticated = stub.authenticate();
|
||||
if (isAuthenticated) {
|
||||
System.out.println("Client authenticated successfully. Door opens.");
|
||||
} else {
|
||||
System.out.println("Client authentication failed. Door remains closed.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Client exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
13
week3_TinsaeGhilay/Task 4/src/compute/Compute.java
Normal file
13
week3_TinsaeGhilay/Task 4/src/compute/Compute.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package compute;
|
||||
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
// this interface extends the interface "java.rmi.Remote"
|
||||
// so its methods can be invoked from another Java virtual machine
|
||||
public interface Compute extends Remote {
|
||||
static final String SERVICE_NAME = "authenticator";
|
||||
|
||||
// say hello method, This is a repeat of task 1(done as part of learning RMI)
|
||||
boolean authenticate() throws RemoteException;
|
||||
}
|
||||
65
week3_TinsaeGhilay/Task 4/src/server/Server.java
Normal file
65
week3_TinsaeGhilay/Task 4/src/server/Server.java
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
package server;
|
||||
|
||||
/**
|
||||
* Server implementation for the Compute interface
|
||||
* @author Tinsae Ghilay
|
||||
*
|
||||
* mimics an authentication service for some client to access a secure resource
|
||||
* (mocked as a door that opens or remains closed based on authentication result)
|
||||
* verification is done randomly for demo purposes
|
||||
*/
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.Random;
|
||||
|
||||
// needs the compute and task interfaces from compute package
|
||||
// define constructor for each
|
||||
// and provide an implementation for each remote method in the remote interfaces
|
||||
import compute.Compute;
|
||||
|
||||
/**
|
||||
* The implementation of the Compute interface for the compute engine.
|
||||
* it has to declare the remote class to be implemented (in this case Compute
|
||||
* (implements it)
|
||||
*/
|
||||
public class Server implements Compute {
|
||||
|
||||
public Server() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean authenticate() throws RemoteException {
|
||||
return new Random().nextBoolean();
|
||||
}
|
||||
|
||||
// entry
|
||||
public static void main(String[] args) {
|
||||
|
||||
// the code provided on the tutorial was deprecated.
|
||||
// so google led me to
|
||||
// [this](https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/hello/hello-world.html#create)
|
||||
try {
|
||||
// create remote object that provides service (server).
|
||||
Compute server = new Server();
|
||||
|
||||
// and export remote object to java RMI runtime
|
||||
// so it may receive remote incoming calls
|
||||
Compute stub = (Compute) UnicastRemoteObject.exportObject(server, 0);
|
||||
// the register the remote object with java RMI registry
|
||||
// the registry is a name service that allows clients get a reference of remote
|
||||
// objects
|
||||
// once a remote object is registered, callers can look up the object by name
|
||||
// and obtain its remote object reference
|
||||
Registry registry = LocateRegistry.createRegistry(1099);
|
||||
registry.rebind(Compute.SERVICE_NAME, stub);
|
||||
System.out.println("Server bound");
|
||||
} catch (Exception e) {
|
||||
System.err.println("Server exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user