diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/in/web/TaskAvailableController.java b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/in/web/TaskAvailableController.java index 14dc3e6..eecc4a3 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/in/web/TaskAvailableController.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/in/web/TaskAvailableController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController; import ch.unisg.executor1.executor.application.port.in.TaskAvailableCommand; import ch.unisg.executor1.executor.application.port.in.TaskAvailableUseCase; +import ch.unisg.executor1.executor.domain.ExecutorType; @RestController public class TaskAvailableController { @@ -20,10 +21,13 @@ public class TaskAvailableController { @GetMapping(path = "/newtask/{taskType}") public ResponseEntity retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) { - TaskAvailableCommand command = new TaskAvailableCommand(taskType); - - taskAvailableUseCase.newTaskAvailable(command); + if (ExecutorType.contains(taskType.toUpperCase())) { + TaskAvailableCommand command = new TaskAvailableCommand( + ExecutorType.valueOf(taskType.toUpperCase())); + taskAvailableUseCase.newTaskAvailable(command); + } + // Add the content type as a response header HttpHeaders responseHeaders = new HttpHeaders(); diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedAdapter.java b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedEventAdapter.java similarity index 91% rename from executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedAdapter.java rename to executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedEventAdapter.java index e1c3a5d..41e68a6 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedAdapter.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/ExecutionFinishedEventAdapter.java @@ -13,7 +13,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort; import ch.unisg.executor1.executor.domain.ExecutionFinishedEvent; -public class ExecutionFinishedAdapter implements ExecutionFinishedEventPort { +public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort { //This is the base URI of the service interested in this event (in my setup, running locally as separate Spring Boot application) String server = "http://127.0.0.1:8082"; @@ -51,7 +51,7 @@ public class ExecutionFinishedAdapter implements ExecutionFinishedEventPort { } **/ - System.out.println("Finish execution event sent"); + System.out.println("Finish execution event sent with result:" + event.getResult()); } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/GetAssignmentAdapter.java b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/GetAssignmentAdapter.java index 2c32f84..c7d8485 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/GetAssignmentAdapter.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/GetAssignmentAdapter.java @@ -8,6 +8,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort; +import ch.unisg.executor1.executor.domain.ExecutorType; import ch.unisg.executor1.executor.domain.Task; @Component @@ -18,7 +19,7 @@ public class GetAssignmentAdapter implements GetAssignmentPort { String server = "http://127.0.0.1:8082"; @Override - public Task getAssignment(String executorType) { + public Task getAssignment(ExecutorType executorType) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() @@ -38,7 +39,7 @@ public class GetAssignmentAdapter implements GetAssignmentPort { // TODO return null or a new Task here depending on the response of the http call - return new Task("1234"); + return null; } } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/NotifyExecutorPoolAdapter.java b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/NotifyExecutorPoolAdapter.java index 866ab91..d7a9238 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/NotifyExecutorPoolAdapter.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/adapter/out/web/NotifyExecutorPoolAdapter.java @@ -12,6 +12,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; +import ch.unisg.executor1.executor.domain.ExecutorType; @Component @Primary @@ -21,12 +22,12 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort { String server = "http://127.0.0.1:8083"; @Override - public boolean notifyExecutorPool(String ip, int port, String executorType) { + public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) { var values = new HashMap() {{ put("ip", ip); put("port", Integer.toString(port)); - put("executorType", executorType); + put("executorType", executorType.toString()); }}; var objectMapper = new ObjectMapper(); diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/in/TaskAvailableCommand.java b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/in/TaskAvailableCommand.java index a5d530d..292d18b 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/in/TaskAvailableCommand.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/in/TaskAvailableCommand.java @@ -1,6 +1,7 @@ package ch.unisg.executor1.executor.application.port.in; import ch.unisg.executor1.common.SelfValidating; +import ch.unisg.executor1.executor.domain.ExecutorType; import javax.validation.constraints.NotNull; @@ -10,9 +11,9 @@ import lombok.Value; public class TaskAvailableCommand extends SelfValidating { @NotNull - private final String taskType; + private final ExecutorType taskType; - public TaskAvailableCommand(String taskType) { + public TaskAvailableCommand(ExecutorType taskType) { this.taskType = taskType; this.validateSelf(); } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/GetAssignmentPort.java b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/GetAssignmentPort.java index bbf9124..7b81b13 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/GetAssignmentPort.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/GetAssignmentPort.java @@ -1,7 +1,8 @@ package ch.unisg.executor1.executor.application.port.out; +import ch.unisg.executor1.executor.domain.ExecutorType; import ch.unisg.executor1.executor.domain.Task; public interface GetAssignmentPort { - Task getAssignment(String executorType); + Task getAssignment(ExecutorType executorType); } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/NotifyExecutorPoolPort.java b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/NotifyExecutorPoolPort.java index 856163f..4086258 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/NotifyExecutorPoolPort.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/application/port/out/NotifyExecutorPoolPort.java @@ -1,5 +1,7 @@ package ch.unisg.executor1.executor.application.port.out; +import ch.unisg.executor1.executor.domain.ExecutorType; + public interface NotifyExecutorPoolPort { - boolean notifyExecutorPool(String ip, int port, String executorType); + boolean notifyExecutorPool(String ip, int port, ExecutorType executorType); } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/application/service/NotifyExecutorPoolService.java b/executor1/src/main/java/ch/unisg/executor1/executor/application/service/NotifyExecutorPoolService.java index 0c05fda..acfae94 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/application/service/NotifyExecutorPoolService.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/application/service/NotifyExecutorPoolService.java @@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; +import ch.unisg.executor1.executor.domain.ExecutorType; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @@ -11,7 +12,7 @@ public class NotifyExecutorPoolService { private final NotifyExecutorPoolPort notifyExecutorPoolPort; - public boolean notifyExecutorPool(String ip, int port, String executorType) { + public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) { return notifyExecutorPoolPort.notifyExecutorPool(ip, port, executorType); } } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/application/service/TaskAvailableService.java b/executor1/src/main/java/ch/unisg/executor1/executor/application/service/TaskAvailableService.java index 795cd8b..6f15b47 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/application/service/TaskAvailableService.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/application/service/TaskAvailableService.java @@ -19,7 +19,7 @@ public class TaskAvailableService implements TaskAvailableUseCase { public void newTaskAvailable(TaskAvailableCommand command) { Executor executor = Executor.getExecutor(); - if (executor.getExecutorType().equalsIgnoreCase(command.getTaskType()) && + if (executor.getExecutorType() == command.getTaskType() && executor.getStatus() == ExecutorStatus.IDLING) { executor.getAssignment(); } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/domain/Executor.java b/executor1/src/main/java/ch/unisg/executor1/executor/domain/Executor.java index 6329f29..d8f9e0b 100644 --- a/executor1/src/main/java/ch/unisg/executor1/executor/domain/Executor.java +++ b/executor1/src/main/java/ch/unisg/executor1/executor/domain/Executor.java @@ -1,77 +1,22 @@ package ch.unisg.executor1.executor.domain; -import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort; -import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort; -import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; - import java.util.concurrent.TimeUnit; -import javax.transaction.Transactional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Configurable; - -import ch.unisg.executor1.executor.adapter.out.web.ExecutionFinishedAdapter; -import ch.unisg.executor1.executor.adapter.out.web.GetAssignmentAdapter; -import ch.unisg.executor1.executor.adapter.out.web.NotifyExecutorPoolAdapter; -import ch.unisg.executor1.executor.application.service.NotifyExecutorPoolService; -import lombok.Getter; - -public class Executor { - - @Getter - private String ip; - - @Getter - private String executorType = "addition"; - - @Getter - private int port; - - @Getter - private ExecutorStatus status; - - private static final Executor executor = new Executor(); - - // TODO Violation of the Dependency Inversion Principle?, but we havn't really got a better solutions to send a http request / access a service from a domain model - // TODO I guess we can somehow autowire this but I don't know why it's not working :D - private final NotifyExecutorPoolPort notifyExecutorPoolPort = new NotifyExecutorPoolAdapter(); - private final NotifyExecutorPoolService notifyExecutorPoolService = new NotifyExecutorPoolService(notifyExecutorPoolPort); - private final GetAssignmentPort getAssignmentPort = new GetAssignmentAdapter(); - private final ExecutionFinishedEventPort executionFinishedEventPort = new ExecutionFinishedAdapter(); - - private Executor() { - System.out.println("Starting Executor"); - // TODO set this automaticly - this.ip = "localhost"; - this.port = 8084; +public class Executor extends ExecutorBase { - this.status = ExecutorStatus.STARTING_UP; - if(!notifyExecutorPoolService.notifyExecutorPool(this.ip, this.port, this.executorType)) { - System.exit(0); - } else { - System.out.println(true); - this.status = ExecutorStatus.IDLING; - getAssignment(); - } - } + private static final Executor executor = new Executor(ExecutorType.ADDITION); public static Executor getExecutor() { return executor; } - public void getAssignment() { - Task newTask = getAssignmentPort.getAssignment(this.getExecutorType()); - if (newTask != null) { - this.executeTask(newTask); - } else { - this.status = ExecutorStatus.IDLING; - } + private Executor(ExecutorType executorType) { + super(executorType); } - private void executeTask(Task task) { - System.out.println("Starting execution"); - this.status = ExecutorStatus.EXECUTING; + @Override + String execution() { + int a = 10; int b = 20; try { @@ -82,12 +27,7 @@ public class Executor { int result = a + b; - task.setResult(Integer.toString(result)); - - executionFinishedEventPort.publishExecutionFinishedEvent(new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS")); - - System.out.println("Finish execution"); - getAssignment(); + return Integer.toString(result); } - + } diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorBase.java b/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorBase.java new file mode 100644 index 0000000..fe65913 --- /dev/null +++ b/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorBase.java @@ -0,0 +1,87 @@ +package ch.unisg.executor1.executor.domain; + +import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort; +import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort; +import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; + +import java.util.concurrent.TimeUnit; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Configurable; + +import ch.unisg.executor1.executor.adapter.out.web.ExecutionFinishedEventAdapter; +import ch.unisg.executor1.executor.adapter.out.web.GetAssignmentAdapter; +import ch.unisg.executor1.executor.adapter.out.web.NotifyExecutorPoolAdapter; +import ch.unisg.executor1.executor.application.service.NotifyExecutorPoolService; +import lombok.Getter; + +abstract class ExecutorBase { + + @Getter + private String ip; + + @Getter + private ExecutorType executorType; + + @Getter + private int port; + + @Getter + private ExecutorStatus status; + + // private static final ExecutorBase executor = new ExecutorBase(); + + // TODO Violation of the Dependency Inversion Principle?, but we havn't really got a better solutions to send a http request / access a service from a domain model + // TODO I guess we can somehow autowire this but I don't know why it's not working :D + private final NotifyExecutorPoolPort notifyExecutorPoolPort = new NotifyExecutorPoolAdapter(); + private final NotifyExecutorPoolService notifyExecutorPoolService = new NotifyExecutorPoolService(notifyExecutorPoolPort); + private final GetAssignmentPort getAssignmentPort = new GetAssignmentAdapter(); + private final ExecutionFinishedEventPort executionFinishedEventPort = new ExecutionFinishedEventAdapter(); + + public ExecutorBase(ExecutorType executorType) { + System.out.println("Starting Executor"); + // TODO set this automaticly + this.ip = "localhost"; + this.port = 8084; + this.executorType = executorType; + + this.status = ExecutorStatus.STARTING_UP; + if(!notifyExecutorPoolService.notifyExecutorPool(this.ip, this.port, this.executorType)) { + System.exit(0); + } else { + System.out.println(true); + this.status = ExecutorStatus.IDLING; + getAssignment(); + } + } + + // public static ExecutorBase getExecutor() { + // return executor; + // } + + public void getAssignment() { + Task newTask = getAssignmentPort.getAssignment(this.getExecutorType()); + if (newTask != null) { + this.executeTask(newTask); + } else { + this.status = ExecutorStatus.IDLING; + } + } + + private void executeTask(Task task) { + System.out.println("Starting execution"); + this.status = ExecutorStatus.EXECUTING; + + task.setResult(execution()); + + executionFinishedEventPort.publishExecutionFinishedEvent(new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS")); + + System.out.println("Finish execution"); + getAssignment(); + } + + abstract String execution(); + +} diff --git a/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorType.java b/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorType.java new file mode 100644 index 0000000..58c9d91 --- /dev/null +++ b/executor1/src/main/java/ch/unisg/executor1/executor/domain/ExecutorType.java @@ -0,0 +1,18 @@ +package ch.unisg.executor1.executor.domain; + +public enum ExecutorType { + ADDITION, ROBOT; + + public static boolean contains(String test) { + + for (ExecutorType x : ExecutorType.values()) { + if (x.name().equals(test)) { + return true; + } + } + return false; + } +} + + +