Restores building and running with a non-8 JDK after r499.
This commit is contained in:
parent
446607e5d7
commit
e9398b9353
13
build.xml
13
build.xml
@ -35,21 +35,26 @@
|
||||
|
||||
<!-- compiles all classes -->
|
||||
<target name="compile" depends="init">
|
||||
<condition property="compile.javac.exclude.name" value="**/jdk8/**">
|
||||
<not>
|
||||
<matches string="${java.runtime.version}" pattern="^1\.8(\..*)?$"/>
|
||||
</not>
|
||||
</condition>
|
||||
<javac classpathref="project.class.path"
|
||||
debug="true"
|
||||
deprecation="true"
|
||||
destdir="${dest}"
|
||||
includeantruntime="false"
|
||||
nowarn="false"
|
||||
optimize="true"
|
||||
target="1.5"
|
||||
source="1.5"
|
||||
includeantruntime="false">
|
||||
<src path="${src}"/>
|
||||
target="1.5">
|
||||
<compilerarg value="-Xlint"/>
|
||||
<src path="${src}"/>
|
||||
<exclude name="${compile.javac.exclude.name}"/>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
|
||||
<!-- compiles all tests -->
|
||||
<target name="compile-tests" depends="init">
|
||||
<javac classpathref="test.class.path"
|
||||
|
||||
@ -56,7 +56,7 @@ public class MultiplexingTcpHostHarvester
|
||||
*/
|
||||
static void closeNoExceptions(Channel channel)
|
||||
{
|
||||
MuxServerSocketChannel.closeNoExceptions(channel);
|
||||
MuxServerSocketChannelFactory.closeNoExceptions(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -606,23 +606,24 @@ public class MultiplexingTcpHostHarvester
|
||||
for (TransportAddress transportAddress : localAddresses)
|
||||
{
|
||||
ServerSocketChannel channel
|
||||
= MuxServerSocketChannel.openAndBind(
|
||||
/* properties */ null,
|
||||
new InetSocketAddress(
|
||||
transportAddress.getAddress(),
|
||||
transportAddress.getPort()),
|
||||
/* backlog */ 0,
|
||||
new DatagramPacketFilter()
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(DatagramPacket p)
|
||||
= MuxServerSocketChannelFactory
|
||||
.openAndBindMuxServerSocketChannel(
|
||||
/* properties */ null,
|
||||
new InetSocketAddress(
|
||||
transportAddress.getAddress(),
|
||||
transportAddress.getPort()),
|
||||
/* backlog */ 0,
|
||||
new DatagramPacketFilter()
|
||||
{
|
||||
return isFirstDatagramPacket(p);
|
||||
}
|
||||
});
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(DatagramPacket p)
|
||||
{
|
||||
return isFirstDatagramPacket(p);
|
||||
}
|
||||
});
|
||||
|
||||
serverSocketChannels.add(channel);
|
||||
}
|
||||
@ -1121,7 +1122,7 @@ public class MultiplexingTcpHostHarvester
|
||||
|
||||
if (lastActive != -1
|
||||
&& now - lastActive
|
||||
> MuxServerSocketChannel
|
||||
> MuxServerSocketChannelFactory
|
||||
.SOCKET_CHANNEL_READ_TIMEOUT)
|
||||
{
|
||||
// De-register from the Selector.
|
||||
@ -1456,7 +1457,8 @@ public class MultiplexingTcpHostHarvester
|
||||
try
|
||||
{
|
||||
readSelector.select(
|
||||
MuxServerSocketChannel.SOCKET_CHANNEL_READ_TIMEOUT
|
||||
MuxServerSocketChannelFactory
|
||||
.SOCKET_CHANNEL_READ_TIMEOUT
|
||||
/ 2);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
|
||||
@ -11,8 +11,6 @@ import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code ServerSocketChannel} which delegates (its method calls)
|
||||
* to another {@code ServerSocketChannel}. In other words, the former wraps the
|
||||
@ -20,9 +18,8 @@ import sun.nio.ch.*;
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
public class BaseDelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
extends ServerSocketChannel
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The {@link ServerSocketChannel} this instance delegates (its method
|
||||
@ -30,12 +27,6 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
*/
|
||||
protected final T delegate;
|
||||
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* The {@code ServerSocket} to be reported by this instance.
|
||||
*/
|
||||
@ -47,20 +38,18 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
private final Object socketSyncRoot = new Object();
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingServerSocketChannel} instance which is
|
||||
* to delegate (its method calls) to a specific {@code ServerSocketChannel}.
|
||||
* Initializes a new {@code BaseDelegatingServerSocketChannel} instance
|
||||
* which is to delegate (its method calls) to a specific
|
||||
* {@code ServerSocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code ServerSocketChannel} the new instance is to
|
||||
* delegate (its method calls) to
|
||||
*/
|
||||
public DelegatingServerSocketChannel(T delegate)
|
||||
public BaseDelegatingServerSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate.provider());
|
||||
|
||||
this.delegate = delegate;
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,28 +79,6 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
@ -193,7 +160,8 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this {@code DelegatingServerSocketChannel} is bound.
|
||||
* Determines whether this {@code BaseDelegatingServerSocketChannel} is
|
||||
* bound.
|
||||
*
|
||||
* @return {@code true} if this instancei bound; otherwise, {@code false}
|
||||
*/
|
||||
@ -209,18 +177,6 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
@ -285,37 +241,4 @@ public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
{
|
||||
return delegate.supportedOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
}
|
||||
@ -12,29 +12,20 @@ import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code SocketChannel} which delegates (its method calls) to
|
||||
* another {@code SocketChannel}. In other words, the former wraps the latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
public class BaseDelegatingSocketChannel<T extends SocketChannel>
|
||||
extends SocketChannel
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The {@link SocketChannel} this instance delegates (its method calls) to.
|
||||
*/
|
||||
protected final T delegate;
|
||||
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* The {@code Socket} to be reported by this instance.
|
||||
*/
|
||||
@ -46,20 +37,17 @@ public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
private final Object socketSyncRoot = new Object();
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingSocketChannel} instance which is to
|
||||
* delegate (its method calls) to a specific {@code SocketChannel}.
|
||||
* Initializes a new {@code BaseDelegatingSocketChannel} instance which is
|
||||
* to delegate (its method calls) to a specific {@code SocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code SocketChannel} the new instance is to delegate
|
||||
* (its method calls) to
|
||||
*/
|
||||
public DelegatingSocketChannel(T delegate)
|
||||
public BaseDelegatingSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate.provider());
|
||||
|
||||
this.delegate = delegate;
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,28 +87,6 @@ public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
return delegate.finishConnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
@ -218,18 +184,6 @@ public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
return delegate.isConnectionPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
@ -345,39 +299,6 @@ public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
return delegate.supportedOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
@ -210,15 +210,15 @@ public class DelegatingSocket
|
||||
|
||||
if (delegate instanceof DelegatingSocket)
|
||||
{
|
||||
// FIXME DelegatingSocketChannel was in need of a Socket which wraps
|
||||
// another Socket and reports a specific SocketChannel. Since
|
||||
// DelegatingSocket does that, DelegatingSocketChannel used it.
|
||||
// FIXME BaseDelegatingSocketChannel was in need of a Socket which
|
||||
// wraps another Socket and reports a specific SocketChannel. Since
|
||||
// DelegatingSocket does that, BaseDelegatingSocketChannel used it.
|
||||
// Unfortunately, it turned out after much debugging that
|
||||
// DelegatingSocket has the additional function of flagging where
|
||||
// UDP packets sent over TCP are to be framed. As a temporary fix,
|
||||
// do not treat the sockets of DelegatingSocketChannel as
|
||||
// DelegatingSockets.
|
||||
if (channel instanceof DelegatingSocketChannel)
|
||||
if (channel instanceof BaseDelegatingSocketChannel)
|
||||
delegateAsDelegatingSocket = null;
|
||||
else
|
||||
delegateAsDelegatingSocket = (DelegatingSocket) delegate;
|
||||
|
||||
@ -13,7 +13,7 @@ import org.ice4j.ice.harvest.*;
|
||||
|
||||
/**
|
||||
* Implements a {@link DatagramPacketFilter} which allows demultiplexing HTTP(S)
|
||||
* out of {@link MuxServerSocketChannel}. Accepts HTTP, SSL v2, and TLS. Rejects
|
||||
* out of {@code MuxServerSocketChannel}. Accepts HTTP, SSL v2, and TLS. Rejects
|
||||
* Google TURN SSLTCP.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
|
||||
250
src/org/ice4j/socket/MuxServerSocketChannelFactory.java
Normal file
250
src/org/ice4j/socket/MuxServerSocketChannelFactory.java
Normal file
@ -0,0 +1,250 @@
|
||||
/*
|
||||
* 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 org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* If supported by the runtime, initializes {@link ServerSocketChannel}s which
|
||||
* are capable of sharing their listening endpoints with multiple others like
|
||||
* them.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class MuxServerSocketChannelFactory
|
||||
{
|
||||
/**
|
||||
* The {@code Logger} used by the {@code MuxServerSocketChannelFactory}
|
||||
* class (and its instances) to print debug information.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(MuxServerSocketChannelFactory.class.getName());
|
||||
|
||||
/**
|
||||
* The reflection of the {@code openAndBind} method of the
|
||||
* {@code MuxServerSocketChannel} class.
|
||||
*/
|
||||
private static final Method OPEN_AND_BIND_METHOD;
|
||||
|
||||
/**
|
||||
* The maximum number of milliseconds to wait for an accepted
|
||||
* {@code SocketChannel} to provide incoming/readable data before it is
|
||||
* considered abandoned by the client.
|
||||
*/
|
||||
public static final int SOCKET_CHANNEL_READ_TIMEOUT = 15 * 1000;
|
||||
|
||||
/**
|
||||
* The name of the {@code boolean} property of the {@code socket} property
|
||||
* of the {@code ServerSocketChannel} returned by {@link #openAndBind(Map,
|
||||
* SocketAddress, int, DatagramPacketFilter)} which specifies the value of
|
||||
* the {@code SO_REUSEADDR} socket option.
|
||||
*/
|
||||
public static final String SOCKET_REUSE_ADDRESS_PROPERTY_NAME
|
||||
= "socket.reuseAddress";
|
||||
|
||||
static
|
||||
{
|
||||
Class<?> clazz;
|
||||
|
||||
try
|
||||
{
|
||||
clazz
|
||||
= Class.forName("org.ice4j.socket.jdk8.MuxServerSocketChannel");
|
||||
}
|
||||
catch (ClassNotFoundException cnfex)
|
||||
{
|
||||
// The class cannot be located probably because ICE4J was not built
|
||||
// on JDK 8.
|
||||
clazz = null;
|
||||
logger.warning(
|
||||
"ICE4J does not support sharing of listening endpoints"
|
||||
+ " (probably because it was not built on JDK 8).");
|
||||
}
|
||||
catch (LinkageError lerr)
|
||||
{
|
||||
// The linkage fails probably because ICE4J was built on JDK 8 but
|
||||
// is not run on JDK 8.
|
||||
clazz = null;
|
||||
logger.warning(
|
||||
"ICE4J does not support sharing of listening endpoints"
|
||||
+ " (probably because it is not running on JDK 8).");
|
||||
}
|
||||
Method method;
|
||||
|
||||
if (clazz == null)
|
||||
{
|
||||
method = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
method
|
||||
= clazz.getDeclaredMethod(
|
||||
"openAndBind",
|
||||
Map.class,
|
||||
SocketAddress.class,
|
||||
int.class,
|
||||
DatagramPacketFilter.class);
|
||||
}
|
||||
catch (NoSuchMethodException nsmex)
|
||||
{
|
||||
throw new RuntimeException(nsmex);
|
||||
}
|
||||
}
|
||||
OPEN_AND_BIND_METHOD = method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a {@code Channel} and swallows any {@link IOException}.
|
||||
*
|
||||
* @param channel the {@code Channel} to close
|
||||
*/
|
||||
public static void closeNoExceptions(Channel channel)
|
||||
{
|
||||
try
|
||||
{
|
||||
channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens and binds a new {@code MuxServerSocketChannel} instance if
|
||||
* supported by the runtime. If there are other (existing)
|
||||
* {@code MuxServerSocketChannel} open and bound on the specified listening
|
||||
* {@code endpoint}, the new instance will share it with them. If the
|
||||
* sharing of listening endpoints is not supported by the runtime, falls
|
||||
* back to {@link #openAndBindServerSocketChannel(Map, SocketAddress, int)}.
|
||||
*
|
||||
* @param properties a {@code Map} of the values to be assigned to
|
||||
* properties of the underlying {@link ServerSocketChannel} which is to
|
||||
* actually listen on the specified {@code endpoint}. If the new instance is
|
||||
* not the first to open and bind the specified {@code endpoint}, the
|
||||
* {@code properties} and their respective values may not be used.
|
||||
* @param endpoint the IP and port the new instance is to bind to
|
||||
* @param backlog the requested maximum number of pending incoming
|
||||
* connections to be queued. If the new instance is not the first to open
|
||||
* and bind the specified {@code endpoint}, the value may not be used.
|
||||
* @param filter the {@code DatagramPacketFilter} to demultiplex (i.e.
|
||||
* recognize) the content meant for the new instance
|
||||
* @return a new {@code MuxServerSocketChannel} instance open and bound on
|
||||
* the specified listening {@code endpoint}
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
public static ServerSocketChannel openAndBindMuxServerSocketChannel(
|
||||
Map<String,Object> properties,
|
||||
SocketAddress endpoint,
|
||||
int backlog,
|
||||
DatagramPacketFilter filter)
|
||||
throws IOException
|
||||
{
|
||||
Method method = OPEN_AND_BIND_METHOD;
|
||||
|
||||
if (method == null)
|
||||
{
|
||||
return
|
||||
openAndBindServerSocketChannel(properties, endpoint, backlog);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
return
|
||||
(ServerSocketChannel)
|
||||
method.invoke(
|
||||
null,
|
||||
properties, endpoint, backlog, filter);
|
||||
}
|
||||
catch (IllegalAccessException iaex)
|
||||
{
|
||||
IllegalAccessError iaerr
|
||||
= new IllegalAccessError(iaex.getMessage());
|
||||
|
||||
iaerr.initCause(iaex);
|
||||
throw iaerr;
|
||||
}
|
||||
catch (InvocationTargetException itex)
|
||||
{
|
||||
Throwable cause = itex.getCause();
|
||||
|
||||
if (cause == null)
|
||||
throw new RuntimeException(itex);
|
||||
else if (cause instanceof Error)
|
||||
throw (Error) cause;
|
||||
else if (cause instanceof IOException)
|
||||
throw (IOException) cause;
|
||||
else if (cause instanceof RuntimeException)
|
||||
throw (RuntimeException) cause;
|
||||
else
|
||||
throw new RuntimeException(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens and binds a new {@code ServerSocketChannel} instance.
|
||||
*
|
||||
* @param properties a {@code Map} of the values to be assigned to
|
||||
* properties of the underlying {@link ServerSocketChannel} which is to
|
||||
* actually listen on the specified {@code endpoint}. If the new instance is
|
||||
* not the first to open and bind the specified {@code endpoint}, the
|
||||
* {@code properties} and their respective values may not be used.
|
||||
* @param endpoint the IP and port the new instance is to bind to
|
||||
* @param backlog the requested maximum number of pending incoming
|
||||
* connections to be queued. If the new instance is not the first to open
|
||||
* and bind the specified {@code endpoint}, the value may not be used.
|
||||
* @return a new {@code ServerSocketChannel} instance open and bound on the
|
||||
* specified listening {@code endpoint}
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
public static ServerSocketChannel openAndBindServerSocketChannel(
|
||||
Map<String,Object> properties,
|
||||
SocketAddress endpoint,
|
||||
int backlog)
|
||||
throws IOException
|
||||
{
|
||||
ServerSocketChannel channel = ServerSocketChannel.open();
|
||||
// Apply the specified properties.
|
||||
ServerSocket socket = channel.socket();
|
||||
|
||||
if (properties != null && !properties.isEmpty())
|
||||
{
|
||||
for (Map.Entry<String,Object> property
|
||||
: properties.entrySet())
|
||||
{
|
||||
String name = property.getKey();
|
||||
|
||||
if (SOCKET_REUSE_ADDRESS_PROPERTY_NAME.equals(name))
|
||||
{
|
||||
Object value = property.getValue();
|
||||
boolean on;
|
||||
|
||||
if (value == null)
|
||||
on = false;
|
||||
else if (value instanceof Boolean)
|
||||
on = ((Boolean) value).booleanValue();
|
||||
else
|
||||
on = Boolean.valueOf(value.toString());
|
||||
|
||||
socket.setReuseAddress(on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket.bind(endpoint, backlog);
|
||||
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
114
src/org/ice4j/socket/jdk8/DelegatingServerSocketChannel.java
Normal file
114
src/org/ice4j/socket/jdk8/DelegatingServerSocketChannel.java
Normal file
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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 org.ice4j.socket.jdk8;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import org.ice4j.socket.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code ServerSocketChannel} which delegates (its method calls)
|
||||
* to another {@code ServerSocketChannel}. In other words, the former wraps the
|
||||
* latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
extends BaseDelegatingServerSocketChannel<T>
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingServerSocketChannel} instance which is
|
||||
* to delegate (its method calls) to a specific {@code ServerSocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code ServerSocketChannel} the new instance is to
|
||||
* delegate (its method calls) to
|
||||
*/
|
||||
public DelegatingServerSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate);
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
}
|
||||
113
src/org/ice4j/socket/jdk8/DelegatingSocketChannel.java
Normal file
113
src/org/ice4j/socket/jdk8/DelegatingSocketChannel.java
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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 org.ice4j.socket.jdk8;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
import org.ice4j.socket.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code SocketChannel} which delegates (its method calls) to
|
||||
* another {@code SocketChannel}. In other words, the former wraps the latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
class DelegatingSocketChannel<T extends SocketChannel>
|
||||
extends BaseDelegatingSocketChannel<T>
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingSocketChannel} instance which is to
|
||||
* delegate (its method calls) to a specific {@code SocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code SocketChannel} the new instance is to delegate
|
||||
* (its method calls) to
|
||||
*/
|
||||
public DelegatingSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate);
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
package org.ice4j.socket.jdk8;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@ -13,6 +13,7 @@ import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.ice4j.ice.harvest.*;
|
||||
import org.ice4j.socket.*;
|
||||
|
||||
/**
|
||||
* Implements a {@link ServerSocketChannel} which is capable of sharing its
|
||||
@ -41,16 +42,8 @@ public class MuxServerSocketChannel
|
||||
* {@code SocketChannel} to provide incoming/readable data before it is
|
||||
* considered abandoned by the client.
|
||||
*/
|
||||
public static final int SOCKET_CHANNEL_READ_TIMEOUT = 15 * 1000;
|
||||
|
||||
/**
|
||||
* The name of the <tt>boolean</tt> property of the <tt>socket</tt> property
|
||||
* of the <tt>ServerSocketChannel</tt> returned by {@link #openAndBind(Map,
|
||||
* SocketAddress, int, DatagramPacketFilter)} which specifies the value of
|
||||
* the <tt>SO_REUSEADDR</tt> socket option.
|
||||
*/
|
||||
public static final String SOCKET_REUSE_ADDRESS_PROPERTY_NAME
|
||||
= "socket.reuseAddress";
|
||||
private static final int SOCKET_CHANNEL_READ_TIMEOUT
|
||||
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
|
||||
|
||||
/**
|
||||
* Asserts that <tt>t</tt> is not <tt>null</tt> by throwing a
|
||||
@ -80,13 +73,7 @@ public class MuxServerSocketChannel
|
||||
*/
|
||||
public static void closeNoExceptions(Channel channel)
|
||||
{
|
||||
try
|
||||
{
|
||||
channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
}
|
||||
MuxServerSocketChannelFactory.closeNoExceptions(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -594,35 +581,12 @@ public class MuxServerSocketChannel
|
||||
muxingChannel = findMuxingServerSocketChannel(endpoint);
|
||||
if (muxingChannel == null)
|
||||
{
|
||||
ServerSocketChannel channel = ServerSocketChannel.open();
|
||||
// Apply the specified properties.
|
||||
ServerSocket socket = channel.socket();
|
||||
|
||||
if (properties != null && !properties.isEmpty())
|
||||
{
|
||||
for (Map.Entry<String,Object> property
|
||||
: properties.entrySet())
|
||||
{
|
||||
String name = property.getKey();
|
||||
|
||||
if (SOCKET_REUSE_ADDRESS_PROPERTY_NAME.equals(name))
|
||||
{
|
||||
Object value = property.getValue();
|
||||
boolean on;
|
||||
|
||||
if (value == null)
|
||||
on = false;
|
||||
else if (value instanceof Boolean)
|
||||
on = ((Boolean) value).booleanValue();
|
||||
else
|
||||
on = Boolean.valueOf(value.toString());
|
||||
|
||||
socket.setReuseAddress(on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket.bind(endpoint, backlog);
|
||||
ServerSocketChannel channel
|
||||
= MuxServerSocketChannelFactory
|
||||
.openAndBindServerSocketChannel(
|
||||
properties,
|
||||
endpoint,
|
||||
backlog);
|
||||
|
||||
muxingChannel = new MuxingServerSocketChannel(channel);
|
||||
addMuxingServerSocketChannel(muxingChannel);
|
||||
@ -4,7 +4,7 @@
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
package org.ice4j.socket.jdk8;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@ -17,7 +17,7 @@ import java.nio.channels.*;
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class PreReadSocketChannel
|
||||
class PreReadSocketChannel
|
||||
extends DelegatingSocketChannel<SocketChannel>
|
||||
{
|
||||
/**
|
||||
Loading…
x
Reference in New Issue
Block a user