-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPServer.java
229 lines (219 loc) · 5.45 KB
/
UDPServer.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.BindException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import java.util.Random;
import java.lang.Math;
public class UDPServer
{
// constante
private static final int PAYLOADLENGTH = 512;
private static final int LONGLENGTH = 8;
private static final int LENGTH = PAYLOADLENGTH+LONGLENGTH;
private static final int MODULO = 256;
// socket
private DatagramSocket dataSocket;
private DatagramSocket messageSocket;
private Packet dataPacket;
private Packet messagePacket;
private DatagramPacket ackPacket;
private InetAddress clientAddress = null ;
private int hostPort = 0 ;
private int clientPort = 0 ;
// data
private long Sequence = 0;
private long nextSequence=1;
private long WIN;
// message
private byte[] message = new byte[LENGTH];
// fichier
private Vector<byte[]> fileSequences;
private int nbSequence;
private int lost;
// CRC
private Checksum CRC;
// constructeur
public UDPServer(int p, long ws, String f, int l) throws Exception
{
// configurer socket
dataSocket = new DatagramSocket(p);
messageSocket = new DatagramSocket();
hostPort = p;
// configurer la fenêtre
WIN = ws;
// Découper le fichier
fileSequences = segmentFile(f);
// pourcentage de perte et d'erreur
lost = l;
// démarrer les transactions
transaction();
}
// public
public void transaction() throws Exception
{
while (true)
{
System.out.println("Attente du destinataire (couple IP/port)");
rx(dataSocket, message);
System.out.println("Configuration destinataire réussi...");
try
{
while(Sequence != nbSequence)
{
System.out.println("Début de la transmission DATA " + Sequence + " " + nextSequence + " " +nbSequence);
// envoie des paquets
for (long i=Sequence;i<((Sequence+WIN>nbSequence)?nbSequence:Sequence+WIN);i++)
{
if (lost == 100 || !sequenceLost(lost))
{
sendData(fileSequences.get((int)i), i);
}
}
System.out.println("Attente des ACKs dans l'ordre' " + Sequence + " " + nextSequence);
// reception des ack
waitAnswer();
}
}
catch (ArrayIndexOutOfBoundsException e)
{
// si la derniere sequence a la meme longueur que le payload
if (fileSequences.lastElement().length == PAYLOADLENGTH)
{
sendData(new byte[0], Sequence);
waitAnswer();
}
reset();
}
reset();
}
}
private void waitAnswer() throws Exception
{
for (long i=Sequence;i<((Sequence+WIN>nbSequence)?nbSequence:Sequence+WIN);i++)
{
DatagramPacket packet=new DatagramPacket(new byte[PAYLOADLENGTH], PAYLOADLENGTH);
rx(dataSocket, new byte[PAYLOADLENGTH]);
System.out.println("verifier si c'est un ACK" +messagePacket.getSequence()+" "+(Sequence+1)%MODULO);
if (messagePacket.getType() == MessageType.ACK.getType()
&& messagePacket.getSequence() == (nextSequence+1)%MODULO)
{
System.out.println("ACK détecté; glisser la fenêtre'");
nextSequence++;
}
else
{
break;
}
}
Sequence = nextSequence;
}
private void reset() throws Exception
{
System.out.println("Réinitialiser la séquence pour le client suivant");
Sequence = 0;
nextSequence = 1;
System.out.println("Réinitialiser le destinataire");
clientAddress = null;
clientPort = 0;
System.out.println("Transaction terminée");
}
private void tx(DatagramSocket socket, byte[] data) throws Exception
{
try
{
socket.send(
new DatagramPacket(
data,
data.length,
clientAddress,
clientPort
)
);
}
catch (IOException e)
{
System.err.println("Erreur : " + e);
}
}
private void rx(DatagramSocket socket, byte[] data) throws Exception
{
try
{
DatagramPacket packet=new DatagramPacket(data, data.length);
socket.receive(packet);
messagePacket = Packet.parseUDPdata(data);
if (clientAddress == null)
{
clientAddress = packet.getAddress();
clientPort = packet.getPort();
}
}
catch (IOException e)
{
System.err.println("Erreur : " + e);
}
}
private void sendData(byte[] data, long seq) throws Exception
{
CRC = new CRC32();
CRC.update(data, 0, data.length);
tx(
dataSocket,
Packet.Create
(
MessageType.DATA,
seq%MODULO,
data,
WIN-1,
(char)CRC.getValue()
).getUDPdata()
);
}
// private
private Vector<byte[]> segmentFile(String f) throws Exception
{
Vector<byte[]> sequenceList = new Vector<byte[]>();
try
{
//Open the input and out files for the streams
FileInputStream file= new FileInputStream(f);
while (file.available() > PAYLOADLENGTH)
{
byte[] b = new byte[PAYLOADLENGTH];
file.read(b);
sequenceList.add(b);
}
byte[] b = new byte[file.available()];
file.read(b);
sequenceList.add(b);
file.close();
}
catch (FileNotFoundException e)
{
System.err.println("Erreur : le fichier "+f+" est introuvable");
}
catch (Exception e)
{
System.err.println("Erreur : " + e);
}
nbSequence = sequenceList.size();
return sequenceList;
}
private boolean sequenceLost(int percentage)
{
boolean b = false;
Random rand = new Random();
if (rand.nextInt(percentage) == 0) {
System.out.println("you got lucky Lost " + nbSequence);
}
return b;
}
}