-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailboxManager.java
More file actions
45 lines (45 loc) · 1.59 KB
/
MailboxManager.java
File metadata and controls
45 lines (45 loc) · 1.59 KB
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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.util.HashMap;
/**
* The MailboxManager class manages the mailbox for NimServer. It tracks all
* relevant view proxies, reads incoming datagrams, and forwards datagrams to
* the correct view proxy.
*
* @author Pavel Rozvora (pxr8306)
* @version 2015-12-08
*/
public class MailboxManager {
private DatagramSocket mailbox;
private HashMap<SocketAddress, NimViewProxy> proxyMap =
new HashMap<SocketAddress, NimViewProxy>();
private byte[] message = new byte[128];
private SessionManager sessionManager = new SessionManager();
/**
* Constructor. Constructs a mailbox manager.
* @param mailbox mailbox to read datagrams from
*/
public MailboxManager(DatagramSocket mailbox) {
this.mailbox = mailbox;
}
/**
* This method reads in a datagram and processes it.
* @throws IOException Thrown when I/O fails or is interrupted
*/
public void receiveMessage() throws IOException {
DatagramPacket packet = new DatagramPacket(message, message.length);
mailbox.receive(packet);
SocketAddress clientAddress = packet.getSocketAddress();
NimViewProxy proxy = proxyMap.get(clientAddress);
if (proxy == null) {
proxy = new NimViewProxy(mailbox, clientAddress);
proxy.setViewListener(sessionManager);
proxyMap.put(clientAddress, proxy);
}
if (proxy.process(packet)) {
proxyMap.remove(clientAddress);
}
}
}