summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica2/CoordinatorServer.java
blob: 06836384e72f0fbf42a227e72d00ff59e9682f0f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package derms.replica2;

import java.io.IOException;
import java.net.InetAddress;
import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

class CoordinatorServer {
	static final Duration timeout = Duration.ofSeconds(5);

	private City city;
	private Resources resources;
	private Servers servers;
	private Logger log;

	CoordinatorServer(City city, Resources resources, Servers servers) throws IOException {
		super();
		this.city = city;
		this.resources = resources;
		this.servers = servers;
		this.log = DermsLogger.getLogger(this.getClass());
	}

	CoordinatorServer() throws IOException {
		this(new City(), new Resources(), new Servers());
	}

	void requestResource(CoordinatorID cid, ResourceID rid, int duration)
			throws ServerCommunicationError, NoSuchResourceException,
			AlreadyBorrowedException, InvalidDurationException
	{
		log.info("Request for "+rid+" from "+cid);

		InetAddress server = servers.get(new City(rid.city));
		if (server == null) {
			throw new ServerCommunicationError("requestResource(): no connection to server "+rid.city.toString());
		}

		RequestResource.Client client = new RequestResource.Client(cid, rid, duration);
		RequestResource.Response response;
		try {
			response = client.sendRequest(server);
		} catch (IOException e) {
			throw new ServerCommunicationError("requestResource(): "+e.getMessage());
		}
		switch (response.status) {
		case SUCCESS:
			log.info("Request "+rid+" from "+cid+" - success");
			break;
		case NO_SUCH_RESOURCE:
			log.warning(response.message);
			throw new NoSuchResourceException(response.message);
		case ALREADY_BORROWED:
			log.warning(response.message);
			throw new AlreadyBorrowedException(response.message);
		case INVALID_DURATION:
			log.warning(response.message);
			throw new InvalidDurationException(response.message);
		default:
			log.warning("Unsuccessful response from server: "+response.message);
			throw new ServerCommunicationError("requestResource(): failed to borrow resource: "+response.message);
		}
	}

	Resource[] findResource(CoordinatorID cid, ResourceType rname) throws ServerCommunicationError {
		log.info("Find Resource "+rname+" from "+cid);
		FindResource.Request request = new FindResource.Request(cid, rname);
		Collection<Resource> response = ConcurrentHashMap.newKeySet();
		ExecutorService pool = Executors.newFixedThreadPool(servers.size());
		try {
			for (InetAddress server : servers.all()) {
				pool.execute(new FindResource.Client(request, server, response));
			}
		} catch (IOException e) {
			String msg = "Failed to start FindResource Client: "+e.getMessage();
			log.severe(msg);
			throw new ServerCommunicationError("findResource(): "+msg);
		}
		log.fine("Started worker threads");
		pool.shutdown();
		boolean terminated;
		try {
			terminated  = pool.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			String msg = "findResource() interrupted: "+e.getMessage();
			log.warning(msg);
			throw new ServerCommunicationError("findResource(): "+msg);
		}
		if (!terminated) {
			String msg = "Request timed out: no response after "+timeout.toString();
			log.warning(msg);
			throw new ServerCommunicationError("findResource(): "+msg);
		}
		Resource[] arr = new Resource[0];
		arr = response.toArray(arr);
		log.info("Find resource "+rname+" from "+cid+" - success. Response length: "+arr.length);
		return arr;
	}

	void returnResource(CoordinatorID cid, ResourceID rid)
			throws ServerCommunicationError, NoSuchResourceException, NotBorrowedException
	{
		log.info("Return resource "+rid+" from "+cid);
		InetAddress server = servers.get(new City(rid.city));
		if (server == null) {
			String msg = "no connection to server "+rid.city;
			log.warning(msg);
			throw new ServerCommunicationError("returnResource(): "+msg);
		}
		log.fine("server address: "+server);

		ReturnResource.Client client = new ReturnResource.Client(cid, rid);
		ReturnResource.Response response;
		try {
			response = client.sendRequest(server);
		} catch (IOException e) {
			log.warning(e.getMessage());
			throw new ServerCommunicationError("returnResource(): "+e.getMessage());
		}
		switch (response.status) {
		case SUCCESS:
			log.info(cid+" return "+rid+" - success");
			break;
		case NO_SUCH_RESOURCE:
			log.warning(response.message);
			throw new NoSuchResourceException(response.message);
		case NOT_BORROWED:
			log.warning(response.message);
			throw new NotBorrowedException(response.message);
		default:
			String msg = "Failed to return resource: "+response.message;
			log.warning(msg);
			throw new ServerCommunicationError("returnResource(): "+msg);
		}
	}

	void swapResource(CoordinatorID cid, ResourceID oldRID, ResourceID newRID) throws ServerCommunicationError, NoSuchResourceException {
		log.info(cid+": swap "+oldRID+", "+newRID);

		InetAddress server = servers.get(new City(oldRID.city));
		if (server == null) {
			String msg = "no connection to server "+oldRID.city;
			log.warning(msg);
			throw new ServerCommunicationError("swapResource(): "+msg);
		}
		log.fine("server address: "+server);

		SwapResource.Client client = new SwapResource.Client(cid, oldRID, newRID);
		SwapResource.Response response;
		try {
			response = client.sendRequest(server);
		} catch (IOException e) {
			throw new ServerCommunicationError("swapResource(): "+e.getMessage());
		}
		switch (response.status) {
		case SUCCESS:
			log.info(cid+": swap "+oldRID+", "+newRID+" - success");
			break;
		case NO_SUCH_RESOURCE:
			throw new NoSuchResourceException(response.message);
		default:
			throw new ServerCommunicationError("swapResource(): "+response.message);
		}
	}
}