- PseudoTcpSocketImpl extends java.net.SocketImpl

- fixed example IcePseudoTcp
- pseudo TCP tests are included in test suite
This commit is contained in:
paweldomas 2012-08-10 09:03:29 +00:00
parent 290572f32f
commit f2529200be
7 changed files with 311 additions and 157 deletions

View File

@ -1089,6 +1089,7 @@ public class PseudoTCPBase
if (nFree < m_slist.get(0).len) if (nFree < m_slist.get(0).len)
{ {
m_slist.get(0).len -= nFree; m_slist.get(0).len -= nFree;
m_slist.get(0).seq += nFree;
nFree = 0; nFree = 0;
} }
else else
@ -1764,7 +1765,7 @@ public class PseudoTCPBase
} }
m_mss = m_mtu_advise - PACKET_OVERHEAD; m_mss = m_mtu_advise - PACKET_OVERHEAD;
// !?! Should we reset m_largest here? // !?! Should we reset m_largest here?
logger.log(Level.INFO, "Adjusting mss to " + m_mss + " bytes"); logger.log(Level.FINE, "Adjusting mss to " + m_mss + " bytes");
// Enforce minimums on ssthresh and cwnd // Enforce minimums on ssthresh and cwnd
m_ssthresh = Math.max(m_ssthresh, 2 * m_mss); m_ssthresh = Math.max(m_ssthresh, 2 * m_mss);

View File

@ -0,0 +1,26 @@
/*
* 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.net.*;
/**
*
* @author Pawel Domas
*/
public class PseudoTcpJavaSocket extends Socket {
public PseudoTcpJavaSocket(long conv_id) throws SocketException {
super(new PseudoTcpSocketImpl(conv_id));
}
public PseudoTcpJavaSocket(long conv_id, DatagramSocket socket)
throws SocketException {
super(new PseudoTcpSocketImpl(conv_id, socket));
}
}

View File

@ -9,9 +9,13 @@ package org.ice4j.pseudotcp;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.*; import java.util.logging.*;
public class PseudoTcpSocket implements IPseudoTcpNotify public class PseudoTcpSocketImpl
extends SocketImpl
implements IPseudoTcpNotify
{ {
/** /**
* The logger. * The logger.
@ -25,7 +29,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
/** /**
* Datagram socket used to handle network operations * Datagram socket used to handle network operations
*/ */
private final DatagramSocket socket; private DatagramSocket socket;
/** /**
* Current socket address of remote socket that we are connected to * Current socket address of remote socket that we are connected to
*/ */
@ -62,12 +66,23 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
*/ */
private IOException exception; private IOException exception;
/**
*
*/
private PseudoTcpInputStream inputStream;
/**
*
*/
private PseudoTcpOutputStream outputstream;
/** /**
* *
* @param conv_id conversation id, must be the same on both sides * @param conv_id conversation id, must be the same on both sides
* @param sock datagram socket used for network operations * @param sock datagram socket used for network operations
*/ */
public PseudoTcpSocket(long conv_id, DatagramSocket sock) public PseudoTcpSocketImpl(long conv_id, DatagramSocket sock)
{ {
pseudoTcp = new PseudoTCPBase(this, conv_id); pseudoTcp = new PseudoTCPBase(this, conv_id);
this.socket = sock; this.socket = sock;
@ -80,7 +95,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* @param conv_id conversation id, must be the same on both sides * @param conv_id conversation id, must be the same on both sides
* @throws SocketException * @throws SocketException
*/ */
public PseudoTcpSocket(long conv_id) public PseudoTcpSocketImpl(long conv_id)
throws SocketException throws SocketException
{ {
this(conv_id, new DatagramSocket()); this(conv_id, new DatagramSocket());
@ -93,7 +108,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* @param local_port * @param local_port
* @throws SocketException * @throws SocketException
*/ */
public PseudoTcpSocket(long conv_id, int local_port) public PseudoTcpSocketImpl(long conv_id, int local_port)
throws SocketException throws SocketException
{ {
this(conv_id, new DatagramSocket(local_port)); this(conv_id, new DatagramSocket(local_port));
@ -108,14 +123,125 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* @throws SocketException * @throws SocketException
* @throws UnknownHostException * @throws UnknownHostException
*/ */
public PseudoTcpSocket(long conv_id, String local_ip, int local_port) public PseudoTcpSocketImpl(long conv_id, String local_ip, int local_port)
throws SocketException, throws SocketException,
UnknownHostException UnknownHostException
{ {
this(conv_id, new DatagramSocket(local_port, this(conv_id, new DatagramSocket(local_port,
InetAddress.getByName(local_ip))); InetAddress.getByName(local_ip)));
} }
/**
* Sets the MTU parameter for this socket
* @param mtu
*/
public void setMTU(int mtu)
{
this.pseudoTcp.NotifyMTU(mtu);
}
/**
* Sets debug name that will be displayed in log messages for this socket
* @param debugName
*/
public void setDebugName(String debugName)
{
this.pseudoTcp.debugName = debugName;
}
/**
* Creates either a stream or a datagram socket.
* @param stream if true, create a stream socket; otherwise, create a datagram socket.
* @throws IOException if an I/O error occurs while creating the socket.
*/
protected void create(boolean stream)
throws IOException
{
//no effect
}
/**
* Connects this socket to the specified port on the named host.
* @param host the name of the remote host.
* @param port the port number.
* @throws IOException
*/
protected void connect(String host, int port)
throws IOException
{
Connect(new InetSocketAddress(InetAddress.getByName(host), port), 0);
}
/**
* Connects this socket to the specified port number on the specified host.
* @param address the IP address of the remote host.
* @param port the port number.
* @throws IOException if an I/O error occurs when attempting a connection.
*/
protected void connect(InetAddress address, int port)
throws IOException
{
connect(address.getHostAddress(), port);
}
/**
* Connects this socket to the specified port number on the specified host.
* A timeout of zero is interpreted as an infinite timeout.
* The connection will then block until established or an error occurs.
* @param address the Socket address of the remote host.
* @param timeout the timeout value, in milliseconds, or zero for no timeout.
* @throws IOException if an I/O error occurs when attempting a connection.
*/
protected void connect(SocketAddress address, int timeout)
throws IOException
{
InetSocketAddress inetAddr = (InetSocketAddress) address;
Connect(inetAddr, timeout);
}
/**
* Binds this socket to the specified port number on the specified host.
* @param host the IP address of the remote host.
* @param port the port number.
* @throws IOException
*/
public void bind(InetAddress host, int port)
throws IOException
{
if(socket != null)
socket.close();
InetSocketAddress newAddr = new InetSocketAddress(host.getHostAddress(),port);
this.socket = new DatagramSocket(newAddr);
}
/**
* Sets the maximum queue length for incoming connection
* indications (a request to connect) to the count argument.
* If a connection indication arrives when the queue is full,
* the connection is refused.
* @param backlog the maximum length of the queue.
* @throws IOException if an I/O error occurs when creating the queue.
*/
protected void listen(int backlog)
throws IOException
{
throw new UnsupportedOperationException("Not supported yet.");
}
private Map<Integer, Object> options = new HashMap<Integer,Object>();
public void setOption(int optID, Object value)
throws SocketException
{
options.put(optID, value);
}
public Object getOption(int optID)
throws SocketException
{
return options.get(optID);
}
/** /**
* Start connection procedure * Start connection procedure
* *
@ -126,10 +252,12 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
public void Connect(InetSocketAddress remoteAddress, long timeout) public void Connect(InetSocketAddress remoteAddress, long timeout)
throws IOException throws IOException
{ {
logger.fine("Connecting to "+remoteAddress);
this.remoteAddr = remoteAddress; this.remoteAddr = remoteAddress;
StartThreads(); StartThreads();
pseudoTcp.Connect(); pseudoTcp.Connect();
UpdateClock(); UpdateClock();
boolean noTimeout = timeout <= 0;
try try
{ {
long elapsed = 0; long elapsed = 0;
@ -138,7 +266,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
synchronized (state_notify) synchronized (state_notify)
{ {
while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED
&& elapsed < timeout) && (noTimeout ? true : (elapsed < timeout)) )
{ {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
state_notify.wait(timeout); state_notify.wait(timeout);
@ -153,27 +281,11 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
} }
catch (InterruptedException ex) catch (InterruptedException ex)
{ {
Close(); close();
throw new IOException("Connect aborted"); 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. * Blocking method waits for connection.
* *
@ -183,7 +295,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
public void Accept(int timeout) public void Accept(int timeout)
throws IOException throws IOException
{ {
try try
{ {
StartThreads(); StartThreads();
PseudoTcpState state = pseudoTcp.getState(); PseudoTcpState state = pseudoTcp.getState();
@ -210,7 +322,20 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
throw e; throw e;
} }
} }
/**
* Accepts a connection.
* @param s the accepted connection.
* @throws IOException if an I/O error occurs when accepting the connection.
*/
protected void accept(SocketImpl s)
throws IOException
{
//TODO: not sure how this should work
int timeout = 5000;
Accept(timeout);
}
/** /**
* *
* @return current TCP state * @return current TCP state
@ -239,6 +364,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
pseudoTcp.NotifyClock(System.currentTimeMillis()); pseudoTcp.NotifyClock(System.currentTimeMillis());
receiveThread = new Thread(new Runnable() receiveThread = new Thread(new Runnable()
{ {
@Override
public void run() public void run()
{ {
ReceivePackets(); ReceivePackets();
@ -246,6 +372,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
}, "PseudoTcpReceiveThread"); }, "PseudoTcpReceiveThread");
clockThread = new Thread(new Runnable() clockThread = new Thread(new Runnable()
{ {
@Override
public void run() public void run()
{ {
RunClock(); RunClock();
@ -257,45 +384,13 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
clockThread.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) * Implements @link(IPseudoTcpNotify)
* Called when TCP enters connected state. * Called when TCP enters connected state.
* *
* @param tcp * @param tcp
*/ */
@Override
public void OnTcpOpen(PseudoTCPBase tcp) public void OnTcpOpen(PseudoTCPBase tcp)
{ {
logger.log(Level.FINE, "tcp opened"); logger.log(Level.FINE, "tcp opened");
@ -313,6 +408,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* *
* @param tcp * @param tcp
*/ */
@Override
public void OnTcpReadable(PseudoTCPBase tcp) public void OnTcpReadable(PseudoTCPBase tcp)
{ {
//release all thread blocked at read_notify monitor //release all thread blocked at read_notify monitor
@ -330,6 +426,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* *
* @param tcp * @param tcp
*/ */
@Override
public void OnTcpWriteable(PseudoTCPBase tcp) public void OnTcpWriteable(PseudoTCPBase tcp)
{ {
@ -350,6 +447,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* @param tcp * @param tcp
* @param e * @param e
*/ */
@Override
public void OnTcpClosed(PseudoTCPBase tcp, IOException e) public void OnTcpClosed(PseudoTCPBase tcp, IOException e)
{ {
if (e != null) if (e != null)
@ -409,6 +507,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
* @param len * @param len
* @return * @return
*/ */
@Override
public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len) public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
{ {
if (logger.isLoggable(Level.FINEST)) if (logger.isLoggable(Level.FINEST))
@ -417,6 +516,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
} }
try try
{ {
//TODO: in case the packet is too long it should return WR_TOO_LARGE
DatagramPacket packet = new DatagramPacket(buffer, len, remoteAddr); DatagramPacket packet = new DatagramPacket(buffer, len, remoteAddr);
socket.send(packet); socket.send(packet);
return WriteResult.WR_SUCCESS; return WriteResult.WR_SUCCESS;
@ -479,7 +579,7 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
if (runReceive) if (runReceive)
{ {
logger.log(Level.SEVERE, logger.log(Level.SEVERE,
"ReceivePackets exception: " + ex); "ReceivePackets exception: " + ex);
pseudoTcp.closedown(ex); pseudoTcp.closedown(ex);
} }
break; break;
@ -536,14 +636,14 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
} }
} }
private OutputStream outputstream;
/** /**
* Lazy initialization for socket class * Returns an output stream for this socket.
* * @return an output stream for writing to this socket.
* @return output socket object * @throws IOException if an I/O error occurs when creating the output stream.
*/ */
public OutputStream getOutputStream() public OutputStream getOutputStream()
throws IOException
{ {
if (outputstream == null) if (outputstream == null)
{ {
@ -551,14 +651,15 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
} }
return outputstream; return outputstream;
} }
private PseudoTcpInputStream inputStream;
/** /**
* Lazy initialization for socket class * Returns an input stream for this socket.
* * @return a stream for reading from this socket.
* @return output socket object * @throws IOException
*/ */
public InputStream getInputStream() public InputStream getInputStream()
throws IOException
{ {
if (inputStream == null) if (inputStream == null)
{ {
@ -567,6 +668,62 @@ public class PseudoTcpSocket implements IPseudoTcpNotify
return inputStream; return inputStream;
} }
/**
* Returns the number of bytes that can be read from this socket without blocking.
* @return the number of bytes that can be read from this socket without blocking.
* @throws IOException if an I/O error occurs when determining the number of bytes available.
*/
protected int available()
throws IOException
{
return getInputStream().available();
}
/**
* Closes this 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);
}
}
/**
* Send one byte of urgent data on the socket. The byte to be sent is the low eight bits of the parameter
* @param data The byte of data to send
* @throws IOException if there is an error sending the data.
*/
protected void sendUrgentData(int data)
throws IOException
{
throw new RuntimeException("Sending urgent data is not supported");
}
/** /**
* This class implements @link(InputStream) * * This class implements @link(InputStream) *
*/ */

View File

@ -73,6 +73,12 @@ public class StunTestSuite
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest.class); suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest.class);
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v6.class); suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v6.class);
suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v4v6.class); suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v4v6.class);
//pseudotcp
suite.addTestSuite(org.ice4j.pseudotcp.PseudoTcpTestTransfer.class);
suite.addTestSuite(org.ice4j.pseudotcp.PseudoTcpTestPingPong.class);
suite.addTestSuite(org.ice4j.pseudotcp.PseudoTcpTestRecvWindow.class);
suite.addTestSuite(org.ice4j.pseudotcp.PseudoTcpStreamTest.class);
return suite; return suite;
} }

View File

@ -23,7 +23,7 @@ public class PseudoTcpStreamTest extends MultiThreadSupportTest
* The logger. * The logger.
*/ */
private static final Logger logger = private static final Logger logger =
Logger.getLogger(PseudoTCPBase.class.getName()); Logger.getLogger(PseudoTcpStreamTest.class.getName());
public PseudoTcpStreamTest() public PseudoTcpStreamTest()
{ {
@ -41,16 +41,18 @@ public class PseudoTcpStreamTest extends MultiThreadSupportTest
long conv_id = 0; long conv_id = 0;
final int size = 1000000; final int size = 1000000;
int transferTimeout = 5000; int transferTimeout = 5000;
final PseudoTcpSocket server = new PseudoTcpSocket(conv_id, server_port); final PseudoTcpSocketImpl server = new PseudoTcpSocketImpl(conv_id);
final PseudoTcpSocket client = new PseudoTcpSocket(conv_id); final PseudoTcpSocketImpl client = new PseudoTcpSocketImpl(conv_id);
Thread serverThread = new Thread(new Runnable() Thread serverThread = new Thread(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
try try
{ {
server.Accept(300); server.bind(InetAddress.getLocalHost(), server_port);
server.accept(null);
int rcvd = 0; int rcvd = 0;
while (rcvd != size) while (rcvd != size)
{ {
@ -80,11 +82,11 @@ public class PseudoTcpStreamTest extends MultiThreadSupportTest
{ {
try try
{ {
client.Connect("localhost", server_port, 30000); client.connect("localhost", server_port);
assertEquals(PseudoTcpState.TCP_ESTABLISHED, client.getState()); assertEquals(PseudoTcpState.TCP_ESTABLISHED, client.getState());
client.getOutputStream().write(new byte[size]); client.getOutputStream().write(new byte[size]);
client.getOutputStream().flush(); client.getOutputStream().flush();
client.Close(); client.close();
} }
catch (IOException ex) catch (IOException ex)
{ {
@ -127,7 +129,7 @@ public class PseudoTcpStreamTest extends MultiThreadSupportTest
{ {
try try
{ {
PseudoTcpSocket server = new PseudoTcpSocket(0); PseudoTcpSocketImpl server = new PseudoTcpSocketImpl(0);
server.Accept(10); server.Accept(10);
fail("Should throw timeout exception"); fail("Should throw timeout exception");
} }

View File

@ -84,7 +84,6 @@ public class PseudoTcpTestRecvWindow extends PseudoTcpTestBase
ScheduleWriteAction(0); ScheduleWriteAction(0);
long transferTout = MaxTransferTime(dummy.length, kMinTransferRate); long transferTout = MaxTransferTime(dummy.length, kMinTransferRate);
System.out.println("Timeout: " + transferTout);
boolean transfferInTime = assert_Disconnected_wait(transferTout); boolean transfferInTime = assert_Disconnected_wait(transferTout);
elapsed = PseudoTCPBase.Now() - start; elapsed = PseudoTCPBase.Now() - start;
StopClocks(); StopClocks();

View File

@ -30,7 +30,7 @@ public class IcePseudoTcp
* The logger. * The logger.
*/ */
private static final Logger logger = private static final Logger logger =
Logger.getLogger(CheckList.class.getName()); Logger.getLogger(IcePseudoTcp.class.getName());
private static long startTime; private static long startTime;
/** /**
* Local job thread variable * Local job thread variable
@ -43,7 +43,7 @@ public class IcePseudoTcp
/** /**
* Test data size * Test data size
*/ */
private static final int TEST_BYTES_COUNT = 1000000; private static final int TEST_BYTES_COUNT = 15000000;
/** /**
* Flag inidcates if STUN should be used * Flag inidcates if STUN should be used
*/ */
@ -164,7 +164,7 @@ public class IcePseudoTcp
try try
{ {
localJob = new LocalPseudoTcpJob( localJob = new LocalPseudoTcpJob(
localCandidate.getTransportAddress()); localCandidate.getDatagramSocket());
} }
catch (UnknownHostException ex) catch (UnknownHostException ex)
{ {
@ -188,20 +188,14 @@ public class IcePseudoTcp
* that Agent instances are to be explicitly prepared for * that Agent instances are to be explicitly prepared for
* garbage collection. * garbage collection.
*/ */
((Agent) evt.getSource()).free();
if (localJob != null if (localJob != null
&& iceProcessingState == IceProcessingState.TERMINATED) && iceProcessingState == IceProcessingState.TERMINATED)
{ {
localJob.start(); localJob.start();
synchronized (localAgentMonitor)
{
localAgentMonitor.notifyAll();
}
} }
else synchronized (localAgentMonitor)
{ {
System.exit(0); localAgentMonitor.notifyAll();
} }
} }
} }
@ -229,7 +223,7 @@ public class IcePseudoTcp
{ {
logger.log(Level.INFO, logger.log(Level.INFO,
"Remote: Total ICE processing time: " "Remote: Total ICE processing time: "
+ (processingEndTime - startTime) + " ms"); + (processingEndTime - startTime) + " ms ");
Agent agent = (Agent) evt.getSource(); Agent agent = (Agent) evt.getSource();
logger.log(Level.INFO, "Remote: Create pseudo tcp stream"); logger.log(Level.INFO, "Remote: Create pseudo tcp stream");
@ -247,8 +241,8 @@ public class IcePseudoTcp
try try
{ {
remoteJob = new RemotePseudoTcpJob( remoteJob = new RemotePseudoTcpJob(
localCandidate.getTransportAddress(), localCandidate.getDatagramSocket(),
remoteCandidate.getTransportAddress()); remoteCandidate.getTransportAddress());
} }
catch (UnknownHostException ex) catch (UnknownHostException ex)
{ {
@ -273,19 +267,14 @@ public class IcePseudoTcp
* that Agent instances are to be explicitly prepared for * that Agent instances are to be explicitly prepared for
* garbage collection. * garbage collection.
*/ */
((Agent) evt.getSource()).free();
if (remoteJob != null if (remoteJob != null
&& iceProcessingState == IceProcessingState.TERMINATED) && iceProcessingState == IceProcessingState.TERMINATED)
{ {
remoteJob.start(); remoteJob.start();
synchronized (remoteAgentMonitor)
{
remoteAgentMonitor.notifyAll();
}
} }
else synchronized (remoteAgentMonitor)
{ {
System.exit(0); remoteAgentMonitor.notifyAll();
} }
} }
} }
@ -356,28 +345,27 @@ public class IcePseudoTcp
{ {
logger.log(Level.FINEST, "Remote thread join started"); logger.log(Level.FINEST, "Remote thread join started");
remoteJob.join(); remoteJob.join();
logger.log(Level.FINEST, "Remote thread joined"); logger.log(Level.FINEST, "Remote thread joined");
} }
remotePeer.free();
if (localJob != null) if (localJob != null)
{ {
logger.log(Level.FINEST, "Local thread join started"); logger.log(Level.FINEST, "Local thread join started");
localJob.join(); localJob.join();
logger.log(Level.FINEST, "Local thread joined"); logger.log(Level.FINEST, "Local thread joined");
} }
localAgent.free();
System.exit(0); System.exit(0);
} }
private static class LocalPseudoTcpJob extends Thread implements Runnable private static class LocalPseudoTcpJob extends Thread implements Runnable
{ {
private InetSocketAddress transportAddr; private DatagramSocket dgramSocket;
public LocalPseudoTcpJob(InetSocketAddress transportAddr) public LocalPseudoTcpJob(DatagramSocket socket)
throws UnknownHostException throws UnknownHostException
{ {
//This is required because of can not bind exception on Linux this.dgramSocket = socket;
InetAddress locIP = InetAddress.getByName(
transportAddr.getAddress().getHostAddress());
this.transportAddr = new InetSocketAddress(locIP, transportAddr.getPort());
} }
@Override @Override
@ -387,9 +375,12 @@ public class IcePseudoTcp
try try
{ {
logger.log(Level.INFO, logger.log(Level.INFO,
"Local pseudotcp is using: " + transportAddr); "Local pseudotcp is using: "
PseudoTcpSocket socket = new PseudoTcpSocket( + dgramSocket.getLocalSocketAddress());
0, new DatagramSocket(transportAddr)); PseudoTcpSocketImpl socket = new PseudoTcpSocketImpl(
1073741824, dgramSocket);
socket.setMTU(1500);
socket.setDebugName("L");
socket.Accept(5000); socket.Accept(5000);
byte[] buffer = new byte[TEST_BYTES_COUNT]; byte[] buffer = new byte[TEST_BYTES_COUNT];
int read = 0; int read = 0;
@ -399,7 +390,7 @@ public class IcePseudoTcp
logger.log(Level.FINEST, "Local job read: " + read); logger.log(Level.FINEST, "Local job read: " + read);
} }
//TODO: close when all received data is acked //TODO: close when all received data is acked
//socket.Close(); //socket.close();
} }
catch (IOException e) catch (IOException e)
{ {
@ -411,18 +402,14 @@ public class IcePseudoTcp
private static class RemotePseudoTcpJob extends Thread implements Runnable private static class RemotePseudoTcpJob extends Thread implements Runnable
{ {
private InetSocketAddress transportAddr; private DatagramSocket dgramSocket;
private InetSocketAddress peerAddr; private InetSocketAddress peerAddr;
public RemotePseudoTcpJob(InetSocketAddress transportAddr, public RemotePseudoTcpJob(DatagramSocket socket,
InetSocketAddress peerAddr) InetSocketAddress peerAddr)
throws UnknownHostException throws UnknownHostException
{ {
//This is required because of can not bind exception on Linux this.dgramSocket = socket;
//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; this.peerAddr = peerAddr;
} }
@ -433,16 +420,21 @@ public class IcePseudoTcp
try try
{ {
logger.log(Level.INFO, logger.log(Level.INFO,
"Remote pseudotcp is using: " + transportAddr); "Remote pseudotcp is using: " +
PseudoTcpSocket socket = new PseudoTcpSocket( dgramSocket.getLocalSocketAddress()
0, new DatagramSocket(transportAddr)); +" and will comunicate with: " + peerAddr);
PseudoTcpSocketImpl socket = new PseudoTcpSocketImpl(
1073741824, dgramSocket);
socket.setMTU(1500);
socket.setDebugName("R");
long start, end; long start, end;
start = System.currentTimeMillis(); start = System.currentTimeMillis();
socket.Connect(peerAddr, 5000); socket.Connect(peerAddr, 5000);
byte[] buffer = new byte[TEST_BYTES_COUNT]; byte[] buffer = new byte[TEST_BYTES_COUNT];
socket.getOutputStream().write(buffer); socket.getOutputStream().write(buffer);
socket.getOutputStream().flush(); socket.getOutputStream().flush();
socket.Close(); //Socket will be closed by the iceAgent
//socket.close();
end = System.currentTimeMillis(); end = System.currentTimeMillis();
logger.log(Level.INFO, logger.log(Level.INFO,
"Transferred " + TEST_BYTES_COUNT "Transferred " + TEST_BYTES_COUNT
@ -456,33 +448,4 @@ public class IcePseudoTcp
} }
} }
/*
* 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); } } }
*/
} }