done task 1 of week 3 - distributed systems

This commit is contained in:
2025-12-21 00:57:44 +01:00
parent 27259a12e0
commit 9687607237
7 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
package org.distributed.client;
public class Client {
}

View File

@@ -0,0 +1,12 @@
package org.distributed.engine;
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 {
// needs a task interface
int executeTask(Operation t) throws RemoteException;
}

View File

@@ -0,0 +1,24 @@
package org.distributed.engine;
public class Operation implements Task<Integer> {
int a,b;
String operand;
public Operation(int a, int b, String operand){
this.a = a;
this.b = b;
this.operand = operand;
}
public Integer execute() {
return switch (this.operand) {
case "+" -> a + b;
case "-" -> a - b;
case "*" -> a * b;
case "/" -> a / b;
default -> a % b;
};
}
}

View File

@@ -0,0 +1,15 @@
package org.distributed.compute;
/**
* This interface is the type of parameter to the executeTask() method of the
* Compute interface
* it defines the interface between the compute engine
* and the work that it needs to do, providing the way to start the work
* It defines a single method execute() whose return type is T
* The interface itself also has a type T, which represents the result type of
* the task's computation
*/
public interface Task<T> {
T execute();
}

View File

@@ -0,0 +1,17 @@
package org.distributed;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ComputeEngine implements Compute {
public ComputeEngine() {
super();
}
@Override
public <T> T executeTask(Task<T> t) throws RemoteException {
return t.execute();
}
}