diff --git a/src/com/barchart/udt/AppClient.java b/src/com/barchart/udt/AppClient.java deleted file mode 100644 index e5383880..00000000 --- a/src/com/barchart/udt/AppClient.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.InetSocketAddress; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; - -import com.barchart.udt.net.NetSocketUDT; - -public class AppClient { - - static boolean finished = false; - - /** - * @param args - * @throws IOException - */ - public static void main(final String[] args) { - - String host; - int port = 9000; - final int size = 10000; - final byte[] data = new byte[size]; - Future monResult = null; - - if (args.length != 2) { - System.out.println("usage: appclient server_host server_port"); - return; - } - - host = args[0]; - port = Integer.parseInt(args[1]); - - try { - - final NetSocketUDT socket = new NetSocketUDT(); - - if (System.getProperty("os.name").contains("win")) - socket.socketUDT().setOption(OptionUDT.UDT_MSS, 1052); - - socket.connect(new InetSocketAddress(host, port)); - final OutputStream os = socket.getOutputStream(); - - // Start the monitor background task - monResult = Executors.newSingleThreadExecutor().submit( - new Callable() { - @Override - public Boolean call() { - return monitor(socket.socketUDT()); - } - }); - - for (int i = 0; i < 1000000; i++) { - os.write(data); - } - - finished = true; - if (monResult != null) - monResult.get(); - - } catch (final IOException ioe) { - ioe.printStackTrace(); - } catch (final InterruptedException e) { - e.printStackTrace(); - } catch (final ExecutionException e) { - e.printStackTrace(); - } - - } - - public static boolean monitor(final SocketUDT socket) { - - System.out - .println("SendRate(Mb/s)\tRTT(ms)\tCWnd\tPktSndPeriod(us)\tRecvACK\tRecvNAK"); - - try { - - while (!finished) { - - Thread.sleep(1000); - - socket.updateMonitor(false); - - System.out.printf("%.2f\t\t" + "%.2f\t" + "%d\t" + "%.2f\t\t\t" - + "%d\t" + "%d\n", socket.monitor().mbpsSendRate, - socket.monitor().msRTT, - socket.monitor().pktCongestionWindow, - socket.monitor().usPktSndPeriod, - socket.monitor().pktRecvACK, - socket.monitor().pktRecvNAK); - } - - return true; - - } catch (final Exception e) { - e.printStackTrace(); - return false; - } - } -} diff --git a/src/com/barchart/udt/AppServer.java b/src/com/barchart/udt/AppServer.java deleted file mode 100644 index f8155304..00000000 --- a/src/com/barchart/udt/AppServer.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import com.barchart.udt.net.NetServerSocketUDT; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; - -public class AppServer { - - /** - * @param args - * @throws IOException - */ - - static Logger log = LoggerFactory.getLogger(AppServer.class); - - public static void main(final String[] args) throws IOException { - - int port = 9000; - - if (args.length > 1) { - System.out.println("usage: appserver [server_port]"); - return; - } - - if (args.length == 1) { - port = Integer.parseInt(args[0]); - } - - final NetServerSocketUDT acceptorSocket = new NetServerSocketUDT(); - acceptorSocket.bind(new InetSocketAddress("0.0.0.0", port), 256); - - System.out.println("Server is ready at port: " + port); - - while (true) { - final Socket clientSocket = acceptorSocket.accept(); - - // Start the read ahead background task - Executors.newSingleThreadExecutor().submit(new Callable() { - @Override - public Boolean call() { - return clientTask(clientSocket); - } - }); - } - } - - public static boolean clientTask(final Socket clientSocket) { - - final byte[] data = new byte[10000]; - - try { - - final InputStream is = clientSocket.getInputStream(); - - while (true) { - - int remain = data.length; - - while (remain > 0) { - final int ret = is.read(data, data.length - remain, remain); - remain -= ret; - } - } - - } catch (final IOException ioe) { - ioe.printStackTrace(); - return false; - } - } -} diff --git a/src/com/barchart/udt/CCC.java b/src/com/barchart/udt/CCC.java deleted file mode 100644 index d178421c..00000000 --- a/src/com/barchart/udt/CCC.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * A wrapper around the base UDT congestion control class - * - * @see reference - * @see tutorial - * @see FactoryUDT - * @see FactoryInterfaceUDT - * - * @author CCob - */ -public class CCC { - - /** Force SocketUDT to load JNI if it hasn't already */ - private static boolean initOk = SocketUDT.INIT_OK; - - /** Used internally by the JNI layer, points to JNICCC class */ - private long nativeHandle; - - private int msINT; - private int pktINT; - private int usRTO; - private final Logger log = LoggerFactory.getLogger(CCC.class); - - private native void initNative(); - - protected native void setACKTimer(final int msINT); - - protected native void setACKInterval(final int pktINT); - - protected native void setRTO(final int usRTO); - - protected native void setPacketSndPeriod(final double sndPeriod); - - protected native void setCWndSize(final double cWndSize); - - protected native MonitorUDT getPerfInfo(); - - public CCC() { - initNative(); - } - - public void init() { - log.info("CCC::init"); - } - - public void close() { - log.info("CCC::close"); - } - - public void onACK(final int ack) { - } - - public void onLoss(final int[] lossList) { - } - - public void onTimeout() { - } - - // TODO: implement Java wrapper around CPacket - // public void onPktSent(const CPacket* pkt) {} - // public void onPktReceived(const CPacket* pkt) {} - // public void processCustomMsg(const CPacket& pkt) {}/ - // void sendCustomMsg(CPacket& pkt) const; -} diff --git a/src/com/barchart/udt/EpollUDT.java b/src/com/barchart/udt/EpollUDT.java deleted file mode 100644 index 8ea1de9a..00000000 --- a/src/com/barchart/udt/EpollUDT.java +++ /dev/null @@ -1,245 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * UDT Epoll Manager - * - * @see Epoll - * @see UDT Epoll - */ -public class EpollUDT { - - /** - * poll interest option mask - *

- * see udt.h enum - EPOLLOpt - * - *

-	 *    UDT_EPOLL_IN = 0x1,
-	 *    UDT_EPOLL_OUT = 0x4,
-	 *    UDT_EPOLL_ERR = 0x8
-	 * 
- * - * this is subset adapted to jdk select pattern - */ - public static enum Opt { - - /** - * not interested - */ - NONE(0x0), // - - /** - * UDT_EPOLL_IN : interested in read - */ - READ(0x1), // - - /** - * UDT_EPOLL_OUT: interested in write - */ - WRITE(0x4), // - - /** - * UDT_EPOLL_ERR: interested in exceptions - */ - ERROR(0x8), // - - BOTH(WRITE.code | READ.code), // - - ERROR_READ(ERROR.code | READ.code), // - - ERROR_WRITE(ERROR.code | WRITE.code), // - - ALL(ERROR.code | WRITE.code | READ.code), // - - UNKNOWN(-1); - - ; - - private static final Opt[] ENUM_VALS = Opt.values(); - - public static Opt from(final int code) { - for (final Opt known : ENUM_VALS) { - if (known.code == code) { - return known; - } - } - return UNKNOWN; - } - - /** - * poll event mask; - *

- * used for both requesting interest and reporting readiness - */ - public final int code; - - Opt(final int code) { - this.code = code; - } - - public boolean hasError() { - return (code & ERROR.code) != 0; - } - - public boolean hasRead() { - return (code & READ.code) != 0; - } - - public boolean hasWrite() { - return (code & WRITE.code) != 0; - } - - /** - * Non-empty mask of 3 parts. - */ - public boolean isValidInterestRequest() { - switch (this) { - case NONE: - case READ: - case WRITE: - case ERROR: - case BOTH: - case ERROR_WRITE: - case ERROR_READ: - case ALL: - return true; - default: - return false; - } - } - - } - - protected static final Logger log = LoggerFactory.getLogger(EpollUDT.class); - - protected final int id; - - protected volatile boolean isActive; - - /** - * place holder socket to work around logic in epoll.h CEPoll::wait() which - * expects at least one socket being monitored with non empty interest - */ - private final SocketUDT socketUDT; - - /** - * allocate poll - */ - public EpollUDT() throws ExceptionUDT { - - id = SocketUDT.epollCreate0(); - isActive = true; - - socketUDT = new SocketUDT(TypeUDT.DATAGRAM); - SocketUDT.epollAdd0(id, socketUDT.id(), Opt.BOTH.code); - - log.debug("ep {} create", id()); - - } - - /** - * deallocate poll; called on {@link #finalize()} - */ - public void destroy() throws ExceptionUDT { - - SocketUDT.epollRemove0(id(), socketUDT.id()); - socketUDT.close(); - - isActive = false; - SocketUDT.epollRelease0(id()); - - log.debug("ep {} delete", id()); - - } - - /** - * poll descriptor id - */ - public int id() { - return id; - } - - /** - * poll becomes active after instance creation and inactive after - * {@link #destroy()} - */ - public boolean isActive() { - return isActive; - } - - /** - * deallocate poll - *

- * NOTE: catch all exceptions; else prevents GC - *

- * NOTE: do not leak "this" references; else prevents GC - */ - @Override - protected void finalize() { - try { - destroy(); - super.finalize(); - } catch (final Throwable e) { - log.error("failed to destroy id=" + id(), e); - } - } - - /** - * register socket into event processing poll - */ - public void add(final SocketUDT socket, final Opt option) - throws ExceptionUDT { - - log.debug("ep {} add {} {}", id(), socket, option); - - // assert option.isValidInterestRequest(); - - SocketUDT.epollAdd0(id(), socket.id(), option.code); - - } - - /** - * unregister socket from event processing poll - */ - public void remove(final SocketUDT socket) throws ExceptionUDT { - - log.debug("ep {} rem {}", id(), socket); - - SocketUDT.epollRemove0(id(), socket.id()); - - } - - /** - * update existing poll/socket registration with changed interest - */ - public void update(final SocketUDT socket, final Opt option) - throws ExceptionUDT { - - log.debug("ep {} mod {} {}", id(), socket, option); - - assert option.isValidInterestRequest(); - - SocketUDT.epollUpdate0(id(), socket.id(), option.code); - - } - - /** report current poll/socket readiness */ - public Opt verify(final SocketUDT socket) throws ExceptionUDT { - - final int code = SocketUDT.epollVerify0(id(), socket.id()); - - return Opt.from(code); - - } - -} diff --git a/src/com/barchart/udt/ErrorUDT.java b/src/com/barchart/udt/ErrorUDT.java deleted file mode 100644 index 7edc983b..00000000 --- a/src/com/barchart/udt/ErrorUDT.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -/** - * keep code values in sync with - * - * @see UDT Error Codes - * List - */ -public enum ErrorUDT { - - SUCCESS(0, "success operation"), // - - ECONNSETUP(1000, "connection setup failure"), // - - NOSERVER(1001, "server does not exist"), // - - ECONNREJ(1002, "connection request was rejected by server"), // - - ESOCKFAIL(1003, "could not create/configure UDP socket"), // - - ESECFAIL(1004, "connection request was aborted due to security reasons"), // - - ECONNFAIL(2000, "connection failure"), // - - ECONNLOST(2001, "connection was broken"), // - - ENOCONN(2002, "connection does not exist"), // - - ERESOURCE(3000, "system resource failure"), // - - ETHREAD(3001, "could not create new thread"), // - - ENOBUF(3002, "no memory space"), // - - EFILE(4000, "file access error"), // - - EINVRDOFF(4001, "invalid read offset"), // - - ERDPERM(4002, "no read permission"), // - - EINVWROFF(4003, "invalid write offset"), // - - EWRPERM(4004, "no write permission"), // - - EINVOP(5000, "operation not supported"), // - - EBOUNDSOCK(5001, "cannot execute the operation on a bound socket"), // - - ECONNSOCK(5002, "cannot execute the operation on a connected socket"), // - - EINVPARAM(5003, "bad parameters"), // - - EINVSOCK(5004, "invalid UDT socket"), // - - EUNBOUNDSOCK(5005, "cannot listen on unbound socket"), // - - ENOLISTEN(5006, "(accept) socket is not in listening state"), // - - ERDVNOSERV(5007, - "rendezvous connection process does not allow listen and accept call"), // - - ERDVUNBOUND( - 5008, - "rendezvous connection setup is enabled but bind has not been called before connect"), // - - ESTREAMILL(5009, "operation not supported in SOCK_STREAM mode"), // - - EDGRAMILL(5010, "operation not supported in SOCK_DGRAM mode"), // - - EDUPLISTEN(5011, "another socket is already listening on the same UDP port"), // - - ELARGEMSG(5012, "message is too large to be hold in the sending buffer"), // - - EINVPOLLID(5013, "epoll ID is invalid"), // - - EASYNCFAIL(6000, "non-blocking call failure"), // - - EASYNCSND(6001, "no buffer available for sending"), // - - EASYNCRCV(6002, "no data available for read"), // - - ETIMEOUT(6003, "timeout before operation completes"), // - - EPEERERR(7000, "error has happened at the peer side"), // - - // non UDT values: - - WRAPPER_UNKNOWN(-1, "unknown error code"), // - WRAPPER_UNIMPLEMENTED(-2, "this feature is not yet implemented"), // - WRAPPER_MESSAGE(-3, "wrapper generated error"), // - USER_DEFINED_MESSAGE(-4, "user defined message"), // - - ; - - private final int code; - - public int getCode() { - return code; - } - - private final String description; - - public String getDescription() { - return description; - } - - private ErrorUDT(final int code, final String description) { - this.code = code; - this.description = description; - } - - static final ErrorUDT[] ENUM_VALS = values(); - - public static ErrorUDT errorFrom(final int code) { - for (final ErrorUDT known : ENUM_VALS) { - if (known.code == code) { - return known; - } - } - return WRAPPER_UNKNOWN; - } - - // - - public static String descriptionFrom(final int socketID, - final int errorCode, final String errorComment) { - final ErrorUDT error = ErrorUDT.errorFrom(errorCode); - return String.format("UDT Error : %d : %s : %s [id: 0x%08x]", // - errorCode, error.description, errorComment, socketID); - } - -} diff --git a/src/com/barchart/udt/ExceptionUDT.java b/src/com/barchart/udt/ExceptionUDT.java deleted file mode 100644 index dc3f082d..00000000 --- a/src/com/barchart/udt/ExceptionUDT.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import java.net.SocketException; - -import com.barchart.udt.anno.Native; - -/** - * The Class ExceptionUDT. Wraps all native UDT exceptions and more. - */ -@SuppressWarnings("serial") -public class ExceptionUDT extends SocketException { - - /** - * The error udt. Keeps error description for this exception. Use this enum - * in switch/case to fine tune exception processing. - */ - @Native - private final ErrorUDT errorUDT; - - public ErrorUDT getError() { - return errorUDT; - } - - /** - * The socket id. Keeps socketID of the socket that produced this exception. - * Can possibly contain '0' when particular method can not determine - * {@link #socketID} that produced the exception. - */ - @Native - private final int socketID; - - public int getSocketID() { - return socketID; - } - - /** - * Instantiates a new exception udt for native UDT::Exception. This - * exception is generated in the underlying UDT method. - * - * @param socketID - * the socket id - * @param errorCode - * the error code - * @param comment - * the comment - */ - @Native - protected ExceptionUDT(final int socketID, final int errorCode, - final String comment) { - super(ErrorUDT.descriptionFrom(socketID, errorCode, comment)); - errorUDT = ErrorUDT.errorFrom(errorCode); - this.socketID = socketID; - } - - /** - * Instantiates a new exception udt for synthetic JNI wrapper exception. - * This exception is generated in the JNI glue code itself. - * - * @param socketID - * the socket id - * @param error - * the error - * @param comment - * the comment - */ - @Native - protected ExceptionUDT(final int socketID, final ErrorUDT error, - final String comment) { - super(ErrorUDT.descriptionFrom(socketID, error.getCode(), comment)); - errorUDT = error; - this.socketID = socketID; - } - -} diff --git a/src/com/barchart/udt/FactoryInterfaceUDT.java b/src/com/barchart/udt/FactoryInterfaceUDT.java deleted file mode 100644 index 3c865d14..00000000 --- a/src/com/barchart/udt/FactoryInterfaceUDT.java +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -/** - * @author CCob - */ -public interface FactoryInterfaceUDT { - - CCC create(); - - FactoryInterfaceUDT cloneFactory(); - -} diff --git a/src/com/barchart/udt/FactoryUDT.java b/src/com/barchart/udt/FactoryUDT.java deleted file mode 100644 index 05e0d33b..00000000 --- a/src/com/barchart/udt/FactoryUDT.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Wrapper around the UDT CCCFactory class - * - * @see reference - * @see tutorial - * @see CCC - * - * @author CCob - */ -public class FactoryUDT implements FactoryInterfaceUDT { - - C classType; - final Class clazz; - - Logger log = LoggerFactory.getLogger(FactoryUDT.class); - - boolean doInit = false; - boolean doClose = false; - boolean doOnACK = false; - boolean doOnLoss = false; - boolean doOnTimeout = false; - - public FactoryUDT(final Class clazz) { - - this.clazz = clazz; - - if (!CCC.class.isAssignableFrom(clazz)) { - throw new IllegalArgumentException( - "Generic argument 'C' must be 'CCC' class or extension"); - } - - try { - - if (clazz.getMethod("init").getDeclaringClass() != CCC.class) - doInit = true; - - if (clazz.getMethod("close").getDeclaringClass() != CCC.class) - doClose = true; - - if (clazz.getMethod("onACK", int.class).getDeclaringClass() != CCC.class) - doOnACK = true; - - if (clazz.getMethod("onLoss", int[].class).getDeclaringClass() != CCC.class) - doOnLoss = true; - - if (clazz.getMethod("onTimeout").getDeclaringClass() != CCC.class) - doOnTimeout = true; - - } catch (final SecurityException e) { - log.error("Error setting up class factory", e); - } catch (final NoSuchMethodException e) { - log.error("Expected CCC method doesn't exsit", e); - } - } - - @Override - public CCC create() { - - try { - final Object cccObj = clazz.newInstance(); - return (CCC) cccObj; - } catch (final InstantiationException e) { - log.error("Failed to instansiate CCC class", e); - } catch (final IllegalAccessException e) { - log.error("Failed to instansiate CCC class", e); - } - - return null; - } - - @Override - public FactoryInterfaceUDT cloneFactory() { - return new FactoryUDT(clazz); - } - -} diff --git a/src/com/barchart/udt/LingerUDT.java b/src/com/barchart/udt/LingerUDT.java deleted file mode 100644 index 63722a5e..00000000 --- a/src/com/barchart/udt/LingerUDT.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -public class LingerUDT extends Number implements Comparable { - - public static final LingerUDT LINGER_ZERO = new LingerUDT(0); - - // measured in seconds - final int timeout; - - /** - * Default constructor. NOTE: linger value is "u_short" on windows and "int" - * on linux:
- * Windows: - * linger Structure on Windows
- * Linux: GCC Socket-Level Options
- * Therefore select smallest range: 0 <= linger <= 65535
- * - * @param lingerSeconds - * the seconds to linger; "0" means "do not linger" - * - * @throws IllegalArgumentException - * when lingerSeconds is out of range - */ - public LingerUDT(int lingerSeconds) throws IllegalArgumentException { - if (65535 < lingerSeconds) { - throw new IllegalArgumentException( - "lingerSeconds is out of range: 0 <= linger <= 65535"); - } - this.timeout = lingerSeconds > 0 ? lingerSeconds : 0; - } - - private static final long serialVersionUID = 3414455799823407217L; - - @Override - public double doubleValue() { - return timeout; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Number#floatValue() - */ - @Override - public float floatValue() { - return timeout; - } - - @Override - public int intValue() { - return timeout; - } - - @Override - public long longValue() { - return timeout; - } - - boolean isLingerOn() { - return timeout > 0; - } - - int timeout() { - return timeout; - } - - @Override - public boolean equals(Object otherLinger) { - if (otherLinger instanceof LingerUDT) { - LingerUDT other = (LingerUDT) otherLinger; - return other.timeout == this.timeout; - } - return false; - } - - @Override - public int hashCode() { - return timeout; - } - - @Override - public int compareTo(LingerUDT other) { - return other.timeout - this.timeout; - } - - @Override - public String toString() { - return String.valueOf(timeout); - } - -} diff --git a/src/com/barchart/udt/MonitorUDT.java b/src/com/barchart/udt/MonitorUDT.java deleted file mode 100644 index 70563796..00000000 --- a/src/com/barchart/udt/MonitorUDT.java +++ /dev/null @@ -1,379 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import java.lang.reflect.Field; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * note: do not change field names; used by JNI - */ -public class MonitorUDT { - - private static final Logger log = LoggerFactory.getLogger(MonitorUDT.class); - - protected final SocketUDT socketUDT; - - protected MonitorUDT(final SocketUDT socketUDT) { - this.socketUDT = socketUDT; - } - - // UDT API - - // ### global measurements - - /** time since the UDT entity is started, in milliseconds. */ - protected volatile long msTimeStamp; - - public long millisSinceStart() { - return msTimeStamp; - } - - /** - * total number of sent data packets, including retransmissions - */ - protected volatile long pktSentTotal; - - public long globalSentTotal() { - return pktSentTotal; - } - - /** - * total number of received packets - */ - protected volatile long pktRecvTotal; - - public long globalReceivedTotal() { - return pktRecvTotal; - } - - /** - * total number of lost packets (sender side) - */ - protected volatile int pktSndLossTotal; - - public int globalSenderLost() { - return pktSndLossTotal; - } - - /** - * total number of lost packets (receiver side) - */ - protected volatile int pktRcvLossTotal; - - public int globalReceiverLost() { - return pktRcvLossTotal; - } - - /** - * total number of retransmitted packets - */ - protected volatile int pktRetransTotal; - - public int globalRetransmittedTotal() { - return pktRetransTotal; - } - - /** - * total number of sent ACK packets - */ - protected volatile int pktSentACKTotal; - - public int globalSentAckTotal() { - return pktSentACKTotal; - } - - /** - * total number of received ACK packets - */ - protected volatile int pktRecvACKTotal; - - public int globalReceivedAckTotal() { - return pktRecvACKTotal; - } - - /** - * total number of sent NAK packets - */ - protected volatile int pktSentNAKTotal; - - public int globalSentNakTotal() { - return pktSentNAKTotal; - } - - /** - * total number of received NAK packets - */ - protected volatile int pktRecvNAKTotal; - - public int globalReceivedNakTotal() { - return pktRecvNAKTotal; - } - - /** - * total time duration when UDT is sending data (idle time exclusive) - */ - protected volatile long usSndDurationTotal; - - public long globalMicrosSendDurationTotal() { - return usSndDurationTotal; - } - - // ### local measurements - - /** - * number of sent data packets, including retransmissions - */ - protected volatile long pktSent; - - public long localPacketsSent() { - return pktSent; - } - - /** - * number of received packets - */ - protected volatile long pktRecv; - - public long localPacketsReceived() { - return pktRecv; - } - - /** - * number of lost packets (sender side) - */ - protected volatile int pktSndLoss; - - public int localSenderLost() { - return pktSndLoss; - } - - /** - * number of lost packets (receiverer side) - */ - protected volatile int pktRcvLoss; - - public int localReceiverLost() { - return pktRcvLoss; - } - - /** - * number of retransmitted packets - */ - protected volatile int pktRetrans; - - public int localRetransmitted() { - return pktRetrans; - } - - /** - * number of sent ACK packets - */ - protected volatile int pktSentACK; - - public int localSentAck() { - return pktSentACK; - } - - /** - * number of received ACK packets - */ - protected volatile int pktRecvACK; - - public int localReceivedAck() { - return pktRecvACK; - } - - /** - * number of sent NAK packets - */ - protected volatile int pktSentNAK; - - public int localSentNak() { - return pktSentNAK; - } - - /** - * number of received NAK packets - */ - protected volatile int pktRecvNAK; - - public int localReceivedNak() { - return pktRecvNAK; - } - - /** - * sending rate in Mb/s - */ - protected volatile double mbpsSendRate; - - public double mbpsSendRate() { - return mbpsSendRate; - } - - /** - * receiving rate in Mb/s - */ - protected volatile double mbpsRecvRate; - - public double mbpsReceiveRate() { - return mbpsRecvRate; - } - - /** - * busy sending time (i.e., idle time exclusive) - */ - protected volatile long usSndDuration; - - public long microsSendTime() { - return usSndDuration; - } - - // ### instant measurements - - /** - * packet sending period, in microseconds - */ - protected volatile double usPktSndPeriod; - - public double currentSendPeriod() { - return usPktSndPeriod; - } - - /** - * flow window size, in number of packets - */ - protected volatile int pktFlowWindow; - - public int currentFlowWindow() { - return pktFlowWindow; - } - - /** - * congestion window size, in number of packets - */ - protected volatile int pktCongestionWindow; - - public int currentCongestionWindow() { - return pktCongestionWindow; - } - - /** - * number of packets on flight - */ - protected volatile int pktFlightSize; - - public int currentFlightSize() { - return pktFlightSize; - } - - /** - * RTT, in milliseconds - */ - protected volatile double msRTT; - - public double currentMillisRTT() { - return msRTT; - } - - /** - * estimated bandwidth, in Mb/s - */ - protected volatile double mbpsBandwidth; - - public double currentMbpsBandwidth() { - return mbpsBandwidth; - } - - /** - * available UDT sender buffer size - */ - protected volatile int byteAvailSndBuf; - - public int currentAvailableInSender() { - return byteAvailSndBuf; - } - - /** - * available UDT receiver buffer size - */ - protected volatile int byteAvailRcvBuf; - - public int currentAvailableInReceiver() { - return byteAvailRcvBuf; - } - - /** - * current monitor status snapshot for all parameters - */ - public void appendSnapshot(final StringBuilder text) { - - text.append("\n\t"); - text.append(String.format("[id: 0x%08x]", socketUDT.id())); - - final Field fieldArray[] = MonitorUDT.class.getDeclaredFields(); - - for (final Field field : fieldArray) { - - if (!isNumeric(field)) { - continue; - } - - try { - - field.setAccessible(true); - - final String fieldName = field.getName(); - final String fieldValue = field.get(this).toString(); - - text.append("\n\t"); - text.append(fieldName); - text.append(" = "); - text.append(fieldValue); - - } catch (final Exception e) { - log.error("unexpected", e); - } - - } - - final double localSendLoss = 100.0 * pktSndLoss / pktSent; - - text.append("\n\t% localSendLoss = "); - text.append(localSendLoss); - - final double localReceiveLoss = 100.0 * pktRcvLoss / pktRecv; - - text.append("\n\t% localReceiveLoss = "); - text.append(localReceiveLoss); - - } - - protected boolean isNumeric(final Field field) { - - final Class fieledType = field.getType(); - - return fieledType == int.class || fieledType == long.class - || fieledType == double.class; - - } - - @Override - public String toString() { - - final StringBuilder text = new StringBuilder(1024); - - appendSnapshot(text); - - return text.toString(); - - } - -} diff --git a/src/com/barchart/udt/OptionUDT.java b/src/com/barchart/udt/OptionUDT.java deleted file mode 100644 index 79200beb..00000000 --- a/src/com/barchart/udt/OptionUDT.java +++ /dev/null @@ -1,323 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import static com.barchart.udt.OptionUDT.Format.BINARY; -import static com.barchart.udt.OptionUDT.Format.BOOLEAN; -import static com.barchart.udt.OptionUDT.Format.DECIMAL; -import static com.barchart.udt.OptionUDT.Format.DEFAULT; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.util.HelpUDT; - -/** - * The Enum OptionUDT. - *

- * provide 2 names: 1) UDT original and 2) human-readble - *

- * keep code values in sync with udt.h - UDT::UDTOpt; enum starts with index 0 - * - * @see udt options - *

- * UDT_MSS, // the Maximum Transfer Unit
- * UDT_SNDSYN, // if sending is blocking
- * UDT_RCVSYN, // if receiving is blocking
- * UDT_CC, // custom congestion control algorithm
- * UDT_FC, // Flight flag size (window size)
- * UDT_SNDBUF, // maximum buffer in sending queue
- * UDT_RCVBUF, // UDT receiving buffer size
- * UDT_LINGER, // waiting for unsent data when closing
- * UDP_SNDBUF, // UDP sending buffer size
- * UDP_RCVBUF, // UDP receiving buffer size
- * UDT_MAXMSG, // maximum datagram message size
- * UDT_MSGTTL, // time-to-live of a datagram message
- * UDT_RENDEZVOUS, // rendezvous connection mode
- * UDT_SNDTIMEO, // send() timeout
- * UDT_RCVTIMEO, // recv() timeout
- * UDT_REUSEADDR, // reuse an existing port or create a new one
- * UDT_MAXBW, // maximum bandwidth (bytes per second) that the connection can  use
- * UDT_STATE, // current socket state, see UDTSTATUS, read only
- * UDT_EVENT, // current avalable events associated with the socket
- * UDT_SNDDATA, // size of data in the sending buffer
- * UDT_RCVDATA // size of data available for recv
- * 
- */ -public class OptionUDT { - - static { - log = LoggerFactory.getLogger(OptionUDT.class); - values = new CopyOnWriteArrayList>(); - } - - /** the Maximum Transfer Unit. */ - public static final OptionUDT UDT_MSS = NEW(0, Integer.class, DECIMAL); - - /** the Maximum Transfer Unit., bytes */ - public static final OptionUDT Maximum_Transfer_Unit = NEW(0, Integer.class, DECIMAL); - - - /** if sending is blocking. */ - public static final OptionUDT UDT_SNDSYN = NEW(1, Boolean.class, BOOLEAN); - - /** if sending is blocking., true/false */ - public static final OptionUDT Is_Send_Synchronous = NEW(1, Boolean.class, BOOLEAN); - - - /** if receiving is blocking. */ - public static final OptionUDT UDT_RCVSYN = NEW(2, Boolean.class, BOOLEAN); - - /** if receiving is blocking, true/false */ - public static final OptionUDT Is_Receive_Synchronous = NEW(2, Boolean.class, BOOLEAN); - - - /** custom congestion control algorithm */ - @SuppressWarnings("rawtypes") - - public static final OptionUDT UDT_CC = NEW(3, FactoryUDT.class, DEFAULT); - /** custom congestion control algorithm, class factory */ - @SuppressWarnings("rawtypes") - public static final OptionUDT Custom_Congestion_Control = NEW(3, FactoryUDT.class, DEFAULT); - - - /** Flight flag size (window size). */ - public static final OptionUDT UDT_FC = NEW(4, Integer.class, BINARY); - - /** Flight flag size (window size), bytes */ - public static final OptionUDT Flight_Window_Size = NEW(4, Integer.class, BINARY); - - - /** maximum buffer in sending queue. */ - public static final OptionUDT UDT_SNDBUF = NEW(5, Integer.class, DECIMAL); - - /** maximum buffer in sending queue. */ - public static final OptionUDT Protocol_Send_Buffer_Size = NEW(5, Integer.class, DECIMAL); - - - /** UDT receiving buffer size. */ - public static final OptionUDT UDT_RCVBUF = NEW(6, Integer.class, DECIMAL); - - /** UDT receiving buffer size limit, bytes */ - public static final OptionUDT Protocol_Receive_Buffer_Size = NEW(6, Integer.class, DECIMAL); - - - /** waiting for unsent data when closing. */ - public static final OptionUDT UDT_LINGER = NEW(7, LingerUDT.class, DECIMAL); - - /** waiting for unsent data when closing. true/false and timeout, seconds */ - public static final OptionUDT Time_To_Linger_On_Close = NEW(7, LingerUDT.class, DECIMAL); - - - /** UDP sending buffer size. */ - public static final OptionUDT UDP_SNDBUF = NEW(8, Integer.class, DECIMAL); - - /** UDP sending buffer size limit, bytes */ - public static final OptionUDT System_Send_Buffer_Size = NEW(8, Integer.class, DECIMAL); - - - /** UDP receiving buffer size. */ - public static final OptionUDT UDP_RCVBUF = NEW(9, Integer.class, DECIMAL); - - /** UDP receiving buffer size limit, bytes */ - public static final OptionUDT System_Receive_Buffer_Size = NEW(9, Integer.class, DECIMAL); - - - /* maximum datagram message size */ - // UDT_MAXMSG(10, Integer.class, DECIMAL); no support in udt core - - /* time-to-live of a datagram message */ - // UDT_MSGTTL(11, Integer.class, DECIMAL); no support in udt core - - /** rendezvous connection mode. */ - public static final OptionUDT UDT_RENDEZVOUS = NEW(12, Boolean.class, BOOLEAN); - - /** rendezvous connection mode, enabled/disabled */ - public static final OptionUDT Is_Randezvous_Connect_Enabled = NEW(12, Boolean.class, BOOLEAN); - - - /** send() timeout. */ - public static final OptionUDT UDT_SNDTIMEO = NEW(13, Integer.class, DECIMAL); - - /** send() timeout. milliseconds */ - public static final OptionUDT Send_Timeout = NEW(13, Integer.class, DECIMAL); - - - /** recv() timeout. */ - public static final OptionUDT UDT_RCVTIMEO = NEW(14, Integer.class, DECIMAL); - - /** recv() timeout. milliseconds */ - public static final OptionUDT Receive_Timeout = NEW(14, Integer.class, DECIMAL); - - - /** reuse an existing port or create a one. */ - public static final OptionUDT UDT_REUSEADDR = NEW(15, Boolean.class, BOOLEAN); - - /** reuse an existing port or create a one. true/false */ - public static final OptionUDT Is_Address_Reuse_Enabled = NEW(15, Boolean.class, BOOLEAN); - - - /** maximum bandwidth (bytes per second) that the connection can use. */ - public static final OptionUDT UDT_MAXBW = NEW(16, Long.class, DECIMAL); - - /** maximum bandwidth (bytes per second) that the connection can use. */ - public static final OptionUDT Maximum_Bandwidth = NEW(16, Long.class, DECIMAL); - - - /** current socket state, see UDTSTATUS, read only */ - public static final OptionUDT UDT_STATE = NEW(17, Integer.class, DECIMAL); - - /** current socket status code, see {@link StatusUDT#getCode()}, read only */ - public static final OptionUDT Status_Code = NEW(17, Integer.class, DECIMAL); - - - /** current available events associated with the socket */ - public static final OptionUDT UDT_EVENT = NEW(18, Integer.class, DECIMAL); - - /** current available epoll events, see {@link EpollUDT.Opt#code} */ - public static final OptionUDT Epoll_Event_Mask = NEW(18, Integer.class, DECIMAL); - - - /** size of data in the sending buffer */ - public static final OptionUDT UDT_SNDDATA = NEW(19, Integer.class, DECIMAL); - - /** current consumed sending buffer utilization, read only, bytes */ - public static final OptionUDT Send_Buffer_Consumed = NEW(19, Integer.class, DECIMAL); - - - /** size of data available for recv */ - public static final OptionUDT UDT_RCVDATA = NEW(20, Integer.class, DECIMAL); - - /** current available receiving buffer capacity, read only, bytes */ - public static final OptionUDT Receive_Buffer_Available = NEW(20, Integer.class, DECIMAL); - - - protected OptionUDT(final int code, final Class klaz, final Format format) { - this.code = code; - this.type = klaz; - this.format = format; - - values.add(this); - } - - protected static OptionUDT NEW(final int code, final Class klaz, final Format format) { - return new OptionUDT(code, klaz, format); - } - - public static void appendSnapshot(final SocketUDT socketUDT, final StringBuilder text) { - text.append("\n\t"); - text.append(String.format("[id: 0x%08x]", socketUDT.id())); - - for (final OptionUDT option : values) { - int optionCode = 0; - String optionName = null; - String optionValue = null; - try { - - optionCode = option.code; - optionName = option.name(); - - optionValue = option.format.convert(// - socketUDT.getOption(option)); - - if (optionName.startsWith("UD")) { - continue; - } - - text.append("\n\t"); - text.append(optionCode); - text.append(") "); - text.append(optionName); - text.append(" = "); - text.append(optionValue); - - } catch (final Exception e) { - log.error("unexpected; " + optionName, e); - } - } - } - - protected static final Logger log; - protected static final List> values; - - private final int code; - private final Class type; - private final Format format; - private String name; - - public int code() { - return code; - } - - public Class type() { - return type; - } - - public Format format() { - return format; - } - - public String name() { - if (name == null) { - name = HelpUDT.constantFieldName(getClass(), this); - } - return name; - } - - /** - * render options in human format - */ - public enum Format { - DECIMAL() { - @Override - public String convert(final Object value) { - if (value instanceof Number) { - final long number = ((Number) value).longValue(); - return String.format("%,d", number); - } - return "invalid format"; - } - }, // - - BINARY() { - @Override - public String convert(final Object value) { - if (value instanceof Number) { - final long number = ((Number) value).longValue(); - return String.format("%,d (%,d K)", number, number / 1024); - } - return "invalid format"; - } - }, // - - BOOLEAN() { - @Override - public String convert(final Object value) { - if (value instanceof Boolean) { - final boolean bool = ((Boolean) value).booleanValue(); - return String.format("%b", bool); - } - return "invalid format"; - } - }, // - - DEFAULT() { - @Override - public String convert(final Object value) { - return "" + value; - } - }, // - - ; - - public abstract String convert(Object value); - } -} diff --git a/src/com/barchart/udt/SocketUDT.java b/src/com/barchart/udt/SocketUDT.java deleted file mode 100644 index 68989ce8..00000000 --- a/src/com/barchart/udt/SocketUDT.java +++ /dev/null @@ -1,1792 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - *

- * All rights reserved. Licensed under the OSI BSD License. - *

- * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import java.io.File; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.URI; -import java.net.URL; -import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.security.CodeSource; -import java.security.ProtectionDomain; -import java.util.List; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.anno.Native; -import com.barchart.udt.nio.KindUDT; -import com.barchart.udt.util.HelpUDT; - -import dorkbox.network.util.NativeLoader; -import dorkbox.util.FileUtil; -import dorkbox.util.OS; -import dorkbox.util.OSType; - -/** - * UDT native socket wrapper - *

- * note: current implementation supports IPv4 only (no IPv6) - */ -public -class SocketUDT { - - /** - * Maximum number of connections queued in listening mode by - * {@link #accept()} - */ - public static final int DEFAULT_ACCEPT_QUEUE_SIZE = 256; - - /** - * Block size used by {@link #sendFile(File, long, long)} - */ - public static final int DEFAULT_FILE_BLOCK_SIZE = 1 * 1024 * 1024; - - /** - * Maximum number sockets that can participate in a - * {@link com.barchart.udt.nio.SelectorUDT#select()} operation; see epoll.h - * to confirm current limit - */ - public static final int DEFAULT_MAX_SELECTOR_SIZE = 1024; - - /** - * Minimum timeout of a {@link com.barchart.udt.nio.SelectorUDT#select()} - * operations. - */ - public static final int DEFAULT_MIN_SELECTOR_TIMEOUT = 10; - - /** - * infinite message time to live; - */ - public static final int INFINITE_TTL = -1; - - /** - * Helper value that can be checked from CCC class and force JNI library - * load - */ - @Native - public static boolean INIT_OK = false; - - protected static final Logger log = LoggerFactory.getLogger(SocketUDT.class); - - /** - * JNI Signature that must match between java code and c++ code on all - * platforms; failure to match will abort native library load, as an - * indication of inconsistent build. - */ - @Native - public static final int SIGNATURE_JNI = 20150706; // VersionUDT.BUILDTIME; - - /** - * infinite timeout: - *

- * blocking send/receive - *

- * epoll wait - */ - public static final int TIMEOUT_INFINITE = -1; - - /** - * zero timeout: - *

- * epoll wait - */ - public static long TIMEOUT_NONE = 0; - - /** - * UDT::select() sizeArray/sizeBuffer index offset for EXCEPTION report - */ - @Native - public static final int UDT_EXCEPT_INDEX = 2; - - /** - * UDT::select() sizeArray/sizeBuffer index offset for READ interest - */ - @Native - public static final int UDT_READ_INDEX = 0; - - /** - * UDT::select() sizeArray/sizeBuffer size count or number of arrays/buffers - */ - @Native - public static final int UDT_SIZE_COUNT = 3; - - /** - * UDT::select() sizeArray/sizeBuffer index offset for WRITE interest - */ - @Native - public static final int UDT_WRITE_INDEX = 1; - - /** - * Native library loader. - * - * @throws RuntimeException - */ - static { - // we are either from a jar or as source - ProtectionDomain pDomain = SocketUDT.class.getProtectionDomain(); - CodeSource cSource = pDomain.getCodeSource(); - - // file:/X:/workspace/XYZ/classes/ when it's in ide/flat - // jar:/X:/workspace/XYZ/jarname.jar when it's jar - URL loc = cSource.getLocation(); - final String path = loc.getPath(); - final boolean isContainer = path.endsWith(".jar") || path.endsWith(".box"); - - final OSType os = OS.get(); - String osName = os.getName(); - boolean loaded = false; - - if (isContainer) { - // have to extract our correct file to temp then load it, ONLY if we are not already loaded! - - String sourceFileName = "udt-core-2.3.2"; - if (OS.isLinux()) { - sourceFileName += ".so"; - } - else if (OS.isWindows()) { - sourceFileName += ".dll"; - } - else { - sourceFileName += ".dylib"; - } - - try { - log.info("Loading release libraries."); - - final String packageName = TypeUDT.class.getPackage() - .getName() - .replaceAll("\\.", "/"); - - sourceFileName = packageName + "/natives/" + osName + "/" + sourceFileName; - - NativeLoader.loadLibrary(sourceFileName, "libnetty-UDT", TypeUDT.class); - - log.info("Release libraries loaded."); - loaded = true; - } catch (Exception e) { - log.error(e.getMessage()); - } - } - else { - try { - log.info("Loading release libraries."); - - final URI uri = TypeUDT.class.getResource("natives/" + osName + "/") - .toURI(); - final String host = uri.getPath(); - - File libPath = new File(host).getAbsoluteFile(); - if (libPath.canRead()) { - List libs = FileUtil.parseDir(libPath, os.getLibraryNames()); - for (File lib : libs) { - // load the libs in that dir (there will be only one) - System.load(lib.getAbsolutePath()); - break; - } - log.info("Release libraries loaded."); - loaded = true; - } - } catch (final Throwable e) { - log.error("Release libraries missing: {}", e.getMessage()); - } - } - - if (!loaded) { - log.error("Failed to load UDT native library"); - throw new RuntimeException("Failed to load UDT native library"); - } - - try { - initClass0(); - } catch (final Throwable e) { - log.error("Failed to INIT native library", e); - throw new RuntimeException("init", e); - } - - if (SIGNATURE_JNI != getSignatureJNI0()) { - log.error("Java/Native SIGNATURE inconsistent"); - throw new RuntimeException("signature"); - } - - INIT_OK = true; - - log.debug("native library load & init OK"); - } - - public static - void init() { - } - - /** - * Cleans up global JNI references and the UDT library. - *

- * The behavior of SocketUDT class after a call to cleanup is undefined, so - * it should only ever be called once you are done and you are ready for the - * class loader to unload the JNI library - * - * @throws ExceptionUDT - */ - public static - void cleanup() throws ExceptionUDT { - stopClass0(); - } - - /** - * @see UDT::epoll_add_usock() - */ - protected static native - void epollAdd0( // - final int epollID, // - final int socketID, // - final int epollOpt // - ) throws ExceptionUDT; - - /** - * @return epoll id - * @see UDT::epoll_create() - */ - protected static native - int epollCreate0() throws ExceptionUDT; - - /** - * @see UDT::epoll_release() - */ - protected static native - void epollRelease0(final int epollID) throws ExceptionUDT; - - /** - * @see UDT::epoll_remove_usock() - */ - protected static native - void epollRemove0( // - final int epollID, final int socketID) throws ExceptionUDT; - - /** - * update epoll mask - */ - protected static native - void epollUpdate0(int epollID, int socketID, int epollMask) throws ExceptionUDT; - - /** - * query epoll mask - */ - protected static native - int epollVerify0(int epollID, int socketID) throws ExceptionUDT; - - /** - * @see UDT::epoll_wait() - */ - protected static native - int epollWait0( // - final int epollID, // - final IntBuffer readBuffer, // - final IntBuffer writeBuffer, // - final IntBuffer sizeBuffer, // - final long millisTimeout) throws ExceptionUDT; - - /** - * Verify that java code and c++ code builds are consistent. - * - * @see #SIGNATURE_JNI - */ - protected static native - int getSignatureJNI0(); - - /** - * Call this after loading native library. - * - * @see UDT::startup() - */ - protected static native - void initClass0() throws ExceptionUDT; - - /** - * receive into a complete byte array - * - * @see UDT::recv() - * @see UDT::recvmsg() - */ - protected static native - int receive0(// - final int socketID, // - final int socketType, // - final byte[] array // - ) throws ExceptionUDT; - - /** - * receive into a portion of a byte array - * - * @see UDT::recv() - * @see UDT::recvmsg() - */ - protected static native - int receive1( // - final int socketID, // - final int socketType, // - final byte[] array, // - final int position, // - final int limit // - ) throws ExceptionUDT; - - /** - * receive into a {@link java.nio.channels.DirectByteBuffer} - * - * @see UDT::recv() - * @see UDT::recvmsg() - */ - protected static native - int receive2( // - final int socketID, // - final int socketType, // - final ByteBuffer buffer, // - final int position, // - final int limit // - ) throws ExceptionUDT; - - /** - * Receive file. - * - * @see UDT::recvfile - */ - protected static native - long receiveFile0( // - final int socketID, // - final String path, // - long offset, // - long length, // - int block // - ) throws ExceptionUDT; - - /** - * Basic access to UDT socket readiness selection feature. Based on - * {@link java.nio.DirectIntBuffer} info exchange.Timeout is in - * milliseconds. - * - * @param millisTimeout http://udt.sourceforge.net/udt4/doc/epoll.htm - *

- * "Finally, for epoll_wait, negative timeout value will make the - * function to wait until an event happens. If the timeout value - * is 0, then the function returns immediately with any sockets - * associated an IO event. If timeout occurs before any event - * happens, the function returns 0". - * @return <0 : should not happen
- * =0 : timeout, no ready sockets
- * >0 : total number or reads, writes, exceptions
- * @see #epollWait0(int, IntBuffer, IntBuffer, IntBuffer, long) - */ - public static - int selectEpoll( // - final int epollId, // - final IntBuffer readBuffer, // - final IntBuffer writeBuffer, // - final IntBuffer sizeBuffer, // - final long millisTimeout) throws ExceptionUDT { - - /** asserts are contracts */ - - assert readBuffer != null && readBuffer.isDirect(); - assert writeBuffer != null && writeBuffer.isDirect(); - assert sizeBuffer != null && sizeBuffer.isDirect(); - - return epollWait0( // - epollId, // - readBuffer, // - writeBuffer, // - sizeBuffer, // - millisTimeout // - ); - - } - - /** - * send from a complete byte[] array; - *

- * wrapper for UDT::send(), UDT::sendmsg() - * - * @see UDT::send() - * @see UDT::sendmsg() - */ - protected static native - int send0( // - final int socketID, // - final int socketType, // - final int timeToLive, // - final boolean isOrdered, // - final byte[] array // - ) throws ExceptionUDT; - - /** - * send from a portion of a byte[] array; - *

- * wrapper for UDT::send(), UDT::sendmsg() - * - * @see UDT::send() - * @see UDT::sendmsg() - */ - protected static native - int send1( // - final int socketID, // - final int socketType, // - final int timeToLive, // - final boolean isOrdered, // - final byte[] array, // / - final int arayPosition, // - final int arrayLimit // - ) throws ExceptionUDT; - - /** - * send from {@link java.nio.DirectByteBuffer}; - *

- * wrapper for UDT::send(), UDT::sendmsg() - * - * @see UDT::send() - * @see UDT::sendmsg() - */ - protected static native - int send2( // - final int socketID, // - final int socketType, // - final int timeToLive, // - final boolean isOrdered, // - final ByteBuffer buffer, // - final int bufferPosition, // - final int bufferLimit // - ) throws ExceptionUDT; - - /** - * Send file. - * - * @see UDT::sendfile - */ - protected static native - long sendFile0( // - final int socketID, // - final String path, // - long offset, // - long length, // - int block // - ) throws ExceptionUDT; - - /** - * Call this before unloading native library. - * - * @see UDT::cleanup() - */ - protected static native - void stopClass0() throws ExceptionUDT; - - // ########################################### - // ### used for development & testing only - // ### - - protected static native - void testCrashJVM0(); - - protected static native - void testDirectByteBufferAccess0(ByteBuffer buffer); - - protected static native - void testDirectIntBufferAccess0(IntBuffer buffer); - - protected static native - void testDirectIntBufferLoad0(IntBuffer buffer); - - protected static native - void testEmptyCall0(); - - protected static native - void testFillArray0(byte[] array); - - protected static native - void testFillBuffer0(ByteBuffer buffer); - - protected static native - void testGetSetArray0(int[] array, boolean isReturn); - - protected static native - void testInvalidClose0(int socketID) throws ExceptionUDT; - - protected static native - void testIterateArray0(Object[] array); - - protected static native - void testIterateSet0(Set set); - - protected static native - int[] testMakeArray0(int size); - - // ### - // ### used for development & testing only - // ########################################### - - /** - * java copy of underlying native accept queue size parameter - * - * @see #listen(int) - * @see #accept() - */ - private volatile int listenQueueSize; - - /** - * local end point; loaded by JNI by {@link #hasLoadedLocalSocketAddress()} - */ - @Native - private volatile InetSocketAddress localSocketAddress; - - /** - */ - private volatile boolean messageIsOrdered; - - /** - */ - private volatile int messageTimeTolive; - - /** - */ - @Native - private final MonitorUDT monitor; - - /** - * remote end point; loaded by JNI by - * {@link #hasLoadedRemoteSocketAddress()} - */ - @Native - private volatile InetSocketAddress remoteSocketAddress; - - /** - * native address family; read by JNI - *

- * TODO add support for AF_INET6 - */ - @Native - private final int socketAddressFamily; - - /** - * native descriptor; read by JNI; see udt.h "typedef int UDTSOCKET;" - */ - @Native - private final int socketID; - - /** - */ - @Native - private final TypeUDT type; - - /** - * "Primary" socket. Default constructor; will apply - * {@link #setDefaultMessageSendMode()} - * - * @param type UDT socket type - */ - public - SocketUDT(final TypeUDT type) throws ExceptionUDT { - synchronized (SocketUDT.class) { - this.type = type; - this.monitor = new MonitorUDT(this); - this.socketID = initInstance0(type.code); - this.socketAddressFamily = 2; // ipv4 - setDefaultMessageSendMode(); - } - log.debug("init : {}", this); - } - - /** - * "Secondary" socket. Made by {@link #accept0()}, will apply - * {@link #setDefaultMessageSendMode()} - * - * @param socketID UDT socket descriptor; - */ - protected - SocketUDT(final TypeUDT type, final int socketID) throws ExceptionUDT { - synchronized (SocketUDT.class) { - this.type = type; - this.monitor = new MonitorUDT(this); - this.socketID = initInstance1(socketID); - this.socketAddressFamily = 2; // ipv4 - setDefaultMessageSendMode(); - } - log.debug("init : {}", this); - } - - /** - * @return null : no incoming connections (non-blocking mode only)
- * non null : newly accepted SocketUDT (both blocking and - * non-blocking)
- */ - public - SocketUDT accept() throws ExceptionUDT { - return accept0(); - } - - /** - * @see UDT::accept() - */ - protected native - SocketUDT accept0() throws ExceptionUDT; - - public - void bind(final InetSocketAddress localSocketAddress) // - throws ExceptionUDT, IllegalArgumentException { - HelpUDT.checkSocketAddress(localSocketAddress); - bind0(localSocketAddress); - } - - /** - * @see UDT::bind() - */ - protected native - void bind0(final InetSocketAddress localSocketAddress) throws ExceptionUDT; - - /** - * Clear error status on a socket, if any. - * - * @see UDT Error - * Handling - */ - public - void clearError() { - clearError0(); - } - - /** - * @see UDT Error - * Handling - */ - protected native - void clearError0(); - - /** - * Flush all buffered data. - * - * @see #flush0() - */ - public - void flush() throws ExceptionUDT { - synchronized (SocketUDT.class) { - flush0(); - } - } - - /** - * Close socket if not already closed. - * - * @see #close0() - */ - public - void close() throws ExceptionUDT { - synchronized (SocketUDT.class) { - switch (status()) { - case INIT: - case OPENED: - case LISTENING: - case CONNECTING: - case CONNECTED: - case BROKEN: - /** Requires close. */ - close0(); - log.debug("done : {}", this); - break; - case CLOSING: - case CLOSED: - case NONEXIST: - /** Effectively closed. */ - log.debug("dead : {}", this); - break; - default: - log.error("Invalid socket/status {}/{}", this, status()); - } - } - } - - /** - * @see UDT::close() - */ - protected native - void flush0() throws ExceptionUDT; - - /** - * @see UDT::close() - */ - protected native - void close0() throws ExceptionUDT; - - /** - * Connect to remote UDT socket. - *

- * Can be blocking or non blocking call; depending on - * {@link OptionUDT#Is_Receive_Synchronous} - *

- * Timing: UDT uses hard coded connect timeout: - *

- * normal socket: 3 seconds - *

- * rendezvous socket: 30 seconds; when - * {@link OptionUDT#Is_Randezvous_Connect_Enabled} is true - * - * @see #connect0(InetSocketAddress) - */ - public - void connect(final InetSocketAddress remoteSocketAddress) // - throws ExceptionUDT { - HelpUDT.checkSocketAddress(remoteSocketAddress); - connect0(remoteSocketAddress); - } - - /** - * @see UDT::connect() - */ - protected native - void connect0(final InetSocketAddress remoteSocketAddress) throws ExceptionUDT; - - /** - * Note: equality is based on {@link #socketID}. - */ - @Override - public - boolean equals(final Object otherSocketUDT) { - if (otherSocketUDT instanceof SocketUDT) { - final SocketUDT other = (SocketUDT) otherSocketUDT; - return other.socketID == this.socketID; - } - return false; - } - - /** - * NOTE: catch all exceptions; else prevents GC - *

- * NOTE: do not leak "this" references; else prevents GC - */ - @Override - protected - void finalize() { - try { - close(); - super.finalize(); - } catch (final Throwable e) { - log.error("failed to close id=" + socketID, e); - } - } - - /** - * Error object wrapper. - * - * @return error status set by last socket operation - **/ - public - ErrorUDT getError() { - final int code = getErrorCode(); - return ErrorUDT.errorFrom(code); - } - - /** - * Error code set by last operation on a socket. - * - * @see UDT Error - * Handling - */ - public - int getErrorCode() { - return getErrorCode0(); - } - - /** - * @see UDT Error - * Handling - */ - protected native - int getErrorCode0(); - - /** - * Native error message set by last operation on a socket. - * - * @see t-error.htm - */ - public - String getErrorMessage() { - return getErrorMessage0(); - } - - /** - * @see UDT Error - * Handling - */ - protected native - String getErrorMessage0(); - - /** - * @see #listen(int) - */ - public - int getListenQueueSize() { - return listenQueueSize; - } - - /** - * @return null : not bound
- * not null : valid address; result of - * {@link #bind(InetSocketAddress)}
- */ - public - InetAddress getLocalInetAddress() { - try { - final InetSocketAddress local = getLocalSocketAddress(); - if (local == null) { - return null; - } - else { - return local.getAddress(); - } - } catch (final Exception e) { - log.debug("failed to get local address", e); - return null; - } - } - - /** - * @return 0 : not bound
- * >0 : valid port; result of {@link #bind(InetSocketAddress)}
- */ - public - int getLocalInetPort() { - try { - final InetSocketAddress local = getLocalSocketAddress(); - if (local == null) { - return 0; - } - else { - return local.getPort(); - } - } catch (final Exception e) { - log.debug("failed to get local port", e); - return 0; - } - } - - /** - * @return null: not bound;
- * not null: local UDT socket address to which the the socket is - * bound
- * @see #hasLoadedLocalSocketAddress() - */ - public - InetSocketAddress getLocalSocketAddress() throws ExceptionUDT { - if (hasLoadedLocalSocketAddress()) { - return localSocketAddress; - } - else { - return null; - } - } - - /** - * default isOrdered value used by sendmsg mode - * - * @see UDT::sendmsg() - */ - public - boolean getMessageIsOdered() { - return messageIsOrdered; - } - - /** - * default timeToLive value used by sendmsg mode - * - * @see UDT::sendmsg() - */ - public - int getMessageTimeTolLive() { - return messageTimeTolive; - } - - /** - * @see #getOption0(int, Class) - */ - public - T getOption(final OptionUDT option) throws ExceptionUDT { - - if (option == null) { - throw new IllegalArgumentException("option == null"); - } - - return (T) getOption0(option.code(), option.type()); - - } - - /** - * @see UDT::getsockopt() - */ - protected native - Object getOption0(final int code, final Class klaz) throws ExceptionUDT; - - /** - * Get maximum receive buffer size. Reflects minimum of protocol-level (UDT) - * and kernel-level(UDP) settings. - * - * @see java.net.Socket#getReceiveBufferSize() - */ - public - int getReceiveBufferSize() throws ExceptionUDT { - final int protocolSize = getOption(OptionUDT.Protocol_Receive_Buffer_Size); - final int kernelSize = getOption(OptionUDT.System_Receive_Buffer_Size); - return Math.min(protocolSize, kernelSize); - } - - // - - /** - * @return null : not connected
- * not null : valid address; result of - * {@link #connect(InetSocketAddress)}
- */ - public - InetAddress getRemoteInetAddress() { - try { - final InetSocketAddress remote = getRemoteSocketAddress(); - if (remote == null) { - return null; - } - else { - return remote.getAddress(); - } - } catch (final Exception e) { - log.debug("failed to get remote address", e); - return null; - } - } - - /** - * @return 0 : not connected
- * >0 : valid port ; result of {@link #connect(InetSocketAddress)}
- */ - public - int getRemoteInetPort() { - try { - final InetSocketAddress remote = getRemoteSocketAddress(); - if (remote == null) { - return 0; - } - else { - return remote.getPort(); - } - } catch (final Exception e) { - log.debug("failed to get remote port", e); - return 0; - } - } - - /** - * @return null : not connected;
- * not null: remote UDT peer socket address to which this socket is - * connected
- * @see #hasLoadedRemoteSocketAddress() - */ - public - InetSocketAddress getRemoteSocketAddress() throws ExceptionUDT { - if (hasLoadedRemoteSocketAddress()) { - return remoteSocketAddress; - } - else { - return null; - } - } - - /** - * Check if local bind address is set to reuse mode. - * - * @see java.net.Socket#getReuseAddress() - */ - public - boolean getReuseAddress() throws ExceptionUDT { - return getOption(OptionUDT.Is_Address_Reuse_Enabled); - } - - /** - * Get maximum send buffer size. Reflects minimum of protocol-level (UDT) - * and kernel-level(UDP) settings. - * - * @see java.net.Socket#getSendBufferSize() - */ - public - int getSendBufferSize() throws ExceptionUDT { - final int protocolSize = getOption(OptionUDT.Protocol_Send_Buffer_Size); - final int kernelSize = getOption(OptionUDT.System_Send_Buffer_Size); - return Math.min(protocolSize, kernelSize); - } - - /** - * Get time to linger on close (seconds). - * - * @see java.net.Socket#getSoLinger() - */ - public - int getSoLinger() throws ExceptionUDT { - return getOption(OptionUDT.Time_To_Linger_On_Close).intValue(); - } - - /** - * Get "any blocking operation" timeout setting. - *

- * Returns milliseconds; zero return means "infinite"; negative means - * invalid - * - * @see java.net.Socket#getSoTimeout() - */ - public - int getSoTimeout() throws ExceptionUDT { - final int sendTimeout = getOption(OptionUDT.Send_Timeout); - final int receiveTimeout = getOption(OptionUDT.Receive_Timeout); - final int millisTimeout; - if (sendTimeout != receiveTimeout) { - log.error("sendTimeout != receiveTimeout"); - millisTimeout = Math.max(sendTimeout, receiveTimeout); - } - else { - // map from UDT value convention to java.net.Socket value convention - if (sendTimeout < 0) { - // UDT infinite - millisTimeout = 0; - } - else if (sendTimeout > 0) { - // UDT finite - millisTimeout = sendTimeout; - } - else { // ==0 - log.error("UDT reported unexpected zero timeout"); - millisTimeout = -1; - } - } - return millisTimeout; - } - - /** - * @see - */ - protected native - int getStatus0(); - - // - - /** - * Note: uses {@link #socketID} as hash code. - */ - @Override - public - int hashCode() { - return socketID; - } - - /** - * Load {@link #localSocketAddress} value. - * - * @see UDT::sockname() - */ - protected native - boolean hasLoadedLocalSocketAddress(); - - /** - * Load {@link #remoteSocketAddress} value. - * - * @see UDT::peername() - */ - protected native - boolean hasLoadedRemoteSocketAddress(); - - /** - * native socket descriptor id; assigned by udt library - */ - public - int id() { - return socketID; - } - - /** - * used by default constructor - */ - protected native - int initInstance0(int typeCode) throws ExceptionUDT; - - /** - * used by accept() internally - */ - protected native - int initInstance1(int socketUDT) throws ExceptionUDT; - - /** - * Check if socket is in strict blocking mode. (JDK semantics) - * - * @return true : socket is valid and both send and receive are set to - * blocking mode; false : at least one channel is set to - * non-blocking mode or socket is invalid; - * @see #isNonBlocking() - * @see #setBlocking(boolean) - */ - public - boolean isBlocking() { - try { - if (isOpen()) { - final boolean isReceiveBlocking = getOption(OptionUDT.Is_Receive_Synchronous); - final boolean isSendBlocking = getOption(OptionUDT.Is_Send_Synchronous); - return isReceiveBlocking && isSendBlocking; - } - } catch (final Exception e) { - log.error("failed to get option", e); - } - return false; - } - - /** - * Check if socket is bound. (JDK semantics) - * - * @return true : {@link #bind(InetSocketAddress)} was successful
- * false : otherwise
- */ - public - boolean isBound() { - switch (status()) { - case OPENED: - case CONNECTING: - case CONNECTED: - case LISTENING: - return true; - default: - return false; - } - } - - /** - * Check if socket is closed. A convenience !isOpen(); - * - * @see #isOpen() - */ - public - boolean isClosed() { - return !isOpen(); - } - - /** - * Check if {@link KindUDT#CONNECTOR} socket is connected. (JDK semantics) - * - * @return true : {@link #connect(InetSocketAddress)} was successful
- * false : otherwise
- */ - public - boolean isConnected() { - switch (status()) { - case CONNECTED: - return true; - default: - return false; - } - } - - /** - * Check if socket is in strict non-blocking mode. - * - * @return true : socket is valid and both send and receive are set to NON - * blocking mode; false : at least one channel is set to blocking - * mode or socket is invalid; - * @see #isBlocking() - * @see #setBlocking(boolean) - */ - public - boolean isNonBlocking() { - try { - if (isOpen()) { - final boolean isReceiveBlocking = getOption(OptionUDT.Is_Receive_Synchronous); - final boolean isSendBlocking = getOption(OptionUDT.Is_Send_Synchronous); - return !isReceiveBlocking && !isSendBlocking; - } - } catch (final Exception e) { - log.error("failed to get option", e); - } - return false; - } - - /** - * Check if socket is open. (JDK semantics). The status of underlying UDT - * socket is mapped into JDK expected categories - * - * @see StatusUDT - */ - public - boolean isOpen() { - switch (status()) { - case INIT: - case OPENED: - case LISTENING: - case CONNECTING: - case CONNECTED: - return true; - default: - return false; - } - } - - public - boolean isRendezvous() { - try { - if (isOpen()) { - return getOption(OptionUDT.Is_Randezvous_Connect_Enabled); - } - } catch (final Exception e) { - log.error("failed to get option", e); - } - return false; - } - - /** - * @param queueSize maximum number of queued clients - * @see #listen0(int) - */ - public - void listen(final int queueSize) throws ExceptionUDT { - if (queueSize <= 0) { - throw new IllegalArgumentException("queueSize <= 0"); - } - listenQueueSize = queueSize; - listen0(queueSize); - } - - /** - * @see UDT::listen() - */ - protected native - void listen0(final int queueSize) throws ExceptionUDT; - - /** - * performance monitor; updated by {@link #updateMonitor(boolean)} in JNI - * - * @see #updateMonitor(boolean) - */ - public - MonitorUDT monitor() { - return monitor; - } - - /** - * receive into byte[] array upto array.length bytes - * - * @return -1 : nothing received (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal receive, byte count
- * @see #receive0(int, int, byte[]) - */ - public - int receive(final byte[] array) throws ExceptionUDT { - - HelpUDT.checkArray(array); - - return receive0(socketID, type.code, array); - - } - - /** - * receive into byte[] array upto size=limit-position bytes - * - * @return -1 : nothing received (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal receive, byte count
- * @see #receive1(int, int, byte[], int, int) - */ - public - int receive(final byte[] array, final int position, final int limit) throws ExceptionUDT { - - HelpUDT.checkArray(array); - - return receive1(socketID, type.code, array, position, limit); - - } - - /** - * receive into {@link java.nio.channels.DirectByteBuffer}; upto - * {@link ByteBuffer#remaining()} bytes - * - * @return -1 : nothing received (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal receive, byte count
- * @see #receive2(int, int, ByteBuffer, int, int) - */ - public - int receive(final ByteBuffer buffer) throws ExceptionUDT { - - HelpUDT.checkBuffer(buffer); - - final int position = buffer.position(); - final int limit = buffer.limit(); - final int remaining = buffer.remaining(); - - final int sizeReceived = // - receive2(socketID, type.code, buffer, position, limit); - - if (sizeReceived <= 0) { - return sizeReceived; - } - - if (sizeReceived <= remaining) { - buffer.position(position + sizeReceived); - return sizeReceived; - } - else { // should not happen - log.error("sizeReceived > remaining"); - return 0; - } - - } - - // - - /** - * Receive file from remote peer. - * - * @see #receiveFile0(int, String, long, long, int) - */ - public - long receiveFile( // - final File file, // - final long offset, // - final long length// - ) throws ExceptionUDT { - - if (type == TypeUDT.DATAGRAM) { - throw new IllegalStateException("invalid socket type : " + type); - } - - if (file == null || !file.exists() || !file.isFile() || !file.canWrite()) { - throw new IllegalArgumentException("invalid file : " + file); - } - - if (offset < 0 || offset > file.length()) { - throw new IllegalArgumentException("invalid offset : " + offset); - } - - if (length < 0 || offset + length > file.length()) { - throw new IllegalArgumentException("invalid length : " + length); - } - - final String path = file.getAbsolutePath(); - - final int block; - if (length > DEFAULT_FILE_BLOCK_SIZE) { - block = DEFAULT_FILE_BLOCK_SIZE; - } - else { - block = (int) length; - } - - return receiveFile0(id(), path, offset, length, block); - - } - - /** - * send from byte[] array upto size=array.length bytes - * - * @param array array to send - * @return -1 : no buffer space (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal send, actual sent byte count
- * @see #send0(int, int, int, boolean, byte[]) - */ - public - int send(final byte[] array) throws ExceptionUDT { - - HelpUDT.checkArray(array); - - return send0( // - socketID, // - type.code, // - messageTimeTolive, // - messageIsOrdered, // - array // - ); - - } - - /** - * send from byte[] array upto size=limit-position bytes - * - * @param array array to send - * @param position start of array portion to send - * @param limit finish of array portion to send - * @return -1 : no buffer space (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal send, actual sent byte count
- * @see #send1(int, int, int, boolean, byte[], int, int) - */ - public - int send( // - final byte[] array, // - final int position, // - final int limit // - ) throws ExceptionUDT { - - HelpUDT.checkArray(array); - - return send1( // - socketID, // - type.code, // - messageTimeTolive, // - messageIsOrdered, // - array, // - position, // - limit // - ); - - } - - /** - * send from {@link java.nio.DirectByteBuffer}, upto - * {@link ByteBuffer#remaining()} bytes - * - * @param buffer buffer to send - * @return -1 : no buffer space (non-blocking only)
- * =0 : timeout expired (blocking only)
- * >0 : normal send, actual sent byte count
- * @see #send2(int, int, int, boolean, ByteBuffer, int, int) - */ - public - int send(final ByteBuffer buffer) throws ExceptionUDT { - - HelpUDT.checkBuffer(buffer); - - final int position = buffer.position(); - final int limit = buffer.limit(); - final int remaining = buffer.remaining(); - - final int sizeSent = send2( // - socketID, // - type.code, // - messageTimeTolive, // - messageIsOrdered, // - buffer, // - position, // - limit // - ); - - if (sizeSent <= 0) { - return sizeSent; - } - if (sizeSent <= remaining) { - buffer.position(position + sizeSent); - return sizeSent; - } - else { // should not happen - log.error("sizeSent > remaining"); - return 0; - } - - } - - /** - * Send file to remote peer. - * - * @see #sendFile0(int, String, long, long, int) - */ - public - long sendFile( // - final File file, // - final long offset, // - final long length// - ) throws ExceptionUDT { - - if (type == TypeUDT.DATAGRAM) { - throw new IllegalStateException("invalid socket type : " + type); - } - - if (file == null || !file.exists() || !file.isFile() || !file.canRead()) { - throw new IllegalArgumentException("invalid file : " + file); - } - - if (offset < 0 || offset > file.length()) { - throw new IllegalArgumentException("invalid offset : " + offset); - } - - if (length < 0 || offset + length > file.length()) { - throw new IllegalArgumentException("invalid length : " + length); - } - - final String path = file.getAbsolutePath(); - - final int block; - if (length > DEFAULT_FILE_BLOCK_SIZE) { - block = DEFAULT_FILE_BLOCK_SIZE; - } - else { - block = (int) length; - } - - return sendFile0(id(), path, offset, length, block); - - } - - // - - /** - * Configure socket in strict blocking / strict non-blocking mode. - * - * @param block true : set both send and receive to blocking mode; false : set - * both send and receive to non-blocking mode - * @see java.nio.channels.SocketChannel#configureBlocking(boolean) - */ - public - void setBlocking(final boolean block) throws ExceptionUDT { - if (block) { - setOption(OptionUDT.Is_Receive_Synchronous, Boolean.TRUE); - setOption(OptionUDT.Is_Send_Synchronous, Boolean.TRUE); - } - else { - setOption(OptionUDT.Is_Receive_Synchronous, Boolean.FALSE); - setOption(OptionUDT.Is_Send_Synchronous, Boolean.FALSE); - } - } - - /** - * Apply default settings for message mode. - *

- * IsOdered = true;
- * TimeTolLive = INFINITE_TTL;
- */ - public - void setDefaultMessageSendMode() { - setMessageIsOdered(true); - setMessageTimeTolLive(INFINITE_TTL); - } - - /** - * default isOrdered value used by sendmsg mode - * - * @see UDT::sendmsg() - */ - public - void setMessageIsOdered(final boolean isOrdered) { - messageIsOrdered = isOrdered; - } - - /** - * default timeToLive value used by sendmsg mode - * - * @see UDT::sendmsg() - */ - public - void setMessageTimeTolLive(final int timeToLive) { - messageTimeTolive = timeToLive; - } - - /** - * @see #setOption0(int, Class, Object) - */ - public - void setOption(final OptionUDT option, final T value) throws ExceptionUDT { - - if (option == null || value == null) { - throw new IllegalArgumentException("option == null || value == null"); - } - - setOption0(option.code(), option.type(), value); - } - - /** - * @see UDT::setsockopt() - */ - protected native - void setOption0(final int code, final Class klaz, final Object value) throws ExceptionUDT; - - /** - * Set maximum receive buffer size. Affects both protocol-level (UDT) and - * kernel-level(UDP) settings. - */ - public - void setReceiveBufferSize(final int size) throws ExceptionUDT { - setOption(OptionUDT.Protocol_Receive_Buffer_Size, size); - setOption(OptionUDT.System_Receive_Buffer_Size, size); - } - - public - void setRendezvous(final boolean isOn) throws ExceptionUDT { - setOption(OptionUDT.Is_Randezvous_Connect_Enabled, isOn); - } - - public - void setReuseAddress(final boolean on) throws ExceptionUDT { - setOption(OptionUDT.Is_Address_Reuse_Enabled, on); - } - - /** - * Set maximum send buffer size. Affects both protocol-level (UDT) and - * kernel-level(UDP) settings - * - * @see java.net.Socket#setSendBufferSize(int) - */ - public - void setSendBufferSize(final int size) throws ExceptionUDT { - setOption(OptionUDT.Protocol_Send_Buffer_Size, size); - setOption(OptionUDT.System_Send_Buffer_Size, size); - } - - public - void setSoLinger(final boolean on, final int linger) throws ExceptionUDT { - if (on) { - if (linger <= 0) { - // keep JDK contract for setSoLinger parameters - throw new IllegalArgumentException("linger <= 0"); - } - setOption(OptionUDT.Time_To_Linger_On_Close, new LingerUDT(linger)); - } - else { - setOption(OptionUDT.Time_To_Linger_On_Close, LingerUDT.LINGER_ZERO); - } - } - - /** - * call timeout (milliseconds); Set a timeout on blocking Socket operations: - * ServerSocket.accept(); SocketInputStream.read(); - * DatagramSocket.receive(); Enable/disable SO_TIMEOUT with the specified - * timeout, in milliseconds. A timeout of zero is interpreted as an infinite - * timeout. - */ - public - void setSoTimeout(/* non-final */int millisTimeout) throws ExceptionUDT { - if (millisTimeout < 0) { - throw new IllegalArgumentException("timeout < 0"); - } - if (millisTimeout == 0) { - // UDT uses different value for "infinite" - millisTimeout = TIMEOUT_INFINITE; - } - setOption(OptionUDT.Send_Timeout, millisTimeout); - setOption(OptionUDT.Receive_Timeout, millisTimeout); - } - - /** - * returns native status of underlying native UDT socket - */ - public - StatusUDT status() { - return StatusUDT.from(getStatus0()); - } - - // - @Override - public - String toString() { - - return String.format( // - "[id: 0x%08x] %s %s bind=%s:%s peer=%s:%s", // - socketID, // - type, // - status(), // - getLocalInetAddress(), // - getLocalInetPort(), // - getRemoteInetAddress(), // - getRemoteInetPort() // - ); - - } - - /** - * Show current monitor status. - */ - public - String toStringMonitor() { - - try { - updateMonitor(false); - } catch (final Exception e) { - return "failed to update monitor : " + e.getMessage(); - } - - final StringBuilder text = new StringBuilder(1024); - - monitor.appendSnapshot(text); - - return text.toString(); - - } - - /** - * Show current socket options. - */ - public - String toStringOptions() { - - final StringBuilder text = new StringBuilder(1024); - - OptionUDT.appendSnapshot(this, text); - - return text.toString(); - - } - - /** - * message/stream socket type; read by JNI - */ - public - TypeUDT type() { - return type; - } - - /** - * Load updated statistics values into {@link #monitor} object. Must call - * this methos only on connected socket. - * - * @param makeClear true : reset all statistics with this call; false : keep - * collecting statistics, load updated values. - * @see #updateMonitor0(boolean) - */ - public - void updateMonitor(final boolean makeClear) throws ExceptionUDT { - updateMonitor0(makeClear); - } - - /** - * @see UDT::perfmon - */ - protected native - void updateMonitor0(final boolean makeClear) throws ExceptionUDT; - -} diff --git a/src/com/barchart/udt/StatusUDT.java b/src/com/barchart/udt/StatusUDT.java deleted file mode 100644 index e05e1f09..00000000 --- a/src/com/barchart/udt/StatusUDT.java +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * status of underlying UDT native socket as reported by - * {@link SocketUDT#getStatus0()} - *

- * keep in sync with udt.h UDTSTATUS enum; see: - *

- * "enum UDTSTATUS { INIT = 1, OPENED = 2, LISTENING = 3, CONNECTING = 4, CONNECTED = 5, BROKEN = 6, CLOSING = 7, CLOSED = 8, NONEXIST = 9 };" - */ -public enum StatusUDT { - - /** note: keep the order */ - - // - - /** newly created socket; both connector and acceptor */ - INIT(1), // - - /** bound socket; both connector and acceptor */ - OPENED(2), // - - /** bound and listening acceptor socket */ - LISTENING(3), // - - /** bound connector socket trying to connect */ - CONNECTING(4), // - - /** bound and connected connector socket */ - CONNECTED(5), // - - /** acceptor socket after close(), connector socket after remote unreachable */ - BROKEN(6), // - - /** connector socket while close() is in progress */ - CLOSING(7), // - - /** connector socket after close() is done */ - CLOSED(8), // - - /** trying to check status on socket that was closed and removed */ - NONEXIST(9), // - - /** non udt constant, catch-all value */ - UNKNOWN(100), // - - ; - - protected static final Logger log = LoggerFactory - .getLogger(StatusUDT.class); - - private final int code; - - private StatusUDT(final int code) { - this.code = code; - } - - public int getCode() { - return code; - } - - public static final StatusUDT from(final int code) { - - switch (code) { - case 1: - return INIT; - case 2: - return OPENED; - case 3: - return LISTENING; - case 4: - return CONNECTING; - case 5: - return CONNECTED; - case 6: - return BROKEN; - case 7: - return CLOSING; - case 8: - return CLOSED; - case 9: - return NONEXIST; - default: - log.error("unexpected code={}", code); - return UNKNOWN; - } - - } - -} diff --git a/src/com/barchart/udt/TypeUDT.java b/src/com/barchart/udt/TypeUDT.java deleted file mode 100644 index 0b5e6e1d..00000000 --- a/src/com/barchart/udt/TypeUDT.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt; - -/** - * UDT socket mode type. - * - * NOTE: {@link #TypeUDT} means stream vs datagram; - * {@link com.barchart.udt.nio.KindUDT} means server vs client. - *

- * maps to socket.h constants
- * SOCK_STREAM = 1
- * SOCK_DGRAM = 2
- */ -public enum TypeUDT { - - /** - * The STREAM type. Defines "byte stream" UDT mode. - */ - STREAM(1), // - - /** - * The DATAGRAM. Defines "datagram or message" UDT mode. - */ - DATAGRAM(2), // - - ; - - protected final int code; - - /** - * native UDT constant - */ - public int code() { - return code; - } - - /** - * @param code - * native UDT constant - */ - private TypeUDT(final int code) { - this.code = code; - } - -} diff --git a/src/com/barchart/udt/anno/Native.java b/src/com/barchart/udt/anno/Native.java deleted file mode 100644 index cee401bb..00000000 --- a/src/com/barchart/udt/anno/Native.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.anno; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * annotation marks a native JNI entity, please not change it in any way - */ -@Documented -@Target( { ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR }) -@Retention(RetentionPolicy.RUNTIME) -public @interface Native { - -} diff --git a/src/com/barchart/udt/anno/ThreadSafe.java b/src/com/barchart/udt/anno/ThreadSafe.java deleted file mode 100644 index d1e2cd2d..00000000 --- a/src/com/barchart/udt/anno/ThreadSafe.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.anno; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * annotation documents thread safe access contract - */ -@Documented -@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR }) -@Retention(RetentionPolicy.RUNTIME) -public @interface ThreadSafe { - - String value(); - -} diff --git a/src/com/barchart/udt/ccc/UDPBlast.java b/src/com/barchart/udt/ccc/UDPBlast.java deleted file mode 100644 index 86b79238..00000000 --- a/src/com/barchart/udt/ccc/UDPBlast.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.ccc; - -import com.barchart.udt.CCC; - -/** - * Wrapper around the CUDPBlast class that demos the use of a custom congestion - * control algorithm - * - * @author CCob - */ -public class UDPBlast extends CCC { - - static final int iSMSS = 1500; - - public UDPBlast() { - setCWndSize(83333.0); - } - - public void setRate(final int mbps) { - setPacketSndPeriod((iSMSS * 8.0) / mbps); - } - -} diff --git a/src/com/barchart/udt/natives/linux_32/udt-core-2.3.2.so b/src/com/barchart/udt/natives/linux_32/udt-core-2.3.2.so deleted file mode 100755 index ae31e131..00000000 Binary files a/src/com/barchart/udt/natives/linux_32/udt-core-2.3.2.so and /dev/null differ diff --git a/src/com/barchart/udt/natives/linux_64/udt-core-2.3.2.so b/src/com/barchart/udt/natives/linux_64/udt-core-2.3.2.so deleted file mode 100755 index 1481d955..00000000 Binary files a/src/com/barchart/udt/natives/linux_64/udt-core-2.3.2.so and /dev/null differ diff --git a/src/com/barchart/udt/natives/macosx_32/udt-core-2.3.2.dylib b/src/com/barchart/udt/natives/macosx_32/udt-core-2.3.2.dylib deleted file mode 100755 index d52a0ab4..00000000 Binary files a/src/com/barchart/udt/natives/macosx_32/udt-core-2.3.2.dylib and /dev/null differ diff --git a/src/com/barchart/udt/natives/macosx_64/udt-core-2.3.2.dylib b/src/com/barchart/udt/natives/macosx_64/udt-core-2.3.2.dylib deleted file mode 100755 index 88ab3f62..00000000 Binary files a/src/com/barchart/udt/natives/macosx_64/udt-core-2.3.2.dylib and /dev/null differ diff --git a/src/com/barchart/udt/natives/windows_32/udt-core-2.3.2.dll b/src/com/barchart/udt/natives/windows_32/udt-core-2.3.2.dll deleted file mode 100755 index b995ef75..00000000 Binary files a/src/com/barchart/udt/natives/windows_32/udt-core-2.3.2.dll and /dev/null differ diff --git a/src/com/barchart/udt/natives/windows_64/udt-core-2.3.2.dll b/src/com/barchart/udt/natives/windows_64/udt-core-2.3.2.dll deleted file mode 100755 index 857a5a77..00000000 Binary files a/src/com/barchart/udt/natives/windows_64/udt-core-2.3.2.dll and /dev/null differ diff --git a/src/com/barchart/udt/net/ExceptionReceiveUDT.java b/src/com/barchart/udt/net/ExceptionReceiveUDT.java deleted file mode 100644 index efc3d88a..00000000 --- a/src/com/barchart/udt/net/ExceptionReceiveUDT.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import com.barchart.udt.ErrorUDT; -import com.barchart.udt.ExceptionUDT; - -/** - * - */ -@SuppressWarnings("serial") -public class ExceptionReceiveUDT extends ExceptionUDT { - - protected ExceptionReceiveUDT(final int socketID, final ErrorUDT error, - final String comment) { - super(socketID, error, comment); - } - -} diff --git a/src/com/barchart/udt/net/ExceptionSendUDT.java b/src/com/barchart/udt/net/ExceptionSendUDT.java deleted file mode 100644 index ec3e2fce..00000000 --- a/src/com/barchart/udt/net/ExceptionSendUDT.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import com.barchart.udt.ErrorUDT; -import com.barchart.udt.ExceptionUDT; - -/** - * - */ -@SuppressWarnings("serial") -public class ExceptionSendUDT extends ExceptionUDT { - - protected ExceptionSendUDT(final int socketID, final ErrorUDT error, - final String comment) { - super(socketID, error, comment); - } - -} diff --git a/src/com/barchart/udt/net/IceCommon.java b/src/com/barchart/udt/net/IceCommon.java deleted file mode 100644 index 7126e672..00000000 --- a/src/com/barchart/udt/net/IceCommon.java +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import com.barchart.udt.SocketUDT; - -/** - * custom/common acceptor/connector socket features - */ -public interface IceCommon { - - /** - * expose underlying socket - */ - SocketUDT socketUDT(); - -} diff --git a/src/com/barchart/udt/net/IceDatagramSocket.java b/src/com/barchart/udt/net/IceDatagramSocket.java deleted file mode 100644 index aa4eb19e..00000000 --- a/src/com/barchart/udt/net/IceDatagramSocket.java +++ /dev/null @@ -1,560 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.PortUnreachableException; -import java.net.SocketAddress; -import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.nio.channels.DatagramChannel; - -/** - * compatibility verification interface - */ -public interface IceDatagramSocket { - - /** - * Binds this DatagramSocket to a specific address & port. - *

- * If the address is null, then the system will pick up an - * ephemeral port and a valid local address to bind the socket. - *

- * - * @param addr - * The address & port to bind to. - * @throws SocketException - * if any error happens during the bind, or if the socket is - * already bound. - * @throws SecurityException - * if a security manager exists and its checkListen - * method doesn't allow the operation. - * @throws IllegalArgumentException - * if addr is a SocketAddress subclass not supported by this - * socket. - * @since 1.4 - */ - void bind(SocketAddress addr) throws SocketException; - - /** - * Connects the socket to a remote address for this socket. When a socket is - * connected to a remote address, packets may only be sent to or received - * from that address. By default a datagram socket is not connected. - * - *

- * If the remote destination to which the socket is connected does not - * exist, or is otherwise unreachable, and if an ICMP destination - * unreachable packet has been received for that address, then a subsequent - * call to send or receive may throw a PortUnreachableException. Note, there - * is no guarantee that the exception will be thrown. - * - *

- * A caller's permission to send and receive datagrams to a given host and - * port are checked at connect time. When a socket is connected, receive and - * send will not perform any security checks on incoming and outgoing - * packets, other than matching the packet's and the socket's address and - * port. On a send operation, if the packet's address is set and the - * packet's address and the socket's address do not match, an - * IllegalArgumentException will be thrown. A socket connected to a - * multicast address may only be used to send packets. - * - * @param address - * the remote address for the socket - * - * @param port - * the remote port for the socket. - * - * @exception IllegalArgumentException - * if the address is null, or the port is out of range. - * - * @exception SecurityException - * if the caller is not allowed to send datagrams to and - * receive datagrams from the address and port. - * - * @see #disconnect - * @see #send - * @see #receive - */ - void connect(InetAddress address, int port); - - /** - * Connects this socket to a remote socket address (IP address + port - * number). - *

- * - * @param addr - * The remote address. - * @throws SocketException - * if the connect fails - * @throws IllegalArgumentException - * if addr is null or addr is a SocketAddress subclass not - * supported by this socket - * @since 1.4 - * @see #connect - */ - void connect(SocketAddress addr) throws SocketException; - - /** - * Disconnects the socket. This does nothing if the socket is not connected. - * - * @see #connect - */ - void disconnect(); - - /** - * Returns the binding state of the socket. - * - * @return true if the socket succesfuly bound to an address - * @since 1.4 - */ - boolean isBound(); - - /** - * Returns the connection state of the socket. - * - * @return true if the socket succesfuly connected to a server - * @since 1.4 - */ - boolean isConnected(); - - /** - * Returns the address to which this socket is connected. Returns null if - * the socket is not connected. - * - * @return the address to which this socket is connected. - */ - InetAddress getInetAddress(); - - /** - * Returns the port for this socket. Returns -1 if the socket is not - * connected. - * - * @return the port to which this socket is connected. - */ - int getPort(); - - /** - * Returns the address of the endpoint this socket is connected to, or - * null if it is unconnected. - * - * @return a SocketAddress representing the remote endpoint of - * this socket, or null if it is not connected yet. - * @see #getInetAddress() - * @see #getPort() - * @see #connect(SocketAddress) - * @since 1.4 - */ - SocketAddress getRemoteSocketAddress(); - - /** - * Returns the address of the endpoint this socket is bound to, or - * null if it is not bound yet. - * - * @return a SocketAddress representing the local endpoint of - * this socket, or null if it is not bound yet. - * @see #getLocalAddress() - * @see #getLocalPort() - * @see #bind(SocketAddress) - * @since 1.4 - */ - - SocketAddress getLocalSocketAddress(); - - /** - * Sends a datagram packet from this socket. The DatagramPacket - * includes information indicating the data to be sent, its length, the IP - * address of the remote host, and the port number on the remote host. - * - *

- * If there is a security manager, and the socket is not currently connected - * to a remote address, this method first performs some security checks. - * First, if p.getAddress().isMulticastAddress() is true, this - * method calls the security manager's checkMulticast method - * with p.getAddress() as its argument. If the evaluation of - * that expression is false, this method instead calls the security - * manager's checkConnect method with arguments - * p.getAddress().getHostAddress() and p.getPort() - * . Each call to a security manager method could result in a - * SecurityException if the operation is not allowed. - * - * @param p - * the DatagramPacket to be sent. - * - * @exception IOException - * if an I/O error occurs. - * @exception SecurityException - * if a security manager exists and its - * checkMulticast or checkConnect - * method doesn't allow the send. - * @exception PortUnreachableException - * may be thrown if the socket is connected to a currently - * unreachable destination. Note, there is no guarantee that - * the exception will be thrown. - * @exception java.nio.channels.IllegalBlockingModeException - * if this socket has an associated channel, and the channel - * is in non-blocking mode. - * - * @see DatagramPacket - * @see SecurityManager#checkMulticast(InetAddress) - * @see SecurityManager#checkConnect revised 1.4 spec JSR-51 - */ - void send(DatagramPacket p) throws IOException; - - /** - * Receives a datagram packet from this socket. When this method returns, - * the DatagramPacket's buffer is filled with the data - * received. The datagram packet also contains the sender's IP address, and - * the port number on the sender's machine. - *

- * This method blocks until a datagram is received. The length - * field of the datagram packet object contains the length of the received - * message. If the message is longer than the packet's length, the message - * is truncated. - *

- * If there is a security manager, a packet cannot be received if the - * security manager's checkAccept method does not allow it. - * - * @param p - * the DatagramPacket into which to place the - * incoming data. - * @exception IOException - * if an I/O error occurs. - * @exception SocketTimeoutException - * if setSoTimeout was previously called and the timeout has - * expired. - * @exception PortUnreachableException - * may be thrown if the socket is connected to a currently - * unreachable destination. Note, there is no guarantee that - * the exception will be thrown. - * @exception java.nio.channels.IllegalBlockingModeException - * if this socket has an associated channel, and the channel - * is in non-blocking mode. - * @see DatagramPacket - * @see java.net.DatagramSocket revised 1.4 spec JSR-51 - */ - void receive(DatagramPacket p) throws IOException; - - /** - * Gets the local address to which the socket is bound. - * - *

- * If there is a security manager, its checkConnect method is - * first called with the host address and -1 as its arguments - * to see if the operation is allowed. - * - * @see SecurityManager#checkConnect - * @return the local address to which the socket is bound, or an - * InetAddress representing any local address if either - * the socket is not bound, or the security manager - * checkConnect method does not allow the operation - * @since 1.1 - */ - InetAddress getLocalAddress(); - - /** - * Returns the port number on the local host to which this socket is bound. - * - * @return the port number on the local host to which this socket is bound. - */ - int getLocalPort(); - - /** - * Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. - * With this option set to a non-zero timeout, a call to receive() for this - * DatagramSocket will block for only this amount of time. If the timeout - * expires, a java.net.SocketTimeoutException is raised, though the - * DatagramSocket is still valid. The option must be enabled prior to - * entering the blocking operation to have effect. The timeout must be > 0. - * A timeout of zero is interpreted as an infinite timeout. - * - * @param timeout - * the specified timeout in milliseconds. - * @throws SocketException - * if there is an error in the underlying protocol, such as an - * UDP error. - * @since JDK1.1 - * @see #getSoTimeout() - */ - void setSoTimeout(int timeout) throws SocketException; - - /** - * Retrieve setting for SO_TIMEOUT. 0 returns implies that the option is - * disabled (i.e., timeout of infinity). - * - * @return the setting for SO_TIMEOUT - * @throws SocketException - * if there is an error in the underlying protocol, such as an - * UDP error. - * @since JDK1.1 - * @see #setSoTimeout(int) - */ - int getSoTimeout() throws SocketException; - - /** - * Sets the SO_SNDBUF option to the specified value for this - * DatagramSocket. The SO_SNDBUF option is used by the network - * implementation as a hint to size the underlying network I/O buffers. The - * SO_SNDBUF setting may also be used by the network implementation to - * determine the maximum size of the packet that can be sent on this socket. - *

- * As SO_SNDBUF is a hint, applications that want to verify what size the - * buffer is should call {@link #getSendBufferSize()}. - *

- * Increasing the buffer size may allow multiple outgoing packets to be - * queued by the network implementation when the send rate is high. - *

- * Note: If {@link #send(DatagramPacket)} is used to send a - * DatagramPacket that is larger than the setting of SO_SNDBUF - * then it is implementation specific if the packet is sent or discarded. - * - * @param size - * the size to which to set the send buffer size. This value must - * be greater than 0. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @exception IllegalArgumentException - * if the value is 0 or is negative. - * @see #getSendBufferSize() - */ - void setSendBufferSize(int size) throws SocketException; - - /** - * Get value of the SO_SNDBUF option for this DatagramSocket, that - * is the buffer size used by the platform for output on this - * DatagramSocket. - * - * @return the value of the SO_SNDBUF option for this - * DatagramSocket - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @see #setSendBufferSize - */ - int getSendBufferSize() throws SocketException; - - /** - * Sets the SO_RCVBUF option to the specified value for this - * DatagramSocket. The SO_RCVBUF option is used by the the network - * implementation as a hint to size the underlying network I/O buffers. The - * SO_RCVBUF setting may also be used by the network implementation to - * determine the maximum size of the packet that can be received on this - * socket. - *

- * Because SO_RCVBUF is a hint, applications that want to verify what size - * the buffers were set to should call {@link #getReceiveBufferSize()}. - *

- * Increasing SO_RCVBUF may allow the network implementation to buffer - * multiple packets when packets arrive faster than are being received using - * {@link #receive(DatagramPacket)}. - *

- * Note: It is implementation specific if a packet larger than SO_RCVBUF can - * be received. - * - * @param size - * the size to which to set the receive buffer size. This value - * must be greater than 0. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @exception IllegalArgumentException - * if the value is 0 or is negative. - * @see #getReceiveBufferSize() - */ - void setReceiveBufferSize(int size) throws SocketException; - - /** - * Get value of the SO_RCVBUF option for this DatagramSocket, that - * is the buffer size used by the platform for input on this - * DatagramSocket. - * - * @return the value of the SO_RCVBUF option for this - * DatagramSocket - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @see #setReceiveBufferSize(int) - */ - int getReceiveBufferSize() throws SocketException; - - /** - * Enable/disable the SO_REUSEADDR socket option. - *

- * For UDP sockets it may be necessary to bind more than one socket to the - * same socket address. This is typically for the purpose of receiving - * multicast packets (See {@link java.net.MulticastSocket}). The - * SO_REUSEADDR socket option allows multiple sockets to be bound - * to the same socket address if the SO_REUSEADDR socket option is - * enabled prior to binding the socket using {@link #bind(SocketAddress)}. - *

- * Note: This functionality is not supported by all existing platforms, so - * it is implementation specific whether this option will be ignored or not. - * However, if it is not supported then {@link #getReuseAddress()} will - * always return false. - *

- * When a DatagramSocket is created the initial setting of - * SO_REUSEADDR is disabled. - *

- * The behaviour when SO_REUSEADDR is enabled or disabled after a - * socket is bound (See {@link #isBound()}) is not defined. - * - * @param on - * whether to enable or disable the - * @exception SocketException - * if an error occurs enabling or disabling the - * SO_RESUEADDR socket option, or the socket is - * closed. - * @since 1.4 - * @see #getReuseAddress() - * @see #bind(SocketAddress) - * @see #isBound() - * @see #isClosed() - */ - void setReuseAddress(boolean on) throws SocketException; - - /** - * Tests if SO_REUSEADDR is enabled. - * - * @return a boolean indicating whether or not SO_REUSEADDR is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @since 1.4 - * @see #setReuseAddress(boolean) - */ - boolean getReuseAddress() throws SocketException; - - /** - * Enable/disable SO_BROADCAST. - * - * @param on - * whether or not to have broadcast turned on. - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @since 1.4 - * @see #getBroadcast() - */ - void setBroadcast(boolean on) throws SocketException; - - /** - * Tests if SO_BROADCAST is enabled. - * - * @return a boolean indicating whether or not SO_BROADCAST is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as - * an UDP error. - * @since 1.4 - * @see #setBroadcast(boolean) - */ - boolean getBroadcast() throws SocketException; - - /** - * Sets traffic class or type-of-service octet in the IP datagram header for - * datagrams sent from this DatagramSocket. As the underlying network - * implementation may ignore this value applications should consider it a - * hint. - * - *

- * The tc must be in the range 0 <= tc <= - * 255 or an IllegalArgumentException will be thrown. - *

- * Notes: - *

- * for Internet Protocol v4 the value consists of an octet with precedence - * and TOS fields as detailed in RFC 1349. The TOS field is bitset created - * by bitwise-or'ing values such the following :- - *

- *

    - *
  • IPTOS_LOWCOST (0x02)
  • - *
  • IPTOS_RELIABILITY (0x04)
  • - *
  • IPTOS_THROUGHPUT (0x08)
  • - *
  • IPTOS_LOWDELAY (0x10)
  • - *
- * The last low order bit is always ignored as this corresponds to the MBZ - * (must be zero) bit. - *

- * Setting bits in the precedence field may result in a SocketException - * indicating that the operation is not permitted. - *

- * for Internet Protocol v6 tc is the value that would be - * placed into the sin6_flowinfo field of the IP header. - * - * @param tc - * an int value for the bitset. - * @throws SocketException - * if there is an error setting the traffic class or - * type-of-service - * @since 1.4 - * @see #getTrafficClass - */ - void setTrafficClass(int tc) throws SocketException; - - /** - * Gets traffic class or type-of-service in the IP datagram header for - * packets sent from this DatagramSocket. - *

- * As the underlying network implementation may ignore the traffic class or - * type-of-service set using {@link #setTrafficClass(int)} this method may - * return a different value than was previously set using the - * {@link #setTrafficClass(int)} method on this DatagramSocket. - * - * @return the traffic class or type-of-service already set - * @throws SocketException - * if there is an error obtaining the traffic class or - * type-of-service value. - * @since 1.4 - * @see #setTrafficClass(int) - */ - int getTrafficClass() throws SocketException; - - /** - * Closes this datagram socket. - *

- * Any thread currently blocked in {@link #receive} upon this socket will - * throw a {@link SocketException}. - * - *

- * If this socket has an associated channel then the channel is closed as - * well. - * - * revised 1.4 spec JSR-51 - */ - void close(); - - /** - * Returns whether the socket is closed or not. - * - * @return true if the socket has been closed - * @since 1.4 - */ - boolean isClosed(); - - /** - * Returns the unique {@link DatagramChannel} object - * associated with this datagram socket, if any. - * - *

- * A datagram socket will have a channel if, and only if, the channel itself - * was created via the {@link DatagramChannel#open - * DatagramChannel.open} method. - * - * @return the datagram channel associated with this datagram socket, or - * null if this socket was not created for a channel - * - * @since 1.4 spec JSR-51 - */ - DatagramChannel getChannel(); - -} diff --git a/src/com/barchart/udt/net/IceServerSocket.java b/src/com/barchart/udt/net/IceServerSocket.java deleted file mode 100644 index 6417df2f..00000000 --- a/src/com/barchart/udt/net/IceServerSocket.java +++ /dev/null @@ -1,375 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.nio.channels.ServerSocketChannel; - -/** - * compatibility verification interface - */ -public interface IceServerSocket { - - /** - * - * Binds the ServerSocket to a specific address (IP address and - * port number). - *

- * If the address is null, then the system will pick up an - * ephemeral port and a valid local address to bind the socket. - *

- * - * @param endpoint - * The IP address & port number to bind to. - * @throws IOException - * if the bind operation fails, or if the socket is already - * bound. - * @throws SecurityException - * if a SecurityManager is present and its - * checkListen method doesn't allow the operation. - * @throws IllegalArgumentException - * if endpoint is a SocketAddress subclass not supported by this - * socket - * @since 1.4 - */ - void bind(SocketAddress endpoint) throws IOException; - - /** - * - * Binds the ServerSocket to a specific address (IP address and - * port number). - *

- * If the address is null, then the system will pick up an - * ephemeral port and a valid local address to bind the socket. - *

- * The backlog argument must be a positive value greater than - * 0. If the value passed if equal or less than 0, then the default value - * will be assumed. - * - * @param endpoint - * The IP address & port number to bind to. - * @param backlog - * The listen backlog length. - * @throws IOException - * if the bind operation fails, or if the socket is already - * bound. - * @throws SecurityException - * if a SecurityManager is present and its - * checkListen method doesn't allow the operation. - * @throws IllegalArgumentException - * if endpoint is a SocketAddress subclass not supported by this - * socket - * @since 1.4 - */ - void bind(SocketAddress endpoint, int backlog) throws IOException; - - /** - * Returns the local address of this server socket. - * - * @return the address to which this socket is bound, or null - * if the socket is unbound. - */ - InetAddress getInetAddress(); - - /** - * Returns the port on which this socket is listening. - * - * @return the port number to which this socket is listening or -1 if the - * socket is not bound yet. - */ - int getLocalPort(); - - /** - * Returns the address of the endpoint this socket is bound to, or - * null if it is not bound yet. - * - * @return a SocketAddress representing the local endpoint of - * this socket, or null if it is not bound yet. - * @see #getInetAddress() - * @see #getLocalPort() - * @see #bind(SocketAddress) - * @since 1.4 - */ - - SocketAddress getLocalSocketAddress(); - - /** - * Listens for a connection to be made to this socket and accepts it. The - * method blocks until a connection is made. - * - *

- * A new Socket s is created and, if there is a security - * manager, the security manager's checkAccept method is called - * with s.getInetAddress().getHostAddress() and - * s.getPort() as its arguments to ensure the operation is - * allowed. This could result in a SecurityException. - * - * @exception IOException - * if an I/O error occurs when waiting for a connection. - * @exception SecurityException - * if a security manager exists and its - * checkAccept method doesn't allow the - * operation. - * @exception SocketTimeoutException - * if a timeout was previously set with setSoTimeout and the - * timeout has been reached. - * @exception java.nio.channels.IllegalBlockingModeException - * if this socket has an associated channel, the channel is - * in non-blocking mode, and there is no connection ready to - * be accepted - * - * @return the new Socket - * @see SecurityManager#checkAccept revised 1.4 spec JSR-51 - */ - Socket accept() throws IOException; - - /** - * Closes this socket. - * - * Any thread currently blocked in {@link #accept()} will throw a - * {@link SocketException}. - * - *

- * If this socket has an associated channel then the channel is closed as - * well. - * - * @exception IOException - * if an I/O error occurs when closing the socket. revised - * 1.4 spec JSR-51 - */ - void close() throws IOException; - - /** - * Returns the unique {@link ServerSocketChannel} object - * associated with this socket, if any. - * - *

- * A server socket will have a channel if, and only if, the channel itself - * was created via the {@link ServerSocketChannel#open - * ServerSocketChannel.open} method. - * - * @return the server-socket channel associated with this socket, or - * null if this socket was not created for a channel - * - * @since 1.4 spec JSR-51 - */ - ServerSocketChannel getChannel(); - - /** - * Returns the binding state of the ServerSocket. - * - * @return true if the ServerSocket succesfuly bound to an address - * @since 1.4 - */ - boolean isBound(); - - /** - * Returns the closed state of the ServerSocket. - * - * @return true if the socket has been closed - * @since 1.4 - */ - boolean isClosed(); - - /** - * Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. - * With this option set to a non-zero timeout, a call to accept() for this - * ServerSocket will block for only this amount of time. If the timeout - * expires, a java.net.SocketTimeoutException is raised, though the - * ServerSocket is still valid. The option must be enabled prior to - * entering the blocking operation to have effect. The timeout must be > 0. - * A timeout of zero is interpreted as an infinite timeout. - * - * @param timeout - * the specified timeout, in milliseconds - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since JDK1.1 - * @see #getSoTimeout() - */ - void setSoTimeout(int timeout) throws SocketException; - - /** - * Retrieve setting for SO_TIMEOUT. 0 returns implies that the option is - * disabled (i.e., timeout of infinity). - * - * @return the SO_TIMEOUT value - * @exception IOException - * if an I/O error occurs - * @since JDK1.1 - * @see #setSoTimeout(int) - */ - int getSoTimeout() throws IOException; - - /** - * Enable/disable the SO_REUSEADDR socket option. - *

- * When a TCP connection is closed the connection may remain in a timeout - * state for a period of time after the connection is closed (typically - * known as the TIME_WAIT state or 2MSL wait state). For - * applications using a well known socket address or port it may not be - * possible to bind a socket to the required SocketAddress if there - * is a connection in the timeout state involving the socket address or - * port. - *

- * Enabling SO_REUSEADDR prior to binding the socket using - * {@link #bind(SocketAddress)} allows the socket to be bound even though a - * previous connection is in a timeout state. - *

- * When a ServerSocket is created the initial setting of - * SO_REUSEADDR is not defined. Applications can use - * {@link #getReuseAddress()} to determine the initial setting of - * SO_REUSEADDR. - *

- * The behaviour when SO_REUSEADDR is enabled or disabled after a - * socket is bound (See {@link #isBound()}) is not defined. - * - * @param on - * whether to enable or disable the socket option - * @exception SocketException - * if an error occurs enabling or disabling the - * SO_RESUEADDR socket option, or the socket is - * closed. - * @since 1.4 - * @see #getReuseAddress() - * @see #bind(SocketAddress) - * @see #isBound() - * @see #isClosed() - */ - void setReuseAddress(boolean on) throws SocketException; - - /** - * Tests if SO_REUSEADDR is enabled. - * - * @return a boolean indicating whether or not SO_REUSEADDR is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since 1.4 - * @see #setReuseAddress(boolean) - */ - boolean getReuseAddress() throws SocketException; - - /** - * Returns the implementation address and implementation port of this socket - * as a String. - * - * @return a string representation of this socket. - */ - @Override - String toString(); - - /** - * Sets a default proposed value for the SO_RCVBUF option for sockets - * accepted from this ServerSocket. The value actually set in the - * accepted socket must be determined by calling - * {@link Socket#getReceiveBufferSize()} after the socket is returned by - * {@link #accept()}. - *

- * The value of SO_RCVBUF is used both to set the size of the internal - * socket receive buffer, and to set the size of the TCP receive window that - * is advertized to the remote peer. - *

- * It is possible to change the value subsequently, by calling - * {@link Socket#setReceiveBufferSize(int)}. However, if the application - * wishes to allow a receive window larger than 64K bytes, as defined by - * RFC1323 then the proposed value must be set in the ServerSocket - * before it is bound to a local address. This implies, that the - * ServerSocket must be created with the no-argument constructor, then - * setReceiveBufferSize() must be called and lastly the ServerSocket is - * bound to an address by calling bind(). - *

- * Failure to do this will not cause an error, and the buffer size may be - * set to the requested value but the TCP receive window in sockets accepted - * from this ServerSocket will be no larger than 64K bytes. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @param size - * the size to which to set the receive buffer size. This value - * must be greater than 0. - * - * @exception IllegalArgumentException - * if the value is 0 or is negative. - * - * @since 1.4 - * @see #getReceiveBufferSize - */ - void setReceiveBufferSize(int size) throws SocketException; - - /** - * Gets the value of the SO_RCVBUF option for this ServerSocket, - * that is the proposed buffer size that will be used for Sockets accepted - * from this ServerSocket. - * - *

- * Note, the value actually set in the accepted socket is determined by - * calling {@link Socket#getReceiveBufferSize()}. - * - * @return the value of the SO_RCVBUF option for this Socket. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @see #setReceiveBufferSize(int) - * @since 1.4 - */ - int getReceiveBufferSize() throws SocketException; - - /** - * Sets performance preferences for this ServerSocket. - * - *

- * Sockets use the TCP/IP protocol by default. Some implementations may - * offer alternative protocols which have different performance - * characteristics than TCP/IP. This method allows the application to - * express its own preferences as to how these tradeoffs should be made when - * the implementation chooses from the available protocols. - * - *

- * Performance preferences are described by three integers whose values - * indicate the relative importance of short connection time, low latency, - * and high bandwidth. The absolute values of the integers are irrelevant; - * in order to choose a protocol the values are simply compared, with larger - * values indicating stronger preferences. If the application prefers short - * connection time over both low latency and high bandwidth, for example, - * then it could invoke this method with the values (1, 0, 0). If - * the application prefers high bandwidth above low latency, and low latency - * above short connection time, then it could invoke this method with the - * values (0, 1, 2). - * - *

- * Invoking this method after this socket has been bound will have no - * effect. This implies that in order to use this capability requires the - * socket to be created with the no-argument constructor. - * - * @param connectionTime - * An int expressing the relative importance of a short - * connection time - * - * @param latency - * An int expressing the relative importance of low - * latency - * - * @param bandwidth - * An int expressing the relative importance of high - * bandwidth - * - * @since 1.5 - */ - void setPerformancePreferences(int connectionTime, int latency, int bandwidth); - -} diff --git a/src/com/barchart/udt/net/IceSocket.java b/src/com/barchart/udt/net/IceSocket.java deleted file mode 100644 index 73f723b8..00000000 --- a/src/com/barchart/udt/net/IceSocket.java +++ /dev/null @@ -1,791 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.ServerSocket; -import java.net.SocketAddress; -import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.nio.channels.SocketChannel; - -/** - * compatibility verification interface - */ -public interface IceSocket { - - /** - * Connects this socket to the server. - * - * @param endpoint - * the SocketAddress - * @throws IOException - * if an error occurs during the connection - * @throws java.nio.channels.IllegalBlockingModeException - * if this socket has an associated channel, and the channel is - * in non-blocking mode - * @throws IllegalArgumentException - * if endpoint is null or is a SocketAddress subclass not - * supported by this socket - * @since 1.4 spec JSR-51 - */ - void connect(SocketAddress endpoint) throws IOException; - - /** - * Connects this socket to the server with a specified timeout value. A - * timeout of zero is interpreted as an infinite timeout. The connection - * will then block until established or an error occurs. - * - * @param endpoint - * the SocketAddress - * @param timeout - * the timeout value to be used in milliseconds. - * @throws IOException - * if an error occurs during the connection - * @throws SocketTimeoutException - * if timeout expires before connecting - * @throws java.nio.channels.IllegalBlockingModeException - * if this socket has an associated channel, and the channel is - * in non-blocking mode - * @throws IllegalArgumentException - * if endpoint is null or is a SocketAddress subclass not - * supported by this socket - * @since 1.4 spec JSR-51 - */ - void connect(SocketAddress endpoint, int timeout) throws IOException; - - /** - * Binds the socket to a local address. - *

- * If the address is null, then the system will pick up an - * ephemeral port and a valid local address to bind the socket. - * - * @param bindpoint - * the SocketAddress to bind to - * @throws IOException - * if the bind operation fails, or if the socket is already - * bound. - * @throws IllegalArgumentException - * if bindpoint is a SocketAddress subclass not supported by - * this socket - * - * @since 1.4 - * @see #isBound - */ - void bind(SocketAddress bindpoint) throws IOException; - - /** - * Returns the address to which the socket is connected. - * - * @return the remote IP address to which this socket is connected, or - * null if the socket is not connected. - */ - InetAddress getInetAddress(); - - /** - * Gets the local address to which the socket is bound. - * - * @return the local address to which the socket is bound or - * InetAddress.anyLocalAddress() if the socket is not - * bound yet. - * @since JDK1.1 - */ - InetAddress getLocalAddress(); - - /** - * Returns the remote port to which this socket is connected. - * - * @return the remote port number to which this socket is connected, or 0 if - * the socket is not connected yet. - */ - int getPort(); - - /** - * Returns the local port to which this socket is bound. - * - * @return the local port number to which this socket is bound or -1 if the - * socket is not bound yet. - */ - int getLocalPort(); - - /** - * Returns the address of the endpoint this socket is connected to, or - * null if it is unconnected. - * - * @return a SocketAddress reprensenting the remote endpoint of - * this socket, or null if it is not connected yet. - * @see #getInetAddress() - * @see #getPort() - * @see #connect(SocketAddress, int) - * @see #connect(SocketAddress) - * @since 1.4 - */ - SocketAddress getRemoteSocketAddress(); - - /** - * Returns the address of the endpoint this socket is bound to, or - * null if it is not bound yet. - * - * @return a SocketAddress representing the local endpoint of - * this socket, or null if it is not bound yet. - * @see #getLocalAddress() - * @see #getLocalPort() - * @see #bind(SocketAddress) - * @since 1.4 - */ - - SocketAddress getLocalSocketAddress(); - - /** - * Returns the unique {@link SocketChannel SocketChannel} - * object associated with this socket, if any. - * - *

- * A socket will have a channel if, and only if, the channel itself was - * created via the {@link SocketChannel#open - * SocketChannel.open} or - * {@link java.nio.channels.ServerSocketChannel#accept - * ServerSocketChannel.accept} methods. - * - * @return the socket channel associated with this socket, or null - * if this socket was not created for a channel - * - * @since 1.4 spec JSR-51 - */ - SocketChannel getChannel(); - - /** - * Returns an input stream for this socket. - * - *

- * If this socket has an associated channel then the resulting input stream - * delegates all of its operations to the channel. If the channel is in - * non-blocking mode then the input stream's read operations will - * throw an {@link java.nio.channels.IllegalBlockingModeException}. - * - *

- * Under abnormal conditions the underlying connection may be broken by the - * remote host or the network software (for example a connection reset in - * the case of TCP connections). When a broken connection is detected by the - * network software the following applies to the returned input stream :- - * - *

    - * - *
  • - *

    - * The network software may discard bytes that are buffered by the socket. - * Bytes that aren't discarded by the network software can be read using - * {@link InputStream#read read}. - * - *

  • - *

    - * If there are no bytes buffered on the socket, or all buffered bytes have - * been consumed by {@link InputStream#read read}, then all - * subsequent calls to {@link InputStream#read read} will throw an - * {@link IOException IOException}. - * - *

  • - *

    - * If there are no bytes buffered on the socket, and the socket has not been - * closed using {@link #close close}, then - * {@link InputStream#available available} will return - * 0. - * - *

- * - *

- * Closing the returned {@link InputStream InputStream} will close - * the associated socket. - * - * @return an input stream for reading bytes from this socket. - * @exception IOException - * if an I/O error occurs when creating the input stream, the - * socket is closed, the socket is not connected, or the - * socket input has been shutdown using - * {@link #shutdownInput()} - * - * revised 1.4 spec JSR-51 - */ - InputStream getInputStream() throws IOException; - - /** - * Returns an output stream for this socket. - * - *

- * If this socket has an associated channel then the resulting output stream - * delegates all of its operations to the channel. If the channel is in - * non-blocking mode then the output stream's write operations will - * throw an {@link java.nio.channels.IllegalBlockingModeException}. - * - *

- * Closing the returned {@link OutputStream OutputStream} will close - * the associated socket. - * - * @return an output stream for writing bytes to this socket. - * @exception IOException - * if an I/O error occurs when creating the output stream or - * if the socket is not connected. revised 1.4 spec JSR-51 - */ - OutputStream getOutputStream() throws IOException; - - /** - * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). - * - * @param on - * true to enable TCP_NODELAY, false to - * disable. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @since JDK1.1 - * - * @see #getTcpNoDelay() - */ - void setTcpNoDelay(boolean on) throws SocketException; - - /** - * Tests if TCP_NODELAY is enabled. - * - * @return a boolean indicating whether or not TCP_NODELAY is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since JDK1.1 - * @see #setTcpNoDelay(boolean) - */ - boolean getTcpNoDelay() throws SocketException; - - /** - * Enable/disable SO_LINGER with the specified linger time in seconds. The - * maximum timeout value is platform specific. - * - * The setting only affects socket close. - * - * @param on - * whether or not to linger on. - * @param linger - * how long to linger for, if on is true. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @exception IllegalArgumentException - * if the linger value is negative. - * @since JDK1.1 - * @see #getSoLinger() - */ - void setSoLinger(boolean on, int linger) throws SocketException; - - /** - * Returns setting for SO_LINGER. -1 returns implies that the option is - * disabled. - * - * The setting only affects socket close. - * - * @return the setting for SO_LINGER. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since JDK1.1 - * @see #setSoLinger(boolean, int) - */ - int getSoLinger() throws SocketException; - - /** - * Send one byte of urgent data on the socket. The byte to be sent is the - * lowest eight bits of the data parameter. The urgent byte is sent after - * any preceding writes to the socket OutputStream and before any future - * writes to the OutputStream. - * - * @param data - * The byte of data to send - * @exception IOException - * if there is an error sending the data. - * @since 1.4 - */ - void sendUrgentData(int data) throws IOException; - - /** - * Enable/disable OOBINLINE (receipt of TCP urgent data) - * - * By default, this option is disabled and TCP urgent data received on a - * socket is silently discarded. If the user wishes to receive urgent data, - * then this option must be enabled. When enabled, urgent data is received - * inline with normal data. - *

- * Note, only limited support is provided for handling incoming urgent data. - * In particular, no notification of incoming urgent data is provided and - * there is no capability to distinguish between normal data and urgent data - * unless provided by a higher level protocol. - * - * @param on - * true to enable OOBINLINE, false to - * disable. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @since 1.4 - * - * @see #getOOBInline() - */ - void setOOBInline(boolean on) throws SocketException; - - /** - * Tests if OOBINLINE is enabled. - * - * @return a boolean indicating whether or not OOBINLINE is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since 1.4 - * @see #setOOBInline(boolean) - */ - boolean getOOBInline() throws SocketException; - - /** - * Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. - * With this option set to a non-zero timeout, a read() call on the - * InputStream associated with this Socket will block for only this amount - * of time. If the timeout expires, a java.net.SocketTimeoutException - * is raised, though the Socket is still valid. The option must be - * enabled prior to entering the blocking operation to have effect. The - * timeout must be > 0. A timeout of zero is interpreted as an infinite - * timeout. - * - * @param timeout - * the specified timeout, in milliseconds. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since JDK 1.1 - * @see #getSoTimeout() - */ - void setSoTimeout(int timeout) throws SocketException; - - /** - * Returns setting for SO_TIMEOUT. 0 returns implies that the option is - * disabled (i.e., timeout of infinity). - * - * @return the setting for SO_TIMEOUT - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since JDK1.1 - * @see #setSoTimeout(int) - */ - int getSoTimeout() throws SocketException; - - /** - * Sets the SO_SNDBUF option to the specified value for this Socket - * . The SO_SNDBUF option is used by the platform's networking code as a - * hint for the size to set the underlying network I/O buffers. - * - *

- * Because SO_SNDBUF is a hint, applications that want to verify what size - * the buffers were set to should call {@link #getSendBufferSize()}. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @param size - * the size to which to set the send buffer size. This value must - * be greater than 0. - * - * @exception IllegalArgumentException - * if the value is 0 or is negative. - * - * @see #getSendBufferSize() - * @since 1.2 - */ - void setSendBufferSize(int size) throws SocketException; - - /** - * Get value of the SO_SNDBUF option for this Socket, that is the - * buffer size used by the platform for output on this Socket. - * - * @return the value of the SO_SNDBUF option for this Socket. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @see #setSendBufferSize(int) - * @since 1.2 - */ - int getSendBufferSize() throws SocketException; - - /** - * Sets the SO_RCVBUF option to the specified value for this Socket - * . The SO_RCVBUF option is used by the platform's networking code as a - * hint for the size to set the underlying network I/O buffers. - * - *

- * Increasing the receive buffer size can increase the performance of - * network I/O for high-volume connection, while decreasing it can help - * reduce the backlog of incoming data. - * - *

- * Because SO_RCVBUF is a hint, applications that want to verify what size - * the buffers were set to should call {@link #getReceiveBufferSize()}. - * - *

- * The value of SO_RCVBUF is also used to set the TCP receive window that is - * advertized to the remote peer. Generally, the window size can be modified - * at any time when a socket is connected. However, if a receive window - * larger than 64K is required then this must be requested before the - * socket is connected to the remote peer. There are two cases to be aware - * of: - *

- *

    - *
  1. For sockets accepted from a ServerSocket, this must be done by - * calling {@link ServerSocket#setReceiveBufferSize(int)} before the - * ServerSocket is bound to a local address. - *

  2. - *
  3. For client sockets, setReceiveBufferSize() must be called before - * connecting the socket to its remote peer. - *

  4. - *
- * - * @param size - * the size to which to set the receive buffer size. This value - * must be greater than 0. - * - * @exception IllegalArgumentException - * if the value is 0 or is negative. - * - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * - * @see #getReceiveBufferSize() - * @see ServerSocket#setReceiveBufferSize(int) - * @since 1.2 - */ - void setReceiveBufferSize(int size) throws SocketException; - - /** - * Gets the value of the SO_RCVBUF option for this Socket, that is - * the buffer size used by the platform for input on this Socket. - * - * @return the value of the SO_RCVBUF option for this Socket. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @see #setReceiveBufferSize(int) - * @since 1.2 - */ - int getReceiveBufferSize() throws SocketException; - - /** - * Enable/disable SO_KEEPALIVE. - * - * @param on - * whether or not to have socket keep alive turned on. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since 1.3 - * @see #getKeepAlive() - */ - void setKeepAlive(boolean on) throws SocketException; - - /** - * Tests if SO_KEEPALIVE is enabled. - * - * @return a boolean indicating whether or not SO_KEEPALIVE is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since 1.3 - * @see #setKeepAlive(boolean) - */ - boolean getKeepAlive() throws SocketException; - - /** - * Sets traffic class or type-of-service octet in the IP header for packets - * sent from this Socket. As the underlying network implementation may - * ignore this value applications should consider it a hint. - * - *

- * The tc must be in the range 0 <= tc <= - * 255 or an IllegalArgumentException will be thrown. - *

- * Notes: - *

- * For Internet Protocol v4 the value consists of an octet with precedence - * and TOS fields as detailed in RFC 1349. The TOS field is bitset created - * by bitwise-or'ing values such the following :- - *

- *

    - *
  • IPTOS_LOWCOST (0x02)
  • - *
  • IPTOS_RELIABILITY (0x04)
  • - *
  • IPTOS_THROUGHPUT (0x08)
  • - *
  • IPTOS_LOWDELAY (0x10)
  • - *
- * The last low order bit is always ignored as this corresponds to the MBZ - * (must be zero) bit. - *

- * Setting bits in the precedence field may result in a SocketException - * indicating that the operation is not permitted. - *

- * As RFC 1122 section 4.2.4.2 indicates, a compliant TCP implementation - * should, but is not required to, let application change the TOS field - * during the lifetime of a connection. So whether the type-of-service field - * can be changed after the TCP connection has been established depends on - * the implementation in the underlying platform. Applications should not - * assume that they can change the TOS field after the connection. - *

- * For Internet Protocol v6 tc is the value that would be - * placed into the sin6_flowinfo field of the IP header. - * - * @param tc - * an int value for the bitset. - * @throws SocketException - * if there is an error setting the traffic class or - * type-of-service - * @since 1.4 - * @see #getTrafficClass - */ - void setTrafficClass(int tc) throws SocketException; - - /** - * Gets traffic class or type-of-service in the IP header for packets sent - * from this Socket - *

- * As the underlying network implementation may ignore the traffic class or - * type-of-service set using {@link #setTrafficClass(int)} this method may - * return a different value than was previously set using the - * {@link #setTrafficClass(int)} method on this Socket. - * - * @return the traffic class or type-of-service already set - * @throws SocketException - * if there is an error obtaining the traffic class or - * type-of-service value. - * @since 1.4 - * @see #setTrafficClass(int) - */ - int getTrafficClass() throws SocketException; - - /** - * Enable/disable the SO_REUSEADDR socket option. - *

- * When a TCP connection is closed the connection may remain in a timeout - * state for a period of time after the connection is closed (typically - * known as the TIME_WAIT state or 2MSL wait state). For - * applications using a well known socket address or port it may not be - * possible to bind a socket to the required SocketAddress if there - * is a connection in the timeout state involving the socket address or - * port. - *

- * Enabling SO_REUSEADDR prior to binding the socket using - * {@link #bind(SocketAddress)} allows the socket to be bound even though a - * previous connection is in a timeout state. - *

- * When a Socket is created the initial setting of - * SO_REUSEADDR is disabled. - *

- * The behaviour when SO_REUSEADDR is enabled or disabled after a - * socket is bound (See {@link #isBound()}) is not defined. - * - * @param on - * whether to enable or disable the socket option - * @exception SocketException - * if an error occurs enabling or disabling the - * SO_RESUEADDR socket option, or the socket is - * closed. - * @since 1.4 - * @see #getReuseAddress() - * @see #bind(SocketAddress) - * @see #isClosed() - * @see #isBound() - */ - void setReuseAddress(boolean on) throws SocketException; - - /** - * Tests if SO_REUSEADDR is enabled. - * - * @return a boolean indicating whether or not SO_REUSEADDR is - * enabled. - * @exception SocketException - * if there is an error in the underlying protocol, such as a - * TCP error. - * @since 1.4 - * @see #setReuseAddress(boolean) - */ - boolean getReuseAddress() throws SocketException; - - /** - * Closes this socket. - *

- * Any thread currently blocked in an I/O operation upon this socket will - * throw a {@link SocketException}. - *

- * Once a socket has been closed, it is not available for further networking - * use (i.e. can't be reconnected or rebound). A new socket needs to be - * created. - * - *

- * Closing this socket will also close the socket's - * {@link InputStream InputStream} and {@link OutputStream - * OutputStream}. - * - *

- * If this socket has an associated channel then the channel is closed as - * well. - * - * @exception IOException - * if an I/O error occurs when closing this socket. revised - * 1.4 spec JSR-51 - * @see #isClosed - */ - void close() throws IOException; - - /** - * Places the input stream for this socket at "end of stream". Any data sent - * to the input stream side of the socket is acknowledged and then silently - * discarded. - *

- * If you read from a socket input stream after invoking shutdownInput() on - * the socket, the stream will return EOF. - * - * @exception IOException - * if an I/O error occurs when shutting down this socket. - * - * @since 1.3 - * @see java.net.Socket#shutdownOutput() - * @see java.net.Socket#close() - * @see java.net.Socket#setSoLinger(boolean, int) - * @see #isInputShutdown - */ - void shutdownInput() throws IOException; - - /** - * Disables the output stream for this socket. For a TCP socket, any - * previously written data will be sent followed by TCP's normal connection - * termination sequence. - * - * If you write to a socket output stream after invoking shutdownOutput() on - * the socket, the stream will throw an IOException. - * - * @exception IOException - * if an I/O error occurs when shutting down this socket. - * - * @since 1.3 - * @see java.net.Socket#shutdownInput() - * @see java.net.Socket#close() - * @see java.net.Socket#setSoLinger(boolean, int) - * @see #isOutputShutdown - */ - void shutdownOutput() throws IOException; - - /** - * Converts this socket to a String. - * - * @return a string representation of this socket. - */ - @Override - String toString(); - - /** - * Returns the connection state of the socket. - * - * @return true if the socket successfuly connected to a server - * @since 1.4 - */ - boolean isConnected(); - - /** - * Returns the binding state of the socket. - * - * @return true if the socket successfuly bound to an address - * @since 1.4 - * @see #bind - */ - boolean isBound(); - - /** - * Returns the closed state of the socket. - * - * @return true if the socket has been closed - * @since 1.4 - * @see #close - */ - boolean isClosed(); - - /** - * Returns whether the read-half of the socket connection is closed. - * - * @return true if the input of the socket has been shutdown - * @since 1.4 - * @see #shutdownInput - */ - boolean isInputShutdown(); - - /** - * Returns whether the write-half of the socket connection is closed. - * - * @return true if the output of the socket has been shutdown - * @since 1.4 - * @see #shutdownOutput - */ - boolean isOutputShutdown(); - - /** - * Sets performance preferences for this socket. - * - *

- * Sockets use the TCP/IP protocol by default. Some implementations may - * offer alternative protocols which have different performance - * characteristics than TCP/IP. This method allows the application to - * express its own preferences as to how these tradeoffs should be made when - * the implementation chooses from the available protocols. - * - *

- * Performance preferences are described by three integers whose values - * indicate the relative importance of short connection time, low latency, - * and high bandwidth. The absolute values of the integers are irrelevant; - * in order to choose a protocol the values are simply compared, with larger - * values indicating stronger preferences. Negative values represent a lower - * priority than positive values. If the application prefers short - * connection time over both low latency and high bandwidth, for example, - * then it could invoke this method with the values (1, 0, 0). If - * the application prefers high bandwidth above low latency, and low latency - * above short connection time, then it could invoke this method with the - * values (0, 1, 2). - * - *

- * Invoking this method after this socket has been connected will have no - * effect. - * - * @param connectionTime - * An int expressing the relative importance of a short - * connection time - * - * @param latency - * An int expressing the relative importance of low - * latency - * - * @param bandwidth - * An int expressing the relative importance of high - * bandwidth - * - * @since 1.5 - */ - void setPerformancePreferences(int connectionTime, int latency, int bandwidth); - -} diff --git a/src/com/barchart/udt/net/NetInputStreamUDT.java b/src/com/barchart/udt/net/NetInputStreamUDT.java deleted file mode 100644 index d4036415..00000000 --- a/src/com/barchart/udt/net/NetInputStreamUDT.java +++ /dev/null @@ -1,146 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.channels.IllegalBlockingModeException; - -import com.barchart.udt.ErrorUDT; -import com.barchart.udt.SocketUDT; - -/** - * {@link InputStream} implementation for UDT sockets. - */ -public class NetInputStreamUDT extends InputStream { - - protected final SocketUDT socketUDT; - - /** - * - * @param socketUDT - * The UDT socket. - */ - public NetInputStreamUDT(final SocketUDT socketUDT) { - - if (!socketUDT.isBlocking()) { - throw new IllegalBlockingModeException(); - } - - this.socketUDT = socketUDT; - - } - - @Override - public int read() throws IOException { - - /* - * Here's the contract from the JavaDoc on this for SocketChannel: - * - * A read operation might not fill the buffer, and in fact it might not - * read any bytes at all. Whether or not it does so depends upon the - * nature and state of the channel. A socket channel in non-blocking - * mode, for example, cannot read any more bytes than are immediately - * available from the socket's input buffer; similarly, a file channel - * cannot read any more bytes than remain in the file. It is guaranteed, - * however, that if a channel is in blocking mode and there is at least - * one byte remaining in the buffer then this method will block until at - * least one byte is read. - * - * Long story short: This UDT InputStream should only ever be created - * when the SocketChannel's in blocking mode, and when it's in blocking - * mode the SocketChannel read call below will block just like we need - * it too. - */ - - final byte[] data = new byte[1]; - - final int count = read(data); - - assert count == 1; - - return data[0] & 0xFF; - - } - - @Override - public int read(final byte[] bytes) throws IOException { - - return read(bytes, 0, bytes.length); - - } - - @SuppressWarnings("serial") - @Override - public int read(final byte[] bytes, final int off, final int len) - throws IOException { - - final int count = socketUDT.receive(bytes, off, off + len); - - if (count > 0) { - assert count <= len; - return count; - } - - if (count == 0) { - throw new ExceptionReceiveUDT(socketUDT.id(), - ErrorUDT.USER_DEFINED_MESSAGE, "UDT receive time out") { - }; - } - - throw new IllegalStateException("should not happen"); - - } - - @Override - public void close() throws IOException { - socketUDT.close(); - } - - @Override - public int available() throws IOException { - // This is the default InputStream return value. - // The java/net/SocketInputStream.java implementation delegates to - // the native implementation, which returns 0 on at least some OSes. - return 0; - } - - @Override - public long skip(final long numbytes) throws IOException { - if (numbytes <= 0) { - return 0; - } - long n = numbytes; - final int buflen = (int) Math.min(1024, n); - final byte data[] = new byte[buflen]; - while (n > 0) { - final int r = read(data, 0, (int) Math.min(buflen, n)); - if (r < 0) { - break; - } - n -= r; - } - return numbytes - n; - } - - @Override - public void mark(final int readlimit) { - throw new UnsupportedOperationException("mark not supported"); - } - - @Override - public void reset() throws IOException { - throw new UnsupportedOperationException("reset not supported"); - } - - @Override - public boolean markSupported() { - return false; - } - -} diff --git a/src/com/barchart/udt/net/NetOutputStreamUDT.java b/src/com/barchart/udt/net/NetOutputStreamUDT.java deleted file mode 100644 index 74f0b7dc..00000000 --- a/src/com/barchart/udt/net/NetOutputStreamUDT.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - *

- * All rights reserved. Licensed under the OSI BSD License. - *

- * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import com.barchart.udt.ErrorUDT; -import com.barchart.udt.SocketUDT; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.channels.IllegalBlockingModeException; - -/** - * {@link OutputStream} for UDT sockets. - */ -public -class NetOutputStreamUDT extends OutputStream { - - protected final SocketUDT socketUDT; - - /** - * @param socketUDT The UDT socket. - */ - public - NetOutputStreamUDT(final SocketUDT socketUDT) { - - if (!socketUDT.isBlocking()) { - throw new IllegalBlockingModeException(); - } - - this.socketUDT = socketUDT; - - } - - @Override - public - void write(final int b) throws IOException { - - // Just cast it -- this is the same thing SocketOutputStream does. - final byte[] bytes = {(byte) b}; - - write(bytes); - - } - - @Override - public - void write(final byte[] bytes) throws IOException { - - write(bytes, 0, bytes.length); - - } - - @Override - public - void write(final byte[] bytes, final int off, final int len) throws IOException { - - int bytesRemaining = len; - - while (bytesRemaining > 0) { - - final int count = socketUDT.send(bytes, off + len - bytesRemaining, off + len); - - if (count > 0) { - bytesRemaining -= count; - continue; - } - - if (count == 0) { - throw new ExceptionSendUDT(socketUDT.id(), ErrorUDT.USER_DEFINED_MESSAGE, "UDT send time out"); - } - - throw new IllegalStateException("Socket has been changed to non-blocking"); - } - - } - - @Override - public - void close() throws IOException { - socketUDT.close(); - } - - @Override - public - void flush() throws IOException { - socketUDT.flush(); - } -} diff --git a/src/com/barchart/udt/net/NetServerSocketUDT.java b/src/com/barchart/udt/net/NetServerSocketUDT.java deleted file mode 100644 index 935ab1ff..00000000 --- a/src/com/barchart/udt/net/NetServerSocketUDT.java +++ /dev/null @@ -1,151 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.SocketException; -import java.nio.channels.ServerSocketChannel; - -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; - -/** - * {@link ServerSocket} - like wrapper for {@link SocketUDT} - */ -public class NetServerSocketUDT extends ServerSocket implements - IceServerSocket, IceCommon { - - protected final SocketUDT socketUDT; - - /** uses {@link TypeUDT#STREAM} socket in blocking mode */ - public NetServerSocketUDT() throws IOException { - this(new SocketUDT(TypeUDT.STREAM)); - this.socketUDT.setBlocking(true); - } - - /** uses provided socket keeping blocking mode */ - protected NetServerSocketUDT(final SocketUDT socketUDT) throws IOException { - this.socketUDT = socketUDT; - } - - @Override - public Socket accept() throws IOException { - final SocketUDT clientUDT = socketUDT.accept(); - return new NetSocketUDT(clientUDT); - } - - @Override - public void bind(final SocketAddress endpoint) throws IOException { - final int backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE; - bind(endpoint, backlog); - } - - /** - * NOTE: bind() means listen() for UDT server socket - */ - @Override - public void bind(SocketAddress bindpoint, int backlog) throws IOException { - if (bindpoint == null) { - bindpoint = new InetSocketAddress(0); - } - if (backlog <= 0) { - backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE; - } - socketUDT.bind((InetSocketAddress) bindpoint); - socketUDT.listen(backlog); - } - - @Override - public void close() throws IOException { - socketUDT.close(); - } - - @Override - public ServerSocketChannel getChannel() { - throw new UnsupportedOperationException("feature not available"); - } - - @Override - public InetAddress getInetAddress() { - return socketUDT.getLocalInetAddress(); - } - - @Override - public int getLocalPort() { - return socketUDT.getLocalInetPort(); - } - - @Override - public SocketAddress getLocalSocketAddress() { - try { - return socketUDT.getLocalSocketAddress(); - } catch (final ExceptionUDT e) { - return null; - } - } - - @Override - public int getReceiveBufferSize() throws SocketException { - return socketUDT.getReceiveBufferSize(); - } - - @Override - public boolean getReuseAddress() throws SocketException { - return socketUDT.getReuseAddress(); - } - - @Override - public int getSoTimeout() throws IOException { - return socketUDT.getSoTimeout(); - } - - @Override - public boolean isBound() { - return socketUDT.isBound(); - } - - @Override - public boolean isClosed() { - return socketUDT.isClosed(); - } - - @Override - public void setPerformancePreferences(final int connectionTime, - final int latency, final int bandwidth) { - throw new UnsupportedOperationException("feature not available"); - } - - // NOTE: set both send and receive, since they are inherited on accept() - @Override - public void setReceiveBufferSize(final int size) throws SocketException { - socketUDT.setReceiveBufferSize(size); - socketUDT.setSendBufferSize(size); - } - - @Override - public void setReuseAddress(final boolean on) throws SocketException { - socketUDT.setReuseAddress(on); - } - - @Override - public void setSoTimeout(final int timeout) throws SocketException { - socketUDT.setSoTimeout(timeout); - } - - @Override - public SocketUDT socketUDT() { - return socketUDT; - } - -} diff --git a/src/com/barchart/udt/net/NetSocketUDT.java b/src/com/barchart/udt/net/NetSocketUDT.java deleted file mode 100644 index 27a3af48..00000000 --- a/src/com/barchart/udt/net/NetSocketUDT.java +++ /dev/null @@ -1,282 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.net; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.SocketException; -import java.nio.channels.SocketChannel; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; -import com.barchart.udt.anno.ThreadSafe; - -/** - * {@link Socket} - like wrapper for {@link SocketUDT} - */ -public class NetSocketUDT extends Socket implements IceSocket, IceCommon { - - private final Logger log = LoggerFactory.getLogger(getClass()); - - @ThreadSafe("this") - protected InputStream inputStream; - @ThreadSafe("this") - protected OutputStream outputStream; - - protected final SocketUDT socketUDT; - - /** uses {@link TypeUDT#STREAM} socket in blocking mode */ - public NetSocketUDT() throws ExceptionUDT { - this(new SocketUDT(TypeUDT.STREAM)); - this.socketUDT.setBlocking(true); - } - - /** uses provided socket keeping blocking mode */ - protected NetSocketUDT(final SocketUDT socketUDT) { - this.socketUDT = socketUDT; - } - - @Override - public void bind(SocketAddress bindpoint) throws IOException { - if (bindpoint == null) { - bindpoint = new InetSocketAddress(0); - } - socketUDT.bind((InetSocketAddress) bindpoint); - } - - @Override - public synchronized void close() throws IOException { - socketUDT.close(); - } - - @Override - public void connect(final SocketAddress endpoint) throws IOException { - socketUDT.connect((InetSocketAddress) endpoint); - } - - @Override - public void connect(final SocketAddress endpoint, final int timeout) - throws IOException { - throw new UnsupportedOperationException("feature not available"); - } - - @Override - public SocketChannel getChannel() { - throw new UnsupportedOperationException("feature not available"); - } - - @Override - public InetAddress getInetAddress() { - return socketUDT.getRemoteInetAddress(); - } - - @Override - public synchronized InputStream getInputStream() throws IOException { - if (inputStream == null) { - inputStream = new NetInputStreamUDT(socketUDT); - } - return inputStream; - } - - @Override - public boolean getKeepAlive() throws SocketException { - // UDT has keep alive automatically under the - // hood which I believe you cannot turn off - return true; - } - - @Override - public InetAddress getLocalAddress() { - return socketUDT.getLocalInetAddress(); - } - - @Override - public int getLocalPort() { - return socketUDT.getLocalInetPort(); - } - - @Override - public SocketAddress getLocalSocketAddress() { - try { - return socketUDT.getLocalSocketAddress(); - } catch (final ExceptionUDT e) { - return null; - } - } - - @Override - public boolean getOOBInline() throws SocketException { - return false; - } - - @Override - public synchronized OutputStream getOutputStream() throws IOException { - if (outputStream == null) { - outputStream = new NetOutputStreamUDT(socketUDT); - } - return outputStream; - } - - @Override - public int getPort() { - return socketUDT.getRemoteInetPort(); - } - - @Override - public synchronized int getReceiveBufferSize() throws SocketException { - return socketUDT.getReceiveBufferSize(); - } - - @Override - public SocketAddress getRemoteSocketAddress() { - try { - return socketUDT.getRemoteSocketAddress(); - } catch (final ExceptionUDT e) { - return null; - } - } - - @Override - public boolean getReuseAddress() throws SocketException { - return socketUDT.getReuseAddress(); - } - - @Override - public synchronized int getSendBufferSize() throws SocketException { - return socketUDT.getSendBufferSize(); - } - - @Override - public int getSoLinger() throws SocketException { - return socketUDT.getSoLinger(); - } - - @Override - public synchronized int getSoTimeout() throws SocketException { - return socketUDT.getSoTimeout(); - } - - @Override - public boolean getTcpNoDelay() throws SocketException { - return false; - } - - @Override - public int getTrafficClass() throws SocketException { - return 0; - } - - @Override - public boolean isBound() { - return socketUDT.isBound(); - } - - @Override - public boolean isClosed() { - return socketUDT.isClosed(); - } - - @Override - public boolean isConnected() { - return socketUDT.isConnected(); - } - - @Override - public boolean isInputShutdown() { - return socketUDT.isClosed(); - } - - @Override - public boolean isOutputShutdown() { - return socketUDT.isClosed(); - } - - @Override - public void sendUrgentData(final int data) throws IOException { - log.debug("Sending urgent data not supported in Barchart UDT..."); - } - - @Override - public void setKeepAlive(final boolean on) throws SocketException { - log.debug("Keep alive not supported in Barchart UDT..."); - } - - @Override - public void setOOBInline(final boolean on) throws SocketException { - log.debug("OOB inline not supported in Barchart UDT..."); - } - - @Override - public void setPerformancePreferences(final int connectionTime, - final int latency, final int bandwidth) { - } - - @Override - public synchronized void setReceiveBufferSize(final int size) - throws SocketException { - socketUDT.setReceiveBufferSize(size); - } - - @Override - public void setReuseAddress(final boolean on) throws SocketException { - socketUDT.setReuseAddress(on); - } - - @Override - public synchronized void setSendBufferSize(final int size) - throws SocketException { - socketUDT.setSendBufferSize(size); - } - - @Override - public void setSoLinger(final boolean on, final int linger) - throws SocketException { - socketUDT.setSoLinger(on, linger); - } - - @Override - public synchronized void setSoTimeout(final int timeout) - throws SocketException { - socketUDT.setSoTimeout(timeout); - } - - @Override - public void setTcpNoDelay(final boolean on) throws SocketException { - log.debug("TCP no delay not supported in Barchart UDT..."); - } - - @Override - public void setTrafficClass(final int tc) throws SocketException { - log.debug("Traffic class not supported in Barchart UDT..."); - } - - @Override - public void shutdownInput() throws IOException { - socketUDT.close(); - } - - @Override - public void shutdownOutput() throws IOException { - socketUDT.close(); - } - - @Override - public SocketUDT socketUDT() { - return socketUDT; - } - -} diff --git a/src/com/barchart/udt/nio/ChannelUDT.java b/src/com/barchart/udt/nio/ChannelUDT.java deleted file mode 100644 index 3f59f78a..00000000 --- a/src/com/barchart/udt/nio/ChannelUDT.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.nio.channels.Channel; -import java.nio.channels.SelectionKey; - -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; - -/** - * Interface shared by all {@link KindUDT} kinds. - */ -public interface ChannelUDT extends Channel { - - /** - * Was connection request - * {@link SocketChannelUDT#connect(java.net.SocketAddress)} acknowledged by - * {@link SocketChannelUDT#finishConnect()}? - */ - boolean isConnectFinished(); - - /** - * The kind of UDT channel. - */ - KindUDT kindUDT(); - - /** - * UDT specific provider which produced this channel. - */ - SelectorProviderUDT providerUDT(); - - /** - * Underlying UDT socket. - */ - SocketUDT socketUDT(); - - /** - * The type of UDT socket. - */ - TypeUDT typeUDT(); - - /** - * Mask of all interest options which are permitted for this channel. - * - * @see SelectionKey - */ - int validOps(); - -} diff --git a/src/com/barchart/udt/nio/KindUDT.java b/src/com/barchart/udt/nio/KindUDT.java deleted file mode 100644 index 26fa0e3e..00000000 --- a/src/com/barchart/udt/nio/KindUDT.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; - -/** - * UDT channel role type, or kind. - *

- * {@link TypeUDT} means stream (byte oriented) vs datagram (message oriented). - *

- * {@link KindUDT} gives distinction between server vs client vs peer. - */ -public enum KindUDT { - - /** - * Server mode: listens and accepts connections; generates - * {@link #CONNECTOR} as a result of {@link SocketUDT#accept()} - * - * @see ServerSocketChannelUDT - */ - ACCEPTOR, // - - /** - * Client mode: channel which initiates connections to servers; options are - * user-provided. - *

- * Server mode: channel which is a result of accept(); inherits options from - * parent {@link #ACCEPTOR}. - * - * @see SocketChannelUDT - */ - CONNECTOR, // - - /** - * Rendezvous mode: symmetric peer channel on each side of the connection. - * - * @see RendezvousChannelUDT - */ - RENDEZVOUS, // - -} diff --git a/src/com/barchart/udt/nio/NioInputStreamUDT.java b/src/com/barchart/udt/nio/NioInputStreamUDT.java deleted file mode 100644 index 52b860f7..00000000 --- a/src/com/barchart/udt/nio/NioInputStreamUDT.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.channels.IllegalBlockingModeException; - -/** - * {@link InputStream} implementation for UDT sockets. - */ -public class NioInputStreamUDT extends InputStream { - - protected final SocketChannelUDT channel; - - /** - * Creates a new input stream for the specified channel. - * - * @param channel - * The UDT socket channel. - */ - protected NioInputStreamUDT(final SocketChannelUDT channel) { - if (channel == null) { - throw new NullPointerException("channel == null"); - } - if (!channel.isBlocking()) { - throw new IllegalBlockingModeException(); - } - this.channel = channel; - } - - @Override - public int read() throws IOException { - - /* - * Here's the contract from the JavaDoc on this for SocketChannel: - * - * A read operation might not fill the buffer, and in fact it might not - * read any bytes at all. Whether or not it does so depends upon the - * nature and state of the channel. A socket channel in non-blocking - * mode, for example, cannot read any more bytes than are immediately - * available from the socket's input buffer; similarly, a file channel - * cannot read any more bytes than remain in the file. It is guaranteed, - * however, that if a channel is in blocking mode and there is at least - * one byte remaining in the buffer then this method will block until at - * least one byte is read. - * - * Long story short: This UDT InputStream should only ever be created - * when the SocketChannel's in blocking mode, and when it's in blocking - * mode the SocketChannel read call below will block just like we need - * it too. - */ - - final byte[] data = new byte[1]; - read(data); - return data[0]; - } - - @Override - public int read(final byte[] bytes) throws IOException { - return read(bytes, 0, bytes.length); - } - - @Override - public int read(final byte[] bytes, final int off, final int len) - throws IOException { - - if (len > bytes.length - off) { - throw new IndexOutOfBoundsException("len > bytes.length - off"); - } - - final ByteBuffer buffer = ByteBuffer.wrap(bytes); - buffer.position(off); - buffer.limit(off + len); - - final int read = channel.read(buffer); - return read; - } - - @Override - public long skip(final long n) throws IOException { - - final ByteBuffer buffer = ByteBuffer.allocateDirect(32768); - long remaining = n; - - while (remaining > 0) { - - buffer.limit((int) Math.min(remaining, buffer.capacity())); - final int ret = channel.read(buffer); - - if (ret <= 0) - break; - - remaining -= ret; - buffer.rewind(); - } - - return n - remaining; - } - - @Override - public int available() throws IOException { - // This is the default InputStream return value. - // The java/net/SocketInputStream.java implementation delegates to - // the native implementation, which returns 0 on at least some OSes. - return 0; - } - - @Override - public void close() throws IOException { - channel.close(); - } - - @Override - public void mark(final int readlimit) { - throw new UnsupportedOperationException("mark not supported"); - } - - @Override - public void reset() throws IOException { - throw new UnsupportedOperationException("reset not supported"); - } - - @Override - public boolean markSupported() { - return false; - } - -} diff --git a/src/com/barchart/udt/nio/NioOutputStreamUDT.java b/src/com/barchart/udt/nio/NioOutputStreamUDT.java deleted file mode 100644 index 987f3a71..00000000 --- a/src/com/barchart/udt/nio/NioOutputStreamUDT.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.ByteBuffer; - -/** - * {@link OutputStream} for UDT sockets. - */ -public class NioOutputStreamUDT extends OutputStream { - - protected final SocketChannelUDT channel; - - /** - * Creates a new UDT output stream. - * - * @param channel - * The UDT socket channel. - */ - protected NioOutputStreamUDT(final SocketChannelUDT channel) { - this.channel = channel; - } - - @Override - public void write(final byte[] bytes, final int off, final int len) - throws IOException { - channel.write(ByteBuffer.wrap(bytes, off, len)); - } - - @Override - public void write(final byte[] bytes) throws IOException { - channel.write(ByteBuffer.wrap(bytes)); - } - - @Override - public void write(final int b) throws IOException { - // Just cast it -- this is the same thing SocketOutputStream does. - final byte[] bytes = { (byte) b }; - channel.write(ByteBuffer.wrap(bytes)); - } - - @Override - public void close() throws IOException { - channel.close(); - } - -} diff --git a/src/com/barchart/udt/nio/NioServerSocketUDT.java b/src/com/barchart/udt/nio/NioServerSocketUDT.java deleted file mode 100644 index 4cbe9358..00000000 --- a/src/com/barchart/udt/nio/NioServerSocketUDT.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.net.Socket; -import java.net.SocketAddress; - -import com.barchart.udt.net.NetServerSocketUDT; - -public class NioServerSocketUDT extends NetServerSocketUDT { - - protected final ServerSocketChannelUDT channelUDT; - - protected NioServerSocketUDT(final ServerSocketChannelUDT channelUDT) - throws IOException { - super(channelUDT.socketUDT()); - this.channelUDT = channelUDT; - } - - @Override - public Socket accept() throws IOException { - throw new RuntimeException("feature not available"); - } - - @Override - public void bind(final SocketAddress endpoint) throws IOException { - final SelectorProviderUDT provider = // - (SelectorProviderUDT) channelUDT.provider(); - final int backlog = provider.getAcceptQueueSize(); - bind(endpoint, backlog); - } - - @Override - public ServerSocketChannelUDT getChannel() { - return channelUDT; - } - -} diff --git a/src/com/barchart/udt/nio/NioSocketUDT.java b/src/com/barchart/udt/nio/NioSocketUDT.java deleted file mode 100644 index 185c6c14..00000000 --- a/src/com/barchart/udt/nio/NioSocketUDT.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.net.NetSocketUDT; - -public class NioSocketUDT extends NetSocketUDT { - - protected final SocketChannelUDT channelUDT; - - protected NioSocketUDT(final SocketChannelUDT channelUDT) - throws ExceptionUDT { - super(channelUDT.socketUDT()); - this.channelUDT = channelUDT; - } - - @Override - public SocketChannelUDT getChannel() { - return channelUDT; - } - - @Override - public synchronized InputStream getInputStream() throws IOException { - if (inputStream == null) { - inputStream = new NioInputStreamUDT(channelUDT); - } - return inputStream; - } - - @Override - public synchronized OutputStream getOutputStream() throws IOException { - if (outputStream == null) { - outputStream = new NioOutputStreamUDT(channelUDT); - } - return outputStream; - } - -} diff --git a/src/com/barchart/udt/nio/RendezvousChannelUDT.java b/src/com/barchart/udt/nio/RendezvousChannelUDT.java deleted file mode 100644 index 17d0f7a6..00000000 --- a/src/com/barchart/udt/nio/RendezvousChannelUDT.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.nio.channels.SocketChannel; - -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; - -/** - * {@link SocketChannel}-like wrapper for {@link SocketUDT}, can be either - * stream or message oriented, depending on {@link TypeUDT} - *

- * See Firewall - * Traversing with UDT - */ -public class RendezvousChannelUDT extends SocketChannelUDT implements - ChannelUDT { - - /** - * Ensure rendezvous mode. - */ - protected RendezvousChannelUDT( // - final SelectorProviderUDT provider, // - final SocketUDT socketUDT // - ) throws ExceptionUDT { - - super(provider, socketUDT); - - socketUDT.setReuseAddress(true); - socketUDT.setRendezvous(true); - - } - - @Override - public KindUDT kindUDT() { - return KindUDT.RENDEZVOUS; - } - -} diff --git a/src/com/barchart/udt/nio/SelectionKeyUDT.java b/src/com/barchart/udt/nio/SelectionKeyUDT.java deleted file mode 100644 index 35df6496..00000000 --- a/src/com/barchart/udt/nio/SelectionKeyUDT.java +++ /dev/null @@ -1,506 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.nio.channels.CancelledKeyException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.EpollUDT; -import com.barchart.udt.EpollUDT.Opt; -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.OptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.StatusUDT; - -/** - * UDT selection key implementation. - */ -public class SelectionKeyUDT extends SelectionKey implements - Comparable { - - /** - * JDK interest to Epoll READ mapping. - */ - protected static final int HAS_READ = OP_ACCEPT | OP_READ; - - /** - * JDK interest to Epoll WRITE mapping. - */ - protected static final int HAS_WRITE = OP_CONNECT | OP_WRITE; - - protected static final Logger log = LoggerFactory - .getLogger(SelectionKeyUDT.class); - - /** - * Convert select options : from jdk into epoll. - */ - protected static Opt from(final int interestOps) { - - final boolean hasRead = (interestOps & HAS_READ) != 0; - final boolean hasWrite = (interestOps & HAS_WRITE) != 0; - - if (hasRead && hasWrite) { - return Opt.ALL; - } - - if (hasRead) { - return Opt.ERROR_READ; - } - - if (hasWrite) { - return Opt.ERROR_WRITE; - } - - return Opt.ERROR; - - } - - /** - * Render select options. - */ - public static final String toStringOps(final int selectOps) { - final char A = (OP_ACCEPT & selectOps) != 0 ? 'A' : '-'; - final char C = (OP_CONNECT & selectOps) != 0 ? 'C' : '-'; - final char R = (OP_READ & selectOps) != 0 ? 'R' : '-'; - final char W = (OP_WRITE & selectOps) != 0 ? 'W' : '-'; - return String.format("%c%c%c%c", A, C, R, W); - } - - /** - * Channel bound to the key. - */ - private final ChannelUDT channelUDT; - - /** - * Requested interest in epoll format. - */ - private volatile Opt epollOpt; - - /** - * Requested interest in JDK format. - */ - private volatile int interestOps; - - /** - * Key validity state. Key is valid when created, and invalid when canceled. - */ - private volatile boolean isValid; - - /** - * Reported ready interest. - */ - private volatile int readyOps; - - /** - * Correlation index for {@link #doRead(int)} vs {@link #doWrite(int)} - */ - private volatile int resultIndex; - - /** - * Selector bound to the key. - */ - private final SelectorUDT selectorUDT; - - protected SelectionKeyUDT( // - final SelectorUDT selectorUDT, // - final ChannelUDT channelUDT, // - final Object attachment // - ) { - - super.attach(attachment); - - this.selectorUDT = selectorUDT; - this.channelUDT = channelUDT; - - makeValid(true); - - } - - /** - * Ensure key is NOT canceled. - */ - protected void assertValidKey() throws CancelledKeyException { - if (isValid()) { - return; - } - throw new CancelledKeyException(); - } - - /** - * Ensure only permitted interest mask bits are present. - */ - protected void assertValidOps(final int interestOps) { - if ((interestOps & ~(channel().validOps())) != 0) { - throw new IllegalArgumentException("invalid interestOps=" - + interestOps); - } - } - - @Override - public void cancel() { - if (isValid()) { - selector().cancel(this); - } - } - - @Override - public SelectableChannel channel() { - return (SelectableChannel) channelUDT; - } - - /** - * Underlying UDT channel. - */ - protected ChannelUDT channelUDT() { - return channelUDT; - } - - @Override - public int compareTo(final SelectionKeyUDT that) { - final int thisId = this.socketId(); - final int thatId = that.socketId(); - if (thisId > thatId) { - return +1; - } - if (thisId < thatId) { - return -1; - } - return 0; - } - - /** - * Apply READ readiness according to {@link KindUDT} channel role. - *

- * Note: {@link #doRead(int)} is invoked before {@link #doWrite(int)} - *

- * Sockets with exceptions are returned to both read and write sets. - * - * @return Should report ready-state change? - */ - protected boolean doRead(final int resultIndex) { - - int readyOps = 0; - final int interestOps = this.interestOps; - - /** Store read/write verifier. */ - this.resultIndex = resultIndex; - - try { - - /** Check error report. */ - if (!epollOpt.hasRead()) { - if (isSocketBroken()) { - readyOps = channel().validOps(); - return true; - } else { - logError("Unexpected error report."); - return false; - } - } - - switch (kindUDT()) { - case ACCEPTOR: - if ((interestOps & OP_ACCEPT) != 0) { - readyOps = OP_ACCEPT; - return true; - } else { - logError("Ready to ACCEPT while not interested."); - return false; - } - case CONNECTOR: - case RENDEZVOUS: - if ((interestOps & OP_READ) != 0) { - readyOps = OP_READ; - return true; - } else { - logError("Ready to READ while not interested."); - return false; - } - default: - logError("Wrong kind."); - return false; - } - - } finally { - - this.readyOps = readyOps; - - } - - } - - /** - * Apply WRITE readiness according to {@link KindUDT} channel role. - *

- * Note: {@link #doRead(int)} is invoked before {@link #doWrite(int)} - *

- * Sockets with exceptions are returned to both read and write sets. - * - * @return Should report ready-state change? - */ - protected boolean doWrite(final int resultIndex) { - - int readyOps = 0; - final int interestOps = this.interestOps; - - /** Verify read/write relationship. */ - final boolean hadReadBeforeWrite = this.resultIndex == resultIndex; - - try { - - /** Check error report. */ - if (!epollOpt.hasWrite()) { - if (isSocketBroken()) { - readyOps = channel().validOps(); - return true; - } else { - logError("Unexpected error report."); - return false; - } - } - - switch (kindUDT()) { - case ACCEPTOR: - logError("Ready to WRITE for acceptor."); - return false; - case CONNECTOR: - case RENDEZVOUS: - if (channelUDT().isConnectFinished()) { - if ((interestOps & OP_WRITE) != 0) { - readyOps = OP_WRITE; - return true; - } else { - logError("Ready to WRITE when not insterested."); - return false; - } - } else { - if ((interestOps & OP_CONNECT) != 0) { - readyOps = OP_CONNECT; - return true; - } else { - logError("Ready to CONNECT when not interested."); - return false; - } - } - default: - logError("Wrong kind."); - return false; - } - - } finally { - if (hadReadBeforeWrite) { - this.readyOps |= readyOps; - } else { - this.readyOps = readyOps; - } - } - - } - - /** - * Requested interest in epoll format. - */ - protected Opt epollOpt() { - return epollOpt; - } - - /** - * Epoll bound to this key. - */ - protected EpollUDT epollUDT() { - return selector().epollUDT(); - } - - /** - * Key equality based on socket-id. - */ - @Override - public boolean equals(final Object otherKey) { - if (otherKey instanceof SelectionKeyUDT) { - final SelectionKeyUDT other = (SelectionKeyUDT) otherKey; - return other.socketId() == this.socketId(); - } - return false; - } - - /** - * Check socket error condition. - */ - boolean hasError() throws ExceptionUDT { - final int code = socketUDT().getOption(OptionUDT.Epoll_Event_Mask); - return Opt.from(code).hasError(); - } - - /** - * Key hach code based on socket-id. - */ - @Override - public int hashCode() { - return socketId(); - } - - @Override - public int interestOps() { - return interestOps; - } - - @Override - public SelectionKey interestOps(final int interestOps) { - - assertValidKey(); - assertValidOps(interestOps); - - try { - - final Opt epollNew = from(interestOps); - - if (epollNew != epollOpt) { - - if (Opt.ERROR == epollNew) { - epollUDT().remove(socketUDT()); - } else { - epollUDT().remove(socketUDT()); - epollUDT().add(socketUDT(), epollNew); - } - - epollOpt = epollNew; - - } - - } catch (final Exception e) { - - log.error("epoll udpate failure", e); - - } finally { - - this.interestOps = interestOps; - - } - - return this; - - } - - /** - * Check socket termination status. - * - * @return true if status is {@link StatusUDT#BROKEN} or worse - */ - protected boolean isSocketBroken() { - switch (socketUDT().status()) { - case INIT: - case OPENED: - case LISTENING: - case CONNECTING: - case CONNECTED: - return false; - case BROKEN: - case CLOSING: - case CLOSED: - case NONEXIST: - return true; - default: - logError("Unknown socket status."); - return true; - } - } - - @Override - public boolean isValid() { - return isValid; - } - - /** - * Channel role. - */ - protected KindUDT kindUDT() { - return channelUDT.kindUDT(); - } - - /** - * Key processing logic error logger. - */ - protected void logError(final String comment) { - - final String message = "logic error : \n\t" + this; - - log.warn(message, new Exception("" + comment)); - - } - - /** - * Change socket registration with epoll, and change key validity status. - */ - protected void makeValid(final boolean isValid) { - try { - if (isValid) { - epollOpt = Opt.ERROR; - epollUDT().add(socketUDT(), epollOpt); - } else { - epollUDT().remove(socketUDT()); - } - } catch (final Throwable e) { - log.error("Epoll failure.", e); - } finally { - this.isValid = isValid; - } - } - - @Override - public int readyOps() { - return readyOps; - } - - protected void readyOps(final int ops) { - readyOps = ops; - } - - @Override - public SelectorUDT selector() { - return selectorUDT; - } - - /** - * Id of a socket bound to this key. - */ - protected int socketId() { - return socketUDT().id(); - } - - /** - * Socket bound to this key. - */ - protected SocketUDT socketUDT() { - return channelUDT.socketUDT(); - } - - @Override - public String toString() { - - return String - .format("[id: 0x%08x] poll=%s ready=%s inter=%s %s %s %s bind=%s:%s peer=%s:%s", // - socketUDT().id(), // - epollOpt, // - toStringOps(readyOps), // - toStringOps(interestOps), // - channelUDT.typeUDT(), // - channelUDT.kindUDT(), // - socketUDT().status(), // - socketUDT().getLocalInetAddress(), // - socketUDT().getLocalInetPort(), // - socketUDT().getRemoteInetAddress(), // - socketUDT().getRemoteInetPort() // - ); - - } - -} diff --git a/src/com/barchart/udt/nio/SelectorProviderUDT.java b/src/com/barchart/udt/nio/SelectorProviderUDT.java deleted file mode 100644 index 85c68593..00000000 --- a/src/com/barchart/udt/nio/SelectorProviderUDT.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.nio.channels.DatagramChannel; -import java.nio.channels.Pipe; -import java.nio.channels.spi.SelectorProvider; - -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; - -/** - * selection provider for UDT - *

- * note: you must use the same system-wide provider instance for the same - * {@link TypeUDT} of UDT channels and UDT selectors; - */ -public class SelectorProviderUDT extends SelectorProvider { - - /** - * system-wide provider instance, for {@link TypeUDT#DATAGRAM} UDT sockets - */ - public static final SelectorProviderUDT DATAGRAM = new SelectorProviderUDT(TypeUDT.DATAGRAM); - - /** - * system-wide provider instance, for {@link TypeUDT#STREAM} UDT sockets - */ - public static final SelectorProviderUDT STREAM = new SelectorProviderUDT(TypeUDT.STREAM); - - public static SelectorProviderUDT from(final TypeUDT type) { - switch (type) { - case DATAGRAM: - return DATAGRAM; - case STREAM: - return STREAM; - default: - throw new IllegalStateException("wrong type=" + type); - } - } - - private volatile int acceptQueueSize = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE; - - private volatile int maxSelectorSize = SocketUDT.DEFAULT_MAX_SELECTOR_SIZE; - - private final TypeUDT type; - - /** - * {@link TypeUDT} of UDT sockets generated by this provider - */ - public final TypeUDT type() { - return type; - } - - protected SelectorProviderUDT(final TypeUDT type) { - this.type = type; - } - - public int getAcceptQueueSize() { - return acceptQueueSize; - } - - public int getMaxSelectorSize() { - return maxSelectorSize; - } - - /** - * Not supported. - */ - @Override - public DatagramChannel openDatagramChannel() throws IOException { - throw new UnsupportedOperationException("feature not available"); - } - - /** - * Not supported. - */ - @Override - public Pipe openPipe() throws IOException { - throw new UnsupportedOperationException("feature not available"); - } - - /** - * Open UDT {@link KindUDT#RENDEZVOUS} socket channel. - * - * @see RendezvousChannelUDT - */ - public RendezvousChannelUDT openRendezvousChannel() throws IOException { - final SocketUDT socketUDT = new SocketUDT(type); - return new RendezvousChannelUDT(this, socketUDT); - } - - /** - * Open UDT specific selector. - * - * @see SelectorUDT - */ - @Override - public SelectorUDT openSelector() throws IOException { - return new SelectorUDT(this, maxSelectorSize); - } - - /** - * Open UDT {@link KindUDT#ACCEPTOR} socket channel. - * - * @see ServerSocketChannelUDT - */ - @Override - public ServerSocketChannelUDT openServerSocketChannel() throws IOException { - final SocketUDT serverSocketUDT = new SocketUDT(type); - return new ServerSocketChannelUDT(this, serverSocketUDT); - } - - /** - * Open UDT {@link KindUDT#CONNECTOR} socket channel. - * - * @see SocketChannelUDT - */ - @Override - public SocketChannelUDT openSocketChannel() throws IOException { - final SocketUDT socketUDT = new SocketUDT(type); - return new SocketChannelUDT(this, socketUDT); - } - - public void setAcceptQueueSize(final int queueSize) { - acceptQueueSize = queueSize; - } - - public void setMaxSelectorSize(final int selectorSize) { - maxSelectorSize = selectorSize; - } -} diff --git a/src/com/barchart/udt/nio/SelectorUDT.java b/src/com/barchart/udt/nio/SelectorUDT.java deleted file mode 100644 index 18f3967a..00000000 --- a/src/com/barchart/udt/nio/SelectorUDT.java +++ /dev/null @@ -1,489 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import static com.barchart.udt.SocketUDT.*; - -import java.io.IOException; -import java.nio.IntBuffer; -import java.nio.channels.ClosedSelectorException; -import java.nio.channels.IllegalSelectorException; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.AbstractSelectableChannel; -import java.nio.channels.spi.AbstractSelector; -import java.nio.channels.spi.SelectorProvider; -import java.util.Iterator; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.EpollUDT; -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; -import com.barchart.udt.util.HelpUDT; - -/** - * selector - *

- * design guidelines: - *

- * 1) follow general contracts of jdk 6 nio; see barchart-udt-reference-jdk6 - *

- * 2) adapt to how netty is doing select; see NioEventLoop - *

- * note: you must use {@link SelectorProviderUDT#openSelector()} to obtain - * instance of this class; do not use JDK - * {@link Selector#open()} - */ -public class SelectorUDT extends AbstractSelector { - - protected static final Logger log = LoggerFactory - .getLogger(SelectorUDT.class); - - /** - * use this call to instantiate a selector for UDT - */ - protected static Selector open(final TypeUDT type) throws IOException { - final SelectorProviderUDT provider; - switch (type) { - case DATAGRAM: - provider = SelectorProviderUDT.DATAGRAM; - break; - case STREAM: - provider = SelectorProviderUDT.STREAM; - break; - default: - log.error("unsupported type={}", type); - throw new IOException("unsupported type"); - } - return provider.openSelector(); - } - - private final EpollUDT epollUDT = new EpollUDT(); - - /** - */ - public final int maximimSelectorSize; - - /** - * list of epoll sockets with read interest - */ - private final IntBuffer readBuffer; - - /** - * [ socket-id : selection-key ] - */ - private final ConcurrentMap // - registeredKeyMap = new ConcurrentHashMap(); - - /** - * public view : immutable - */ - private final Set // - registeredKeySet = HelpUDT.unmodifiableSet(registeredKeyMap.values()); - - /** - * tracks correlation read with write for the same key - */ - private volatile int resultIndex; - - /** - * set of keys with data ready for an operation - */ - private final ConcurrentMap // - selectedKeyMap = new ConcurrentHashMap(); - - /** - * public view : removal allowed, but not addition - */ - private final Set // - selectedKeySet = HelpUDT.ungrowableSet(selectedKeyMap.keySet()); - - /** select is exclusive */ - private final Lock selectLock = new ReentrantLock(); - - /** reported epoll socket list sizes */ - private final IntBuffer sizeBuffer; - - /** - * Canceled keys. - */ - private final ConcurrentMap // - terminatedKeyMap = new ConcurrentHashMap(); - - /** guarded by {@link #doSelectLocked} */ - private volatile int wakeupBaseCount; - - private volatile int wakeupStepCount; - - /** list of epoll sockets with write interest */ - private final IntBuffer writeBuffer; - - protected SelectorUDT( // - final SelectorProvider provider, // - final int maximumSelectorSize // - ) throws ExceptionUDT { - - super(provider); - - this.maximimSelectorSize = maximumSelectorSize; - - readBuffer = HelpUDT.newDirectIntBufer(maximumSelectorSize); - writeBuffer = HelpUDT.newDirectIntBufer(maximumSelectorSize); - sizeBuffer = HelpUDT.newDirectIntBufer(UDT_SIZE_COUNT); - - } - - /** - * Enqueue cancel request. - */ - protected void cancel(final SelectionKeyUDT keyUDT) { - terminatedKeyMap.putIfAbsent(keyUDT, keyUDT); - } - - /** - * Process pending cancel requests. - */ - protected void doCancel() { - - if (terminatedKeyMap.isEmpty()) { - return; - } - - final Iterator iterator = terminatedKeyMap.values() - .iterator(); - - while (iterator.hasNext()) { - final SelectionKeyUDT keyUDT = iterator.next(); - iterator.remove(); - if (keyUDT.isValid()) { - keyUDT.makeValid(false); - registeredKeyMap.remove(keyUDT.socketId()); - } - } - - } - - /** - * @param millisTimeout - * <0 : invinite; =0 : immediate; >0 : finite; - */ - protected int doEpollEnter(final long millisTimeout) throws IOException { - - if (!isOpen()) { - log.error("slector is closed"); - throw new ClosedSelectorException(); - } - - try { - selectLock.lock(); - return doEpollExclusive(millisTimeout); - } finally { - selectLock.unlock(); - } - - } - - /** - * @param millisTimeout - * - * <0 : invinite; - * - * =0 : immediate; - * - * >0 : finite; - * @return - * - * <0 : should not happen - * - * =0 : means nothing was selected/timeout - * - * >0 : number of selected keys - */ - - protected int doEpollExclusive(final long millisTimeout) throws IOException { - - try { - - /** java.nio.Selector contract for wakeup() */ - // begin(); - - /** pre select */ - doCancel(); - - /** select proper */ - doEpollSelect(millisTimeout); - - /** post select */ - doResults(); - - } finally { - /** java.nio.Selector contract for wakeup() */ - // end(); - } - - return selectedKeyMap.size(); - - } - - /** - * @param millisTimeout - * - * <0 : infinite - * - * =0 : immediate - * - * >0 : finite - */ - protected int doEpollSelect(long millisTimeout) throws ExceptionUDT { - - wakeupMarkBase(); - - int readyCount = 0; - - if (millisTimeout < 0) { - - /** infinite: do select in slices; check for wakeup; */ - - do { - readyCount = doEpollSelectUDT(DEFAULT_MIN_SELECTOR_TIMEOUT); - if (readyCount > 0 || wakeupIsPending()) { - break; - } - } while (true); - - } else if (millisTimeout > 0) { - - /** finite: do select in slices; check for wakeup; count down */ - - do { - readyCount = doEpollSelectUDT(DEFAULT_MIN_SELECTOR_TIMEOUT); - if (readyCount > 0 || wakeupIsPending()) { - break; - } - millisTimeout -= DEFAULT_MIN_SELECTOR_TIMEOUT; - } while (millisTimeout > 0); - - } else { - - /** immediate */ - - readyCount = doEpollSelectUDT(0); - - } - - return readyCount; - - } - - protected int doEpollSelectUDT(final long timeout) throws ExceptionUDT { - return SocketUDT.selectEpoll(// - epollUDT.id(), // - readBuffer, // - writeBuffer, // - sizeBuffer, // - timeout // - ); - } - - protected void doResults() { - - final int resultIndex = this.resultIndex++; - - doResultsRead(resultIndex); - - doResultsWrite(resultIndex); - - } - - protected void doResultsRead(final int resultIndex) { - - final int readSize = sizeBuffer.get(UDT_READ_INDEX); - - for (int index = 0; index < readSize; index++) { - - final int socketId = readBuffer.get(index); - - final SelectionKeyUDT keyUDT = registeredKeyMap.get(socketId); - - /** - * Epoll will report closed socket once in both read and write sets. - * But selector consumer may cancel the key before close. - */ - if (keyUDT == null) { - logSocketId("missing from read ", socketId); - continue; - } - - if (keyUDT.doRead(resultIndex)) { - selectedKeyMap.putIfAbsent(keyUDT, keyUDT); - } - - } - - } - - protected void doResultsWrite(final int resultIndex) { - - final int writeSize = sizeBuffer.get(UDT_WRITE_INDEX); - - for (int index = 0; index < writeSize; index++) { - - final int socketId = writeBuffer.get(index); - - final SelectionKeyUDT keyUDT = registeredKeyMap.get(socketId); - - /** - * Epoll will report closed socket once in both read and write sets. - * But selector consumer may cancel the key before close. - */ - if (keyUDT == null) { - logSocketId("missing from write", socketId); - continue; - } - - if (keyUDT.doWrite(resultIndex)) { - selectedKeyMap.putIfAbsent(keyUDT, keyUDT); - } - - } - - } - - protected EpollUDT epollUDT() { - return epollUDT; - } - - @Override - protected void implCloseSelector() throws IOException { - - wakeup(); - - try { - selectLock.lock(); - - for (final SelectionKeyUDT keyUDT : registeredKeyMap.values()) { - cancel(keyUDT); - } - - } finally { - selectLock.unlock(); - } - - doCancel(); - - } - - @SuppressWarnings("unchecked") - @Override - public Set keys() { - if (!isOpen()) { - throw new ClosedSelectorException(); - } - return (Set) registeredKeySet; - } - - protected void logSocketId(final String title, final int socketId) { - if (log.isDebugEnabled()) { - log.debug("{} {}", title, String.format("[id: 0x%08x]", socketId)); - } - } - - /** - */ - @Override - protected SelectionKey register( // - final AbstractSelectableChannel channel, // - final int interestOps, // - final Object attachment // - ) { - - if (registeredKeyMap.size() >= maximimSelectorSize) { - log.error("reached maximimSelectorSize"); - throw new IllegalSelectorException(); - } - - if (!(channel instanceof ChannelUDT)) { - log.error("!(channel instanceof ChannelUDT)"); - throw new IllegalSelectorException(); - } - - final ChannelUDT channelUDT = (ChannelUDT) channel; - - final Integer socketId = channelUDT.socketUDT().id(); - - SelectionKeyUDT keyUDT = registeredKeyMap.get(socketId); - - if (keyUDT == null) { - keyUDT = new SelectionKeyUDT(this, channelUDT, attachment); - registeredKeyMap.putIfAbsent(socketId, keyUDT); - keyUDT = registeredKeyMap.get(socketId); - } - - keyUDT.interestOps(interestOps); - - return keyUDT; - - } - - @Override - public int select() throws IOException { - return select(0); - } - - @Override - public int select(final long timeout) throws IOException { - if (timeout < 0) { - throw new IllegalArgumentException("negative timeout"); - } else if (timeout > 0) { - return doEpollEnter(timeout); - } else { - return doEpollEnter(SocketUDT.TIMEOUT_INFINITE); - } - } - - @SuppressWarnings("unchecked") - @Override - public Set selectedKeys() { - if (!isOpen()) { - throw new ClosedSelectorException(); - } - return (Set) selectedKeySet; - } - - @Override - public int selectNow() throws IOException { - return doEpollEnter(SocketUDT.TIMEOUT_NONE); - } - - @Override - public Selector wakeup() { - wakeupStepCount++; - return this; - } - - protected boolean wakeupIsPending() { - return wakeupBaseCount != wakeupStepCount; - } - - protected void wakeupMarkBase() { - wakeupBaseCount = wakeupStepCount; - } - -} diff --git a/src/com/barchart/udt/nio/ServerSocketChannelUDT.java b/src/com/barchart/udt/nio/ServerSocketChannelUDT.java deleted file mode 100644 index 3cad6df4..00000000 --- a/src/com/barchart/udt/nio/ServerSocketChannelUDT.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.nio.channels.ServerSocketChannel; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; -import com.barchart.udt.anno.ThreadSafe; - -/** - * {@link ServerSocketChannel}-like wrapper for {@link SocketUDT} can be either - * stream or message oriented, depending on {@link TypeUDT} - *

- * you must use {@link SelectorProviderUDT#openServerSocketChannel()} to obtain - * instance of this class; do not use JDK - * {@link ServerSocketChannel#open()}; - *

- * example: - * - *

- * SelectorProvider provider = SelectorProviderUDT.DATAGRAM;
- * ServerSocketChannel acceptChannel = provider.openServerSocketChannel();
- * ServerSocket acceptSocket = acceptChannel.socket();
- * InetSocketAddress acceptAddress = new InetSocketAddress("localhost", 12345);
- * acceptorSocket.bind(acceptAddress);
- * assert acceptSocket.isBound();
- * SocketChannel connectChannel = acceptChannel.accept();
- * assert connectChannel.isConnected();
- * 
- */ -public class ServerSocketChannelUDT extends ServerSocketChannel implements ChannelUDT { - - protected static final Logger log = LoggerFactory.getLogger(ServerSocketChannelUDT.class); - - @ThreadSafe("this") - protected NioServerSocketUDT socketAdapter; - - protected final SocketUDT socketUDT; - - protected ServerSocketChannelUDT(final SelectorProviderUDT provider, final SocketUDT socketUDT) { - super(provider); - this.socketUDT = socketUDT; - } - - @Override - public SocketChannelUDT accept() throws IOException { - try { - begin(); - - final SocketUDT clientUDT = socketUDT.accept(); - - if (clientUDT == null) { - - return null; - - } else { - - return new SocketChannelUDT( // - providerUDT(), // - clientUDT, // - clientUDT.isConnected() // - ); - - } - } finally { - end(true); - } - } - - @Override - protected void implCloseSelectableChannel() throws IOException { - socketUDT.close(); - } - - @Override - protected void implConfigureBlocking(final boolean block) throws IOException { - socketUDT.setBlocking(block); - } - - @Override - public boolean isConnectFinished() { - return true; - } - - @Override - public KindUDT kindUDT() { - return KindUDT.ACCEPTOR; - } - - @Override - public SelectorProviderUDT providerUDT() { - return (SelectorProviderUDT) super.provider(); - } - - @Override - public synchronized NioServerSocketUDT socket() { - if (socketAdapter == null) { - try { - socketAdapter = new NioServerSocketUDT(this); - } catch (final Exception e) { - log.error("failed to make socket", e); - return null; - } - } - return socketAdapter; - } - - @Override - public SocketUDT socketUDT() { - return socketUDT; - } - - @Override - public String toString() { - return socketUDT.toString(); - } - - @Override - public TypeUDT typeUDT() { - return providerUDT().type(); - } -} diff --git a/src/com/barchart/udt/nio/SocketChannelUDT.java b/src/com/barchart/udt/nio/SocketChannelUDT.java deleted file mode 100644 index ba8d3942..00000000 --- a/src/com/barchart/udt/nio/SocketChannelUDT.java +++ /dev/null @@ -1,506 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.nio; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.nio.ByteBuffer; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.ConnectionPendingException; -import java.nio.channels.IllegalBlockingModeException; -import java.nio.channels.SocketChannel; -import java.nio.channels.UnresolvedAddressException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.ExceptionUDT; -import com.barchart.udt.SocketUDT; -import com.barchart.udt.TypeUDT; -import com.barchart.udt.anno.ThreadSafe; - -/** - * {@link SocketChannel}-like wrapper for {@link SocketUDT}, can be either - * stream or message oriented, depending on {@link TypeUDT} - *

- * The UDT socket that this SocketChannel wraps will be switched to blocking - * mode since this is the default for all SocketChannels on construction. If you - * require non-blocking functionality, you will need to call configureBlocking - * on the constructed SocketChannel class. - *

- * you must use {@link SelectorProviderUDT#openSocketChannel()} to obtain - * instance of this class; do not use JDK - * {@link SocketChannel#open()}; - *

- * example: - * - *

- * SelectorProvider provider = SelectorProviderUDT.DATAGRAM;
- * SocketChannel clientChannel = provider.openSocketChannel();
- * clientChannel.configureBlocking(true);
- * Socket clientSocket = clientChannel.socket();
- * InetSocketAddress clientAddress = new InetSocketAddress("localhost", 10000);
- * clientSocket.bind(clientAddress);
- * assert clientSocket.isBound();
- * InetSocketAddress serverAddress = new InetSocketAddress("localhost", 12345);
- * clientChannel.connect(serverAddress);
- * assert clientSocket.isConnected();
- * 
- */ -public class SocketChannelUDT extends SocketChannel implements ChannelUDT { - - protected static final Logger log = LoggerFactory.getLogger(SocketChannelUDT.class); - - protected final Object connectLock = new Object(); - - /** - * local volatile variable, which mirrors super.blocking, to avoid the cost - * of synchronized call inside isBlocking() - */ - protected volatile boolean isBlockingMode = isBlocking(); - - protected volatile boolean isConnectFinished; - - protected volatile boolean isConnectionPending; - - @ThreadSafe("this") - protected NioSocketUDT socketAdapter; - - protected final SocketUDT socketUDT; - - private InetSocketAddress remoteSocket; - - protected SocketChannelUDT(final SelectorProviderUDT provider, final SocketUDT socketUDT) throws ExceptionUDT { - super(provider); - this.socketUDT = socketUDT; - this.socketUDT.setBlocking(true); - } - - protected SocketChannelUDT(final SelectorProviderUDT provider, final SocketUDT socketUDT, final boolean isConnected) throws ExceptionUDT { - this(provider, socketUDT); - - if (isConnected) { - isConnectFinished = true; - isConnectionPending = false; - } else { - isConnectFinished = false; - isConnectionPending = true; - } - - } - - @Override - public boolean connect(final SocketAddress remote) throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - - if (isConnected()) { - log.warn("already connected; ignoring remote={}", remote); - return true; - } - - if (remote == null) { - close(); - log.error("remote == null"); - throw new NullPointerException(); - } - - remoteSocket = (InetSocketAddress) remote; - - if (remoteSocket.isUnresolved()) { - log.error("can not use unresolved address: remote={}", remote); - close(); - throw new UnresolvedAddressException(); - } - - if (isBlocking()) { - synchronized (connectLock) { - try { - - if (isConnectionPending) { - close(); - throw new ConnectionPendingException(); - } - - isConnectionPending = true; - - begin(); - - socketUDT.connect(remoteSocket); - - } finally { - - end(true); - - isConnectionPending = false; - - connectLock.notifyAll(); - - } - } - - return socketUDT.isConnected(); - - } else { - - /** non Blocking */ - - if (!isRegistered()) { - - /** this channel is independent of any selector */ - - log.error("UDT channel is in NON blocking mode; " - + "must register with a selector " // - + "before trying to connect(); " // - + "socketId=" + socketUDT.id()); - - throw new IllegalBlockingModeException(); - - } - - /** this channel is registered with a selector */ - - synchronized (connectLock) { - - if (isConnectionPending) { - close(); - log.error("connection already in progress"); - throw new ConnectionPendingException(); - } - - isConnectFinished = false; - isConnectionPending = true; - - socketUDT.connect(remoteSocket); - - } - - /** - * connection operation must later be completed by invoking the - * #finishConnect() method. - */ - - return false; - - } - - } - - @Override - public boolean finishConnect() throws IOException { - if (!isOpen()) { - throw new ClosedChannelException(); - } - - if (isBlocking()) { - - synchronized (connectLock) { - while (isConnectionPending) { - try { - connectLock.wait(); - } catch (final InterruptedException e) { - throw new IOException(e); - } - } - } - - } - - if (isConnected()) { - - isConnectFinished = true; - isConnectionPending = false; - - return true; - - } else { - - log.error("connect failure : {}", socketUDT); - throw new IOException(); - - } - } - - @Override - protected void implCloseSelectableChannel() throws IOException { - socketUDT.close(); - } - - @Override - protected void implConfigureBlocking(final boolean block) - throws IOException { - socketUDT.setBlocking(block); - isBlockingMode = block; - } - - @Override - public boolean isConnected() { - return socketUDT.isConnected(); - } - - @Override - public boolean isConnectFinished() { - return isConnectFinished; - } - - @Override - public boolean isConnectionPending() { - return isConnectionPending; - } - - @Override - public KindUDT kindUDT() { - return KindUDT.CONNECTOR; - } - - @Override - public SelectorProviderUDT providerUDT() { - return (SelectorProviderUDT) super.provider(); - } - - - /** - * See {@link SocketChannel#read(ByteBuffer)} contract; - * note: this method does not return (-1) as EOS (end of stream flag) - * - * @return <0 should not happen
- * =0 blocking mode: timeout occurred on receive
- * =0 non-blocking mode: nothing is received by the - * underlying UDT socket
- * >0 actual bytes received count
- * @see SocketUDT#receive(ByteBuffer) - * @see SocketUDT#receive(byte[], int, int) - */ - @Override - public int read(final ByteBuffer buffer) throws IOException { - - final int remaining = buffer.remaining(); - - if (remaining <= 0) { - return 0; - } - - final SocketUDT socket = socketUDT; - final boolean isBlocking = isBlockingMode; - - final int sizeReceived; - - try { - - if (isBlocking) { - begin(); // JDK contract for NIO blocking calls - } - - if (buffer.isDirect()) { - - sizeReceived = socket.receive(buffer); - - } else { - - final byte[] array = buffer.array(); - final int position = buffer.position(); - final int limit = buffer.limit(); - - sizeReceived = socket.receive(array, position, limit); - - if (0 < sizeReceived && sizeReceived <= remaining) { - buffer.position(position + sizeReceived); - } - - } - - } finally { - if (isBlocking) { - end(true); // JDK contract for NIO blocking calls - } - } - - // see contract for receive() - - if (sizeReceived < 0) { - // log.trace("nothing was received; socket={}", socket); - return 0; - } - - if (sizeReceived == 0) { - // log.trace("receive timeout; socket={}", socket); - return 0; - } - - if (sizeReceived <= remaining) { - return sizeReceived; - } else { - log.error("should not happen: socket={}", socket); - return 0; - } - } - - @Override - public long read(final ByteBuffer[] dsts, final int offset, final int length) - throws IOException { - throw new RuntimeException("feature not available"); - } - - @Override - public synchronized NioSocketUDT socket() { - if (socketAdapter == null) { - try { - socketAdapter = new NioSocketUDT(this); - } catch (final ExceptionUDT e) { - log.error("failed to make socket", e); - } - } - return socketAdapter; - } - - @Override - public SocketUDT socketUDT() { - return socketUDT; - } - - @Override - public String toString() { - return socketUDT.toString(); - } - - /** - * See {@link SocketChannel#write(ByteBuffer)} contract; - * - * @return <0 should not happen
- * =0 blocking mode: timeout occurred on send
- * =0 non-blocking mode: buffer is full in the - * underlying UDT socket; nothing is sent
- * >0 actual bytes sent count
- * @see SocketUDT#send(ByteBuffer) - * @see SocketUDT#send(byte[], int, int) - */ - @Override - public int write(final ByteBuffer buffer) throws IOException { - - // writeCount.incrementAndGet(); - - if (buffer == null) { - throw new NullPointerException("buffer == null"); - } - - final int remaining = buffer.remaining(); - - if (remaining <= 0) { - return 0; - } - - final SocketUDT socket = socketUDT; - final boolean isBlocking = isBlockingMode; - - int sizeSent = 0; - int ret = 0; - - try { - - if (isBlocking) { - begin(); // JDK contract for NIO blocking calls - } - - if (buffer.isDirect()) { - - do { - ret = socket.send(buffer); - - if (ret > 0) - sizeSent += ret; - - } while (buffer.hasRemaining() && isBlocking); - - } else { - - final byte[] array = buffer.array(); - int position = buffer.position(); - final int limit = buffer.limit(); - - do { - ret = socket.send(array, position, limit); - - if (0 < ret && ret <= remaining) { - sizeSent += ret; - position += ret; - buffer.position(position); - } - - } while (buffer.hasRemaining() && isBlocking); - } - } finally { - if (isBlocking) { - end(true); // JDK contract for NIO blocking calls - } - } - - // see contract for send() - - if (ret < 0) { - // log.trace("no buffer space; socket={}", socket); - return 0; - } - - if (ret == 0) { - // log.trace("send timeout; socket={}", socket); - return 0; - } - - if (sizeSent <= remaining) { - return sizeSent; - } else { - log.error("should not happen; socket={}", socket); - return 0; - } - } - - @Override - public long write(final ByteBuffer[] bufferArray, final int offset, final int length) throws IOException { - - try { - long total = 0; - - for (int index = offset; index < offset + length; index++) { - - final ByteBuffer buffer = bufferArray[index]; - - final int remaining = buffer.remaining(); - final int processed = write(buffer); - - if (remaining == processed) { - total += processed; - } else { - throw new IllegalStateException( - "failed to write buffer in array"); - } - - } - - return total; - - } catch (final Throwable e) { - throw new IOException("failed to write buffer array", e); - } - } - - @Override - public TypeUDT typeUDT() { - return providerUDT().type(); - } - - /** java 7 */ - public SocketChannelUDT bind(final SocketAddress localAddress) throws IOException { - - socketUDT.bind((InetSocketAddress) localAddress); - - return this; - - } -} diff --git a/src/com/barchart/udt/util/HelpUDT.java b/src/com/barchart/udt/util/HelpUDT.java deleted file mode 100644 index d67f5468..00000000 --- a/src/com/barchart/udt/util/HelpUDT.java +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.util; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Collection; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.barchart.udt.EpollUDT; - -/** - * miscellaneous utilities - */ -public class HelpUDT { - - protected static final Logger log = LoggerFactory.getLogger(EpollUDT.class); - - public static long md5sum(final String text) { - - final byte[] defaultBytes = text.getBytes(); - - try { - - final MessageDigest algorithm = MessageDigest.getInstance("MD5"); - - algorithm.reset(); - - algorithm.update(defaultBytes); - - final byte digest[] = algorithm.digest(); - - final ByteBuffer buffer = ByteBuffer.wrap(digest); - - return buffer.getLong(); - - } catch (final NoSuchAlgorithmException e) { - - log.error("md5 failed", e); - - return 0; - - } - - } - - /** - * direct integer buffer with proper native byte order - */ - public static final IntBuffer newDirectIntBufer(final int capacity) { - /** java int is 4 bytes */ - return ByteBuffer. // - allocateDirect(capacity * 4). // - order(ByteOrder.nativeOrder()). // - asIntBuffer(); - } - - public static Set ungrowableSet(final Set set) { - return new UngrowableSet(set); - } - - public static Set unmodifiableSet(final Collection values) { - return new UnmodifiableSet(values); - } - - private HelpUDT() { - } - - public static final void checkBuffer(final ByteBuffer buffer) { - if (buffer == null) { - throw new IllegalArgumentException("buffer == null"); - } - if (!buffer.isDirect()) { - throw new IllegalArgumentException("must use DirectByteBuffer"); - } - } - - public static final void checkArray(final byte[] array) { - if (array == null) { - throw new IllegalArgumentException("array == null"); - } - } - - public static String constantFieldName(final Class klaz, - final Object instance) { - - final Field[] filedArray = klaz.getDeclaredFields(); - - for (final Field field : filedArray) { - - final int modifiers = field.getModifiers(); - - final boolean isConstant = true && // - Modifier.isPublic(modifiers) && // - Modifier.isStatic(modifiers) && // - Modifier.isFinal(modifiers) // - ; - - if (isConstant) { - try { - if (instance == field.get(null)) { - return field.getName(); - } - } catch (final Throwable e) { - log.debug("", e); - } - } - - } - - return "unknown"; - - } - - public static void checkSocketAddress(final InetSocketAddress socketAddress) { - if (socketAddress == null) { - throw new IllegalArgumentException("socketAddress can't be null"); - } - /** can not use in JNI ; internal InetAddress field is null */ - if (socketAddress.isUnresolved()) { - throw new IllegalArgumentException("socketAddress is unresolved : " - + socketAddress + " : check your DNS settings"); - } - } - -} diff --git a/src/com/barchart/udt/util/UngrowableSet.java b/src/com/barchart/udt/util/UngrowableSet.java deleted file mode 100644 index d8406adb..00000000 --- a/src/com/barchart/udt/util/UngrowableSet.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.util; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -class UngrowableSet implements Set { - - private final Set set; - - UngrowableSet(final Set set) { - this.set = set; - } - - @Override - public boolean add(final E o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean addAll(final Collection coll) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - set.clear(); - } - - @Override - public boolean contains(final Object o) { - return set.contains(o); - } - - @Override - public boolean containsAll(final Collection coll) { - return set.containsAll(coll); - } - - @Override - public boolean equals(final Object o) { - return set.equals(o); - } - - @Override - public int hashCode() { - return set.hashCode(); - } - - @Override - public boolean isEmpty() { - return set.isEmpty(); - } - - @Override - public Iterator iterator() { - return set.iterator(); - } - - @Override - public boolean remove(final Object o) { - return set.remove(o); - } - - @Override - public boolean removeAll(final Collection coll) { - return set.removeAll(coll); - } - - @Override - public boolean retainAll(final Collection coll) { - return set.retainAll(coll); - } - - @Override - public int size() { - return set.size(); - } - - @Override - public Object[] toArray() { - return set.toArray(); - } - - @Override - public T[] toArray(final T[] a) { - return set.toArray(a); - } - - @Override - public String toString() { - return set.toString(); - } - -} diff --git a/src/com/barchart/udt/util/UnmodifiableSet.java b/src/com/barchart/udt/util/UnmodifiableSet.java deleted file mode 100644 index fd08cdbe..00000000 --- a/src/com/barchart/udt/util/UnmodifiableSet.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Copyright (C) 2009-2013 Barchart, Inc. - * - * All rights reserved. Licensed under the OSI BSD License. - * - * http://www.opensource.org/licenses/bsd-license.php - */ -package com.barchart.udt.util; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -class UnmodifiableSet implements Set { - - private final Collection values; - - UnmodifiableSet(final Collection values) { - this.values = values; - } - - @Override - public boolean add(final E e) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean addAll(final Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean contains(final Object o) { - return values.contains(o); - } - - @Override - public boolean containsAll(final Collection c) { - return values.containsAll(c); - } - - @Override - public boolean isEmpty() { - return values.isEmpty(); - } - - @Override - public Iterator iterator() { - return values.iterator(); - } - - @Override - public boolean remove(final Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(final Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(final Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public int size() { - return values.size(); - } - - @Override - public Object[] toArray() { - return values.toArray(); - } - - @Override - public T[] toArray(final T[] a) { - return values.toArray(a); - } - -}