Help would be greatly appreciated! PLEASE make sure it runs in command prompt/te
ID: 3823833 • Letter: H
Question
Help would be greatly appreciated! PLEASE make sure it runs in command prompt/terminal! Will send some Bitcoin to whoever answer if they fit all the guidelines! Just put your wallet at the end!
: -> Java Implementation of a reliable data transfer protocol on top of UDP sockets using Go-Back-N protocol with window size equal to 4 packets.
Build a simple File Transfer Service that consists of a client and server. The server exports a set of files from the computer on which it runs to be downloaded on the client computer. That is: a client requests a file from the server and the server responds to the client by sending the file. Client will request one file at a time. Server will send the contents of the file followed by the end-of-transmission packet and then exits. Client must save the file on the local computer with the same file name. Server records all file download activities in a log file. Build the MFTP on UDP ports. Use sequence number on data blocks to enforce reliability.
The message from the client and the response from the server are transmitted as the content of UDP datagrams. Each message is identified by a packet type as follows:
Packet type| Explanation
0 | ACK packet(i.e., acknowledgement packet).
1 | Request a file to be read from the server i.e., this packet carries file name to be downloaded.
2 | Data packet, i.e, contains data being transferred
3 | Reports an error [error message: File not found]
4 | EOT packet, i.e., it is an end-of-transmission (EOT) packet
Format of the UDP packet being transmitted between the client and the server is:
Type of the packet Sequence number Length (data size) Data (<= 16 bytes), depending on the packet type.
The EOT packet is in the same format as a regular data packet, except that its type field is set to 4 and its length is set to zero.
The server can close its connection and exit only after it has received ACKs for all data packets it has sent.
The length field specifies the number of characters carried in the data field. It should be in the range of 0 to 15.
For ACK packets, length should be set to zero.
Implementation:
Both client and server will run from the command line as follows: >client >server The is the name of the file to be downloaded from the server.
The RN is used by the server to emulate lost packets as,
If RN = 0 all packets are received by the receiver,
If RN = 1 first packet of the current winodw is lost,
If RN = 2 all packets of the current window are lost.
Output: In order to observe the performance of the sliding window protocol and to check whether the implementation is working properly, collect statistics. Collect statistics for both the client and server. After transferring a file print out the following suggested statistics
1. the total number of data packets transmitted
2. the total number of retransmissions
3. the total number of acknowledgments sent
4. the total number of acknowledgments received
5. the total number of duplicate packets received
6. the total amount of data sent.
7. A log file of actions, recording all activities between the client and server.
Supplied Code:
A packet class:
public class Packet
{
/* Packet Constructor:
* t = type
* i = seqno
*j = size of the packet
*/ public
Packet(int t, int i, int j, byte abyte[]) {
type = t;
seqno = i;
size = j;
data = new byte[size];
data = abyte; }
public byte[] getData() {
return data; }
public int getSeqNo() {
return seqno; }
public int getSize() {
return size; }
public boolean getType() {
return type; }
public String toString() {
return "type: " + type + "seq: " + seqno + " size: " + size + " data: " + data; }
private int type;
private int seqno;
private int size;
private byte data[];
}
Explanation / Answer
JAVA CODE:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class Go_BACK_N_Protocol implements Runnable {
private int Window_size;
private long Time_out;
private Substrate Sub_;
private ArrayBlockingQueue<String> frmSource;
private ArrayBlockingQueue<String> toSink;
private Packet[] Data_send_Buffer;
private short Data_send_Base = 0;
private short Data_send_Seq_num = 0;
private short Duplicate_ACKS = 0;
private Packet[] Received_Buff;
private short Received_BASE = 0;
private short Excepotential_SEQ_NUM = 0;
private short Last_received = -1;
private long Data_Now = 0;
private long Data_sndAGN = 0;
private Thread mythrd;
private boolean quit;
public static void main(String args[]){
Go_BACK_N_Protocol .obj=new Go_BACK_N_Protocol();
obj.Go_BACK_N_Protocol(Window_size, Time_out, Sub_);
}
Go_BACK_N_Protocol(int Window_size, double Time_out, Substrate Sub_)
{
this.Window_size = Math.min(Window_size,(1 << 14) - 1);
this.Time_out = ((long) (Time_out * 1000000000));
this.Sub_ = Sub_;
frmSource = new ArrayBlockingQueue<String>(1000,true);
toSink = new ArrayBlockingQueue<String>(1000,true);
quit = false;
Data_send_Buffer = new Packet[2*Window_size];
Received_Buff = new Packet[2*Window_size];
}
/** Start the Go_BACK_N_Protocol running. */
public void start() throws Exception {
mythrd = new Thread(this); mythrd.start();
}
/** Stop the Go_BACK_N_Protocol. */
public void stop() throws Exception { quit = true; mythrd.join(); }
private short incr(short x) {
x++; return (x < 2*Window_size ? x : 0);
}
private short decr(short x) {
--x; return (short)(x >= 0 ? x : 2*Window_size-1);
}
private int diff(short x, short y) {
return (x >= y ? x-y : (x + 2*Window_size) - y);
}
private void waitForSub() {
while (!Sub_.Ready_to_transmit()) {
try {
Thread.sleep(1);
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:run: sleep exception " + e);
}
}
}
private void reData_SendAll() {
short next = Data_send_Base;
while (diff(Data_send_Seq_num, next) > 0) {
waitForSub();
Sub_.Data_Send(Data_send_Buffer[next]);
next = incr(next);
}
}
public void run() {
long t0 = System.nanoTime();
long Data_Now = 0; // current time (relative to t0)
boolean stopped = false; // since we don't have a timer "pause" command
Packet p;
while (!quit || diff(Data_send_Seq_num,Data_send_Base) > 0) {
Data_Now = System.nanoTime() - t0;
if (stopped) Data_sndAGN = Data_Now + Time_out;
// We have a packet to be delivered up to the application
if (Received_Buff[Received_BASE] != null &&
toSink.remainingCapacity() > 0)
{
p = Received_Buff[Received_BASE];
try {
toSink.put(p.payload);
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:run: queue "+ "exception " + e);
}
Received_Buff[Received_BASE] = null;
Received_BASE = incr(Received_BASE);
}
// We are receiving an Recieving packet from another Data_Sender
else if (Sub_.Recieving())
{
p = Sub_.incoming();
if (p.type == 0) // data packet
{
Packet ack = new Packet();
ack.type = 1;
if (p.seqNum == Excepotential_SEQ_NUM &&
Received_Buff[Excepotential_SEQ_NUM] == null)
{
Received_Buff[Excepotential_SEQ_NUM] = p;
Last_received = Excepotential_SEQ_NUM;
ack.seqNum = Excepotential_SEQ_NUM;
Excepotential_SEQ_NUM = incr(Excepotential_SEQ_NUM);
}
else
{
if (Last_received == -1) continue;
ack.seqNum = Last_received;
}
waitForSub();
Sub_.Data_Send(ack);
}
else if (p.type == 1) // ack packet
{
if (p.seqNum == decr(Data_send_Base))
{
++Duplicate_ACKS;
if (Duplicate_ACKS == 3) {
reData_SendAll();
Data_sndAGN = Data_Now + Time_out;
stopped = false;
}
}
else if (diff(p.seqNum,Data_send_Base) < diff(Data_send_Seq_num,Data_send_Base) &&
Data_send_Buffer[p.seqNum] != null)
{
Duplicate_ACKS = 0;
while (Data_send_Base != incr(p.seqNum)) {
Data_send_Buffer[Data_send_Base] = null;
Data_send_Base = incr(Data_send_Base);
}
Data_sndAGN = Data_Now + Time_out;
if (Data_send_Base == Data_send_Seq_num) {
stopped = true;
}
}
}
}
else if (Data_send_Base != Data_send_Seq_num &&
Data_Now > Data_sndAGN)
{
reData_SendAll();
Data_sndAGN = Data_Now + Time_out;
stopped = false;
}
else if (frmSource.size() > 0 &&
Sub_.Ready_to_transmit() &&
diff(Data_send_Seq_num,Data_send_Base) < Window_size)
{
p = new Packet();
p.type = 0;
p.seqNum = Data_send_Seq_num;
try {
p.payload = frmSource.take();
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:run: queue exception " + e);
}
Data_send_Buffer[Data_send_Seq_num] = p;
Sub_.Data_Send(p);
if (Data_send_Base == Data_send_Seq_num) {
Data_sndAGN = Data_Now + Time_out;
stopped = false;
}
Data_send_Seq_num = incr(Data_send_Seq_num);
}
else
{
try {
Thread.sleep(1);
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:run: sleep exception " + e);
}
}
}
}
public void Data_Send(String message) {
try {
frmSource.put(message);
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:Data_Send: put exception" + e);
System.exit(1);
}
}
public boolean Ready_to_transmit() { return frmSource.remainingCapacity() > 0; }
public String incoming() {
String s = null;
try {
s = toSink.take();
} catch(Exception e) {
System.err.println("Go_BACK_N_Protocol:Data_Send: take exception" + e);
System.exit(1);
}
return s;
}
public boolean Recieving() { return toSink.size() > 0; }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.