/* * Created on Nov 23, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package UnreliableNetwork; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocketImpl; import java.net.InetAddress; import java.net.SocketAddress; import java.net.SocketException; /** * @author marcod * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class DatagramSocket extends java.net.DatagramSocket { /** * threshold, packets where Math random returns less than this are lost ... */ double threshold = 0.0; /** by default 10% of the packets get lost
* Using the DatagramSocket(int,InetAddress,double) constructor, * you can set up the proper value of lost packets (a double in * the range [0,1] 0 => no packets lost, 1 => all packets are lost */ private final double defaultThreshold = 0.1; /** * @throws java.net.SocketException */ public DatagramSocket() throws SocketException { super(); threshold = defaultThreshold; } /** * @param arg0 */ public DatagramSocket(DatagramSocketImpl arg0) { super(arg0); threshold = defaultThreshold; } /** * @param arg0 * @throws java.net.SocketException */ public DatagramSocket(SocketAddress arg0) throws SocketException { super(arg0); threshold = defaultThreshold; } /** * @param arg0 * @throws java.net.SocketException */ public DatagramSocket(int arg0) throws SocketException { super(arg0); threshold = defaultThreshold; } /** * @param arg0 * @param arg1 * @throws java.net.SocketException */ public DatagramSocket(int arg0, InetAddress arg1) throws SocketException { super(arg0, arg1); threshold = defaultThreshold; } public DatagramSocket(int arg0, InetAddress arg1, double threshold) throws SocketException { super(arg0, arg1); this.threshold = threshold; } /** questo di fatto è l'unico metodo "modificato" rispetto ai metodi originali * se il valore pseuodcasuale stà sotto alla soglia, il pacchetto viene scartato ma dal punto di vista * di chi invoca la send è come se fosse stato spedito ... */ public void send(DatagramPacket p) throws IOException { double random = Math.random(); if(random < threshold) { System.out.println("!!! Packet LOST: to "+p.getAddress() + " port "+ p.getPort()); return; } // System.out.println("Packet sent: to "+p.getAddress() + " port "+ p.getPort()); super.send(p); } }