summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica1/ResponderClient.java
blob: c022fd3ed4c6010aa1a49ad065a191583a12e481 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package derms.replica1;

import derms.replica1.jaxws.DERMSInterface;
import derms.replica1.jaxws.DERMSServerService;

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;
import java.util.Map;

public class ResponderClient {

    DERMSInterface server;
    private String responderID;

    public ResponderClient(String responderID) {
        try {
            this.responderID = responderID;
            HashMap<String, String> servers = new HashMap<String, String>() {{
                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(responderID.substring(0, 3)));
            DERMSServerService service = new DERMSServerService(url);
            server = service.getDERMSServerPort();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void respond() {
        try {
            String response = server.removeResource("MTL0012", 5);
            System.out.println("Response: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ResponderClient client = new ResponderClient("Responder1");
        client.respond();
    }

    public String addResource(String resourceID, String resourceName, int duration) {
        try {
            String response = server.addResource( resourceID, resourceName, duration);
            System.out.println(response);
            logOperation("addResource", resourceID, resourceName, duration, response);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public void removeResource(String resourceID, int duration) {
        try {
            String response = server.removeResource(resourceID, duration);
            System.out.println(response);
            logOperation("removeResource", resourceID, duration, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> listResourceAvailability(String resourceName) {
        try {
            List<String> response = server.listResourceAvailability(resourceName);
            logOperation("listResourceAvailability", resourceName, response.toString());
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    private void logOperation(String operation, String resourceID, String resourceName, int duration, String response) {
        try (FileWriter writer = new FileWriter(responderID + "_log.txt", true)) {
            writer.write(LocalDateTime.now() + " - " + operation + " - " + resourceID + " - " + resourceName + " - " + duration + " - " + response + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void logOperation(String operation, String resourceID, int duration, String response) {
        try (FileWriter writer = new FileWriter(responderID + "_log.txt", true)) {
            writer.write(LocalDateTime.now() + " - " + operation + " - " + resourceID + " - " + duration + " - " + response + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void logOperation(String operation, String resourceName, String response) {
        try (FileWriter writer = new FileWriter(responderID + "_log.txt", true)) {
            writer.write(LocalDateTime.now() + " - " + operation + " - " + resourceName + " - " + response + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}