summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/replica/replica1/ResourceAvailability.java
blob: a6083ab279a6643fd9af82c5d361c33c19881d86 (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
package derms.replica.replica1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.logging.Logger;

public class ResourceAvailability {
	public static final int port = 5556;

	public static class Client implements Runnable {
		private InetAddress serverAddr;
		private ResourceName request;
		private Collection<Resource> resources;
		private Logger log;

		public Client(InetAddress serverAddr, ResourceName request, Collection<Resource> response) throws IOException {
			this.serverAddr = serverAddr;
			this.request = request;
			this.resources = response;
			this.log = DermsLogger.getLogger(this.getClass());
		}

		@Override
		public void run() {
			DatagramSocket sock;
			try {
				sock = new DatagramSocket();
			} catch (Exception e) {
				log.severe("Error binding socket: "+e.getMessage());
				return;
			}
			log.fine("Created socket");

			DatagramPacket reqPkt;
			try {
				reqPkt = ObjectPacket.create(request, new InetSocketAddress(serverAddr, port));
			} catch (IOException e) {
				log.severe("Error creating request: "+e.getMessage());
				sock.close();
				return;
			}

			try {
				sock.send(reqPkt);
			} catch (IOException e) {
				log.severe("Error sending request: "+e.getMessage());
				sock.close();
				return;
			}
			log.fine("Sent request");

			Resource[] response;
			try {
				response = ResourceTransfer.receive(sock);
			} catch (IOException e) {
				log.severe(e.getMessage());
				return;
			} finally {
				sock.close();
			}

			for (Resource resource : response) {
				resources.add(resource);
			}
		}
	}

	public static class Server implements Runnable {
		public static final int bufsize = 1024;

		private InetAddress localAddr;
		private Resources resources;
		private Logger log;

		public Server(InetAddress localAddr, Resources resources) throws IOException {
			this.localAddr = localAddr;
			this.resources = resources;
			this.log = DermsLogger.getLogger(this.getClass());
		}

		@Override
		public void run() {
			DatagramSocket sock = null;
			try {
				sock = new DatagramSocket(port, localAddr);
			} catch (Exception e) {
				log.severe("Failed to bind socket to "+localAddr.toString()+": "+e.getMessage());
				return;
			}

			log.info("Listening on "+localAddr.toString()+":"+port);

			DatagramPacket request = new DatagramPacket(new byte[bufsize], bufsize);
			try {
				for (;;) {
					try {
						sock.receive(request);
					} catch (Exception e) {
						log.warning("Error receiving from socket: "+e.getMessage());
						continue;
					}

					ResourceName requestedName = null;
					try {
						requestedName = ObjectPacket.deserialize(request, ResourceName.class);
					} catch (IOException e) {
						log.warning("Failed to deserialize request: "+e.getMessage());
						continue;
					}
					log.info("Got request: "+requestedName);

					Resource[] response = resources.getByName(requestedName);
					try {
						ResourceTransfer.send(response, request.getSocketAddress());
					} catch (IOException e) {
						log.warning("Error transfering resources: "+e.getMessage());
					}
				}
			} finally {
				sock.close();
			}
		}
	}
}