-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.java
More file actions
171 lines (147 loc) · 3.5 KB
/
Copy pathPacket.java
File metadata and controls
171 lines (147 loc) · 3.5 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
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
171
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Wrapper for handling basic tasks around `DatagramSocket` and
* `DatagramPacket`.
*
* @author James Li
*/
class Packet {
private byte[] data;
private DatagramPacket packet;
private DatagramSocket socket;
/**
* Opcodes based on Section 5
*/
static final class opcode {
/** Read request */
static final short RRQ = 1;
/** Write request */
static final short WRQ = 2;
/** Data */
static final short DATA = 3;
/** Acknowledgement */
static final short ACK = 4;
/** Error */
static final short ERROR = 5;
}
/**
* Error codes based on Appendix
* <p>
* Most are not used in this package, because the errors that are returned
* from Java methods tend not to be specific enough to be distinguishable.
*/
static final class errcode {
/** Not defined, see error message **/
static final short NOT_DEFINED = 0;
/** File not found **/
static final short FILE_NOT_FOUND = 1;
/** Access violation **/
static final short ACCESS_VIOLATION = 2;
/** Disk full or allocation exceeded **/
static final short DISK_FULL = 3;
/** Illegal TFTP operation **/
static final short ILLEGAL_OPERATION = 4;
/** Unknown transfer ID **/
static final short UNKNOWN_TID = 5;
/** File already exists **/
static final short FILE_EXISTS = 6;
/** No such user **/
static final short NO_SUCH_USER = 7;
}
/**
* Error strings based on Appendix
* <p>
* Most are not used in this package, because the errors that are returned
* from Java methods tend not to be specific enough to be distinguishable.
*/
static final String[] errstr = {
"",
"File not found",
"Access violation",
"Disk full or allocation exceeded",
"Illegal TFTP operation",
"Unknown transfer ID",
"File already exists",
"No such user"
};
/**
* Mode strings
*/
static final class modestr {
static final String NETASCII = "netascii";
static final String OCTET = "octet";
static final String MAIL = "mail"; // Not supported
}
/**
* Retrieves the data buffer in this packet.
*
* @return Data buffer
*/
byte[] getData() {
return this.data;
}
/**
* Retrieves the `DatagramPacket` stored in this packet.
*
* @return `DatagramPacket` object
*/
DatagramPacket getDatagramPacket() {
return this.packet;
}
/**
* Retrieves the `DatagramSocket` stored in this packet.
*
* @return DatagramSocket object
*/
DatagramSocket getSocket() {
return this.socket;
}
/**
* Sets up this object for receiving data.
*
* @return This object
*/
Packet setupReceive() {
this.data = new byte[Constants.maxAnyPacketLength];
this.packet = new DatagramPacket(this.data, this.data.length);
return this;
}
/**
* Sets up this object for sending data.
*
* @return This object
*/
Packet setupSend(byte[] data, InetAddress address, int port) {
this.data = data;
this.packet = new DatagramPacket(this.data, this.data.length, address, port);
return this;
}
/**
* Receives datagram packet.
*
* @return This object
*/
Packet receivePacket() throws IOException {
this.getSocket().receive(this.getDatagramPacket());
return this;
}
/**
* Sends datagram packet.
*
* @return This object
*/
Packet sendPacket() throws IOException {
this.getSocket().send(this.getDatagramPacket());
return this;
}
/**
* @param socket
* `DatagramSocket` object
*/
Packet(DatagramSocket socket) {
this.socket = socket;
}
}