Getting UDT to properly load from eclipse/idea/natively

This commit is contained in:
nathan 2015-07-17 02:46:51 +02:00
parent a35935ff85
commit e961f395a5
2 changed files with 178 additions and 185 deletions

View File

@ -27,7 +27,7 @@ import java.util.Set;
/** /**
* UDT native socket wrapper * UDT native socket wrapper
* <p> * <p/>
* note: current implementation supports IPv4 only (no IPv6) * note: current implementation supports IPv4 only (no IPv6)
*/ */
public public
@ -77,20 +77,20 @@ class SocketUDT {
* indication of inconsistent build. * indication of inconsistent build.
*/ */
@Native @Native
public static final int SIGNATURE_JNI = 20130512; // VersionUDT.BUILDTIME; public static final int SIGNATURE_JNI = 20150706; // VersionUDT.BUILDTIME;
/** /**
* infinite timeout: * infinite timeout:
* <p> * <p/>
* blocking send/receive * blocking send/receive
* <p> * <p/>
* epoll wait * epoll wait
*/ */
public static final int TIMEOUT_INFINITE = -1; public static final int TIMEOUT_INFINITE = -1;
/** /**
* zero timeout: * zero timeout:
* <p> * <p/>
* epoll wait * epoll wait
*/ */
public static long TIMEOUT_NONE = 0; public static long TIMEOUT_NONE = 0;
@ -133,7 +133,7 @@ class SocketUDT {
Method nativeMethod = exitClass.getMethod("isNative"); Method nativeMethod = exitClass.getMethod("isNative");
Object invoke = nativeMethod.invoke(null); Object invoke = nativeMethod.invoke(null);
if (invoke != null) { if (invoke != null) {
isNativeDeployed = (boolean) invoke; isNativeDeployed = (Boolean) invoke;
} }
} }
} catch (Throwable t) { } catch (Throwable t) {
@ -144,7 +144,13 @@ class SocketUDT {
final Logger logger = LoggerFactory.getLogger(SocketUDT.class); final Logger logger = LoggerFactory.getLogger(SocketUDT.class);
String moduleDir = System.getProperty("user.dir"); String moduleDir = System.getProperty("user.dir");
final String libraryDir = FileUtil.normalizeAsFile(moduleDir + "../../../dorkbox/Dorkbox-Network/natives"); String libraryDir = FileUtil.getParentRelativeToDir(moduleDir, "dorkbox");
File file = new File(libraryDir);
if (file.getName()
.equals("dorkbox")) {
file = file.getParentFile();
}
libraryDir = FileUtil.normalizeAsFile(file.getAbsolutePath() + "/dorkbox/dorkbox/Dorkbox-Network/natives");
if (libraryDir == null || libraryDir.length() == 0) { if (libraryDir == null || libraryDir.length() == 0) {
@ -192,12 +198,15 @@ class SocketUDT {
INIT_OK = true; INIT_OK = true;
log.debug("native library load & init OK"); log.debug("native library load & init OK");
}
public static
void init() {
} }
/** /**
* Cleans up global JNI references and the UDT library. * Cleans up global JNI references and the UDT library.
* <p> * <p/>
* The behavior of SocketUDT class after a call to cleanup is undefined, so * 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 * it should only ever be called once you are done and you are ready for the
* class loader to unload the JNI library * class loader to unload the JNI library
@ -222,7 +231,6 @@ class SocketUDT {
/** /**
* @return epoll id * @return epoll id
*
* @see <a * @see <a
* href="http://udt.sourceforge.net/udt4/doc/epoll.htm">UDT::epoll_create()</a> * href="http://udt.sourceforge.net/udt4/doc/epoll.htm">UDT::epoll_create()</a>
*/ */
@ -261,6 +269,7 @@ class SocketUDT {
* href="http://udt.sourceforge.net/udt4/doc/epoll.htm">UDT::epoll_wait()</a> * href="http://udt.sourceforge.net/udt4/doc/epoll.htm">UDT::epoll_wait()</a>
*/ */
protected static native protected static native
int epollWait0( //
int epollWait0( // int epollWait0( //
final int epollID, // final int epollID, //
final IntBuffer readBuffer, // final IntBuffer readBuffer, //
@ -354,21 +363,16 @@ class SocketUDT {
* {@link java.nio.DirectIntBuffer} info exchange.Timeout is in * {@link java.nio.DirectIntBuffer} info exchange.Timeout is in
* milliseconds. * milliseconds.
* *
* @param millisTimeout * @param millisTimeout http://udt.sourceforge.net/udt4/doc/epoll.htm
* * <p/>
* http://udt.sourceforge.net/udt4/doc/epoll.htm
*
* "Finally, for epoll_wait, negative timeout value will make the * "Finally, for epoll_wait, negative timeout value will make the
* function to wait until an event happens. If the timeout value * function to wait until an event happens. If the timeout value
* is 0, then the function returns immediately with any sockets * is 0, then the function returns immediately with any sockets
* associated an IO event. If timeout occurs before any event * associated an IO event. If timeout occurs before any event
* happens, the function returns 0". * happens, the function returns 0".
*
*
* @return <code><0</code> : should not happen<br> * @return <code><0</code> : should not happen<br>
* <code>=0</code> : timeout, no ready sockets<br> * <code>=0</code> : timeout, no ready sockets<br>
* <code>>0</code> : total number or reads, writes, exceptions<br> * <code>>0</code> : total number or reads, writes, exceptions<br>
*
* @see #epollWait0(int, IntBuffer, IntBuffer, IntBuffer, long) * @see #epollWait0(int, IntBuffer, IntBuffer, IntBuffer, long)
*/ */
public static public static
@ -397,7 +401,7 @@ class SocketUDT {
/** /**
* send from a complete byte[] array; * send from a complete byte[] array;
* * <p/>
* wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em> * wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em>
* *
* @see <a * @see <a
@ -416,7 +420,7 @@ class SocketUDT {
/** /**
* send from a portion of a byte[] array; * send from a portion of a byte[] array;
* * <p/>
* wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em> * wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em>
* *
* @see <a * @see <a
@ -437,7 +441,7 @@ class SocketUDT {
/** /**
* send from {@link java.nio.DirectByteBuffer}; * send from {@link java.nio.DirectByteBuffer};
* * <p/>
* wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em> * wrapper for <em>UDT::send()</em>, <em>UDT::sendmsg()</em>
* *
* @see <a * @see <a
@ -560,7 +564,7 @@ class SocketUDT {
/** /**
* native address family; read by JNI * native address family; read by JNI
* <p> * <p/>
* TODO add support for AF_INET6 * TODO add support for AF_INET6
*/ */
@Native @Native
@ -581,8 +585,7 @@ class SocketUDT {
* "Primary" socket. Default constructor; will apply * "Primary" socket. Default constructor; will apply
* {@link #setDefaultMessageSendMode()} * {@link #setDefaultMessageSendMode()}
* *
* @param type * @param type UDT socket type
* UDT socket type
*/ */
public public
SocketUDT(final TypeUDT type) throws ExceptionUDT { SocketUDT(final TypeUDT type) throws ExceptionUDT {
@ -600,8 +603,7 @@ class SocketUDT {
* "Secondary" socket. Made by {@link #accept0()}, will apply * "Secondary" socket. Made by {@link #accept0()}, will apply
* {@link #setDefaultMessageSendMode()} * {@link #setDefaultMessageSendMode()}
* *
* @param socketID * @param socketID UDT socket descriptor;
* UDT socket descriptor;
*/ */
protected protected
SocketUDT(final TypeUDT type, final int socketID) throws ExceptionUDT { SocketUDT(final TypeUDT type, final int socketID) throws ExceptionUDT {
@ -723,14 +725,14 @@ class SocketUDT {
/** /**
* Connect to remote UDT socket. * Connect to remote UDT socket.
* <p> * <p/>
* Can be blocking or non blocking call; depending on * Can be blocking or non blocking call; depending on
* {@link OptionUDT#Is_Receive_Synchronous} * {@link OptionUDT#Is_Receive_Synchronous}
* <p> * <p/>
* Timing: UDT uses hard coded connect timeout: * Timing: UDT uses hard coded connect timeout:
* <p> * <p/>
* normal socket: 3 seconds * normal socket: 3 seconds
* <p> * <p/>
* rendezvous socket: 30 seconds; when * rendezvous socket: 30 seconds; when
* {@link OptionUDT#Is_Randezvous_Connect_Enabled} is true * {@link OptionUDT#Is_Randezvous_Connect_Enabled} is true
* *
@ -765,7 +767,7 @@ class SocketUDT {
/** /**
* NOTE: catch all exceptions; else prevents GC * NOTE: catch all exceptions; else prevents GC
* <p> * <p/>
* NOTE: do not leak "this" references; else prevents GC * NOTE: do not leak "this" references; else prevents GC
*/ */
@Override @Override
@ -1042,7 +1044,7 @@ class SocketUDT {
/** /**
* Get "any blocking operation" timeout setting. * Get "any blocking operation" timeout setting.
* * <p/>
* Returns milliseconds; zero return means "infinite"; negative means * Returns milliseconds; zero return means "infinite"; negative means
* invalid * invalid
* *
@ -1136,7 +1138,6 @@ class SocketUDT {
* @return true : socket is valid and both send and receive are set to * @return true : socket is valid and both send and receive are set to
* blocking mode; false : at least one channel is set to * blocking mode; false : at least one channel is set to
* non-blocking mode or socket is invalid; * non-blocking mode or socket is invalid;
*
* @see #isNonBlocking() * @see #isNonBlocking()
* @see #setBlocking(boolean) * @see #setBlocking(boolean)
*/ */
@ -1255,9 +1256,7 @@ class SocketUDT {
} }
/** /**
* @param queueSize * @param queueSize maximum number of queued clients
* maximum number of queued clients
*
* @see #listen0(int) * @see #listen0(int)
*/ */
public public
@ -1403,8 +1402,7 @@ class SocketUDT {
/** /**
* send from byte[] array upto <code>size=array.length</code> bytes * send from byte[] array upto <code>size=array.length</code> bytes
* *
* @param array * @param array array to send
* array to send
* @return <code>-1</code> : no buffer space (non-blocking only) <br> * @return <code>-1</code> : no buffer space (non-blocking only) <br>
* <code>=0</code> : timeout expired (blocking only) <br> * <code>=0</code> : timeout expired (blocking only) <br>
* <code>>0</code> : normal send, actual sent byte count <br> * <code>>0</code> : normal send, actual sent byte count <br>
@ -1428,12 +1426,9 @@ class SocketUDT {
/** /**
* send from byte[] array upto <code>size=limit-position</code> bytes * send from byte[] array upto <code>size=limit-position</code> bytes
* *
* @param array * @param array array to send
* array to send * @param position start of array portion to send
* @param position * @param limit finish of array portion to send
* start of array portion to send
* @param limit
* finish of array portion to send
* @return <code>-1</code> : no buffer space (non-blocking only) <br> * @return <code>-1</code> : no buffer space (non-blocking only) <br>
* <code>=0</code> : timeout expired (blocking only) <br> * <code>=0</code> : timeout expired (blocking only) <br>
* <code>>0</code> : normal send, actual sent byte count <br> * <code>>0</code> : normal send, actual sent byte count <br>
@ -1464,8 +1459,7 @@ class SocketUDT {
* send from {@link java.nio.DirectByteBuffer}, upto * send from {@link java.nio.DirectByteBuffer}, upto
* {@link ByteBuffer#remaining()} bytes * {@link ByteBuffer#remaining()} bytes
* *
* @param buffer * @param buffer buffer to send
* buffer to send
* @return <code>-1</code> : no buffer space (non-blocking only)<br> * @return <code>-1</code> : no buffer space (non-blocking only)<br>
* <code>=0</code> : timeout expired (blocking only)<br> * <code>=0</code> : timeout expired (blocking only)<br>
* <code>>0</code> : normal send, actual sent byte count<br> * <code>>0</code> : normal send, actual sent byte count<br>
@ -1551,8 +1545,7 @@ class SocketUDT {
/** /**
* Configure socket in strict blocking / strict non-blocking mode. * Configure socket in strict blocking / strict non-blocking mode.
* *
* @param block * @param block true : set both send and receive to blocking mode; false : set
* true : set both send and receive to blocking mode; false : set
* both send and receive to non-blocking mode * both send and receive to non-blocking mode
* @see java.nio.channels.SocketChannel#configureBlocking(boolean) * @see java.nio.channels.SocketChannel#configureBlocking(boolean)
*/ */
@ -1570,7 +1563,7 @@ class SocketUDT {
/** /**
* Apply default settings for message mode. * Apply default settings for message mode.
* <p> * <p/>
* IsOdered = true;<br> * IsOdered = true;<br>
* TimeTolLive = INFINITE_TTL;<br> * TimeTolLive = INFINITE_TTL;<br>
*/ */
@ -1768,8 +1761,7 @@ class SocketUDT {
* Load updated statistics values into {@link #monitor} object. Must call * Load updated statistics values into {@link #monitor} object. Must call
* this methos only on connected socket. * this methos only on connected socket.
* *
* @param makeClear * @param makeClear true : reset all statistics with this call; false : keep
* true : reset all statistics with this call; false : keep
* collecting statistics, load updated values. * collecting statistics, load updated values.
* @see #updateMonitor0(boolean) * @see #updateMonitor0(boolean)
*/ */

View File

@ -1,32 +1,32 @@
/** /**
* Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/> * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
* * <p/>
* All rights reserved. Licensed under the OSI BSD License. * All rights reserved. Licensed under the OSI BSD License.
* * <p/>
* http://www.opensource.org/licenses/bsd-license.php * http://www.opensource.org/licenses/bsd-license.php
*/ */
package com.barchart.udt.net; package com.barchart.udt.net;
import com.barchart.udt.ErrorUDT;
import com.barchart.udt.SocketUDT;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.channels.IllegalBlockingModeException; import java.nio.channels.IllegalBlockingModeException;
import com.barchart.udt.ErrorUDT;
import com.barchart.udt.SocketUDT;
/** /**
* {@link OutputStream} for UDT sockets. * {@link OutputStream} for UDT sockets.
*/ */
public class NetOutputStreamUDT extends OutputStream { public
class NetOutputStreamUDT extends OutputStream {
protected final SocketUDT socketUDT; protected final SocketUDT socketUDT;
/** /**
* * @param socketUDT The UDT socket.
* @param socketUDT
* The UDT socket.
*/ */
public NetOutputStreamUDT(final SocketUDT socketUDT) { public
NetOutputStreamUDT(final SocketUDT socketUDT) {
if (!socketUDT.isBlocking()) { if (!socketUDT.isBlocking()) {
throw new IllegalBlockingModeException(); throw new IllegalBlockingModeException();
@ -37,32 +37,33 @@ public class NetOutputStreamUDT extends OutputStream {
} }
@Override @Override
public void write(final int b) throws IOException { public
void write(final int b) throws IOException {
// Just cast it -- this is the same thing SocketOutputStream does. // Just cast it -- this is the same thing SocketOutputStream does.
final byte[] bytes = { (byte) b }; final byte[] bytes = {(byte) b};
write(bytes); write(bytes);
} }
@Override @Override
public void write(final byte[] bytes) throws IOException { public
void write(final byte[] bytes) throws IOException {
write(bytes, 0, bytes.length); write(bytes, 0, bytes.length);
} }
@Override @Override
public void write(final byte[] bytes, final int off, final int len) public
throws IOException { void write(final byte[] bytes, final int off, final int len) throws IOException {
int bytesRemaining = len; int bytesRemaining = len;
while (bytesRemaining > 0) { while (bytesRemaining > 0) {
final int count = socketUDT.send(bytes, off + len - bytesRemaining, final int count = socketUDT.send(bytes, off + len - bytesRemaining, off + len);
off + len);
if (count > 0) { if (count > 0) {
bytesRemaining -= count; bytesRemaining -= count;
@ -70,23 +71,23 @@ public class NetOutputStreamUDT extends OutputStream {
} }
if (count == 0) { if (count == 0) {
throw new ExceptionSendUDT(socketUDT.id(), throw new ExceptionSendUDT(socketUDT.id(), ErrorUDT.USER_DEFINED_MESSAGE, "UDT send time out");
ErrorUDT.USER_DEFINED_MESSAGE, "UDT send time out");
} }
throw new IllegalStateException( throw new IllegalStateException("Socket has been changed to non-blocking");
"Socket has been chaged to non-blocking");
} }
} }
@Override @Override
public void close() throws IOException { public
void close() throws IOException {
socketUDT.close(); socketUDT.close();
} }
@Override @Override
public void flush() throws IOException { public
void flush() throws IOException {
socketUDT.flush(); socketUDT.flush();
} }
} }