From bf496d8917b5427508b842823f67a1bef5621938 Mon Sep 17 00:00:00 2001 From: ShazaAli Date: Mon, 2 Dec 2024 21:47:50 -0500 Subject: replica 1 and parallel FE --- .../java/derms/replica1/CoordinatorClient.java | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/main/java/derms/replica1/CoordinatorClient.java (limited to 'src/main/java/derms/replica1/CoordinatorClient.java') diff --git a/src/main/java/derms/replica1/CoordinatorClient.java b/src/main/java/derms/replica1/CoordinatorClient.java new file mode 100644 index 0000000..23b5a01 --- /dev/null +++ b/src/main/java/derms/replica1/CoordinatorClient.java @@ -0,0 +1,101 @@ +package derms.replica1; + +import derms.replica1.jaxws.DERMSInterface; +import derms.replica1.jaxws.DERMSServerService; + +import javax.xml.namespace.QName; +import javax.xml.ws.Service; +import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDateTime; + +public class CoordinatorClient { + private final DERMSInterface server; + private String coordinatorID; + + public CoordinatorClient(String coordinatorID) { + try { + this.coordinatorID = coordinatorID; + HashMap servers = new HashMap() {{ + put("MTL", "http://localhost:8387/ws/derms?wsdl"); + put("QUE", "http://localhost:8081/ws/derms?wsdl"); + put("SHE", "http://localhost:8082/ws/derms?wsdl"); + }}; + URL url = new URL(servers.get(coordinatorID.substring(0, 3))); + DERMSServerService service = new DERMSServerService(url); + server = service.getDERMSServerPort(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public String requestResource(String coordinatorID, String resourceID, int duration) { + try { + String response = server.requestResource(coordinatorID, resourceID, duration); + logOperation("requestResource", coordinatorID, resourceID, duration, response); + return response; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public List findResource(String coordinatorID, String resourceName) { + try { + List response = server.findResource(coordinatorID, resourceName); + logOperation("findResource", coordinatorID, resourceName, response.toString()); + return response; + } catch (Exception e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + + public void returnResource(String coordinatorID, String resourceID) { + try { + String response = server.returnResource(coordinatorID, resourceID); + System.out.println(response); + logOperation("returnResource", coordinatorID, resourceID, response); + } catch (Exception e) { + e.printStackTrace(); + } + } + public String swapResource(String coordinatorID, String oldResourceID, String oldResourceType, String newResourceID, String newResourceType) { + try { + String response = server.swapResource(coordinatorID, oldResourceID, oldResourceType, newResourceID, newResourceType); + logOperation("swapResource", coordinatorID, oldResourceID, newResourceID, response); + return response; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + private void logOperation(String operation, String coordinatorID, String oldResourceID, String newResourceID, String response) { + try (FileWriter writer = new FileWriter(coordinatorID + "_log.txt", true)) { + writer.write(LocalDateTime.now() + " - " + operation + " - " + coordinatorID + " - " + oldResourceID + " - " + newResourceID + " - " + response + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + } + private void logOperation(String operation, String coordinatorID, String resourceID, int duration, String response) { + try (FileWriter writer = new FileWriter(coordinatorID + "_log.txt", true)) { + writer.write(LocalDateTime.now() + " - " + operation + " - " + coordinatorID + " - " + resourceID + " - " + duration + " - " + response + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void logOperation(String operation, String coordinatorID, String resourceName, String response) { + try (FileWriter writer = new FileWriter(coordinatorID + "_log.txt", true)) { + writer.write(LocalDateTime.now() + " - " + operation + " - " + coordinatorID + " - " + resourceName + " - " + response + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file -- cgit v1.2.3