Added pseudoTCP
This commit is contained in:
parent
46400e0009
commit
fa8ec11955
54
build.xml
54
build.xml
@ -145,10 +145,10 @@
|
||||
for a general build success while this might not be the case.-->
|
||||
<echo message="Generating HTML test reports ..."/>
|
||||
<java fork="true"
|
||||
classname="org.apache.tools.ant.launch.Launcher"
|
||||
classpath="${java.class.path}:${lib}/xalan-2.6.0.jar"
|
||||
failonerror="true"
|
||||
output="${junit.html.reports}/output.txt">
|
||||
classname="org.apache.tools.ant.launch.Launcher"
|
||||
classpath="${java.class.path}:${lib}/xalan-2.6.0.jar"
|
||||
failonerror="true"
|
||||
output="${junit.html.reports}/output.txt">
|
||||
<arg value="htmlreport"/>
|
||||
</java>
|
||||
</target>
|
||||
@ -156,36 +156,64 @@
|
||||
<!-- runs our sample distributed application -->
|
||||
<target name="run-sample">
|
||||
<java fork="true"
|
||||
classname="test.Ice"
|
||||
classpathref="project.class.path">
|
||||
classname="test.Ice"
|
||||
classpathref="project.class.path">
|
||||
|
||||
<!-- Tell java.util.logging about our logging preferences -->
|
||||
<sysproperty key="java.util.logging.config.file"
|
||||
value="lib/logging.properties"/>
|
||||
value="lib/logging.properties"/>
|
||||
</java>
|
||||
</target>
|
||||
|
||||
<!-- Run pseudotcp sample application -->
|
||||
<target name="run-pseudotcp" depends="compile">
|
||||
<java fork="true"
|
||||
classname="test.IcePseudoTcp"
|
||||
classpathref="project.class.path">
|
||||
|
||||
<!-- Tell java.util.logging about our logging preferences -->
|
||||
<sysproperty key="java.util.logging.config.file"
|
||||
value="lib/logging.properties"/>
|
||||
</java>
|
||||
</target>
|
||||
<!-- Debug pseudotcp example with netbeans -->
|
||||
<target name="nb-debug-pseudotcp" depends="compile">
|
||||
<nbjpdastart addressproperty="jpda.address" name="ice4j" transport="dt_socket">
|
||||
<classpath refid="project.class.path"/>
|
||||
</nbjpdastart>
|
||||
<java classname="test.IcePseudoTcp" fork="true">
|
||||
<classpath refid="project.class.path"/>
|
||||
<jvmarg value="-Xdebug"/>
|
||||
<jvmarg value="-Xnoagent"/>
|
||||
<jvmarg value="-Djava.compiler=none"/>
|
||||
<jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/>
|
||||
<!-- Tell java.util.logging about our logging preferences -->
|
||||
<sysproperty key="java.util.logging.config.file"
|
||||
value="lib/logging.properties"/>
|
||||
</java>
|
||||
</target>
|
||||
|
||||
<!-- runs our sample distributed application -->
|
||||
<target name="run-dist">
|
||||
<java fork="true"
|
||||
classname="test.IceDistributed"
|
||||
classpathref="project.class.path">
|
||||
classname="test.IceDistributed"
|
||||
classpathref="project.class.path">
|
||||
|
||||
<!-- Tell java.util.logging about our logging preferences -->
|
||||
<sysproperty key="java.util.logging.config.file"
|
||||
value="lib/logging.properties"/>
|
||||
value="lib/logging.properties"/>
|
||||
</java>
|
||||
</target>
|
||||
|
||||
<!-- runs our sample lite application -->
|
||||
<target name="run-lite">
|
||||
<java fork="true"
|
||||
classname="test.IceLite"
|
||||
classpathref="project.class.path">
|
||||
classname="test.IceLite"
|
||||
classpathref="project.class.path">
|
||||
|
||||
<!-- Tell java.util.logging about our logging preferences -->
|
||||
<sysproperty key="java.util.logging.config.file"
|
||||
value="lib/logging.properties"/>
|
||||
value="lib/logging.properties"/>
|
||||
</java>
|
||||
</target>
|
||||
|
||||
|
||||
52
src/org/ice4j/pseudotcp/IPseudoTcpNotify.java
Executable file
52
src/org/ice4j/pseudotcp/IPseudoTcpNotify.java
Executable file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Notification of tcp events.
|
||||
* Is implemented by @link(PseudoTcpSocket) to expose stream functionality.
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
interface IPseudoTcpNotify
|
||||
{
|
||||
/**
|
||||
* Called when tcp enters opened state
|
||||
* @param tcp
|
||||
*/
|
||||
void OnTcpOpen(PseudoTCPBase tcp);
|
||||
|
||||
/**
|
||||
* Called when any data is available in read buffer
|
||||
* @param tcp
|
||||
*/
|
||||
void OnTcpReadable(PseudoTCPBase tcp);
|
||||
|
||||
/**
|
||||
* Called when there is free space available in the send buffer
|
||||
* @param tcp
|
||||
*/
|
||||
void OnTcpWriteable(PseudoTCPBase tcp);
|
||||
|
||||
/**
|
||||
* Called when tcp enters closed state
|
||||
* @param tcp
|
||||
* @param e null means no error
|
||||
*/
|
||||
void OnTcpClosed(PseudoTCPBase tcp, IOException e);
|
||||
|
||||
/**
|
||||
* Called when protocol requests packet transfer through the network.
|
||||
* @param tcp
|
||||
* @param buffer data
|
||||
* @param len data length
|
||||
* @return the result, see {@link WriteResult} description for more info
|
||||
*/
|
||||
WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len);
|
||||
}
|
||||
2132
src/org/ice4j/pseudotcp/PseudoTCPBase.java
Executable file
2132
src/org/ice4j/pseudotcp/PseudoTCPBase.java
Executable file
File diff suppressed because it is too large
Load Diff
788
src/org/ice4j/pseudotcp/PseudoTcpSocket.java
Executable file
788
src/org/ice4j/pseudotcp/PseudoTcpSocket.java
Executable file
@ -0,0 +1,788 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
public class PseudoTcpSocket implements IPseudoTcpNotify
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final java.util.logging.Logger logger =
|
||||
java.util.logging.Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
/**
|
||||
* Pseudotcp logic instance
|
||||
*/
|
||||
private final PseudoTCPBase pseudoTcp;
|
||||
/**
|
||||
* Datagram socket used to handle network operations
|
||||
*/
|
||||
private final DatagramSocket socket;
|
||||
/**
|
||||
* Current socket address of remote socket that we are connected to
|
||||
*/
|
||||
private SocketAddress remoteAddr;
|
||||
/**
|
||||
* Receive buffer size used for receiving packets TODO: this should be
|
||||
* checked with MTU ?
|
||||
*/
|
||||
private int DATAGRAM_RCV_BUFFER_SIZE = 70000;
|
||||
/**
|
||||
* Monitor object used to block threads on write operation. That is when the
|
||||
* send buffer is full.
|
||||
*/
|
||||
private final Object write_notify = new Object();
|
||||
/**
|
||||
* Monitor object used to block threads on read operation. That is when
|
||||
* there's no more data available for reading.
|
||||
*/
|
||||
private final Object read_notify = new Object();
|
||||
/**
|
||||
* Monitor object used to block thread waiting for change of TCP state.
|
||||
*/
|
||||
private final Object state_notify = new Object();
|
||||
/**
|
||||
* Monitor object used by clock thread. Clock thread sleeps for some
|
||||
* intervals given by pseudotcp logic, but sometimes it is required to
|
||||
* finish this sleep earlier. In that case notify method of this monitor is
|
||||
* called.
|
||||
*/
|
||||
private final Object clock_notify = new Object();
|
||||
/**
|
||||
* Exception which occurred in pseudotcp logic and must be propagated to
|
||||
* threads blocked on any operations.
|
||||
*/
|
||||
private IOException exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param conv_id conversation id, must be the same on both sides
|
||||
* @param sock datagram socket used for network operations
|
||||
*/
|
||||
public PseudoTcpSocket(long conv_id, DatagramSocket sock)
|
||||
{
|
||||
pseudoTcp = new PseudoTCPBase(this, conv_id);
|
||||
this.socket = sock;
|
||||
}
|
||||
|
||||
/**
|
||||
* This constructor creates <tt>DatagramSocket</tt> with random port. Should
|
||||
* be used for clients.
|
||||
*
|
||||
* @param conv_id conversation id, must be the same on both sides
|
||||
* @throws SocketException
|
||||
*/
|
||||
public PseudoTcpSocket(long conv_id)
|
||||
throws SocketException
|
||||
{
|
||||
this(conv_id, new DatagramSocket());
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds <tt>DatagramSocket</tt> to given <tt>local_port</tt>
|
||||
*
|
||||
* @param conv_id conversation id, must be the same on both sides
|
||||
* @param local_port
|
||||
* @throws SocketException
|
||||
*/
|
||||
public PseudoTcpSocket(long conv_id, int local_port)
|
||||
throws SocketException
|
||||
{
|
||||
this(conv_id, new DatagramSocket(local_port));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates DatagramSocket for <tt>local_ip</tt>:<tt>local_port</tt>
|
||||
*
|
||||
* @param conv_id conversation id, must be the same on both sides
|
||||
* @param local_ip used by <tt>DatagramSocket</tt>
|
||||
* @param local_port used by <tt>DatagramSocket</tt>
|
||||
* @throws SocketException
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
public PseudoTcpSocket(long conv_id, String local_ip, int local_port)
|
||||
throws SocketException,
|
||||
UnknownHostException
|
||||
{
|
||||
this(conv_id, new DatagramSocket(local_port,
|
||||
InetAddress.getByName(local_ip)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Start connection procedure
|
||||
*
|
||||
* @param remoteAddress to which this socket connects to
|
||||
* @param timeout for this operation in ms
|
||||
* @throws IOException
|
||||
*/
|
||||
public void Connect(InetSocketAddress remoteAddress, long timeout)
|
||||
throws IOException
|
||||
{
|
||||
this.remoteAddr = remoteAddress;
|
||||
StartThreads();
|
||||
pseudoTcp.Connect();
|
||||
UpdateClock();
|
||||
try
|
||||
{
|
||||
long elapsed = 0;
|
||||
//Here the threads is blocked untill we reach TCP_ESTABLISHED state
|
||||
//There's also check for timeout for that op
|
||||
synchronized (state_notify)
|
||||
{
|
||||
while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED
|
||||
&& elapsed < timeout)
|
||||
{
|
||||
long start = System.currentTimeMillis();
|
||||
state_notify.wait(timeout);
|
||||
long end = System.currentTimeMillis();
|
||||
elapsed += end - start;
|
||||
}
|
||||
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
|
||||
{
|
||||
throw new IOException("Connect timeout");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
Close();
|
||||
throw new IOException("Connect aborted");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start connection procedure
|
||||
*
|
||||
* @param ip destination ip address
|
||||
* @param port destination port
|
||||
* @param timeout for this operation in ms
|
||||
* @throws IOException
|
||||
*/
|
||||
public void Connect(String ip, int port, long timeout)
|
||||
throws IOException
|
||||
{
|
||||
//this.remoteAddr = InetAddress.getByName(ip);
|
||||
//this.remotePort = port;
|
||||
Connect(new InetSocketAddress(InetAddress.getByName(ip), port), timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocking method waits for connection.
|
||||
*
|
||||
* @param timeout for this operation in ms
|
||||
* @throws IOException If socket gets closed or timeout expires
|
||||
*/
|
||||
public void Accept(int timeout)
|
||||
throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
StartThreads();
|
||||
PseudoTcpState state = pseudoTcp.getState();
|
||||
if (state == PseudoTcpState.TCP_CLOSED)
|
||||
{
|
||||
throw new IOException("Socket closed");
|
||||
}
|
||||
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
|
||||
{
|
||||
synchronized (state_notify)
|
||||
{
|
||||
state_notify.wait(timeout);
|
||||
}
|
||||
}
|
||||
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
|
||||
{
|
||||
throw new IOException("Accept timeout");
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
IOException e = new IOException("Accept aborted");
|
||||
pseudoTcp.closedown(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return current TCP state
|
||||
*/
|
||||
public PseudoTcpState getState()
|
||||
{
|
||||
return pseudoTcp.getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Interrupts clock thread's wait method to force time update
|
||||
*/
|
||||
private void UpdateClock()
|
||||
{
|
||||
synchronized (clock_notify)
|
||||
{
|
||||
clock_notify.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts all threads required by the socket
|
||||
*/
|
||||
private void StartThreads()
|
||||
{
|
||||
pseudoTcp.NotifyClock(System.currentTimeMillis());
|
||||
receiveThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
ReceivePackets();
|
||||
}
|
||||
}, "PseudoTcpReceiveThread");
|
||||
clockThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
RunClock();
|
||||
}
|
||||
}, "PseudoTcpClockThread");
|
||||
runReceive = true;
|
||||
runClock = true;
|
||||
receiveThread.start();
|
||||
clockThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the socket
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void Close() throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
pseudoTcp.Close(true);
|
||||
//System.out.println("ON CLOSE: in flight "+pseudoTcp.GetBytesInFlight());
|
||||
//System.out.println("ON CLOSE: buff not sent "+pseudoTcp.GetBytesBufferedNotSent());
|
||||
OnTcpClosed(pseudoTcp, null);
|
||||
socket.close();
|
||||
JoinAllThreads();
|
||||
//UpdateClock();
|
||||
//TODO: closing procedure
|
||||
//Here the thread should be blocked until TCP
|
||||
//reaches CLOSED state, but there's no closing procedure
|
||||
/*
|
||||
* synchronized(state_notify){ while(pseudoTcp.getState() !=
|
||||
* PseudoTcpState.TCP_CLOSED){ try { state_notify.wait(); } catch
|
||||
* (InterruptedException ex) { throw new IOException("Close
|
||||
* connection aborted"); } } }
|
||||
*/
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
throw new IOException("Closing socket interrupted", ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(IPseudoTcpNotify)
|
||||
* Called when TCP enters connected state.
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpOpen(PseudoTCPBase tcp)
|
||||
{
|
||||
logger.log(Level.FINE, "tcp opened");
|
||||
//Release threads blocked at state_notify monitor object.
|
||||
synchronized (state_notify)
|
||||
{
|
||||
state_notify.notifyAll();
|
||||
}
|
||||
//TCP is considered writeable at this point
|
||||
OnTcpWriteable(tcp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(IPseudoTcpNotify)
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpReadable(PseudoTCPBase tcp)
|
||||
{
|
||||
//release all thread blocked at read_notify monitor
|
||||
synchronized (read_notify)
|
||||
{
|
||||
logger.log(
|
||||
Level.FINER,
|
||||
"TCP READABLE data available for reading: ");
|
||||
read_notify.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(IPseudoTcpNotify)
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpWriteable(PseudoTCPBase tcp)
|
||||
{
|
||||
|
||||
logger.log(Level.FINER, "stream writeable");
|
||||
//release all threads blocked at write monitor
|
||||
synchronized (write_notify)
|
||||
{
|
||||
write_notify.notifyAll();
|
||||
}
|
||||
//writeSemaphore.release(1);
|
||||
logger.log(Level.FINER, "write notified - now !");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(IPseudoTcpNotify)
|
||||
*
|
||||
* @param tcp
|
||||
* @param e
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpClosed(PseudoTCPBase tcp, IOException e)
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, "PseudoTcp closed: " + e);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.FINE, "PseudoTcp closed");
|
||||
}
|
||||
runReceive = false;
|
||||
runClock = false;
|
||||
this.exception = e;
|
||||
releaseAllLocks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases all monitor objects so that the threads will check their "run
|
||||
* flags"
|
||||
*/
|
||||
private void releaseAllLocks()
|
||||
{
|
||||
synchronized (read_notify)
|
||||
{
|
||||
read_notify.notifyAll();
|
||||
}
|
||||
synchronized (write_notify)
|
||||
{
|
||||
write_notify.notifyAll();
|
||||
}
|
||||
synchronized (state_notify)
|
||||
{
|
||||
state_notify.notifyAll();
|
||||
}
|
||||
//this interrupt won't work for DatagramSocket read packet operation
|
||||
//receiveThread.interrupt();
|
||||
clockThread.interrupt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins all running threads
|
||||
*
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private void JoinAllThreads() throws InterruptedException
|
||||
{
|
||||
clockThread.join();
|
||||
receiveThread.join();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(IPseudoTcpNotify)
|
||||
*
|
||||
* @param tcp
|
||||
* @param buffer
|
||||
* @param len
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
|
||||
{
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
{
|
||||
logger.log(Level.FINEST, "write packet to network " + len);
|
||||
}
|
||||
try
|
||||
{
|
||||
DatagramPacket packet = new DatagramPacket(buffer, len, remoteAddr);
|
||||
socket.send(packet);
|
||||
return WriteResult.WR_SUCCESS;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE, "TcpWritePacket exception: " + ex);
|
||||
return WriteResult.WR_FAIL;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Flag which enables packets receive thread
|
||||
*/
|
||||
private boolean runReceive = false;
|
||||
/**
|
||||
* Thread receiving packets from the network
|
||||
*/
|
||||
private Thread receiveThread;
|
||||
|
||||
/**
|
||||
* Receives packets from the network and passes them to TCP logic class
|
||||
*/
|
||||
private void ReceivePackets()
|
||||
{
|
||||
byte[] buffer = new byte[DATAGRAM_RCV_BUFFER_SIZE];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, DATAGRAM_RCV_BUFFER_SIZE);
|
||||
while (runReceive)
|
||||
{
|
||||
try
|
||||
{
|
||||
socket.receive(packet);
|
||||
//Here is the binding point for remote socket if wasn't
|
||||
//specified earlier
|
||||
if (remoteAddr == null)
|
||||
{
|
||||
remoteAddr = packet.getSocketAddress();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!packet.getSocketAddress().equals(remoteAddr))
|
||||
{
|
||||
logger.log(Level.WARNING,
|
||||
"Ignoring packet from " + packet.getAddress()
|
||||
+ ":" + packet.getPort() + " should be: " + remoteAddr);
|
||||
}
|
||||
}
|
||||
synchronized (pseudoTcp)
|
||||
{
|
||||
pseudoTcp.NotifyPacket(buffer, packet.getLength());
|
||||
//we need to update the clock after new packet is receivied
|
||||
UpdateClock();
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
//this exception occurs even when the socket
|
||||
//is closed with the close operation, so we check
|
||||
//here if this exception is important
|
||||
if (runReceive)
|
||||
{
|
||||
logger.log(Level.SEVERE,
|
||||
"ReceivePackets exception: " + ex);
|
||||
pseudoTcp.closedown(ex);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* The run flag for clock thread
|
||||
*/
|
||||
private boolean runClock = false;
|
||||
private Thread clockThread;
|
||||
|
||||
/**
|
||||
* Method runs cyclic notification about time progress for TCP logic class
|
||||
* It runs in a separate thread
|
||||
*/
|
||||
private void RunClock()
|
||||
{
|
||||
long sleep;
|
||||
while (runClock)
|
||||
{
|
||||
synchronized (pseudoTcp)
|
||||
{
|
||||
pseudoTcp.NotifyClock(System.currentTimeMillis());
|
||||
sleep = pseudoTcp.GetNextClock(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
//there might be negative interval even if there's no error
|
||||
if (sleep == -1)
|
||||
{
|
||||
releaseAllLocks();
|
||||
if (exception != null)
|
||||
{
|
||||
logger.log(Level.SEVERE,
|
||||
"STATE: " + pseudoTcp.getState()
|
||||
+ " ERROR: " + exception.getMessage());
|
||||
}
|
||||
break;
|
||||
}
|
||||
synchronized (clock_notify)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.log(Level.FINEST, "Clock sleep for " + sleep);
|
||||
clock_notify.wait(sleep);
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
//interruption means end of task, as in normal operation
|
||||
//notify will be called
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private OutputStream outputstream;
|
||||
|
||||
/**
|
||||
* Lazy initialization for socket class
|
||||
*
|
||||
* @return output socket object
|
||||
*/
|
||||
public OutputStream getOutputStream()
|
||||
{
|
||||
if (outputstream == null)
|
||||
{
|
||||
outputstream = new PseudoTcpOutputStream();
|
||||
}
|
||||
return outputstream;
|
||||
}
|
||||
private PseudoTcpInputStream inputStream;
|
||||
|
||||
/**
|
||||
* Lazy initialization for socket class
|
||||
*
|
||||
* @return output socket object
|
||||
*/
|
||||
public InputStream getInputStream()
|
||||
{
|
||||
if (inputStream == null)
|
||||
{
|
||||
inputStream = new PseudoTcpInputStream();
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class implements @link(InputStream) *
|
||||
*/
|
||||
class PseudoTcpInputStream extends InputStream
|
||||
{
|
||||
public PseudoTcpInputStream()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* There's no end of stream detection at the moment. Method blocks until
|
||||
* it returns any data or an exception is thrown
|
||||
*
|
||||
* @return read byte count
|
||||
* @throws IOException in case of en error
|
||||
*/
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
{
|
||||
byte[] buff = new byte[1];
|
||||
int readCount = read(buff, 0, 1);
|
||||
return readCount == 1 ? buff[0] : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] bytes) throws IOException
|
||||
{
|
||||
return read(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method blocks until any data is available
|
||||
*
|
||||
* @param buffer destination buffer
|
||||
* @param offset destination buffer's offset
|
||||
* @param length maximum count of bytes that can be read
|
||||
* @return byte count actually read
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException
|
||||
{
|
||||
int read;
|
||||
while (true)
|
||||
{
|
||||
logger.log(Level.FINER, "Read Recv");
|
||||
read = pseudoTcp.Recv(buffer, offset, length);
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER, "Read Recv read count: " + read);
|
||||
}
|
||||
if (read > 0)
|
||||
{
|
||||
return read;
|
||||
}
|
||||
try
|
||||
{
|
||||
synchronized (read_notify)
|
||||
{
|
||||
logger.log(Level.FINER, "Read wait for data available");
|
||||
read_notify.wait();
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER,
|
||||
"Read notified: " + pseudoTcp.GetAvailable());
|
||||
}
|
||||
}
|
||||
if (exception != null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
if (exception != null)
|
||||
{
|
||||
throw new IOException("Read aborted", exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException("Read aborted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException
|
||||
{
|
||||
return pseudoTcp.GetAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements @link(OutputStream)
|
||||
*/
|
||||
class PseudoTcpOutputStream extends OutputStream
|
||||
{
|
||||
@Override
|
||||
public void write(int b) throws IOException
|
||||
{
|
||||
byte[] bytes = new byte[1];
|
||||
bytes[0] = (byte) b;
|
||||
write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] bytes) throws IOException
|
||||
{
|
||||
write(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method blocks until all data has been written or an exception
|
||||
* occurs
|
||||
*
|
||||
* @param buffer source buffer
|
||||
* @param offset source buffer's offset
|
||||
* @param length byte count to be written
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void write(byte[] buffer, int offset, int length) throws IOException
|
||||
{
|
||||
int toSend = length;
|
||||
int sent;
|
||||
while (toSend > 0)
|
||||
{
|
||||
synchronized (pseudoTcp)
|
||||
{
|
||||
sent = pseudoTcp.Send(buffer, offset + length - toSend, toSend);
|
||||
}
|
||||
if (sent > 0)
|
||||
{
|
||||
toSend -= sent;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.log(Level.FINER, "Write wait for notify");
|
||||
synchronized (write_notify)
|
||||
{
|
||||
write_notify.wait();
|
||||
}
|
||||
logger.log(Level.FINER,
|
||||
"Write notified, available: "
|
||||
+ pseudoTcp.GetAvailableSendBuffer());
|
||||
if (exception != null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
if (exception != null)
|
||||
{
|
||||
throw new IOException("Write aborted", exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException("Write aborted", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method block until all buffered data has been written
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public synchronized void flush() throws IOException
|
||||
{
|
||||
logger.log(Level.FINE, "Flushing...");
|
||||
final Object ackNotify = pseudoTcp.GetAckNotify();
|
||||
while (pseudoTcp.GetBytesBufferedNotSent() > 0)
|
||||
{
|
||||
synchronized (ackNotify)
|
||||
{
|
||||
try
|
||||
{
|
||||
ackNotify.wait();
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
throw new IOException("Flush stream interrupted", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.log(Level.FINE, "Flushing completed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
38
src/org/ice4j/pseudotcp/PseudoTcpState.java
Executable file
38
src/org/ice4j/pseudotcp/PseudoTcpState.java
Executable file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
/**
|
||||
* TCP states defined for pseudoTCP
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public enum PseudoTcpState
|
||||
{
|
||||
/**
|
||||
* Initial state, can accept connection
|
||||
*/
|
||||
TCP_LISTEN,// = 0,
|
||||
/**
|
||||
* SYN sent to remote peer, wits for SYN
|
||||
*/
|
||||
TCP_SYN_SENT,// = 1,
|
||||
/**
|
||||
* SYN received from remote peer, sends back SYN
|
||||
*/
|
||||
TCP_SYN_RECEIVED,// = 2;
|
||||
/**
|
||||
* SYN sent and received - connection established
|
||||
*/
|
||||
TCP_ESTABLISHED,// = 3;
|
||||
/**
|
||||
* Closed state. In current implementation reached on error
|
||||
* or explicite by close method with force option
|
||||
* TODO: closing procedure
|
||||
*/
|
||||
TCP_CLOSED// = 4;
|
||||
};
|
||||
28
src/org/ice4j/pseudotcp/WriteResult.java
Executable file
28
src/org/ice4j/pseudotcp/WriteResult.java
Executable file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
/**
|
||||
* The result of write packet operations
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public enum WriteResult
|
||||
{
|
||||
/**
|
||||
* Packet successfully transmitted
|
||||
*/
|
||||
WR_SUCCESS,
|
||||
/**
|
||||
* Packet was too large
|
||||
*/
|
||||
WR_TOO_LARGE,
|
||||
/**
|
||||
* Write failed
|
||||
*/
|
||||
WR_FAIL
|
||||
};
|
||||
381
src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java
Executable file
381
src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java
Executable file
@ -0,0 +1,381 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp.util;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
/**
|
||||
* First in - first out byte buffer
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class ByteFifoBuffer
|
||||
{
|
||||
/**
|
||||
* Backing byte array
|
||||
*/
|
||||
private byte[] array;
|
||||
/**
|
||||
* Current write position
|
||||
*/
|
||||
private int write_pos = 0;
|
||||
/**
|
||||
* Stored bytes count
|
||||
*/
|
||||
private int buffered = 0;
|
||||
/**
|
||||
* Current read position
|
||||
*/
|
||||
private int read_pos = 0;
|
||||
|
||||
/**
|
||||
* Creates buffer of specified size
|
||||
*
|
||||
* @param len buffer's size
|
||||
*/
|
||||
public ByteFifoBuffer(int len)
|
||||
{
|
||||
array = new byte[len];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return buffer's capacity
|
||||
*/
|
||||
public int Length()
|
||||
{
|
||||
return array.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method reads <tt>count</tt> bytes into <tt>out_buffer</tt>
|
||||
* Current read position is incremented by count of bytes
|
||||
* that has been successfully read
|
||||
*
|
||||
* @param out_buffer
|
||||
* @param count
|
||||
* @return bytes successfully read
|
||||
*/
|
||||
public int Read(byte[] out_buffer, int count)
|
||||
{
|
||||
count = readLimit(count);
|
||||
if (count > 0)
|
||||
{
|
||||
readOp(out_buffer, 0, count, array, read_pos, array.length);
|
||||
read_pos = (read_pos + count) % array.length;
|
||||
buffered -= count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits <tt>desiredReadCount</tt> to count that is actually available
|
||||
* @param desiredReadCount
|
||||
* @return
|
||||
*/
|
||||
private int readLimit(int desiredReadCount)
|
||||
{
|
||||
return desiredReadCount > buffered ? buffered : desiredReadCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method used for read operations
|
||||
* @param outBuffer
|
||||
* @param dst_buff_offset
|
||||
* @param count
|
||||
* @param srcBuffer
|
||||
* @param read_pos
|
||||
* @param buff_len
|
||||
*/
|
||||
private static void readOp(byte[] outBuffer, int dst_buff_offset, int count,
|
||||
byte[] srcBuffer, int read_pos, int buff_len)
|
||||
{
|
||||
if (read_pos + count <= buff_len)
|
||||
{
|
||||
//single operation
|
||||
System.arraycopy(srcBuffer, read_pos, outBuffer, dst_buff_offset,
|
||||
count);
|
||||
}
|
||||
else
|
||||
{
|
||||
//two operations
|
||||
int tillEndCount = buff_len - read_pos;
|
||||
System.arraycopy(srcBuffer, read_pos, outBuffer,
|
||||
dst_buff_offset, tillEndCount);
|
||||
int fromStartCount = count - tillEndCount;
|
||||
System.arraycopy(srcBuffer, 0, outBuffer,
|
||||
dst_buff_offset + tillEndCount, fromStartCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return space left in buffer for write
|
||||
*/
|
||||
public int GetWriteRemaining()
|
||||
{
|
||||
return array.length - buffered;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bytes stored in buffer and available for reading
|
||||
*/
|
||||
public int GetBuffered()
|
||||
{
|
||||
return buffered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes <tt>count</tt> of bytes from the <tt>buffer</tt>
|
||||
*
|
||||
* @param buffer
|
||||
* @param count
|
||||
* @return bytes successfully written to buffer
|
||||
*/
|
||||
public int Write(byte[] buffer, int count)
|
||||
{
|
||||
return Write(buffer, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data source data
|
||||
* @param offset source buffer's offset
|
||||
* @param count
|
||||
* @return byte count actually read
|
||||
*/
|
||||
public int Write(byte[] data, int offset, int count)
|
||||
{
|
||||
/*
|
||||
* System.out.println("----write " + this + " " + len + " buffered " +
|
||||
* GetBuffered() + " buff avail: " + GetWriteRemaining());
|
||||
*/
|
||||
count = writeLimit(count);
|
||||
writeOp(data, offset, count, array, write_pos, array.length);
|
||||
write_pos = (write_pos + count) % array.length;
|
||||
buffered += count;
|
||||
/*
|
||||
* System.out.println("----write "+this+" "+len+" buffered
|
||||
* "+GetBuffered()); for(int i=0; i < len; i++){
|
||||
* System.out.println("WDATA: "+data[i]); }
|
||||
*/
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for write operations
|
||||
* @param inBuffer
|
||||
* @param inOffset
|
||||
* @param count
|
||||
* @param outBuffer
|
||||
* @param write_pos
|
||||
* @param buff_len
|
||||
*/
|
||||
private static void writeOp(byte[] inBuffer,
|
||||
int inOffset,
|
||||
int count,
|
||||
byte[] outBuffer,
|
||||
int write_pos,
|
||||
int buff_len)
|
||||
{
|
||||
if ((write_pos + count) <= buff_len)
|
||||
{
|
||||
//single op
|
||||
System.arraycopy(inBuffer, inOffset, outBuffer, write_pos, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
//till end and from beginning
|
||||
int tillEndCount;
|
||||
int fromStartCount;
|
||||
tillEndCount = buff_len - write_pos;
|
||||
fromStartCount = count - tillEndCount;
|
||||
System.arraycopy(inBuffer, inOffset, outBuffer,
|
||||
write_pos, tillEndCount);
|
||||
System.arraycopy(inBuffer, inOffset + tillEndCount,
|
||||
outBuffer, 0, fromStartCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits <tt>desiredWriteCount</tt> to what's actually available
|
||||
* @param desiredWriteCount
|
||||
* @return
|
||||
*/
|
||||
private int writeLimit(int desiredWriteCount)
|
||||
{
|
||||
return desiredWriteCount > (array.length - buffered) ?
|
||||
(array.length - buffered) : desiredWriteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if new write position is correct
|
||||
*
|
||||
* @param newWrPos new write position
|
||||
*/
|
||||
private void assertWriteLimit(int newWrPos)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
int spaceReq;
|
||||
int availSpace = GetWriteRemaining();
|
||||
if (newWrPos < write_pos)
|
||||
{
|
||||
spaceReq = newWrPos + (array.length - write_pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
spaceReq = newWrPos - write_pos;
|
||||
}
|
||||
|
||||
if (spaceReq > availSpace)
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances current buffer's write position by <tt>count</tt> bytes
|
||||
*
|
||||
* @param count
|
||||
*/
|
||||
public void ConsumeWriteBuffer(int count)
|
||||
throws IllegalArgumentException,
|
||||
BufferOverflowException
|
||||
{
|
||||
if (count > GetWriteRemaining())
|
||||
{
|
||||
throw new BufferOverflowException();
|
||||
}
|
||||
if (count < 0)
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
int newPos = (write_pos + count) % array.length;
|
||||
assertWriteLimit(newPos);
|
||||
|
||||
write_pos = newPos;
|
||||
buffered += count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets new buffer's capacity
|
||||
*
|
||||
* @param new_size
|
||||
* @return <tt>true</tt> if operation is possible to perform, that is if new
|
||||
* buffered data fits into new buffer
|
||||
*/
|
||||
public boolean SetCapacity(int new_size)
|
||||
{
|
||||
if (new_size < GetBuffered())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
byte[] newBuff = new byte[new_size];
|
||||
readOp(newBuff, 0, buffered, array, read_pos, array.length);
|
||||
this.array = newBuff;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aligns current read position by <tt>count</tt>
|
||||
*
|
||||
* @param count
|
||||
* @throws BufferUnderflowException if new position exceeds buffered data
|
||||
* count
|
||||
*/
|
||||
public void ConsumeReadData(int count)
|
||||
throws IllegalArgumentException,
|
||||
BufferUnderflowException
|
||||
{
|
||||
/*
|
||||
* System.out.println("Consume read " + this + " " + count + " read pos:
|
||||
* " + read_pos);
|
||||
*/
|
||||
if (count > buffered)
|
||||
{
|
||||
throw new BufferUnderflowException();
|
||||
}
|
||||
if (count < 0)
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.read_pos = (read_pos + count) % array.length;
|
||||
buffered -= count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads <tt>count</tt> bytes from buffer without storing new read position
|
||||
*
|
||||
* @param dst_buff
|
||||
* @param dst_buff_offset offset of destination buffer
|
||||
* @param count bytes to read
|
||||
* @param offset from current read position
|
||||
* @return bytes successfully read
|
||||
*/
|
||||
public int ReadOffset(byte[] dst_buff,
|
||||
int dst_buff_offset,
|
||||
int count,
|
||||
int offset)
|
||||
{
|
||||
//TODO: not sure if should decrease read count or throw an exception
|
||||
/*
|
||||
* System.out.println("Read dst offset " + dst_buff_offset + " offset "
|
||||
* + offset + " len " + count + " " + this);
|
||||
*/
|
||||
int read_offset = (this.read_pos + offset) % array.length;
|
||||
readOp(dst_buff, dst_buff_offset, count, array, read_offset, array.length);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes <tt>count</tt> bytes from <tt>data</tt> to the buffer without
|
||||
* affecting buffer's write position
|
||||
*
|
||||
* @param data
|
||||
* @param count
|
||||
* @param nOffset from buffer's write position
|
||||
* @return bytes successfully written
|
||||
*/
|
||||
public int WriteOffset(byte[] data, int count, int nOffset)
|
||||
throws BufferOverflowException
|
||||
{
|
||||
if (count > GetWriteRemaining())
|
||||
{
|
||||
throw new BufferOverflowException();
|
||||
}
|
||||
if (count < 0)
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
int offWritePos = (this.write_pos + nOffset) % array.length;
|
||||
count = writeLimit(count);
|
||||
//try{
|
||||
assertWriteLimit(offWritePos + count);
|
||||
//}catch(IllegalArgumentException e){
|
||||
// System.out.println("Write offset: " + nOffset + " count: " + count
|
||||
//+ " wpos: " + write_pos + " buffered: " + buffered + " len: " + length + this);
|
||||
// throw e;
|
||||
//}
|
||||
//buffer.position(writePos + nOffset);
|
||||
writeOp(data, 0, count, array, offWritePos, array.length);
|
||||
//buffer.position(writePos);
|
||||
return count;
|
||||
}
|
||||
|
||||
public void ResetReadPosition()
|
||||
{
|
||||
this.read_pos = 0;
|
||||
}
|
||||
|
||||
public void ResetWritePosition()
|
||||
{
|
||||
this.write_pos = 0;
|
||||
this.buffered = 0;
|
||||
}
|
||||
}
|
||||
75
test/org/ice4j/pseudotcp/MultiThreadSupportTest.java
Executable file
75
test/org/ice4j/pseudotcp/MultiThreadSupportTest.java
Executable file
@ -0,0 +1,75 @@
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import junit.framework.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* This class enables multi thread tests where main thread waits in loop
|
||||
* for specified condition to be met, while others perform some operations.
|
||||
* For example until the connection is established or closed.
|
||||
* It also registers default uncaught exception handler to catch exceptions
|
||||
* from other threads.
|
||||
*
|
||||
* Condition checks are passed as IWaitUntilDone interface.
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class MultiThreadSupportTest extends TestCase
|
||||
{
|
||||
private volatile Throwable testError;
|
||||
private volatile Thread errorThread;
|
||||
private final Object testLock = new Object();
|
||||
|
||||
public MultiThreadSupportTest()
|
||||
{
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
|
||||
{
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e)
|
||||
{
|
||||
synchronized (testLock)
|
||||
{
|
||||
testError = e;
|
||||
errorThread = t;
|
||||
testLock.notifyAll();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private long assertWaitInterval = 100;
|
||||
protected interface IWaitUntilDone
|
||||
{
|
||||
public boolean isDone();
|
||||
}
|
||||
|
||||
protected boolean assert_wait_until(IWaitUntilDone wait, long timeoutMs)
|
||||
{
|
||||
try
|
||||
{
|
||||
long start = System.currentTimeMillis();
|
||||
while (!wait.isDone() && (System.currentTimeMillis() - start) < timeoutMs)
|
||||
{
|
||||
synchronized (testLock)
|
||||
{
|
||||
testLock.wait(assertWaitInterval);
|
||||
if (testError != null)
|
||||
{
|
||||
testError.printStackTrace();
|
||||
fail("Error in thread: " + errorThread.getName() + " : " + testError.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
return wait.isDone();
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
fail("assert_wait - interrupted");
|
||||
//return is unreachable
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
139
test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java
Executable file
139
test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java
Executable file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.logging.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class PseudoTcpStreamTest extends MultiThreadSupportTest
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
|
||||
public PseudoTcpStreamTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test one-way transfer with @link(PseudoTcpStream)
|
||||
*
|
||||
* @throws SocketException
|
||||
*/
|
||||
public void testConnectTransferClose() throws SocketException
|
||||
{
|
||||
String ip = "";
|
||||
final int server_port = 49999;
|
||||
long conv_id = 0;
|
||||
final int size = 1000000;
|
||||
int transferTimeout = 5000;
|
||||
final PseudoTcpSocket server = new PseudoTcpSocket(conv_id, server_port);
|
||||
final PseudoTcpSocket client = new PseudoTcpSocket(conv_id);
|
||||
Thread serverThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
server.Accept(300);
|
||||
int rcvd = 0;
|
||||
while (rcvd != size)
|
||||
{
|
||||
rcvd += server.getInputStream().read(new byte[size]);
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER, "Received: " + rcvd);
|
||||
}
|
||||
}
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER, "Total received: " + rcvd);
|
||||
}
|
||||
//server.Close();
|
||||
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
Thread clientThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
client.Connect("localhost", server_port, 30000);
|
||||
assertEquals(PseudoTcpState.TCP_ESTABLISHED, client.getState());
|
||||
client.getOutputStream().write(new byte[size]);
|
||||
client.getOutputStream().flush();
|
||||
client.Close();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
serverThread.start();
|
||||
clientThread.start();
|
||||
try
|
||||
{
|
||||
boolean success = assert_wait_until(new IWaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return client.getState() == PseudoTcpState.TCP_CLOSED;
|
||||
}
|
||||
}, transferTimeout);
|
||||
if (success)
|
||||
{
|
||||
clientThread.join();
|
||||
serverThread.join();
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Transfer timeout");
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the timeout on accept method
|
||||
*/
|
||||
public void testAccept()
|
||||
{
|
||||
try
|
||||
{
|
||||
PseudoTcpSocket server = new PseudoTcpSocket(0);
|
||||
server.Accept(10);
|
||||
fail("Should throw timeout exception");
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
//success
|
||||
}
|
||||
}
|
||||
}
|
||||
684
test/org/ice4j/pseudotcp/PseudoTcpTestBase.java
Executable file
684
test/org/ice4j/pseudotcp/PseudoTcpTestBase.java
Executable file
@ -0,0 +1,684 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
import org.ice4j.pseudotcp.util.*;
|
||||
|
||||
/**
|
||||
* Base class for other pseduoTCP logic tests. Runs all threads required for the
|
||||
* protocol to work. There are two instances of pseudoTCP logic being run. Data
|
||||
* packets between them are passes directly with some loss and/or delay
|
||||
* introduced optionally.
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public abstract class PseudoTcpTestBase extends MultiThreadSupportTest
|
||||
implements IPseudoTcpNotify
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
/**
|
||||
* Remote peer TCP logic instance
|
||||
*/
|
||||
private final PseudoTCPBase remoteTcp;
|
||||
/**
|
||||
* Local peer TCP logic instance
|
||||
*/
|
||||
private final PseudoTCPBase localTcp;
|
||||
private int local_mtu_;
|
||||
private int remote_mtu_;
|
||||
/**
|
||||
* Delay in ms introduced to packets delivery
|
||||
*/
|
||||
private int delay_;
|
||||
/**
|
||||
* Simulated packets loss in %
|
||||
*/
|
||||
private int loss_;
|
||||
/**
|
||||
* Stores info about connection state for use by child classes
|
||||
*/
|
||||
protected boolean have_connected_;
|
||||
protected boolean have_disconnected_;
|
||||
/**
|
||||
* Timer used to delay packets delivery
|
||||
*/
|
||||
private Timer timer = new Timer("Delay timer");
|
||||
/**
|
||||
* Timeout for connect operation in ms
|
||||
*/
|
||||
static final int kConnectTimeoutMs = 5000; // ~3 * default RTO of 3000ms
|
||||
//static final int kMinTransferRate = 1050000;
|
||||
/**
|
||||
* Transfer rate used to calculate timeout for transfer operations. This
|
||||
* timeout counts before the transfer tests will fail.
|
||||
*/
|
||||
static final int kMinTransferRate = 1000;
|
||||
/**
|
||||
* Transfer blocks size
|
||||
*/
|
||||
static final int kBlockSize = 4096;
|
||||
|
||||
public PseudoTcpTestBase()
|
||||
{
|
||||
this.remoteTcp = new PseudoTCPBase(this, 1);
|
||||
//Debug names are usefull to identify peers in log messages
|
||||
remoteTcp.debugName = "REM";
|
||||
this.localTcp = new PseudoTCPBase(this, 1);
|
||||
localTcp.debugName = "LOC";
|
||||
SetLocalMtu(65535);
|
||||
SetRemoteMtu(65535);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates some random data array
|
||||
*
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
static byte[] createDummyData(int size)
|
||||
{
|
||||
byte[] dummy = new byte[size];
|
||||
Random r = new Random();
|
||||
r.nextBytes(dummy);
|
||||
return dummy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <tt>mtu</tt> for local peer
|
||||
*
|
||||
* @param mtu
|
||||
*/
|
||||
void SetLocalMtu(int mtu)
|
||||
{
|
||||
localTcp.NotifyMTU(mtu);
|
||||
local_mtu_ = mtu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <tt>mtu</tt> for remote peer
|
||||
*
|
||||
* @param mtu
|
||||
*/
|
||||
void SetRemoteMtu(int mtu)
|
||||
{
|
||||
remoteTcp.NotifyMTU(mtu);
|
||||
remote_mtu_ = mtu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay introduced to packets delivery between peers
|
||||
*
|
||||
* @param delay
|
||||
*/
|
||||
void SetDelay(int delay)
|
||||
{
|
||||
delay_ = delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets loss % of packets transferred between local and remote peers
|
||||
*
|
||||
* @param percent
|
||||
*/
|
||||
void SetLoss(int percent)
|
||||
{
|
||||
loss_ = percent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets OptNagling for both local and remote peers
|
||||
*
|
||||
* @param enable_nagles
|
||||
*/
|
||||
void SetOptNagling(boolean enable_nagles)
|
||||
{
|
||||
localTcp.SetOption(Option.OPT_NODELAY, enable_nagles ? 0 : 1);
|
||||
remoteTcp.SetOption(Option.OPT_NODELAY, enable_nagles ? 0 : 1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ack delay option for local and remote peers
|
||||
*
|
||||
* @param ack_delay
|
||||
*/
|
||||
void SetOptAckDelay(int ack_delay)
|
||||
{
|
||||
localTcp.SetOption(Option.OPT_ACKDELAY, ack_delay);
|
||||
remoteTcp.SetOption(Option.OPT_ACKDELAY, ack_delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets send buffer option for local and remote peers
|
||||
*
|
||||
* @param size
|
||||
*/
|
||||
void SetOptSndBuf(int size)
|
||||
{
|
||||
localTcp.SetOption(Option.OPT_SNDBUF, size);
|
||||
remoteTcp.SetOption(Option.OPT_SNDBUF, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets receive buffer size option for remote peer
|
||||
*
|
||||
* @param size
|
||||
*/
|
||||
void SetRemoteOptRcvBuf(int size)
|
||||
{
|
||||
remoteTcp.SetOption(Option.OPT_RCVBUF, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets receive buffer size option for local peer
|
||||
*
|
||||
* @param size
|
||||
*/
|
||||
void SetLocalOptRcvBuf(int size)
|
||||
{
|
||||
localTcp.SetOption(Option.OPT_RCVBUF, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable window scaling for remote peer
|
||||
*/
|
||||
void DisableRemoteWindowScale()
|
||||
{
|
||||
remoteTcp.disableWindowScale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable window scaling for local peer
|
||||
*/
|
||||
void DisableLocalWindowScale()
|
||||
{
|
||||
localTcp.disableWindowScale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the connection from local to remote peer
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
void Connect() throws IOException
|
||||
{
|
||||
localTcp.Connect();
|
||||
UpdateLocalClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection
|
||||
*/
|
||||
void Close()
|
||||
{
|
||||
localTcp.Close(false);
|
||||
UpdateLocalClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches the event OnTcpOpen on the local peer and marks have_connected
|
||||
* flag
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpOpen(PseudoTCPBase tcp)
|
||||
{
|
||||
if (tcp == localTcp)
|
||||
{
|
||||
have_connected_ = true;
|
||||
OnTcpWriteable(tcp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches OnTcpClosed event on remote peer and marks have_disconnected flag
|
||||
*
|
||||
* @param tcp
|
||||
* @param exc
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpClosed(PseudoTCPBase tcp, IOException exc)
|
||||
{
|
||||
assert exc == null;
|
||||
if (tcp == remoteTcp)
|
||||
{
|
||||
have_disconnected_ = true;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Randomizer instance used to decide about packet loss
|
||||
*/
|
||||
private Random random = new Random();
|
||||
|
||||
int RandomInt()
|
||||
{
|
||||
return random.nextInt(100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the <tt>data</tt> from local to remote peer
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
int LocalSend(byte[] data, int len) throws IOException
|
||||
{
|
||||
return localTcp.Send(data, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive data as local peer
|
||||
*
|
||||
* @param buffer
|
||||
* @param len
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
int LocalRecv(byte[] buffer, int len) throws IOException
|
||||
{
|
||||
return localTcp.Recv(buffer, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive data as remote peer
|
||||
*
|
||||
* @param buffer
|
||||
* @param len
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
int RemoteRecv(byte[] buffer, int len) throws IOException
|
||||
{
|
||||
|
||||
return remoteTcp.Recv(buffer, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the <tt>data</tt> from remote to local peer
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
int RemoteSend(byte[] data, int len)
|
||||
throws IOException
|
||||
{
|
||||
return remoteTcp.Send(data, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulates packet received by local peer
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @throws IOException
|
||||
*/
|
||||
private void LocalPacket(byte[] data, int len)
|
||||
throws IOException
|
||||
{
|
||||
localTcp.NotifyPacket(data, len);
|
||||
UpdateLocalClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulates packet received by remote peer
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @throws IOException
|
||||
*/
|
||||
private void RemotePacket(byte[] data, int len)
|
||||
throws IOException
|
||||
{
|
||||
remoteTcp.NotifyPacket(data, len);
|
||||
UpdateRemoteClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates <tt>TimerTask</tt> with @link(RemotePacket) action
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @return
|
||||
*/
|
||||
private TimerTask getWriteRemotePacketTask(final byte[] data, final int len)
|
||||
{
|
||||
return new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
RemotePacket(data, len);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates <tt>TimerTask</tt> with @link(LocalPacket) action
|
||||
*
|
||||
* @param data
|
||||
* @param len
|
||||
* @return
|
||||
*/
|
||||
private TimerTask getWriteLocalPacketTask(final byte[] data, final int len)
|
||||
{
|
||||
return new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
LocalPacket(data, len);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles passing packets between local and remote peers. Here are taken
|
||||
* decisions about packets loss and delay.
|
||||
*
|
||||
* @param tcp
|
||||
* @param buffer
|
||||
* @param len
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
|
||||
{
|
||||
// Randomly drop the desired percentage of packets.
|
||||
// Also drop packets that are larger than the configured MTU.
|
||||
if (RandomInt() < loss_)
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Randomly dropping packet, size=" + len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len > Math.min(local_mtu_, remote_mtu_))
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE,
|
||||
"Dropping packet that exceeds path MTU, size="
|
||||
+ len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tcp == localTcp)
|
||||
{
|
||||
timer.schedule(getWriteRemotePacketTask(buffer, len), delay_);
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.schedule(getWriteLocalPacketTask(buffer, len), delay_);
|
||||
}
|
||||
}
|
||||
}
|
||||
return WriteResult.WR_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wakes up local clock thread from wait method causing forced time update
|
||||
*/
|
||||
protected void UpdateLocalClock()
|
||||
{
|
||||
if (localClockThread != null)
|
||||
{
|
||||
synchronized (localClockLock)
|
||||
{
|
||||
localClockLock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wakes up remote clock thread from wait method causing forced time update
|
||||
*/
|
||||
protected void UpdateRemoteClock()
|
||||
{
|
||||
if (remoteClockThread != null)
|
||||
{
|
||||
synchronized (remoteClockLock)
|
||||
{
|
||||
remoteClockLock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method handles time update for pseudoTCP logic class
|
||||
*
|
||||
* @param tcp
|
||||
* @param lock
|
||||
*/
|
||||
private void UpdateNextClock(final PseudoTCPBase tcp, final Object lock)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
long now = PseudoTCPBase.Now();
|
||||
//System.out.println(tcp.debugName + " NOTIFY CLOCK: " + now);
|
||||
synchronized (tcp)
|
||||
{
|
||||
tcp.NotifyClock(now);
|
||||
}
|
||||
//UpdateClock(tcp);
|
||||
long interval; // NOLINT
|
||||
synchronized (tcp)
|
||||
{
|
||||
interval = tcp.GetNextClock(PseudoTCPBase.Now());
|
||||
}
|
||||
//interval = Math.max(interval, 0L); // sometimes interval is < 0
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
{
|
||||
logger.log(Level.FINEST,
|
||||
tcp.debugName + " CLOCK sleep for " + interval);
|
||||
}
|
||||
if (interval < 0)
|
||||
{
|
||||
if (interval == -1)
|
||||
{
|
||||
interval = 1000;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
synchronized (lock)
|
||||
{
|
||||
lock.wait(interval);
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
//Logger.getLogger(PseudoTcpTestBase.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Local peer clock thread
|
||||
*/
|
||||
private Thread localClockThread;
|
||||
private final Object localClockLock = new Object();
|
||||
/**
|
||||
* Remote peer clock thread
|
||||
*/
|
||||
private Thread remoteClockThread;
|
||||
private final Object remoteClockLock = new Object();
|
||||
/**
|
||||
* The "run flag" for clock threads
|
||||
*/
|
||||
private boolean runClocks = false;
|
||||
|
||||
/**
|
||||
* Start clock threads
|
||||
*/
|
||||
protected void StartClocks()
|
||||
{
|
||||
if (localClockThread == null && remoteClockThread == null)
|
||||
{
|
||||
runClocks = true;
|
||||
localClockThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (runClocks)
|
||||
{
|
||||
//localTcp.NotifyClock(PseudoTCPBase.Now());
|
||||
UpdateNextClock(localTcp, localClockLock);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}, "LocalClockThread");
|
||||
remoteClockThread = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (runClocks)
|
||||
{
|
||||
//remoteTcp.NotifyClock(PseudoTCPBase.Now());
|
||||
UpdateNextClock(remoteTcp, remoteClockLock);
|
||||
}
|
||||
|
||||
}
|
||||
}, "RemoteClockThread");
|
||||
localClockThread.start();
|
||||
remoteClockThread.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops clock threads
|
||||
*/
|
||||
protected void StopClocks()
|
||||
{
|
||||
if (localClockThread != null && remoteClockThread != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
runClocks = false;
|
||||
localClockThread.interrupt();
|
||||
remoteClockThread.interrupt();
|
||||
localClockThread.join();
|
||||
localClockThread = null;
|
||||
remoteClockThread.join();
|
||||
remoteClockThread = null;
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method waits <tt>kConnectTimeoutMs</tt> miliseconds or until the
|
||||
* connection has been established between local and remote peers
|
||||
*
|
||||
* @param kConnectTimeoutMs
|
||||
* @return <tt>isDone</tt> result
|
||||
*/
|
||||
protected boolean assert_Connected_wait(int kConnectTimeoutMs)
|
||||
{
|
||||
return assert_wait_until(new IWaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return PseudoTcpTestBase.this.have_connected_;
|
||||
}
|
||||
}, kConnectTimeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method waits <tt>kTransferTimeoutMs</tt> miliseconds or until the
|
||||
* connection has been closed, which means that the data was transferred
|
||||
*
|
||||
* @param kTransferTimeoutMs
|
||||
* @return <tt>isDone</tt> result
|
||||
*/
|
||||
protected boolean assert_Disconnected_wait(long kTransferTimeoutMs)
|
||||
{
|
||||
return assert_wait_until(new IWaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return PseudoTcpTestBase.this.have_disconnected_;
|
||||
}
|
||||
}, kTransferTimeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the remoteTcp
|
||||
*/
|
||||
PseudoTCPBase getRemoteTcp()
|
||||
{
|
||||
return remoteTcp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the localTcp
|
||||
*/
|
||||
PseudoTCPBase getLocalTcp()
|
||||
{
|
||||
return localTcp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates maximum transfer time of <tt>size</tt> bytes for specified
|
||||
* transfer rate
|
||||
*
|
||||
* @param size
|
||||
* @param kBps
|
||||
* @return timeout for transfer in ms(minimum 3000 ms)
|
||||
*/
|
||||
public long MaxTransferTime(long size, long kBps)
|
||||
{
|
||||
long transferTout = ((size) / kBps) * 8 * 1000;
|
||||
return transferTout > 3000 ? transferTout : 3000;
|
||||
}
|
||||
}
|
||||
357
test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java
Executable file
357
test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java
Executable file
@ -0,0 +1,357 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.logging.*;
|
||||
import org.ice4j.pseudotcp.util.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
* This class implements test for two way transfers
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class PseudoTcpTestPingPong extends PseudoTcpTestBase
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
/**
|
||||
* The sender
|
||||
*/
|
||||
private PseudoTCPBase sender;
|
||||
/**
|
||||
* The receiver
|
||||
*/
|
||||
private PseudoTCPBase receiver;
|
||||
/**
|
||||
* How much data is sent per ping
|
||||
*/
|
||||
private int bytesPerSend;
|
||||
/**
|
||||
* Iterations count
|
||||
*/
|
||||
private int iterationsRemaining;
|
||||
|
||||
public PseudoTcpTestPingPong()
|
||||
{
|
||||
}
|
||||
|
||||
public void setBytesPerSend(int bytes_per_send)
|
||||
{
|
||||
this.bytesPerSend = bytes_per_send;
|
||||
}
|
||||
/**
|
||||
* The send stream buffer
|
||||
*/
|
||||
ByteFifoBuffer send_stream;
|
||||
/**
|
||||
* The receive stream buffer
|
||||
*/
|
||||
ByteFifoBuffer recv_stream;
|
||||
|
||||
/**
|
||||
* Performs ping-pong test for <tt>iterations</tt> with packets of
|
||||
* <tt>size</tt> bytes
|
||||
*
|
||||
* @param size
|
||||
* @param iterations
|
||||
*/
|
||||
public void TestPingPong(int size, int iterations)
|
||||
{
|
||||
long start, end;
|
||||
iterationsRemaining = iterations;
|
||||
receiver = getRemoteTcp();
|
||||
sender = getLocalTcp();
|
||||
// Create some dummy data
|
||||
byte[] dummy = createDummyData(size);
|
||||
send_stream = new ByteFifoBuffer(size);
|
||||
send_stream.Write(dummy, size);
|
||||
//Prepare the receive stream
|
||||
recv_stream = new ByteFifoBuffer(size);
|
||||
//Connect and wait until connected
|
||||
start = PseudoTCPBase.Now();
|
||||
StartClocks();
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
//assert Connect() == 0;
|
||||
assert_Connected_wait(kConnectTimeoutMs);
|
||||
// Sending will start from OnTcpWriteable and stop when the required
|
||||
// number of iterations have completed.
|
||||
assert_Disconnected_wait(kMinTransferRate);
|
||||
long elapsed = PseudoTCPBase.Now() - start;
|
||||
StopClocks();
|
||||
logger.log(Level.INFO,
|
||||
"Performed " + iterations + " pings in " + elapsed + " ms");
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches onTcpReadable event for receiver
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpReadable(PseudoTCPBase tcp)
|
||||
{
|
||||
assertEquals("Unexpected onTcpReadable", receiver, tcp);
|
||||
try
|
||||
{
|
||||
// Stream bytes to the recv stream as they arrive.
|
||||
ReadData();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
//will be caught by default handler and test will fail
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
// If we've received the desired amount of data, rewind things
|
||||
// and send it back the other way!
|
||||
int recvd = recv_stream.GetBuffered();
|
||||
int required = send_stream.Length();
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER,
|
||||
"test - receivied: " + recvd + " required: " + required);
|
||||
}
|
||||
|
||||
if (recvd == required)
|
||||
{
|
||||
if (receiver == getLocalTcp() && --iterationsRemaining == 0)
|
||||
{
|
||||
Close();
|
||||
// TODO: Fake OnTcpClosed() on the receiver for now.
|
||||
OnTcpClosed(getRemoteTcp(), null);
|
||||
return;
|
||||
}
|
||||
//switches receivier with sender and performs test the other way
|
||||
PseudoTCPBase tmp = receiver;
|
||||
receiver = sender;
|
||||
sender = tmp;
|
||||
send_stream.ResetReadPosition();
|
||||
send_stream.ConsumeWriteBuffer(send_stream.GetWriteRemaining());
|
||||
recv_stream.ResetWritePosition();
|
||||
OnTcpWriteable(sender);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches the ontcpWriteable event for sender
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpWriteable(PseudoTCPBase tcp)
|
||||
{
|
||||
if (tcp != sender)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Write bytes from the send stream when we can.
|
||||
// Shut down when we've sent everything.
|
||||
logger.log(Level.FINER, "Flow Control Lifted");
|
||||
try
|
||||
{
|
||||
WriteData();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the data in loop until is something available
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void ReadData() throws IOException
|
||||
{
|
||||
byte[] block = new byte[kBlockSize];
|
||||
int rcvd = 0;
|
||||
do
|
||||
{
|
||||
rcvd = receiver.Recv(block, block.length);
|
||||
if (rcvd > 0)
|
||||
{
|
||||
recv_stream.Write(block, rcvd);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE,
|
||||
"Receivied: " + recv_stream.GetBuffered());
|
||||
}
|
||||
}
|
||||
}
|
||||
while (rcvd > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes all data to the receiver
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void WriteData() throws IOException
|
||||
{
|
||||
int tosend;
|
||||
int sent = 0;
|
||||
byte[] block = new byte[kBlockSize];
|
||||
do
|
||||
{
|
||||
tosend = bytesPerSend != 0 ? bytesPerSend : block.length;
|
||||
tosend = send_stream.Read(block, tosend);
|
||||
if (tosend > 0)
|
||||
{
|
||||
sent = sender.Send(block, tosend);
|
||||
UpdateLocalClock();
|
||||
if (sent != -1)
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Sent: " + sent);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.FINE, "Flow controlled");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sent = tosend = 0;
|
||||
}
|
||||
}
|
||||
while (sent > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Ping-pong (request/response) tests
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* Test sending <= 1x MTU of data in each ping/pong. Should take <10ms.
|
||||
*/
|
||||
public void testPingPong1xMtu()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong 1xMTU");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.TestPingPong(100, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending 2x-3x MTU of data in each ping/pong. Should take <10ms.
|
||||
*/
|
||||
public void testPingPong3xMtu()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong 3xMTU");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.TestPingPong(400, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending 1x-2x MTU of data in each ping/pong. Should take ~1s, due to
|
||||
* interaction between Nagling and Delayed ACK.
|
||||
*/
|
||||
public void testPingPong2xMtu()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong 2xMTU");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.TestPingPong(2000, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending 1x-2x MTU of data in each ping/pong with Delayed ACK off.
|
||||
* Should take <10ms.
|
||||
*/
|
||||
public void testPingPong2xMtuWithAckDelayOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong 2xMTU ack delay off");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptAckDelay(0);
|
||||
test.TestPingPong(2000, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending 1x-2x MTU of data in each ping/pong with Nagling off. Should
|
||||
* take <10ms.
|
||||
*/
|
||||
public void testPingPong2xMtuWithNaglingOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong 2xMTU nagling off");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptNagling(false);
|
||||
test.TestPingPong(2000, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending a ping as pair of short (non-full) segments. Should take
|
||||
* ~1s, due to Delayed ACK interaction with Nagling.
|
||||
*/
|
||||
public void testPingPongShortSegments()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong short segments");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptAckDelay(5000);
|
||||
test.setBytesPerSend(50); // i.e. two Send calls per payload
|
||||
test.TestPingPong(100, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending ping as a pair of short (non-full) segments, with Nagling
|
||||
* off. Should take <10ms.
|
||||
*/
|
||||
public void testPingPongShortSegmentsWithNaglingOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong short segments nagling off");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptNagling(false);
|
||||
test.setBytesPerSend(50); // i.e. two Send calls per payload
|
||||
test.TestPingPong(100, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending <= 1x MTU of data ping/pong, in two segments, no Delayed
|
||||
* ACK. Should take ~1s.
|
||||
*/
|
||||
public void testPingPongShortSegmentsWithAckDelayOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test ping - pong short segments nagling off");
|
||||
PseudoTcpTestPingPong test = new PseudoTcpTestPingPong();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.setBytesPerSend(50); // i.e. two Send calls per payload
|
||||
test.SetOptAckDelay(0);
|
||||
test.TestPingPong(100, 5);
|
||||
}
|
||||
}
|
||||
404
test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java
Executable file
404
test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java
Executable file
@ -0,0 +1,404 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
import org.ice4j.pseudotcp.util.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
* Fill the receiver window until it is full, drain it and then fill it with the
|
||||
* same amount. This is to test that receiver window contracts and enlarges
|
||||
* correctly.
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class PseudoTcpTestRecvWindow extends PseudoTcpTestBase
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
/**
|
||||
* Send data buffer
|
||||
*/
|
||||
private ByteFifoBuffer send_stream;
|
||||
/**
|
||||
* List which stores stream position after each write cycle
|
||||
*/
|
||||
private List<Integer> send_position;
|
||||
/**
|
||||
* Receive data buffer
|
||||
*/
|
||||
private ByteFifoBuffer recv_stream;
|
||||
/**
|
||||
* List which stores stream position after each read cycle
|
||||
*/
|
||||
private List<Integer> recv_position;
|
||||
/**
|
||||
* Times used for write operations scheduling
|
||||
*/
|
||||
private Timer writeTimer = new Timer("WriteTimer");
|
||||
/**
|
||||
* Stores data length used for the test.
|
||||
*/
|
||||
private int testDataSize;
|
||||
|
||||
public void TestTransfer(int size)
|
||||
{
|
||||
testDataSize = size;
|
||||
long start, end, elapsed;
|
||||
send_position = new ArrayList<Integer>();
|
||||
recv_position = new ArrayList<Integer>();
|
||||
// Create some dummy data
|
||||
byte[] dummy = createDummyData(size);
|
||||
send_stream = new ByteFifoBuffer(size);
|
||||
send_stream.Write(dummy, size);
|
||||
//Prepare the receive stream
|
||||
recv_stream = new ByteFifoBuffer(size);
|
||||
//Connect and wait until connected
|
||||
start = PseudoTCPBase.Now();
|
||||
StartClocks();
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
//assert Connect() == 0;
|
||||
//TODO: check assert result and fail
|
||||
// Connect and wait until connected.
|
||||
assert_Connected_wait(kConnectTimeoutMs);
|
||||
|
||||
ScheduleWriteAction(0);
|
||||
|
||||
long transferTout = MaxTransferTime(dummy.length, kMinTransferRate);
|
||||
System.out.println("Timeout: " + transferTout);
|
||||
boolean transfferInTime = assert_Disconnected_wait(transferTout);
|
||||
elapsed = PseudoTCPBase.Now() - start;
|
||||
StopClocks();
|
||||
int received = recv_stream.GetBuffered();
|
||||
assertEquals("Transfer timeout, transferred: " + received
|
||||
+ " required: " + dummy.length
|
||||
+ " elapsed: "
|
||||
+ elapsed + " limit: " + transferTout,
|
||||
true, transfferInTime);
|
||||
|
||||
assert 2 == send_position.size();
|
||||
assert 2 == recv_position.size();
|
||||
|
||||
int estimated_recv_window = EstimateReceiveWindowSize();
|
||||
|
||||
// The difference in consecutive send positions should equal the
|
||||
// receive window size or match very closely. This verifies that receive
|
||||
// window is open after receiver drained all the data.
|
||||
int send_position_diff = send_position.get(1) - send_position.get(0);
|
||||
assertTrue(estimated_recv_window - send_position_diff <= 1024);
|
||||
|
||||
// Receiver drained the receive window twice.(+-2 because of window scaling)
|
||||
assert ((recv_position.get(1) - 2 * estimated_recv_window)
|
||||
<= getShadowedBytes(getRemoteScaleFactor()));
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculates amount of bytes witch may introduce error to
|
||||
* estimation of receive window size caused by scale factor. This is because
|
||||
* data is being sent until all sent data is available in remote side's
|
||||
* buffer. But because of scale factor window size 0 is reached earlier than
|
||||
* expected and some data may still wait for window to open in the send
|
||||
* queue.
|
||||
*
|
||||
* For example: m_rcv_scale == 1 and m_rcv_wnd < 2 then rcv_wnd == 0 (1 byte
|
||||
* may block) m_rcv_scale == 2 and m_rcv_wnd < 4 then rcv_wnd == 0 (3 bytes
|
||||
* may block) m_rcv_scale == 3 and m_rcv_wnd < 8 then rcv_wnd == 0 (7 bytes
|
||||
* may block) and so on...
|
||||
*
|
||||
* In normal operation something would read data on remote side causing
|
||||
* window to expand.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
|
||||
*
|
||||
* @param scaleFactor
|
||||
* @return count bytes shadowed by scale actor
|
||||
*/
|
||||
static int getShadowedBytes(int scaleFactor)
|
||||
{
|
||||
return (int) (Math.pow(2, scaleFactor) - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all data available at the buffer
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
void ReadUntilIOPending() throws IOException
|
||||
{
|
||||
byte[] block = new byte[getRemoteTcp().getRecvBufferSize() * 2];
|
||||
int position = recv_stream.GetBuffered();
|
||||
int rcvd, total = 0;
|
||||
do
|
||||
{
|
||||
rcvd = RemoteRecv(block, block.length);
|
||||
if (rcvd > 0)
|
||||
{
|
||||
recv_stream.Write(block, rcvd);
|
||||
total += rcvd;
|
||||
position += rcvd;
|
||||
}
|
||||
}
|
||||
while (rcvd > 0 && total != 0);
|
||||
recv_position.add(position);
|
||||
|
||||
// Disconnect if we have done two transfers.
|
||||
if (recv_position.size() == 2)
|
||||
{
|
||||
Close();
|
||||
OnTcpClosed(getRemoteTcp(), null);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules write operation with <tt>delay</tt> given in ms
|
||||
*
|
||||
* @param delay
|
||||
*/
|
||||
void ScheduleWriteAction(long delay)
|
||||
{
|
||||
writeTimer.schedule(new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
WriteData();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
//it will get cought by
|
||||
//deafult exception handler in PseudoTcpTestBase
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the data
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
void WriteData() throws IOException
|
||||
{
|
||||
//writeOpCount++;
|
||||
int tosend;
|
||||
int sent;
|
||||
int totalSent = 0;
|
||||
byte[] block = new byte[getRemoteTcp().getRecvBufferSize() * 2];
|
||||
int position = testDataSize - send_stream.GetBuffered();
|
||||
synchronized (getLocalTcp())
|
||||
{
|
||||
do
|
||||
{
|
||||
tosend = send_stream.ReadOffset(block, 0, block.length, 0);
|
||||
if (tosend > 0)
|
||||
{
|
||||
sent = LocalSend(block, tosend);
|
||||
UpdateLocalClock();
|
||||
if (sent > 0)
|
||||
{
|
||||
totalSent += sent;
|
||||
send_stream.ConsumeReadData(sent);
|
||||
position += sent;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.FINE, "Flow Controlled");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sent = tosend = 0;
|
||||
}
|
||||
}
|
||||
while (sent > 0);
|
||||
//position = send_stream.GetBuffered();
|
||||
|
||||
}
|
||||
// Measured with precision according to window scale option used
|
||||
if (totalSent - getRemoteTcp().GetAvailable()
|
||||
> getShadowedBytes(getRemoteScaleFactor()))
|
||||
{
|
||||
//send buffer was fully filled
|
||||
//waits until it will be received by remote peer
|
||||
while (totalSent - getRemoteTcp().GetAvailable()
|
||||
> getShadowedBytes(getRemoteScaleFactor())
|
||||
&& !getRemoteTcp().isReceiveBufferFull())
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(50);
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER,
|
||||
"Waiting... sent: " + totalSent + " avail: "
|
||||
+ getRemoteTcp().GetAvailable() + " buffered not sent: "
|
||||
+ getLocalTcp().GetBytesBufferedNotSent()
|
||||
+ " isFull? " + getRemoteTcp().isReceiveBufferFull());
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
send_position.add(position);
|
||||
writeTimer.schedule(new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ReadUntilIOPending();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return estimated receive window size
|
||||
*/
|
||||
int EstimateReceiveWindowSize()
|
||||
{
|
||||
return recv_position.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return estimated send window size
|
||||
*/
|
||||
int EstimateSendWindowSize()
|
||||
{
|
||||
return send_position.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnTcpReadable(PseudoTCPBase tcp)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnTcpWriteable(PseudoTCPBase tcp)
|
||||
{
|
||||
}
|
||||
|
||||
void SetLocalOptSndBuf(int len)
|
||||
{
|
||||
getLocalTcp().SetOption(Option.OPT_SNDBUF, len);
|
||||
}
|
||||
|
||||
int getRemoteScaleFactor()
|
||||
{
|
||||
return getRemoteTcp().getM_rwnd_scale();
|
||||
}
|
||||
|
||||
public void testGetShadowedBytes()
|
||||
{
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(0) == 0);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(1) == 1);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(2) == 3);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(3) == 7);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(4) == 15);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(11) == 2047);
|
||||
assert (PseudoTcpTestRecvWindow.getShadowedBytes(14) == 16383);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that receive window expands and contract correctly.
|
||||
*/
|
||||
public void testReceiveWindow()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test receive window");
|
||||
PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptNagling(false);
|
||||
test.SetOptAckDelay(0);
|
||||
test.TestTransfer(1024 * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test setting send window size to a very small value.
|
||||
*/
|
||||
public void testSetVerySmallSendWindowSize()
|
||||
{
|
||||
//TODO: finish test
|
||||
logger.log(Level.INFO, "Test very small receive window");
|
||||
PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptNagling(false);
|
||||
test.SetOptAckDelay(0);
|
||||
test.SetOptSndBuf(900);
|
||||
test.TestTransfer(1024 * 1000);
|
||||
assertEquals(900, test.EstimateSendWindowSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test setting receive window size to a value other than default.
|
||||
*/
|
||||
public void testSetReceiveWindowSize()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test set receive window size");
|
||||
PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetOptNagling(false);
|
||||
test.SetOptAckDelay(0);
|
||||
int wndSize = 300000;
|
||||
test.SetRemoteOptRcvBuf(wndSize);
|
||||
test.SetLocalOptSndBuf(wndSize);
|
||||
int wndScale = test.getRemoteScaleFactor();
|
||||
//logger.log(Level.INFO, "Using scale factor: {0}", wndScale);
|
||||
test.TestTransfer(1024 * 3000);
|
||||
//beacuse there may be situations
|
||||
//when 1 byte may be waiting in send queue
|
||||
//before
|
||||
//scaling factor == 1 not allows to determine exact window size (+-1)
|
||||
assert (wndSize - test.EstimateReceiveWindowSize()
|
||||
<= PseudoTcpTestRecvWindow.getShadowedBytes(wndScale));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test sending data with mismatched MTUs. We should detect this and reduce
|
||||
* // our packet size accordingly. // TODO: This doesn't actually work right
|
||||
* now. The current code // doesn't detect if the MTU is set too high on
|
||||
* either side. TEST_F(PseudoTcpTest, TestSendWithMismatchedMtus) {
|
||||
* SetLocalMtu(1500); SetRemoteMtu(1280); TestTransfer(1000000); }
|
||||
*/
|
||||
}
|
||||
445
test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java
Executable file
445
test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java
Executable file
@ -0,0 +1,445 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.logging.*;
|
||||
import org.ice4j.pseudotcp.util.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
* Implements one way transfer test
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class PseudoTcpTestTransfer extends PseudoTcpTestBase
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(PseudoTCPBase.class.getName());
|
||||
|
||||
public PseudoTcpTestTransfer()
|
||||
{
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* The send data
|
||||
*/
|
||||
private byte[] sendData;
|
||||
/**
|
||||
* Send stream size
|
||||
*/
|
||||
private int sendStreamSize;
|
||||
/**
|
||||
* Total bytes sent counter
|
||||
*/
|
||||
private int totalSent;
|
||||
/**
|
||||
* Receive stream
|
||||
*/
|
||||
private ByteArrayOutputStream recvStream;
|
||||
|
||||
/**
|
||||
* Transfers the data of <tt>size</tt> bytes
|
||||
*
|
||||
* @param size
|
||||
*/
|
||||
public void TestTransfer(int size)
|
||||
{
|
||||
long start, elapsed;
|
||||
int received;
|
||||
// Create some dummy data to send
|
||||
sendData = createDummyData(size);
|
||||
sendStreamSize = size;
|
||||
|
||||
// Prepare the receive stream.
|
||||
recvStream = new ByteArrayOutputStream(size);
|
||||
// Connect and wait until connected.
|
||||
start = PseudoTCPBase.Now();
|
||||
StartClocks();
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
//assertEquals(0, Connect());
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
|
||||
assert_Connected_wait(kConnectTimeoutMs);
|
||||
// Sending will start from OnTcpWriteable and complete when all data has
|
||||
// been received.
|
||||
long transferTout = MaxTransferTime(sendData.length, kMinTransferRate);
|
||||
boolean transfferInTime = assert_Disconnected_wait(transferTout);
|
||||
elapsed = PseudoTCPBase.Now() - start;
|
||||
StopClocks();
|
||||
received = recvStream.size();
|
||||
assertEquals("Transfer timeout, transferred: " + received
|
||||
+ " required: " + sendData.length
|
||||
+ " elapsed: "
|
||||
+ elapsed + " limit: " + transferTout,
|
||||
true, transfferInTime);
|
||||
|
||||
// Ensure we closed down OK and we got the right data.
|
||||
assertEquals(size, received);
|
||||
byte[] recvdArray = recvStream.toByteArray();
|
||||
assertArrayEquals(sendData, recvdArray);
|
||||
|
||||
logger.log(Level.INFO,
|
||||
"Transferred " + received + " bytes in " + elapsed
|
||||
+ " ms (" + (size * 8 / elapsed) + " Kbps");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all data available at remote peer's buffer
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
void ReadData() throws IOException
|
||||
{
|
||||
byte[] block = new byte[kBlockSize];
|
||||
int rcvd;
|
||||
do
|
||||
{
|
||||
rcvd = RemoteRecv(block, block.length);
|
||||
UpdateRemoteClock();
|
||||
if (rcvd != -1)
|
||||
{
|
||||
recvStream.write(block, 0, rcvd);
|
||||
}
|
||||
}
|
||||
while (rcvd > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the data until there's space available
|
||||
*
|
||||
* @return true if there's no more data left to write
|
||||
* @throws IOException
|
||||
*/
|
||||
boolean WriteData() throws IOException
|
||||
{
|
||||
int tosend;
|
||||
int sent;
|
||||
byte[] block = new byte[kBlockSize];
|
||||
do
|
||||
{
|
||||
tosend = Math.min(sendStreamSize - totalSent, block.length);
|
||||
System.arraycopy(sendData, totalSent, block, 0, tosend);
|
||||
if (tosend > 0)
|
||||
{
|
||||
sent = LocalSend(block, tosend);
|
||||
UpdateLocalClock();
|
||||
if (sent != -1)
|
||||
{
|
||||
totalSent += sent;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.FINE, "Flow Controlled");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sent = tosend = 0;
|
||||
}
|
||||
}
|
||||
while (sent > 0);
|
||||
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER, "Sent: " + totalSent
|
||||
+ " remaining: " + (sendStreamSize - totalSent));
|
||||
}
|
||||
|
||||
return tosend == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches TCP readable event for remote peer and reads the data. When total
|
||||
* read count equals send data size the test is finished.
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpReadable(PseudoTCPBase tcp)
|
||||
{
|
||||
if (tcp == getRemoteTcp())
|
||||
{
|
||||
try
|
||||
{
|
||||
ReadData();
|
||||
// TODO: OnTcpClosed() is currently only notified on error -
|
||||
// there is no on-the-wire equivalent of TCP FIN.
|
||||
// So we fake the notification when all the data has been read.
|
||||
int received, required;
|
||||
received = recvStream.size();
|
||||
required = sendStreamSize;
|
||||
if (logger.isLoggable(Level.FINER))
|
||||
{
|
||||
logger.log(Level.FINER, "Receivied: " + received
|
||||
+ " required: " + required);
|
||||
}
|
||||
if (received == required)
|
||||
{
|
||||
OnTcpClosed(getRemoteTcp(), null);
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches on TCP writeable event for local peer. Writes all data and closes
|
||||
* the stream
|
||||
*
|
||||
* @param tcp
|
||||
*/
|
||||
@Override
|
||||
public void OnTcpWriteable(PseudoTCPBase tcp)
|
||||
{
|
||||
if (tcp == getLocalTcp())
|
||||
{
|
||||
// Write bytes from the send stream when we can.
|
||||
// Shut down when we've sent everything.
|
||||
logger.log(Level.FINER, "Flow Control Lifted");
|
||||
try
|
||||
{
|
||||
if (WriteData())
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic end-to-end data transfer tests Test the normal case of sending data
|
||||
* from one side to the other.
|
||||
*/
|
||||
public void testSend()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with a 50 ms RTT. Transmission should take longer due
|
||||
* to a slower ramp-up in send rate.
|
||||
*/
|
||||
public void testSendWithDelay()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with delay");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetDelay(50);
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with packet loss. Transmission should take much longer
|
||||
* due to send back-off when loss occurs.
|
||||
*/
|
||||
public void testSendWithLoss()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with loss");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetLoss(10);
|
||||
test.TestTransfer(100000); // less data so test runs faster
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with a 50 ms RTT and 10% packet loss. Transmission
|
||||
* should take much longer due to send back-off and slower detection of
|
||||
* loss.
|
||||
*/
|
||||
public void testSendWithDelayAndLoss()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with delay and loss");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetDelay(50);
|
||||
test.SetLoss(10);
|
||||
test.TestTransfer(100000); // less data so test runs faster
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with 10% packet loss and Nagling disabled. Transmission
|
||||
* should take about the same time as with Nagling enabled.
|
||||
*/
|
||||
public void testSendWithLossAndOptNaglingOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with loss and OptNagling off");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetLoss(10);
|
||||
test.SetOptNagling(false);
|
||||
test.TestTransfer(100000); // less data so test runs faster
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with 10% packet loss and Delayed ACK disabled.
|
||||
* Transmission should be slightly faster than with it enabled.
|
||||
*/
|
||||
public void testSendWithLossAndOptAckDelayOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with loss and OptAckDelay off");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetLoss(10);
|
||||
test.SetOptAckDelay(0);
|
||||
test.TestTransfer(100000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with 50ms delay and Nagling disabled.
|
||||
*/
|
||||
public void testSendWithDelayAndOptNaglingOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with delay and OptNagling off");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetDelay(50);
|
||||
test.SetOptNagling(false);
|
||||
test.TestTransfer(100000); // less data so test runs faster
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sending data with 50ms delay and Delayed ACK disabled.
|
||||
*/
|
||||
public void testSendWithDelayAndOptAckDelayOff()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send with delay and OptAckDelay off");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetDelay(50);
|
||||
test.SetOptAckDelay(0);
|
||||
test.TestTransfer(100000); // less data so test runs faster
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a large receive buffer with a sender that doesn't support scaling.
|
||||
*/
|
||||
public void testSendRemoteNoWindowScale()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send - remote no window scale");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetLocalOptRcvBuf(100000);
|
||||
test.DisableRemoteWindowScale();
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a large sender-side receive buffer with a receiver that doesn't
|
||||
* support scaling.
|
||||
*/
|
||||
public void testSendLocalNoWindowScale()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send - local no window scale");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(100000);
|
||||
test.DisableLocalWindowScale();
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test when both sides use window scaling.
|
||||
*/
|
||||
public void testSendBothUseWindowScale()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send - both use window scale");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(100000);
|
||||
test.SetLocalOptRcvBuf(100000);
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test using a large window scale value.
|
||||
*/
|
||||
public void testSendLargeInFlight()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send large in flight");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(100000);
|
||||
test.SetLocalOptRcvBuf(100000);
|
||||
test.SetOptSndBuf(150000);
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
public void testSendBothUseLargeWindowScale()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send both use large window scale");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(1000000);
|
||||
test.SetLocalOptRcvBuf(1000000);
|
||||
test.TestTransfer(10000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test using a small receive buffer.
|
||||
*/
|
||||
public void testSendSmallReceiveBuffer()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send small receive buffer");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(10000);
|
||||
test.SetLocalOptRcvBuf(10000);
|
||||
test.TestTransfer(1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test using a very small receive buffer.
|
||||
*/
|
||||
public void testSendVerySmallReceiveBuffer()
|
||||
{
|
||||
//logger.log(Level.INFO, "Test send very small receive buffer");
|
||||
PseudoTcpTestTransfer test = new PseudoTcpTestTransfer();
|
||||
test.SetLocalMtu(1500);
|
||||
test.SetRemoteMtu(1500);
|
||||
test.SetRemoteOptRcvBuf(100);
|
||||
test.SetLocalOptRcvBuf(100);
|
||||
test.TestTransfer(100000);
|
||||
}
|
||||
}
|
||||
339
test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java
Executable file
339
test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java
Executable file
@ -0,0 +1,339 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the Jitsi community (https://jitsi.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.pseudotcp.util;
|
||||
|
||||
import junit.framework.*;
|
||||
import java.nio.*;
|
||||
import java.util.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class ByteFifoBufferTest extends TestCase
|
||||
{
|
||||
public ByteFifoBufferTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of Length method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testLength()
|
||||
{
|
||||
//System.out.println("Length");
|
||||
int expResult = 1000;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(expResult);
|
||||
assertEquals(expResult, instance.Length());
|
||||
int wSize = 100;
|
||||
instance.Write(getWData(wSize), wSize);
|
||||
int result = instance.Length();
|
||||
assertEquals(expResult, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of Read method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testRead()
|
||||
{
|
||||
//System.out.println("Read");
|
||||
int count = 1024;
|
||||
byte[] wData = getWData(count);
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(count);
|
||||
instance.Write(wData, count);
|
||||
|
||||
byte[] readBuff = new byte[count];
|
||||
int expResult = count;
|
||||
int result = instance.Read(readBuff, count);
|
||||
assertEquals(expResult, result);
|
||||
assertArrayEquals(wData, readBuff);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* return some random array
|
||||
* @param count array size
|
||||
* @return
|
||||
*/
|
||||
private byte[] getWData(int count)
|
||||
{
|
||||
Random r = new Random();
|
||||
byte[] res = new byte[count];
|
||||
r.nextBytes(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of GetWriteRemaining method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testGetWriteRemaining()
|
||||
{
|
||||
//System.out.println("GetWriteRemaining");
|
||||
int len = 100;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int expResult = len;
|
||||
int result = instance.GetWriteRemaining();
|
||||
assertEquals(expResult, result);
|
||||
|
||||
int w_size = 23;
|
||||
byte[] w_data = getWData(w_size);
|
||||
instance.Write(w_data, w_size);
|
||||
|
||||
expResult = len - w_size;
|
||||
result = instance.GetWriteRemaining();
|
||||
assertEquals(expResult, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of GetBuffered method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testGetBuffered()
|
||||
{
|
||||
//System.out.println("GetBuffered");
|
||||
int len = 1000;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int w_len = 100;
|
||||
byte[] w_data = getWData(w_len);
|
||||
|
||||
instance.Write(w_data, w_len);
|
||||
|
||||
int expResult = w_len;
|
||||
int result = instance.GetBuffered();
|
||||
assertEquals(expResult, result);
|
||||
int consume = 5;
|
||||
expResult = w_len + consume;
|
||||
instance.ConsumeWriteBuffer(consume);
|
||||
result = instance.GetBuffered();
|
||||
assertEquals(expResult, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of Write method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testWrite()
|
||||
{
|
||||
//System.out.println("Write");
|
||||
int len = 2048;
|
||||
byte[] data = getWData(len);
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int expResult = len;
|
||||
int result = instance.Write(data, len);
|
||||
assertEquals(expResult, result);
|
||||
|
||||
byte[] read = new byte[len];
|
||||
int readCount = instance.Read(read, len);
|
||||
assertEquals(result, readCount);
|
||||
assertArrayEquals(data, read);
|
||||
}
|
||||
|
||||
|
||||
public void testWriteWithOffset()
|
||||
{
|
||||
//System.out.println("Write");
|
||||
int len = 2048;
|
||||
byte[] data = getWData(len);
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int expResult = len/2;
|
||||
int result = instance.Write(data,0, len/2);
|
||||
assertEquals(expResult, result);
|
||||
result = instance.Write(data, len/2, len/2);
|
||||
assertEquals(expResult, result);
|
||||
|
||||
byte[] read = new byte[len];
|
||||
int readCount = instance.Read(read, len);
|
||||
assertEquals(len, readCount);
|
||||
assertArrayEquals(data, read);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test of ConsumeWriteBuffer method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testConsumeWriteBuffer()
|
||||
{
|
||||
//System.out.println("ConsumeWriteBuffer");
|
||||
int len = 100;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
instance.ConsumeWriteBuffer(len / 2);
|
||||
instance.ConsumeWriteBuffer(len / 2);
|
||||
try
|
||||
{
|
||||
instance.ConsumeWriteBuffer(1);
|
||||
fail();
|
||||
}
|
||||
catch (BufferOverflowException e)
|
||||
{
|
||||
}
|
||||
|
||||
instance = new ByteFifoBuffer(len);
|
||||
instance.ConsumeWriteBuffer(95);
|
||||
instance.ConsumeReadData(40);
|
||||
instance.ConsumeWriteBuffer(20);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of SetCapacity method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testSetCapacity()
|
||||
{
|
||||
//System.out.println("SetCapacity");
|
||||
int old_size = 100;
|
||||
int new_size = 200;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(old_size);
|
||||
boolean expResult = true;
|
||||
instance.Write(getWData(old_size), old_size);
|
||||
boolean result = instance.SetCapacity(new_size);
|
||||
assertEquals(expResult, result);
|
||||
|
||||
expResult = false;
|
||||
instance.ResetWritePosition();
|
||||
instance.Write(getWData(new_size), new_size);
|
||||
result = instance.SetCapacity(old_size);
|
||||
assertEquals(expResult, result);
|
||||
|
||||
/*
|
||||
* int write_size = old_size; instance = new
|
||||
* ByteFifoBuffer(old_size); byte[] written = getWData(write_size);
|
||||
* assertEquals(write_size, instance.Write(written, write_size));
|
||||
* instance.SetCapacity(new_size); byte[] read = new byte[write_size];
|
||||
* instance.Read(read, write_size); assertArrayEquals(written, read);
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of ConsumeReadData method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testConsumeReadData()
|
||||
{
|
||||
//System.out.println("ConsumeReadData");
|
||||
int lCount = 100;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(lCount);
|
||||
instance.Write(getWData(lCount), lCount);
|
||||
instance.ConsumeReadData(lCount / 2);
|
||||
instance.ConsumeReadData(lCount / 2);
|
||||
try
|
||||
{
|
||||
instance.ConsumeReadData(1);
|
||||
fail();
|
||||
}
|
||||
catch (BufferUnderflowException e)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of ReadOffset method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testReadOffset()
|
||||
{
|
||||
//System.out.println("ReadOffset");
|
||||
int dst_buff_offset = 0;
|
||||
int len = 100;
|
||||
byte[] src_buff = getWData(len);
|
||||
byte[] dst_buff = new byte[len];
|
||||
int offset = 0;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int expResult = len;
|
||||
instance.Write(src_buff, len);
|
||||
int result = instance.ReadOffset(dst_buff, dst_buff_offset, len, offset);
|
||||
assertEquals(expResult, result);
|
||||
assertArrayEquals(dst_buff, src_buff);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of WriteOffset method, of class ByteFifoBuffer.
|
||||
*/
|
||||
public void testWriteOffset()
|
||||
{
|
||||
//System.out.println("WriteOffset");
|
||||
int len = 200;
|
||||
int dataLen = 100;
|
||||
byte[] srcData = getWData(dataLen);
|
||||
byte[] data = new byte[dataLen];
|
||||
int nOffset = 10;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
int result = instance.WriteOffset(srcData, dataLen, nOffset);
|
||||
int readCount = instance.ReadOffset(data, 0, dataLen, nOffset);
|
||||
assertEquals(result, readCount);
|
||||
assertArrayEquals(srcData, data);
|
||||
|
||||
|
||||
byte[] halfFilled = new byte[dataLen * 2];
|
||||
System.arraycopy(srcData, 0, halfFilled, dataLen, dataLen);
|
||||
byte[] halfFilledRead = new byte[dataLen * 2];
|
||||
instance.ReadOffset(halfFilledRead, dataLen, dataLen, nOffset);
|
||||
assertArrayEquals(halfFilled, halfFilledRead);
|
||||
|
||||
//case when w_pos+offset exceeds current backing array length
|
||||
instance = new ByteFifoBuffer(len);
|
||||
instance.Write(srcData, dataLen);
|
||||
instance.Write(srcData, dataLen / 2);//current writePos = 150
|
||||
instance.Read(data, dataLen);// curretn readPos = 100
|
||||
instance.WriteOffset(srcData, dataLen, 50);
|
||||
|
||||
instance = new ByteFifoBuffer(61440);
|
||||
instance.WriteOffset(getWData(1384), 1384, 31832);
|
||||
|
||||
}
|
||||
|
||||
public void testWriteReadWriteRead()
|
||||
{
|
||||
int len = 2000;
|
||||
ByteFifoBuffer instance = new ByteFifoBuffer(len);
|
||||
byte[] wrData = getWData(len * 2);
|
||||
int written = 0;
|
||||
byte[] readBuff = new byte[wrData.length];
|
||||
int read = 0;
|
||||
do
|
||||
{
|
||||
int wrRemaining = instance.GetWriteRemaining();
|
||||
if (wrRemaining > 0 && written < wrData.length)
|
||||
{
|
||||
int wrCount = instance.WriteOffset(wrData, wrRemaining, 0);
|
||||
instance.ConsumeWriteBuffer(wrCount);
|
||||
written += wrCount;
|
||||
}
|
||||
int readAvailable = instance.GetBuffered();
|
||||
if (readAvailable > 0)
|
||||
{
|
||||
int rCount = instance.ReadOffset(readBuff, read, readAvailable, 0);
|
||||
instance.ConsumeReadData(rCount);
|
||||
read += rCount;
|
||||
}
|
||||
}
|
||||
while ((read != wrData.length) || (written != wrData.length));
|
||||
}
|
||||
|
||||
public void testSomeMultiTest()
|
||||
{
|
||||
int Alen = 16;
|
||||
int Blen = 32;
|
||||
int Clen = 64;
|
||||
int Dlen = 256;
|
||||
int len = Alen + Blen + Clen + Dlen;
|
||||
ByteFifoBuffer fifo = new ByteFifoBuffer(len);
|
||||
byte[] A = getWData(Alen);
|
||||
byte[] B = getWData(Blen);
|
||||
byte[] C = getWData(Clen);
|
||||
byte[] D = getWData(Dlen);
|
||||
byte[] Aread = getWData(Alen);
|
||||
byte[] Bread = getWData(Blen);
|
||||
byte[] Cread = getWData(Clen);
|
||||
byte[] Dread = getWData(Dlen);
|
||||
|
||||
fifo.WriteOffset(A, Alen, 0);
|
||||
fifo.ConsumeWriteBuffer(Alen);
|
||||
fifo.ReadOffset(Aread, 0, Alen, 0);
|
||||
assertArrayEquals(A, Aread);
|
||||
}
|
||||
}
|
||||
@ -176,7 +176,7 @@ public class Ice
|
||||
* local
|
||||
* @param remotePeer a reference to what we'll pretend to be a remote agent.
|
||||
*/
|
||||
private static void transferRemoteCandidates(Agent localAgent,
|
||||
static void transferRemoteCandidates(Agent localAgent,
|
||||
Agent remotePeer)
|
||||
{
|
||||
List<IceMediaStream> streams = localAgent.getStreams();
|
||||
|
||||
488
test/test/IcePseudoTcp.java
Executable file
488
test/test/IcePseudoTcp.java
Executable file
@ -0,0 +1,488 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package test;
|
||||
|
||||
import java.beans.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.ice.*;
|
||||
import org.ice4j.ice.harvest.*;
|
||||
import org.ice4j.security.*;
|
||||
import org.ice4j.pseudotcp.*;
|
||||
|
||||
/**
|
||||
* Sample program which first uses ICE to discover UDP connectivity. After that
|
||||
* selected cadidates are used by "remote" and "local" pseudoTCP peers to
|
||||
* transfer some test data.
|
||||
*
|
||||
* @author Pawel Domas
|
||||
*/
|
||||
public class IcePseudoTcp
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(CheckList.class.getName());
|
||||
private static long startTime;
|
||||
/**
|
||||
* Local job thread variable
|
||||
*/
|
||||
private static LocalPseudoTcpJob localJob = null;
|
||||
/**
|
||||
* Remote job thread variable
|
||||
*/
|
||||
private static RemotePseudoTcpJob remoteJob = null;
|
||||
/**
|
||||
* Test data size
|
||||
*/
|
||||
private static final int TEST_BYTES_COUNT = 1000000;
|
||||
/**
|
||||
* Flag inidcates if STUN should be used
|
||||
*/
|
||||
private static final boolean USE_STUN = true;
|
||||
/**
|
||||
* Flag inidcates if TURN should be used
|
||||
*/
|
||||
private static final boolean USE_TURN = true;
|
||||
/**
|
||||
* Monitor object used to wait for remote agent to finish it's job
|
||||
*/
|
||||
private static final Object remoteAgentMonitor = new Object();
|
||||
/**
|
||||
* Monitor object used to wait for local agent to finish it's job
|
||||
*/
|
||||
private static final Object localAgentMonitor = new Object();
|
||||
/**
|
||||
* Timeout for ICE discovery
|
||||
*/
|
||||
private static long agentJobTimeout = 15000;
|
||||
|
||||
protected static Agent createAgent(int pTcpPort)
|
||||
throws Throwable
|
||||
{
|
||||
Agent agent = new Agent();
|
||||
// STUN
|
||||
if (USE_STUN)
|
||||
{
|
||||
StunCandidateHarvester stunHarv = new StunCandidateHarvester(
|
||||
new TransportAddress("sip-communicator.net",
|
||||
3478, Transport.UDP));
|
||||
StunCandidateHarvester stun6Harv = new StunCandidateHarvester(
|
||||
new TransportAddress("ipv6.sip-communicator.net",
|
||||
3478, Transport.UDP));
|
||||
|
||||
agent.addCandidateHarvester(stunHarv);
|
||||
agent.addCandidateHarvester(stun6Harv);
|
||||
}
|
||||
// TURN
|
||||
if (USE_TURN)
|
||||
{
|
||||
String[] hostnames = new String[]
|
||||
{
|
||||
"130.79.90.150",
|
||||
"2001:660:4701:1001:230:5ff:fe1a:805f"
|
||||
};
|
||||
int port = 3478;
|
||||
LongTermCredential longTermCredential = new LongTermCredential(
|
||||
"guest", "anonymouspower!!");
|
||||
|
||||
for (String hostname : hostnames)
|
||||
{
|
||||
agent.addCandidateHarvester(new TurnCandidateHarvester(
|
||||
new TransportAddress(hostname, port,
|
||||
Transport.UDP), longTermCredential));
|
||||
}
|
||||
}
|
||||
//STREAM
|
||||
createStream(pTcpPort, "data", agent);
|
||||
|
||||
return agent;
|
||||
}
|
||||
|
||||
private static IceMediaStream createStream(int pTcpPort,
|
||||
String streamName,
|
||||
Agent agent)
|
||||
throws Throwable
|
||||
{
|
||||
IceMediaStream stream = agent.createMediaStream(streamName);
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
//udp component
|
||||
agent.createComponent(
|
||||
stream, Transport.UDP, pTcpPort, pTcpPort, pTcpPort + 100);
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
logger.log(Level.INFO,
|
||||
"UDP Component created in " + (endTime - startTime) + " ms");
|
||||
startTime = endTime;
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
private static final class LocalIceProcessingListener
|
||||
implements PropertyChangeListener
|
||||
{
|
||||
/**
|
||||
* System.exit()s as soon as ICE processing enters a final state.
|
||||
*
|
||||
* @param evt the {@link PropertyChangeEvent} containing the old and new
|
||||
* states of ICE processing.
|
||||
*/
|
||||
public void propertyChange(PropertyChangeEvent evt)
|
||||
{
|
||||
long processingEndTime = System.currentTimeMillis();
|
||||
|
||||
Object iceProcessingState = evt.getNewValue();
|
||||
|
||||
logger.log(Level.INFO,
|
||||
"Local agent entered the " + iceProcessingState + " state.");
|
||||
if (iceProcessingState == IceProcessingState.COMPLETED)
|
||||
{
|
||||
logger.log(Level.INFO,
|
||||
"Local - Total ICE processing time: "
|
||||
+ (processingEndTime - startTime) + "ms");
|
||||
Agent agent = (Agent) evt.getSource();
|
||||
logger.log(Level.INFO, "Local: Create pseudo tcp stream");
|
||||
IceMediaStream dataStream = agent.getStream("data");
|
||||
Component udpComponent = dataStream.getComponents().get(0);
|
||||
CandidatePair selectedPair = udpComponent.getSelectedPair();
|
||||
if (selectedPair != null)
|
||||
{
|
||||
LocalCandidate localCandidate = selectedPair.getLocalCandidate();
|
||||
Candidate remoteCandidate = selectedPair.getRemoteCandidate();
|
||||
logger.log(Level.INFO, "Local: " + localCandidate);
|
||||
logger.log(Level.INFO, "Remote: " + remoteCandidate);
|
||||
try
|
||||
{
|
||||
localJob = new LocalPseudoTcpJob(
|
||||
localCandidate.getTransportAddress());
|
||||
}
|
||||
catch (UnknownHostException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE,
|
||||
"Error while trying to create"
|
||||
+ " local pseudotcp thread " + ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.INFO, "Failed to select any candidate pair");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (iceProcessingState == IceProcessingState.TERMINATED
|
||||
|| iceProcessingState == IceProcessingState.FAILED)
|
||||
{
|
||||
/*
|
||||
* Though the process will be instructed to die, demonstrate
|
||||
* that Agent instances are to be explicitly prepared for
|
||||
* garbage collection.
|
||||
*/
|
||||
((Agent) evt.getSource()).free();
|
||||
|
||||
if (localJob != null
|
||||
&& iceProcessingState == IceProcessingState.TERMINATED)
|
||||
{
|
||||
localJob.start();
|
||||
synchronized (localAgentMonitor)
|
||||
{
|
||||
localAgentMonitor.notifyAll();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RemoteIceProcessingListener
|
||||
implements PropertyChangeListener
|
||||
{
|
||||
/**
|
||||
* System.exit()s as soon as ICE processing enters a final state.
|
||||
*
|
||||
* @param evt the {@link PropertyChangeEvent} containing the old and new
|
||||
* states of ICE processing.
|
||||
*/
|
||||
public void propertyChange(PropertyChangeEvent evt)
|
||||
{
|
||||
long processingEndTime = System.currentTimeMillis();
|
||||
|
||||
Object iceProcessingState = evt.getNewValue();
|
||||
|
||||
logger.log(Level.INFO,
|
||||
"Remote agent entered the " + iceProcessingState + " state.");
|
||||
if (iceProcessingState == IceProcessingState.COMPLETED)
|
||||
{
|
||||
logger.log(Level.INFO,
|
||||
"Remote: Total ICE processing time: "
|
||||
+ (processingEndTime - startTime) + " ms");
|
||||
Agent agent = (Agent) evt.getSource();
|
||||
|
||||
logger.log(Level.INFO, "Remote: Create pseudo tcp stream");
|
||||
IceMediaStream dataStream = agent.getStream("data");
|
||||
Component udpComponent = dataStream.getComponents().get(0);
|
||||
CandidatePair usedPair = udpComponent.getSelectedPair();
|
||||
if (usedPair != null)
|
||||
{
|
||||
LocalCandidate localCandidate = usedPair.getLocalCandidate();
|
||||
Candidate remoteCandidate = usedPair.getRemoteCandidate();
|
||||
logger.log(Level.INFO,
|
||||
"Remote: Local address " + localCandidate);
|
||||
logger.log(Level.INFO,
|
||||
"Remote: Peer address " + remoteCandidate);
|
||||
try
|
||||
{
|
||||
remoteJob = new RemotePseudoTcpJob(
|
||||
localCandidate.getTransportAddress(),
|
||||
remoteCandidate.getTransportAddress());
|
||||
}
|
||||
catch (UnknownHostException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE,
|
||||
"Error while trying to create"
|
||||
+ " remote pseudotcp thread " + ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log(Level.SEVERE,
|
||||
"Remote: Failed to select any candidate pair");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (iceProcessingState == IceProcessingState.TERMINATED
|
||||
|| iceProcessingState == IceProcessingState.FAILED)
|
||||
{
|
||||
/*
|
||||
* Though the process will be instructed to die, demonstrate
|
||||
* that Agent instances are to be explicitly prepared for
|
||||
* garbage collection.
|
||||
*/
|
||||
((Agent) evt.getSource()).free();
|
||||
if (remoteJob != null
|
||||
&& iceProcessingState == IceProcessingState.TERMINATED)
|
||||
{
|
||||
remoteJob.start();
|
||||
synchronized (remoteAgentMonitor)
|
||||
{
|
||||
remoteAgentMonitor.notifyAll();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable
|
||||
{
|
||||
startTime = System.currentTimeMillis();
|
||||
|
||||
int localPort = 7999;
|
||||
int remotePort = 6000;
|
||||
|
||||
Agent localAgent = createAgent(localPort);
|
||||
localAgent.setNominationStrategy(
|
||||
NominationStrategy.NOMINATE_HIGHEST_PRIO);
|
||||
Agent remotePeer =
|
||||
createAgent(remotePort);
|
||||
|
||||
localAgent.addStateChangeListener(new IcePseudoTcp.LocalIceProcessingListener());
|
||||
remotePeer.addStateChangeListener(new IcePseudoTcp.RemoteIceProcessingListener());
|
||||
|
||||
//let them fight ... fights forge character.
|
||||
localAgent.setControlling(true);
|
||||
remotePeer.setControlling(false);
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
Ice.transferRemoteCandidates(localAgent, remotePeer);
|
||||
for (IceMediaStream stream : localAgent.getStreams())
|
||||
{
|
||||
stream.setRemoteUfrag(remotePeer.getLocalUfrag());
|
||||
stream.setRemotePassword(remotePeer.getLocalPassword());
|
||||
}
|
||||
|
||||
Ice.transferRemoteCandidates(remotePeer, localAgent);
|
||||
|
||||
for (IceMediaStream stream : remotePeer.getStreams())
|
||||
{
|
||||
stream.setRemoteUfrag(localAgent.getLocalUfrag());
|
||||
stream.setRemotePassword(localAgent.getLocalPassword());
|
||||
}
|
||||
|
||||
logger.log(Level.INFO, "Total candidate gathering time: {0} ms",
|
||||
(endTime - startTime));
|
||||
logger.log(Level.INFO, "LocalAgent: {0}",
|
||||
localAgent);
|
||||
|
||||
localAgent.startConnectivityEstablishment();
|
||||
|
||||
//if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER)
|
||||
remotePeer.startConnectivityEstablishment();
|
||||
|
||||
|
||||
IceMediaStream dataStream = localAgent.getStream("data");
|
||||
|
||||
if (dataStream != null)
|
||||
{
|
||||
logger.log(Level.INFO,
|
||||
"Local data clist:" + dataStream.getCheckList());
|
||||
}
|
||||
//wait for one of the agents to complete it's job
|
||||
synchronized (remoteAgentMonitor)
|
||||
{
|
||||
remoteAgentMonitor.wait(agentJobTimeout);
|
||||
}
|
||||
if (remoteJob != null)
|
||||
{
|
||||
logger.log(Level.FINEST, "Remote thread join started");
|
||||
remoteJob.join();
|
||||
logger.log(Level.FINEST, "Remote thread joined");
|
||||
}
|
||||
if (localJob != null)
|
||||
{
|
||||
logger.log(Level.FINEST, "Local thread join started");
|
||||
localJob.join();
|
||||
logger.log(Level.FINEST, "Local thread joined");
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static class LocalPseudoTcpJob extends Thread implements Runnable
|
||||
{
|
||||
private InetSocketAddress transportAddr;
|
||||
|
||||
public LocalPseudoTcpJob(InetSocketAddress transportAddr)
|
||||
throws UnknownHostException
|
||||
{
|
||||
//This is required because of can not bind exception on Linux
|
||||
InetAddress locIP = InetAddress.getByName(
|
||||
transportAddr.getAddress().getHostAddress());
|
||||
this.transportAddr = new InetSocketAddress(locIP, transportAddr.getPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
logger.log(Level.FINEST, "Local pseudotcp worker started");
|
||||
try
|
||||
{
|
||||
logger.log(Level.INFO,
|
||||
"Local pseudotcp is using: " + transportAddr);
|
||||
PseudoTcpSocket socket = new PseudoTcpSocket(
|
||||
0, new DatagramSocket(transportAddr));
|
||||
socket.Accept(5000);
|
||||
byte[] buffer = new byte[TEST_BYTES_COUNT];
|
||||
int read = 0;
|
||||
while (read != TEST_BYTES_COUNT)
|
||||
{
|
||||
read += socket.getInputStream().read(buffer);
|
||||
logger.log(Level.FINEST, "Local job read: " + read);
|
||||
}
|
||||
//TODO: close when all received data is acked
|
||||
//socket.Close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logger.log(Level.FINEST, "Local pseudotcp worker finished");
|
||||
}
|
||||
}
|
||||
|
||||
private static class RemotePseudoTcpJob extends Thread implements Runnable
|
||||
{
|
||||
private InetSocketAddress transportAddr;
|
||||
private InetSocketAddress peerAddr;
|
||||
|
||||
public RemotePseudoTcpJob(InetSocketAddress transportAddr,
|
||||
InetSocketAddress peerAddr)
|
||||
throws UnknownHostException
|
||||
{
|
||||
//This is required because of can not bind exception on Linux
|
||||
//InetAddress locIP = InetAddress.getByName("127.0.0.1");
|
||||
InetAddress locIP = InetAddress.getByName(
|
||||
transportAddr.getAddress().getHostAddress());
|
||||
this.transportAddr = new InetSocketAddress(locIP, transportAddr.getPort());
|
||||
this.peerAddr = peerAddr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
logger.log(Level.FINEST, "Remote pseudotcp worker started");
|
||||
try
|
||||
{
|
||||
logger.log(Level.INFO,
|
||||
"Remote pseudotcp is using: " + transportAddr);
|
||||
PseudoTcpSocket socket = new PseudoTcpSocket(
|
||||
0, new DatagramSocket(transportAddr));
|
||||
long start, end;
|
||||
start = System.currentTimeMillis();
|
||||
socket.Connect(peerAddr, 5000);
|
||||
byte[] buffer = new byte[TEST_BYTES_COUNT];
|
||||
socket.getOutputStream().write(buffer);
|
||||
socket.getOutputStream().flush();
|
||||
socket.Close();
|
||||
end = System.currentTimeMillis();
|
||||
logger.log(Level.INFO,
|
||||
"Transferred " + TEST_BYTES_COUNT
|
||||
+ " bytes in " + ((end - start) / 1000) + " sec");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
logger.log(Level.FINEST, "Remote pseudotcp worker finished");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* static void TransferWithTcp() { try { final ServerSocket server = new
|
||||
* ServerSocket(); int testPort = 60000; server.bind(new
|
||||
* InetSocketAddress(testPort)); final Socket client = new Socket(); new
|
||||
* Thread(new RunnableImpl(client, server)).start(); long start =
|
||||
* System.currentTimeMillis(); byte[] serverBuffer = new
|
||||
* byte[TEST_BYTES_COUNT]; Socket clientAtServer = server.accept(); int
|
||||
* received = 0; do { received +=
|
||||
* clientAtServer.getInputStream().read(serverBuffer); } while (received <
|
||||
* serverBuffer.length); long end = System.currentTimeMillis();
|
||||
* logger.log(Level.INFO, "Transferred " + received + " bytes in " + ((end -
|
||||
* start) / 1000) + " sec"); } catch (IOException ex) {
|
||||
* Logger.getLogger(IcePseudoTcp.class.getName()).log(Level.SEVERE, null,
|
||||
* ex); }
|
||||
*
|
||||
* }
|
||||
*
|
||||
* static class RunnableImpl implements Runnable { private final Socket
|
||||
* client; private final ServerSocket server;
|
||||
*
|
||||
* public RunnableImpl(Socket client, ServerSocket server) { this.client =
|
||||
* client; this.server = server; }
|
||||
*
|
||||
* public void run() { try { client.connect(server.getLocalSocketAddress());
|
||||
* client.getOutputStream().write(new byte[TEST_BYTES_COUNT]); } catch
|
||||
* (IOException ex) {
|
||||
* Logger.getLogger(IcePseudoTcp.class.getName()).log(Level.SEVERE, null,
|
||||
* ex); } } }
|
||||
*/
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user