diff options
Diffstat (limited to 'bindings/java')
-rwxr-xr-x | bindings/java/Context.cpp | 112 | ||||
-rw-r--r-- | bindings/java/Makefile.am | 72 | ||||
-rwxr-xr-x | bindings/java/Poller.cpp | 126 | ||||
-rwxr-xr-x | bindings/java/Socket.cpp | 345 | ||||
-rwxr-xr-x | bindings/java/org/zmq/Context.java | 58 | ||||
-rwxr-xr-x | bindings/java/org/zmq/Poller.java | 135 | ||||
-rw-r--r-- | bindings/java/org/zmq/Socket.java | 134 |
7 files changed, 0 insertions, 982 deletions
diff --git a/bindings/java/Context.cpp b/bindings/java/Context.cpp deleted file mode 100755 index 5556ed8..0000000 --- a/bindings/java/Context.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <assert.h> -#include <errno.h> - -#include "../c/zmq.h" - -#include "org_zmq_Context.h" - -/** Handle to Java's Context::contextHandle. */ -static jfieldID ctx_handle_fid = NULL; - -/** - * Make sure we have a valid pointer to Java's Context::contextHandle. - */ -static void ensure_context (JNIEnv *env, jobject obj) -{ - if (ctx_handle_fid == NULL) { - jclass cls = env->GetObjectClass (obj); - assert (cls); - ctx_handle_fid = env->GetFieldID (cls, "contextHandle", "J"); - assert (ctx_handle_fid); - env->DeleteLocalRef (cls); - } -} - -/** - * Get the value of Java's Context::contextHandle. - */ -static void *get_context (JNIEnv *env, jobject obj) -{ - ensure_context (env, obj); - void *s = (void*) env->GetLongField (obj, ctx_handle_fid); - return s; -} - -/** - * Set the value of Java's Context::contextHandle. - */ -static void put_context (JNIEnv *env, jobject obj, void *s) -{ - ensure_context (env, obj); - env->SetLongField (obj, ctx_handle_fid, (jlong) s); -} - -/** - * Raise an exception that includes 0MQ's error message. - */ -static void raise_exception (JNIEnv *env, int err) -{ - // Get exception class. - jclass exception_class = env->FindClass ("java/lang/Exception"); - assert (exception_class); - - // Get text description of the exception. - const char *err_msg = zmq_strerror (err); - - // Raise the exception. - int rc = env->ThrowNew (exception_class, err_msg); - env->DeleteLocalRef (exception_class); - - assert (rc == 0); -} - -/** - * Called to construct a Java Context object. - */ -JNIEXPORT void JNICALL Java_org_zmq_Context_construct (JNIEnv *env, - jobject obj, jint app_threads, jint io_threads, jint flags) -{ - void *c = get_context (env, obj); - assert (!c); - - c = zmq_init (app_threads, io_threads, flags); - put_context (env, obj, c); - - if (!c) { - raise_exception (env, errno); - return; - } -} - -/** - * Called to destroy a Java Context object. - */ -JNIEXPORT void JNICALL Java_org_zmq_Context_finalize (JNIEnv *env, - jobject obj) -{ - void *c = get_context (env, obj); - assert (c); - - int rc = zmq_term (c); - put_context (env, obj, NULL); - assert (rc == 0); -} diff --git a/bindings/java/Makefile.am b/bindings/java/Makefile.am deleted file mode 100644 index f2da01c..0000000 --- a/bindings/java/Makefile.am +++ /dev/null @@ -1,72 +0,0 @@ -# We do not want to install Jzmq.class file -# user has to copy it to the right location. -#jzmqdir = /tmp - -jarfile = Zmq.jar -jardir = $(datadir)/java - -$(jarfile): $(dist_noinst_JAVA) - $(JAR) cf $(JARFLAGS) $@ org/zmq/*.class - -jar_DATA = $(jarfile) - -dist_noinst_JAVA = \ - org/zmq/Context.java \ - org/zmq/Socket.java \ - org/zmq/Poller.java - -lib_LTLIBRARIES = libjzmq.la -libjzmq_la_SOURCES = \ - Context.cpp \ - Socket.cpp \ - Poller.cpp -nodist_libjzmq_la_SOURCES = \ - org_zmq_Context.h \ - org_zmq_Socket.h \ - org_zmq_Poller.h - -libjzmq_la_CXXFLAGS = @JAVA_INCLUDE@ -I$(top_srcdir)/bindings/c -Wall -libjzmq_la_LDFLAGS = -version-info @JLTVER@ -libjzmq_la_LIBADD = $(top_builddir)/src/libzmq.la - -BUILT_SOURCES = \ - org/zmq/Context.class \ - org_zmq_Context.h \ - org/zmq/Socket.class \ - org_zmq_Socket.h \ - org/zmq/Poller.class \ - org_zmq_Poller.h - -CLEANFILES = \ - org/zmq/Context.class \ - org_zmq_Context.h \ - org/zmq/Socket.class \ - org_zmq_Socket.h \ - org/zmq/Poller.class \ - org_zmq_Poller.h \ - Zmq.jar - -$(srcdir)/Context.cpp: org_zmq_Context.h - -org_zmq_Context.h: org/zmq/Context.class - $(CLASSPATH_ENV) $(JAVAH) -jni -classpath . org.zmq.Context - -./org/zmq/Context.class: classdist_noinst.stamp - -$(srcdir)/Socket.cpp: org_zmq_Socket.h - -org_zmq_Socket.h: org/zmq/Socket.class - $(CLASSPATH_ENV) $(JAVAH) -jni -classpath . org.zmq.Socket - -./org/zmq/Socket.class: classdist_noinst.stamp - -$(srcdir)/Poller.cpp: org_zmq_Poller.h - -org_zmq_Poller.h: org/zmq/Poller.class - $(CLASSPATH_ENV) $(JAVAH) -jni -classpath . org.zmq.Poller - -./org/zmq/Poller.class: classdist_noinst.stamp - -dist-hook: - -rm $(distdir)/*.h - diff --git a/bindings/java/Poller.cpp b/bindings/java/Poller.cpp deleted file mode 100755 index bf7a043..0000000 --- a/bindings/java/Poller.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <assert.h> -#include <errno.h> - -#include "../c/zmq.h" - -#include "org_zmq_Poller.h" - -static void *fetch_socket (JNIEnv *env, jobject socket); - -JNIEXPORT jlong JNICALL Java_org_zmq_Poller_run_1poll (JNIEnv *env, - jobject obj, - jint count, - jobjectArray socket_0mq, - jshortArray event_0mq, - jshortArray revent_0mq, - jlong timeout) -{ - int ls = (int) count; - if (ls <= 0) - return 0; - - int ls_0mq = 0; - int le_0mq = 0; - int lr_0mq = 0; - - if (socket_0mq) - ls_0mq = env->GetArrayLength (socket_0mq); - if (event_0mq) - le_0mq = env->GetArrayLength (event_0mq); - if (revent_0mq) - lr_0mq = env->GetArrayLength (revent_0mq); - - if (ls > ls_0mq || ls > le_0mq || ls > ls_0mq) - return 0; - - zmq_pollitem_t *pitem = new zmq_pollitem_t [ls]; - short pc = 0; - int rc = 0; - - // Add 0MQ sockets. - if (ls_0mq > 0) { - jshort *e_0mq = env->GetShortArrayElements (event_0mq, 0); - if (e_0mq != NULL) { - for (int i = 0; i < ls_0mq; ++i) { - jobject s_0mq = env->GetObjectArrayElement (socket_0mq, i); - if (!s_0mq) - continue; - void *s = fetch_socket (env, s_0mq); - if (!s) - continue; - pitem [pc].socket = s; - pitem [pc].fd = 0; - pitem [pc].events = e_0mq [i]; - pitem [pc].revents = 0; - ++pc; - } - env->ReleaseShortArrayElements(event_0mq, e_0mq, 0); - } - } - - if (pc == ls) { - pc = 0; - long tout = (long) timeout; - rc = zmq_poll (pitem, ls, tout); - } - - // Set 0MQ results. - if (ls_0mq > 0) { - jshort *r_0mq = env->GetShortArrayElements (revent_0mq, 0); - if (r_0mq) { - for (int i = 0; i < ls_0mq; ++i) { - r_0mq [i] = pitem [pc].revents; - ++pc; - } - env->ReleaseShortArrayElements(revent_0mq, r_0mq, 0); - } - } - - delete [] pitem; - return rc; -} - -/** - * Get the value of socketHandle for the specified Java Socket. - * TODO: move this to a single util.h file. - */ -static void *fetch_socket (JNIEnv *env, jobject socket) -{ - static jmethodID get_socket_handle_mid = NULL; - - if (get_socket_handle_mid == NULL) { - jclass cls = env->GetObjectClass (socket); - assert (cls); - get_socket_handle_mid = env->GetMethodID (cls, - "getSocketHandle", "()J"); - env->DeleteLocalRef (cls); - assert (get_socket_handle_mid); - } - - void *s = (void*) env->CallLongMethod (socket, get_socket_handle_mid); - if (env->ExceptionCheck ()) { - s = NULL; - } - - assert (s); - return s; -} diff --git a/bindings/java/Socket.cpp b/bindings/java/Socket.cpp deleted file mode 100755 index 51c473d..0000000 --- a/bindings/java/Socket.cpp +++ /dev/null @@ -1,345 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <string.h> -#include <assert.h> -#include <errno.h> - -#include "../../src/stdint.hpp" -#include "../c/zmq.h" - -#include "org_zmq_Socket.h" - -/** Handle to Java's Socket::socketHandle. */ -static jfieldID socket_handle_fid = NULL; - -/** - * Make sure we have a valid pointer to Java's Socket::socketHandle. - */ -static void ensure_socket (JNIEnv *env, jobject obj) -{ - if (socket_handle_fid == NULL) { - jclass cls = env->GetObjectClass (obj); - assert (cls); - socket_handle_fid = env->GetFieldID (cls, "socketHandle", "J"); - assert (socket_handle_fid); - env->DeleteLocalRef (cls); - } -} - -/** - * Get the value of Java's Socket::socketHandle. - */ -static void *get_socket (JNIEnv *env, jobject obj) -{ - ensure_socket (env, obj); - void *s = (void*) env->GetLongField (obj, socket_handle_fid); - return s; -} - -/** - * Set the value of Java's Socket::socketHandle. - */ -static void put_socket (JNIEnv *env, jobject obj, void *s) -{ - ensure_socket (env, obj); - env->SetLongField (obj, socket_handle_fid, (jlong) s); -} - -/** - * Get the value of contextHandle for the specified Java Context. - */ -static void *fetch_context (JNIEnv *env, jobject context) -{ - static jmethodID get_context_handle_mid = NULL; - - if (!get_context_handle_mid) { - jclass cls = env->GetObjectClass (context); - assert (cls); - get_context_handle_mid = env->GetMethodID (cls, - "getContextHandle", "()J"); - env->DeleteLocalRef (cls); - assert (get_context_handle_mid); - } - - void *c = (void*) env->CallLongMethod (context, get_context_handle_mid); - if (env->ExceptionCheck ()) { - c = NULL; - } - - assert (c); - return c; -} - -/** - * Raise an exception that includes 0MQ's error message. - */ -static void raise_exception (JNIEnv *env, int err) -{ - // Get exception class. - jclass exception_class = env->FindClass ("java/lang/Exception"); - assert (exception_class); - - // Get text description of the exception. - const char *err_msg = zmq_strerror (err); - - // Raise the exception. - int rc = env->ThrowNew (exception_class, err_msg); - env->DeleteLocalRef (exception_class); - - assert (rc == 0); -} - -/** - * Called to construct a Java Socket object. - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_construct (JNIEnv *env, - jobject obj, jobject context, jint type) -{ - void *s = get_socket (env, obj); - assert (! s); - - void *c = fetch_context (env, context); - s = zmq_socket (c, type); - put_socket(env, obj, s); - - if (s == NULL) { - raise_exception (env, errno); - return; - } -} - -/** - * Called to destroy a Java Socket object. - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_finalize (JNIEnv *env, - jobject obj) -{ - void *s = get_socket (env, obj); - assert (s); - - int rc = zmq_close (s); - put_socket (env, obj, NULL); - assert (rc == 0); -} - -/** - * Called by Java's Socket::setsockopt(int option, long optval). - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__IJ (JNIEnv *env, - jobject obj, jint option, jlong optval) -{ - switch (option) { - case ZMQ_HWM: - case ZMQ_LWM: - case ZMQ_SWAP: - case ZMQ_AFFINITY: - case ZMQ_RATE: - case ZMQ_RECOVERY_IVL: - case ZMQ_MCAST_LOOP: - { - void *s = get_socket (env, obj); - assert (s); - - int64_t value = optval; - int rc = zmq_setsockopt (s, option, &value, sizeof (value)); - if (rc != 0) - raise_exception (env, errno); - return; - } - default: - raise_exception (env, EINVAL); - return; - } -} - -/** - * Called by Java's Socket::setsockopt(int option, String optval). - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__ILjava_lang_String_2 ( - JNIEnv *env, jobject obj, jint option, jstring optval) -{ - switch (option) { - case ZMQ_IDENTITY: - case ZMQ_SUBSCRIBE: - case ZMQ_UNSUBSCRIBE: - { - if (optval == NULL) { - raise_exception (env, EINVAL); - return; - } - - void *s = get_socket (env, obj); - assert (s); - - const char *value = env->GetStringUTFChars (optval, NULL); - assert (value); - int rc = zmq_setsockopt (s, option, value, strlen (value)); - env->ReleaseStringUTFChars (optval, value); - if (rc != 0) - raise_exception (env, errno); - return; - } - default: - raise_exception (env, EINVAL); - return; - } -} - -/** - * Called by Java's Socket::bind(String addr). - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj, - jstring addr) -{ - void *s = get_socket (env, obj); - assert (s); - - if (addr == NULL) { - raise_exception (env, EINVAL); - return; - } - - const char *c_addr = env->GetStringUTFChars (addr, NULL); - if (c_addr == NULL) { - raise_exception (env, EINVAL); - return; - } - - int rc = zmq_bind (s, c_addr); - env->ReleaseStringUTFChars (addr, c_addr); - - if (rc == -1) - raise_exception (env, errno); -} - -/** - * Called by Java's Socket::connect(String addr). - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_connect (JNIEnv *env, - jobject obj, jstring addr) -{ - void *s = get_socket (env, obj); - assert (s); - - if (addr == NULL) { - raise_exception (env, EINVAL); - return; - } - - const char *c_addr = env->GetStringUTFChars (addr, NULL); - if (c_addr == NULL) { - raise_exception (env, EINVAL); - return; - } - - int rc = zmq_connect (s, c_addr); - env->ReleaseStringUTFChars (addr, c_addr); - - if (rc == -1) - raise_exception (env, errno); -} - -/** - * Called by Java's Socket::send(byte [] msg, long flags). - */ -JNIEXPORT jboolean JNICALL Java_org_zmq_Socket_send (JNIEnv *env, - jobject obj, jbyteArray msg, jlong flags) -{ - void *s = get_socket (env, obj); - assert (s); - - jsize size = env->GetArrayLength (msg); - jbyte *data = env->GetByteArrayElements (msg, 0); - - zmq_msg_t message; - int rc = zmq_msg_init_size (&message, size); - assert (rc == 0); - memcpy (zmq_msg_data (&message), data, size); - - env->ReleaseByteArrayElements (msg, data, 0); - - rc = zmq_send (s, &message, (int) flags); - - if (rc == -1 && errno == EAGAIN) { - rc = zmq_msg_close (&message); - assert (rc == 0); - return JNI_FALSE; - } - - if (rc == -1) { - raise_exception (env, errno); - rc = zmq_msg_close (&message); - assert (rc == 0); - return JNI_FALSE; - } - - rc = zmq_msg_close (&message); - assert (rc == 0); - return JNI_TRUE; -} - -/** - * Called by Java's Socket::flush(). - */ -JNIEXPORT void JNICALL Java_org_zmq_Socket_flush (JNIEnv *env, jobject obj) -{ - void *s = get_socket (env, obj); - assert (s); - - int rc = zmq_flush (s); - - if (rc == -1) { - raise_exception (env, errno); - return ; - } -} - -/** - * Called by Java's Socket::recv(long flags). - */ -JNIEXPORT jbyteArray JNICALL Java_org_zmq_Socket_recv (JNIEnv *env, - jobject obj, jlong flags) -{ - void *s = get_socket (env, obj); - assert (s); - - zmq_msg_t message; - zmq_msg_init (&message); - int rc = zmq_recv (s, &message, (int) flags); - - if (rc == -1 && errno == EAGAIN) { - zmq_msg_close (&message); - return NULL; - } - - if (rc == -1) { - raise_exception (env, errno); - zmq_msg_close (&message); - return NULL; - } - - jbyteArray data = env->NewByteArray (zmq_msg_size (&message)); - assert (data); - env->SetByteArrayRegion (data, 0, zmq_msg_size (&message), - (jbyte*) zmq_msg_data (&message)); - - return data; -} - diff --git a/bindings/java/org/zmq/Context.java b/bindings/java/org/zmq/Context.java deleted file mode 100755 index a727749..0000000 --- a/bindings/java/org/zmq/Context.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -package org.zmq; - -public class Context { - static { - System.loadLibrary("jzmq"); - } - - public static final int POLL = 1; - - /** - * Class constructor. - * - * @param appThreads maximum number of application threads. - * @param ioThreads size of the threads pool to handle I/O operations. - */ - public Context (int appThreads, int ioThreads, int flags) { - construct (appThreads, ioThreads, flags); - } - - /** Initialize the JNI interface */ - protected native void construct (int appThreads, int ioThreads, int flags); - - /** Free all resources used by JNI interface. */ - protected native void finalize (); - - /** - * Get the underlying context handle. - * This is private because it is only accessed from JNI, where - * Java access controls are ignored. - * - * @return the internal 0MQ context handle. - */ - private long getContextHandle () { - return contextHandle; - } - - /** Opaque data used by JNI driver. */ - private long contextHandle; -} diff --git a/bindings/java/org/zmq/Poller.java b/bindings/java/org/zmq/Poller.java deleted file mode 100755 index dd168b2..0000000 --- a/bindings/java/org/zmq/Poller.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -package org.zmq; - -public class Poller { - static { - System.loadLibrary("jzmq"); - } - - public static final int POLLIN = 1; - public static final int POLLOUT = 2; - public static final int POLLERR = 4; - - /** - * Class constructor. - * - * @param context a 0MQ context previously created. - */ - public Poller (Context context, int size) { - this.context = context; - this.size = size; - this.next = 0; - - this.socket = new Socket[size]; - this.event = new short[size]; - this.revent = new short[size]; - - for (int i = 0; i < size; ++i) { - this.event[i] = (POLLIN | POLLOUT | POLLERR); - } - } - - public int register (Socket socket) { - if (next >= size) - return -1; - this.socket[next] = socket; - return next++; - } - - public long getTimeout () { - return this.timeout; - } - - public void setTimeout (long timeout) { - this.timeout = timeout; - } - - public int getSize () { - return this.size; - } - - public int getNext () { - return this.next; - } - - /** - * Issue a poll call. - * @return how many objects where signalled by poll(). - */ - public long poll () { - if (size <= 0 || next <= 0) - return 0; - - for (int i = 0; i < next; ++i) { - revent[i] = 0; - } - - return run_poll(next, socket, event, revent, timeout); - } - - public boolean pollin(int index) { - return poll_mask(index, POLLIN); - } - - public boolean pollout(int index) { - return poll_mask(index, POLLOUT); - } - - public boolean pollerr(int index) { - return poll_mask(index, POLLERR); - } - - /** - * Issue a poll call on the specified 0MQ sockets. - * - * @param socket an array of 0MQ Socket objects to poll. - * @param event an array of short values specifying what to poll for. - * @param revent an array of short values with the results. - * @param timeout the maximum timeout in microseconds. - * @return how many objects where signalled by poll(). - */ - private native long run_poll(int count, - Socket[] socket, - short[] event, - short[] revent, - long timeout); - - /** - * Check whether a specific mask was signalled by latest poll call. - * - * @param index the index indicating the socket. - * @param mask a combination of POLLIN, POLLOUT and POLLERR. - * @return true if specific socket was signalled as specified. - */ - private boolean poll_mask(int index, int mask) { - if (mask <= 0 || index < 0 || index >= next) - return false; - return (revent[index] & mask) > 0; - } - - private Context context = null; - private long timeout = 0; - private int size = 0; - private int next = 0; - private Socket[] socket = null; - private short[] event = null; - private short[] revent = null; -} diff --git a/bindings/java/org/zmq/Socket.java b/bindings/java/org/zmq/Socket.java deleted file mode 100644 index 851b7b8..0000000 --- a/bindings/java/org/zmq/Socket.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - Copyright (c) 2007-2010 iMatix Corporation - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -package org.zmq; - -public class Socket { - static { - System.loadLibrary("jzmq"); - } - - public static final int NOBLOCK = 1; - public static final int NOFLUSH = 2; - - public static final int P2P = 0; - public static final int PUB = 1; - public static final int SUB = 2; - public static final int REQ = 3; - public static final int REP = 4; - public static final int XREQ = 5; - public static final int XREP = 6; - public static final int UPSTREAM = 7; - public static final int DOWNSTREAM = 8; - - public static final int HWM = 1; - public static final int LWM = 2; - public static final int SWAP = 3; - public static final int AFFINITY = 4; - public static final int IDENTITY = 5; - public static final int SUBSCRIBE = 6; - public static final int UNSUBSCRIBE = 7; - public static final int RATE = 8; - public static final int RECOVERY_IVL = 9; - public static final int MCAST_LOOP = 10; - public static final int SNDBUF = 11; - public static final int RCVBUF = 12; - - /** - * Class constructor. - * - * @param context a 0MQ context previously created. - * @param type the socket type. - */ - public Socket (Context context, int type) { - construct (context, type); - } - - /** - * Set the socket option value, given as a long. - * - * @param option ID of the option to set. - * @param optval value (as a long) to set the option to. - */ - public native void setsockopt (int option, long optval); - - /** - * Set the socket option value, given as a String. - * - * @param option ID of the option to set. - * @param optval value (as a String) to set the option to. - */ - public native void setsockopt (int option, String optval); - - /** - * Bind to network interface. Start listening for new connections. - * - * @param addr the endpoint to bind to. - */ - public native void bind (String addr); - - /** - * Connect to remote application. - * - * @param addr the endpoint to connect to. - */ - public native void connect (String addr); - - /** - * Send a message. - * - * @param msg the message to send, as an array of bytes. - * @param flags the flags to apply to the send operation. - * @return true if send was successful, false otherwise. - */ - public native boolean send (byte [] msg, long flags); - - /** - * Flush the messages down the stream. - */ - public native void flush (); - - /** - * Receive a message. - * - * @param flags the flags to apply to the receive operation. - * @return the message received, as an array of bytes; null on error. - */ - public native byte [] recv (long flags); - - /** Initialize the JNI interface */ - protected native void construct (Context context, int type); - - /** Free all resources used by JNI interface. */ - protected native void finalize (); - - /** - * Get the underlying socket handle. - * This is private because it is only accessed from JNI, where - * Java access controls are ignored. - * - * @return the internal 0MQ socket handle. - */ - private long getSocketHandle () { - return socketHandle; - } - - /** Opaque data used by JNI driver. */ - private long socketHandle; -} |