Added abstraction

This commit is contained in:
Marcel 2021-10-11 21:20:45 +02:00
parent 1d78bb63ab
commit 3bc39b70aa
12 changed files with 140 additions and 84 deletions

View File

@ -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.TaskAvailableCommand;
import ch.unisg.executor1.executor.application.port.in.TaskAvailableUseCase; import ch.unisg.executor1.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executor1.executor.domain.ExecutorType;
@RestController @RestController
public class TaskAvailableController { public class TaskAvailableController {
@ -20,9 +21,12 @@ public class TaskAvailableController {
@GetMapping(path = "/newtask/{taskType}") @GetMapping(path = "/newtask/{taskType}")
public ResponseEntity<String> retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) { public ResponseEntity<String> 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 // Add the content type as a response header
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();

View File

@ -13,7 +13,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort; import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executor1.executor.domain.ExecutionFinishedEvent; 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) //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"; 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());
} }

View File

@ -8,6 +8,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort; import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort;
import ch.unisg.executor1.executor.domain.ExecutorType;
import ch.unisg.executor1.executor.domain.Task; import ch.unisg.executor1.executor.domain.Task;
@Component @Component
@ -18,7 +19,7 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
String server = "http://127.0.0.1:8082"; String server = "http://127.0.0.1:8082";
@Override @Override
public Task getAssignment(String executorType) { public Task getAssignment(ExecutorType executorType) {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() 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 // TODO return null or a new Task here depending on the response of the http call
return new Task("1234"); return null;
} }
} }

View File

@ -12,6 +12,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executor1.executor.domain.ExecutorType;
@Component @Component
@Primary @Primary
@ -21,12 +22,12 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
String server = "http://127.0.0.1:8083"; String server = "http://127.0.0.1:8083";
@Override @Override
public boolean notifyExecutorPool(String ip, int port, String executorType) { public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
var values = new HashMap<String, String>() {{ var values = new HashMap<String, String>() {{
put("ip", ip); put("ip", ip);
put("port", Integer.toString(port)); put("port", Integer.toString(port));
put("executorType", executorType); put("executorType", executorType.toString());
}}; }};
var objectMapper = new ObjectMapper(); var objectMapper = new ObjectMapper();

View File

@ -1,6 +1,7 @@
package ch.unisg.executor1.executor.application.port.in; package ch.unisg.executor1.executor.application.port.in;
import ch.unisg.executor1.common.SelfValidating; import ch.unisg.executor1.common.SelfValidating;
import ch.unisg.executor1.executor.domain.ExecutorType;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -10,9 +11,9 @@ import lombok.Value;
public class TaskAvailableCommand extends SelfValidating<TaskAvailableCommand> { public class TaskAvailableCommand extends SelfValidating<TaskAvailableCommand> {
@NotNull @NotNull
private final String taskType; private final ExecutorType taskType;
public TaskAvailableCommand(String taskType) { public TaskAvailableCommand(ExecutorType taskType) {
this.taskType = taskType; this.taskType = taskType;
this.validateSelf(); this.validateSelf();
} }

View File

@ -1,7 +1,8 @@
package ch.unisg.executor1.executor.application.port.out; package ch.unisg.executor1.executor.application.port.out;
import ch.unisg.executor1.executor.domain.ExecutorType;
import ch.unisg.executor1.executor.domain.Task; import ch.unisg.executor1.executor.domain.Task;
public interface GetAssignmentPort { public interface GetAssignmentPort {
Task getAssignment(String executorType); Task getAssignment(ExecutorType executorType);
} }

View File

@ -1,5 +1,7 @@
package ch.unisg.executor1.executor.application.port.out; package ch.unisg.executor1.executor.application.port.out;
import ch.unisg.executor1.executor.domain.ExecutorType;
public interface NotifyExecutorPoolPort { public interface NotifyExecutorPoolPort {
boolean notifyExecutorPool(String ip, int port, String executorType); boolean notifyExecutorPool(String ip, int port, ExecutorType executorType);
} }

View File

@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort; import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executor1.executor.domain.ExecutorType;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -11,7 +12,7 @@ public class NotifyExecutorPoolService {
private final NotifyExecutorPoolPort notifyExecutorPoolPort; 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); return notifyExecutorPoolPort.notifyExecutorPool(ip, port, executorType);
} }
} }

View File

@ -19,7 +19,7 @@ public class TaskAvailableService implements TaskAvailableUseCase {
public void newTaskAvailable(TaskAvailableCommand command) { public void newTaskAvailable(TaskAvailableCommand command) {
Executor executor = Executor.getExecutor(); Executor executor = Executor.getExecutor();
if (executor.getExecutorType().equalsIgnoreCase(command.getTaskType()) && if (executor.getExecutorType() == command.getTaskType() &&
executor.getStatus() == ExecutorStatus.IDLING) { executor.getStatus() == ExecutorStatus.IDLING) {
executor.getAssignment(); executor.getAssignment();
} }

View File

@ -1,77 +1,22 @@
package ch.unisg.executor1.executor.domain; 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 java.util.concurrent.TimeUnit;
import javax.transaction.Transactional; public class Executor extends ExecutorBase {
import org.springframework.beans.factory.annotation.Autowired; private static final Executor executor = new Executor(ExecutorType.ADDITION);
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;
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 Executor getExecutor() { public static Executor getExecutor() {
return executor; return executor;
} }
public void getAssignment() { private Executor(ExecutorType executorType) {
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType()); super(executorType);
if (newTask != null) {
this.executeTask(newTask);
} else {
this.status = ExecutorStatus.IDLING;
}
} }
private void executeTask(Task task) { @Override
System.out.println("Starting execution"); String execution() {
this.status = ExecutorStatus.EXECUTING;
int a = 10; int a = 10;
int b = 20; int b = 20;
try { try {
@ -82,12 +27,7 @@ public class Executor {
int result = a + b; int result = a + b;
task.setResult(Integer.toString(result)); return Integer.toString(result);
executionFinishedEventPort.publishExecutionFinishedEvent(new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS"));
System.out.println("Finish execution");
getAssignment();
} }
} }

View File

@ -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();
}

View File

@ -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;
}
}