started task 7
This commit is contained in:
BIN
week3_TinsaeGhilay/Task 3/out/client/Client.class
Normal file
BIN
week3_TinsaeGhilay/Task 3/out/client/Client.class
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 3/out/hello/Hello.class
Normal file
BIN
week3_TinsaeGhilay/Task 3/out/hello/Hello.class
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 3/out/server/Server.class
Normal file
BIN
week3_TinsaeGhilay/Task 3/out/server/Server.class
Normal file
Binary file not shown.
41
week3_TinsaeGhilay/Task 3/src/client/Client.java
Normal file
41
week3_TinsaeGhilay/Task 3/src/client/Client.java
Normal file
@@ -0,0 +1,41 @@
|
||||
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 hello.Hello;
|
||||
|
||||
// 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];
|
||||
String name = "Hello";
|
||||
|
||||
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
|
||||
Hello stub = (Hello) registry.lookup(name);
|
||||
String response = stub.sayHello();
|
||||
System.out.println("response: " + response);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Client exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
12
week3_TinsaeGhilay/Task 3/src/hello/Hello.java
Normal file
12
week3_TinsaeGhilay/Task 3/src/hello/Hello.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package hello;
|
||||
|
||||
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 Hello extends Remote {
|
||||
|
||||
// say hello method, This is a repeat of task 1(done as part of learning RMI)
|
||||
String sayHello() throws RemoteException;
|
||||
}
|
||||
56
week3_TinsaeGhilay/Task 3/src/server/Server.java
Normal file
56
week3_TinsaeGhilay/Task 3/src/server/Server.java
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
package server;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
// 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 hello.Hello;
|
||||
|
||||
/**
|
||||
* 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 Hello {
|
||||
|
||||
public Server() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sayHello() throws RemoteException {
|
||||
return "Hello, world!";
|
||||
}
|
||||
|
||||
// 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 {
|
||||
String name = "Hello";
|
||||
// create remote object that provides service (server).
|
||||
Hello server = new Server();
|
||||
|
||||
// and export remote object to java RMI runtime
|
||||
// so it may receive remote incoming calls
|
||||
Hello stub = (Hello) 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.getRegistry();
|
||||
registry.rebind(name, stub);
|
||||
System.out.println("Server bound");
|
||||
} catch (Exception e) {
|
||||
System.err.println("Server exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user