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
|
package org.bouncycastle.crypto.tls.test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.Vector;
import org.bouncycastle.crypto.tls.DatagramTransport;
public class MockDatagramAssociation
{
private int mtu;
private MockDatagramTransport client, server;
public MockDatagramAssociation(int mtu)
{
this.mtu = mtu;
Vector clientQueue = new Vector();
Vector serverQueue = new Vector();
this.client = new MockDatagramTransport(clientQueue, serverQueue);
this.server = new MockDatagramTransport(serverQueue, clientQueue);
}
public DatagramTransport getClient()
{
return client;
}
public DatagramTransport getServer()
{
return server;
}
private class MockDatagramTransport
implements DatagramTransport
{
private Vector receiveQueue, sendQueue;
MockDatagramTransport(Vector receiveQueue, Vector sendQueue)
{
this.receiveQueue = receiveQueue;
this.sendQueue = sendQueue;
}
public int getReceiveLimit()
throws IOException
{
return mtu;
}
public int getSendLimit()
throws IOException
{
return mtu;
}
public int receive(byte[] buf, int off, int len, int waitMillis)
throws IOException
{
synchronized (receiveQueue)
{
if (receiveQueue.isEmpty())
{
try
{
receiveQueue.wait(waitMillis);
}
catch (InterruptedException e)
{
// TODO Keep waiting until full wait expired?
}
if (receiveQueue.isEmpty())
{
return -1;
}
}
DatagramPacket packet = (DatagramPacket)receiveQueue.remove(0);
int copyLength = Math.min(len, packet.getLength());
System.arraycopy(packet.getData(), packet.getOffset(), buf, off, copyLength);
return copyLength;
}
}
public void send(byte[] buf, int off, int len)
throws IOException
{
if (len > mtu)
{
// TODO Simulate rejection?
}
byte[] copy = new byte[len];
System.arraycopy(buf, off, copy, 0, len);
DatagramPacket packet = new DatagramPacket(copy, len);
synchronized (sendQueue)
{
sendQueue.addElement(packet);
sendQueue.notify();
}
}
public void close()
throws IOException
{
// TODO?
}
}
}
|