forked from NachoSquad/nachos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailMessage.java
105 lines (89 loc) · 3.39 KB
/
MailMessage.java
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
package nachos.network;
import nachos.machine.*;
/**
* A mail message. Includes a packet header, a mail header, and the actual
* payload.
*
* @see nachos.machine.Packet
*/
public class MailMessage {
/**
* Allocate a new mail message to be sent, using the specified parameters.
*
* @param dstLink the destination link address.
* @param dstPort the destination port.
* @param srcLink the source link address.
* @param srcPort the source port.
* @param contents the contents of the packet.
*/
public MailMessage(int dstLink, int dstPort, int srcLink, int srcPort,
byte[] contents) throws MalformedPacketException {
// make sure the paramters are valid
if (dstPort < 0 || dstPort >= portLimit ||
srcPort < 0 || srcPort >= portLimit ||
contents.length > maxContentsLength)
throw new MalformedPacketException();
this.dstPort = (byte) dstPort;
this.srcPort = (byte) srcPort;
this.contents = contents;
byte[] packetContents = new byte[headerLength + contents.length];
packetContents[0] = (byte) dstPort;
packetContents[1] = (byte) srcPort;
System.arraycopy(contents, 0, packetContents, headerLength,
contents.length);
packet = new Packet(dstLink, srcLink, packetContents);
}
/**
* Allocate a new mail message using the specified packet from the network.
*
* @param packet the packet containg the mail message.
*/
public MailMessage(Packet packet) throws MalformedPacketException {
this.packet = packet;
// make sure we have a valid header
if (packet.contents.length < headerLength ||
packet.contents[0] < 0 || packet.contents[0] >= portLimit ||
packet.contents[1] < 0 || packet.contents[1] >= portLimit)
throw new MalformedPacketException();
dstPort = packet.contents[0];
srcPort = packet.contents[1];
contents = new byte[packet.contents.length - headerLength];
System.arraycopy(packet.contents, headerLength, contents, 0,
contents.length);
}
/**
* Return a string representation of the message headers.
*/
public String toString() {
return "from (" + packet.srcLink + ":" + srcPort +
") to (" + packet.dstLink + ":" + dstPort +
"), " + contents.length + " bytes";
}
/** This message, as a packet that can be sent through a network link. */
public Packet packet;
/** The port used by this message on the destination machine. */
public int dstPort;
/** The port used by this message on the source machine. */
public int srcPort;
/** The contents of this message, excluding the mail message header. */
public byte[] contents;
/**
* The number of bytes in a mail header. The header is formatted as
* follows:
*
* <table>
* <tr><td>offset</td><td>size</td><td>value</td></tr>
* <tr><td>0</td><td>1</td><td>destination port</td></tr>
* <tr><td>1</td><td>1</td><td>source port</td></tr>
* </table>
*/
public static final int headerLength = 2;
/** Maximum payload (real data) that can be included in a single mesage. */
public static final int maxContentsLength =
Packet.maxContentsLength - headerLength;
/**
* The upper limit on mail ports. All ports fall between <tt>0</tt> and
* <tt>portLimit - 1</tt>.
*/
public static final int portLimit = 128;
}