Java BUMP implementation.
BUMP implementation in Java.docx
The project is to implement the BUMP client in java, with window size 1. Here is an overview of the three WUMP protocols (BUMP, HUMP, and CHUMP). Here are the files wumppkt.java , containing the packet format classes, and wclient.java , which contains an outline of the actual program. Only the latter file should be modified; you should not have to make changes to wumppkt.java.
What you are to do is the following, by modifying and extending the wclient.java outline file:
· Implement the basic transfer
· Add all appropriate packet sanity checks: timeouts, host/port, size, opcode, and block number
· Generate output. The transferred file is to be written to System.out. A status message about every packet (listing size and block number) is to be written to System.err. Do not confuse these!
· Terminate after a packet of size less than 512 is received
· Implement an appropriate "dallying" strategy
· send an ERROR packet if it receives a packet from the wrong port. The appropriate ERRCODE in this case is EBADPORT.
An outline of the program main loop is attached
recommended that you implement this in phases, as follows.
1. Latch on to the new port: save the port number from Data[1], and make sure all ACKs get sent to this port. This will mean that the transfer completes. You should also make sure the client stops when a packet with less than 512 bytes of data is received. Unless you properly record the source port for Data[1], you have no place to which to send ACK[1]!
2. For each data packet received, write the data to System.out. All status messages should go to System.err, so the two data streams are separate if stdout is redirected. To write to System.out, use System.out.write: System.out.write(byte[] buf, int offset, int length); For your program, offset will be 0, buf will typically be dpacket.data(), where dpacket is of type DATA (wumppkt.DATA). The length will be dpacket.size() - wumppkt.DHEADERSIZE (or, equivalently, dg.getLength() - wumppkt.DHEADERSIZE, where dg is a DatagramPacket object).
3. Add sanity checks, for (in order) host/port, packet size, opcode, and block number.
4. Handle timeouts, by retransmitting the most recently sent packet when the elapsed time exceeds a certain amount (4 seconds?). One way to do this is to keep a DatagramPacket variable LastSent, which can either be reqDG or ackDG, and just resend LastSent. Note that the response to an InterruptedIOException, a "true" timeout, will simply be to continue the loop again.
5. Add support for an dallying and error packets. After the client has received the file, dallying means to wait 2.0 - 3.0 timeout intervals (or more) to see if the final data packet is retransmitted. If it is, it means that the final ACK was lost. The dally period gives the client an opportunity to resend the final ACK. Error packets are to be sent to any sender of an apparent data packet that comes from the wrong port.
lose Lose everything after the first windowful (min 3). It will be retransmitted when you retransmit the previous ACK.
spray Constant barrage of data[1]. Implies LOSE too. In this case, no timeout events will occur; you must check for elapsed time.
delay Delays sending packet 1, prompting a duplicate REQ and thus results in multiple server instances on multiple ports.
reorder Sends the first windowful in the wrong order. You're probably not implementing windows.
dupdata2
DATA[2] is sent twice
losedata2
DATA[2] is lost on initial send, until you resend ACK[1]
marspacket
A packet from the wrong port (a "martian" port) arrives
badopcode
a packet arrives with an incorrect opcode
nofile
you get an error packet with a NO FILE error code.
At this point, the only ones that work on port 4716 are vanilla, lose2 (losedata2) and dup2 (dupdata2).
wumppkt.java
wumppkt.java
// The following implements the packet formats for BUMP and HUMP.
// Each individual packet type derives from class BASE, containing
// protocol and opcode fields only; we don't really use this inheritance
// hierarchy though.
// packets can be constructed with the applicable constructors;
// each ctor requires parameters for the necessary fields.
// when possible, there is a "convenience" ctor setting proto = BUMPPROTO.
// The "raw" packet format, as sent and received via DatagramSocket,
// is byte[]. Packets (at least those that one might *receive*)
// can be constructed from a byte[]. For DATA packets, we also need
// to specify the length of the packet, not necessarily the same as
// the length of the byte[] buffer.
// All packet classes also have a write() member function that
// writes out the packet fields into a byte[], for sending.
//import java.lang.*; //pld
//import java.net.*; //pld
//import java.lang.System.*;
import
java
.
io
.
*
;
public
class
wumppkt
{
public
static
final
short
BUMPPROTO
=
1
;
public
static
final
short
HUMPPROTO
=
2
;
public
static
final
short
CHUMPPROTO
=
3
;
public
static
final
short
REQop
=
1
;
public
static
final
short
DATAop
=
2
;
public
static
final
short
ACKop
=
3
;
public
static
final
short
ERRORop
=
4
;
public
static
final
short
HANDOFFop
=
5
;
public
static
final
short
SERVERPORT
=
4715
;
public
static
final
short
SAMEPORT
=
4716
;
public
static
final
int
INITTIMEOUT
=
3000
;
// milliseconds
public
static
final
int
SHORTSIZE
=
2
;
// in bytes
public
static
final
int
INTSIZE
=
4
;
public
static
final
int
BASESIZE
=
2
;
public
static
final
int
MAXDATASIZE
=
512
;
public
static
final
int
DHEADERSIZE
=
BASESIZE
+
SHORTSIZE
+
INTSIZE
;
// DATA header size
public
static
final
int
MAXSIZE
=
DHEADERSIZE
+
MAXDATASIZE
;
public
static
final
int
EBADPORT
=
1
;
/* packet from wrong port */
public
static
final
int
EBADPROTO
=
2
;
/* unknown protocol */
public
static
final
int
EBADOPCODE
=
3
;
/* unknown opcode */
public
static
final
int
ENOFILE
=
4
;
/* REQ for nonexistent file */
public
static
final
int
ENOPERM
=
5
;
/* REQ for file with wrong permissions */
static
int
proto
(
byte
[]
buf
)
{
return
buf
[
0
];
}
static
int
opcode
(
byte
[]
buf
)
{
return
buf
[
1
];
}
public
static
void
w_assert
(
boolean
cond
,
String
s
)
{
if
(
cond
)
return
;
System
.
err
.
println
(
"assertion failed: "
+
s
);
java
.
lang
.
System
.
exit
(
1
);
}
//************************************************************************
public
class
BASE
{
//implements Externalizable {
// don't construct these unless the buffer has length >=4!
// the data:
private
byte
_proto
;
private
byte
_opcode
;
//---------------------------------
public
BASE
(
int
proto
,
int
opcode
)
{
//super(); // call to base ctor
_proto
=
(
byte
)
proto
;
_opcode
=
(
byte
)
opcode
;
}
public
BASE
(
byte
[]
buf
)
{
// constructs pkt out of packetbuf
}
public
BASE
()
{}
// packet ctors do all the work!
public
byte
[]
write
()
{
// not used
return
null
;
}
public
int
size
()
{
return
BASESIZE
;
}
public
int
proto
()
{
return
_proto
;}
public
int
opcode
()
{
return
_opcode
;}
}
//*******************
public
class
REQ
extends
BASE
{
private
short
_winsize
;
private
String
_filename
;
//---------------------------------
public
REQ
(
int
proto
,
int
winsize
,
String
filename
)
{
super
(
proto
,
REQop
);
_winsize
=
(
short
)
winsize
;
_filename
=
filename
;
}
public
REQ
(
int
winsize
,
String
filename
)
{
this
(
BUMPPROTO
,
winsize
,
filename
);
}
public
REQ
(
byte
[]
buf
)
{
// not complete but not needed
//super(BUMPPROTO, REQop);
}
public
byte
[]
write
()
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
size
());
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
try
{
//writeExternal(dos);
dos
.
writeByte
(
super
.
proto
());
dos
.
writeByte
(
super
.
opcode
());
dos
.
writeShort
(
_winsize
);
dos
.
writeBytes
(
_filename
);
dos
.
writeByte
(
0
);
return
baos
.
toByteArray
();
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"BASE packet output conversion failed"
);
return
null
;
}
//return null;
}
public
int
size
()
{
return
super
.
size
()
+
SHORTSIZE
+
_filename
.
length
()
+
1
;
}
public
String
filename
()
{
return
_filename
;}
}
//*******************
public
class
ACK
extends
BASE
{
private
int
_blocknum
;
//---------------------------------
public
ACK
(
int
blocknum
)
{
this
(
BUMPPROTO
,
blocknum
);
}
public
ACK
(
short
proto
,
int
blocknum
)
{
super
(
proto
,
ACKop
);
_blocknum
=
blocknum
;
}
public
int
blocknum
()
{
return
_blocknum
;}
public
void
setblock
(
int
blocknum
)
{
_blocknum
=
blocknum
;}
public
byte
[]
write
()
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
size
());
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
try
{
//writeExternal(dos);
dos
.
writeByte
(
super
.
proto
());
dos
.
writeByte
(
super
.
opcode
());
dos
.
writeShort
(
0
);
// padding
dos
.
writeInt
(
_blocknum
);
return
baos
.
toByteArray
();
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"ACK packet output conversion failed"
);
return
null
;
}
}
public
int
size
()
{
return
super
.
size
()
+
SHORTSIZE
+
INTSIZE
;
}
public
ACK
(
byte
[]
buf
)
{}
// not complete but not needed
}
//*******************
public
class
DATA
extends
BASE
{
private
int
_blocknum
;
private
byte
[]
_data
;
//---------------------------------
public
DATA
(
int
proto
,
int
blocknum
,
byte
[]
data
)
{
super
(
proto
,
DATAop
);
_blocknum
=
blocknum
;
_data
=
data
;
}
public
DATA
(
int
proto
,
int
blocknum
,
byte
[]
data
,
int
len
)
{
super
(
proto
,
DATAop
);
_blocknum
=
blocknum
;
_data
=
data
;
}
public
DATA
(
byte
[]
buf
,
int
bufsize
)
{
this
(
BUMPPROTO
,
buf
,
bufsize
);
}
// for building a DATA out of incoming buffer:
public
DATA
(
int
proto
,
byte
[]
buf
,
int
bufsize
)
{
super
(
proto
,
DATAop
);
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
buf
,
0
,
bufsize
);
DataInputStream
dis
=
new
DataInputStream
(
bais
);
try
{
int
p
=
dis
.
readByte
();
w_assert
(
p
==
proto
,
"Expecting proto "
+
proto
+
", got "
+
p
);
int
o
=
dis
.
readByte
();
w_assert
(
o
==
DATAop
,
"Expecting opcode=DATA, got "
+
o
);
int
pad
=
dis
.
readShort
();
_blocknum
=
(
dis
.
readInt
());
_data
=
new
byte
[
dis
.
available
()];
dis
.
read
(
_data
);
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"DATA packet conversion failed"
);
return
;
}
}
public
DATA
(
int
proto
)
{
// for creating "empty" DATA objects
super
(
proto
,
DATAop
);
_blocknum
=
0
;
_data
=
new
byte
[
MAXDATASIZE
];
}
public
DATA
()
{
this
(
BUMPPROTO
);
}
public
int
blocknum
()
{
return
_blocknum
;}
public
byte
[]
data
()
{
return
_data
;}
public
byte
[]
write
()
{
// not complete but not needed
return
null
;
}
public
int
size
()
{
return
super
.
size
()
+
SHORTSIZE
+
INTSIZE
+
_data
.
length
;
}
}
//*******************
public
class
ERROR
extends
BASE
{
private
short
_errcode
;
//---------------------------------
public
ERROR
(
short
proto
,
short
errcode
)
{
super
(
proto
,
ERRORop
);
_errcode
=
errcode
;
}
public
short
errcode
()
{
return
_errcode
;}
public
byte
[]
write
()
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
size
());
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
try
{
//writeExternal(dos);
dos
.
writeByte
(
super
.
proto
());
dos
.
writeByte
(
super
.
opcode
());
dos
.
writeShort
(
_errcode
);
return
baos
.
toByteArray
();
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"ERROR packet output conversion failed"
);
return
null
;
}
}
public
ERROR
(
byte
[]
buf
)
{
this
(
BUMPPROTO
,
buf
);}
public
ERROR
(
int
proto
,
byte
[]
buf
)
{
super
(
proto
,
DATAop
);
int
opcode
=
wumppkt
.
this
.
opcode
(
buf
);
w_assert
(
opcode
==
ERRORop
,
"Expecting opcode=ERROR, got "
+
opcode
);