summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@fastmq.commkdir>2009-09-05 09:30:12 +0200
committerMartin Sustrik <sustrik@fastmq.commkdir>2009-09-05 09:30:12 +0200
commit67253f3186488db93dee23bd9194419f58f0b1d3 (patch)
treee5e3ed1c3f1d4b2cf5f9a1b81f869a9bd9f14c06 /java
parent63b56d7fb38624c32111f9188d54b6fefb10a0e5 (diff)
Java perf tests added
Diffstat (limited to 'java')
-rw-r--r--java/Context.cpp15
-rw-r--r--java/Makefile.am22
-rw-r--r--java/Socket.cpp140
-rw-r--r--java/org/zmq/Context.java3
-rw-r--r--java/org/zmq/Message.java66
-rw-r--r--java/org/zmq/Socket.java42
6 files changed, 106 insertions, 182 deletions
diff --git a/java/Context.cpp b/java/Context.cpp
index d8fcf99..3fc66bd 100644
--- a/java/Context.cpp
+++ b/java/Context.cpp
@@ -27,8 +27,7 @@
static jfieldID ctx_handle_fid = NULL;
-static void
-raise_exception (JNIEnv *env, int err)
+static void raise_exception (JNIEnv *env, int err)
{
// Get exception class.
jclass exception_class = env->FindClass ("java/lang/Exception");
@@ -45,9 +44,8 @@ raise_exception (JNIEnv *env, int err)
env->DeleteLocalRef (exception_class);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Context_construct (JNIEnv *env, jobject obj,
- jint app_threads, jint io_threads)
+JNIEXPORT void JNICALL Java_org_zmq_Context_construct (JNIEnv *env, jobject obj,
+ jint app_threads, jint io_threads)
{
if (ctx_handle_fid == NULL) {
jclass cls = env->GetObjectClass (obj);
@@ -66,8 +64,7 @@ Java_org_zmq_Context_construct (JNIEnv *env, jobject obj,
env->SetLongField (obj, ctx_handle_fid, (jlong) ctx);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Context_finalize (JNIEnv *env, jobject obj)
+JNIEXPORT void JNICALL Java_org_zmq_Context_finalize (JNIEnv *env, jobject obj)
{
void *ctx = (void*) env->GetLongField (obj, ctx_handle_fid);
assert (ctx);
@@ -76,8 +73,8 @@ Java_org_zmq_Context_finalize (JNIEnv *env, jobject obj)
assert (rc == 0);
}
-JNIEXPORT jlong JNICALL
-Java_org_zmq_Context_createSocket (JNIEnv *env, jobject obj, jint type)
+JNIEXPORT jlong JNICALL Java_org_zmq_Context_createSocket (JNIEnv *env,
+ jobject obj, jint type)
{
void *ctx = (void*) env->GetLongField (obj, ctx_handle_fid);
assert (ctx);
diff --git a/java/Makefile.am b/java/Makefile.am
index 363f941..403da20 100644
--- a/java/Makefile.am
+++ b/java/Makefile.am
@@ -12,20 +12,17 @@ jar_DATA = $(jarfile)
dist_noinst_JAVA = \
org/zmq/Context.java \
- org/zmq/Socket.java \
- org/zmq/Message.java
+ org/zmq/Socket.java
lib_LTLIBRARIES = libjzmq.la
libjzmq_la_SOURCES = \
Context.cpp \
org_zmq_Context.h \
Socket.cpp \
- org_zmq_Socket.h \
- Message.cpp \
- org_zmq_Message.h
+ org_zmq_Socket.h
libjzmq_la_CXXFLAGS = -I$(top_builddir)/src/libzmq \
-@JAVA_INCLUDE@ -I$(top_builddir)/include -I$(top_srcdir)/libjzmq -Wall -I /usr/lib/jvm/java-6-openjdk/include
+@JAVA_INCLUDE@ -I$(top_builddir)/include -I$(top_srcdir)/libjzmq -Wall
libjzmq_la_LDFLAGS = -version-info 0:0:0
libjzmq_la_LIBADD = $(top_builddir)/src/libzmq.la
@@ -33,17 +30,13 @@ BUILT_SOURCES = \
org/zmq/Context.class \
org_zmq_Context.h \
org/zmq/Socket.class \
- org_zmq_Socket.h \
- org/zmq/Message.class \
- org_zmq_Message.h
+ org_zmq_Socket.h
CLEANFILES = \
org/zmq/Context.class \
org_zmq_Context.h \
org/zmq/Socket.class \
org_zmq_Socket.h \
- org/zmq/Message.class \
- org_zmq_Message.h \
Zmq.jar
$(srcdir)/Context.cpp: org_zmq_Context.h
@@ -60,13 +53,6 @@ org_zmq_Socket.h: org/zmq/Socket.class
./org/zmq/Socket.class: classdist_noinst.stamp
-$(srcdir)/Message.cpp: org_zmq_Message.h
-
-org_zmq_Message.h: org/zmq/Message.class
- $(CLASSPATH_ENV) $(JAVAH) -jni -classpath . org.zmq.Message
-
-./org/zmq/Message.class: classdist_noinst.stamp
-
dist-hook:
-rm $(distdir)/*.h
diff --git a/java/Socket.cpp b/java/Socket.cpp
index eb3c1e3..f87c298 100644
--- a/java/Socket.cpp
+++ b/java/Socket.cpp
@@ -26,13 +26,9 @@
#include "org_zmq_Socket.h"
static jfieldID socket_handle_fid = NULL;
-static jclass msg_class = NULL;
-static jmethodID msg_constructor;
-static jmethodID get_msg_handle_mid = NULL;
static jmethodID create_socket_mid = NULL;
-static void
-raise_exception (JNIEnv *env, int err)
+static void raise_exception (JNIEnv *env, int err)
{
// Get exception class.
jclass exception_class = env->FindClass ("java/lang/Exception");
@@ -49,9 +45,8 @@ raise_exception (JNIEnv *env, int err)
env->DeleteLocalRef (exception_class);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_construct (JNIEnv *env, jobject obj, jobject context,
- jint type)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_construct (JNIEnv *env, jobject obj,
+ jobject context, jint type)
{
if (socket_handle_fid == NULL) {
jclass cls = env->GetObjectClass (obj);
@@ -69,30 +64,14 @@ Java_org_zmq_Socket_construct (JNIEnv *env, jobject obj, jobject context,
env->DeleteLocalRef (cls);
}
- if (msg_class == NULL) {
- jclass cls = env->FindClass ("org/zmq/Message");
- assert (cls);
-
- msg_constructor = env->GetMethodID (cls, "<init>", "()V");
- assert (msg_constructor);
-
- get_msg_handle_mid = env->GetMethodID (cls, "getMsgHandle", "()J");
- assert (get_msg_handle_mid);
-
- msg_class = (jclass) env->NewGlobalRef (cls);
- assert (msg_class);
- env->DeleteLocalRef (cls);
- }
-
- void *s = (void *) env->CallLongMethod (context, create_socket_mid, type);
+ void *s = (void*) env->CallLongMethod (context, create_socket_mid, type);
if (env->ExceptionCheck ())
return;
env->SetLongField (obj, socket_handle_fid, (jlong) s);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_finalize (JNIEnv *env, jobject obj)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_finalize (JNIEnv *env, jobject obj)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -100,8 +79,8 @@ Java_org_zmq_Socket_finalize (JNIEnv *env, jobject obj)
assert (rc == 0);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setHwm (JNIEnv *env, jobject obj, jlong hwm)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setHwm (JNIEnv *env, jobject obj,
+ jlong hwm)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -110,8 +89,8 @@ Java_org_zmq_Socket_setHwm (JNIEnv *env, jobject obj, jlong hwm)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setLwm (JNIEnv *env, jobject obj, jlong lwm)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setLwm (JNIEnv *env, jobject obj,
+ jlong lwm)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -121,8 +100,8 @@ Java_org_zmq_Socket_setLwm (JNIEnv *env, jobject obj, jlong lwm)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setSwap (JNIEnv *env, jobject obj, jlong swap_size)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setSwap (JNIEnv *env, jobject obj,
+ jlong swap_size)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -132,8 +111,8 @@ Java_org_zmq_Socket_setSwap (JNIEnv *env, jobject obj, jlong swap_size)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setMask (JNIEnv *env, jobject obj, jlong mask)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setMask (JNIEnv *env, jobject obj,
+ jlong mask)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -143,8 +122,8 @@ Java_org_zmq_Socket_setMask (JNIEnv *env, jobject obj, jlong mask)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setAffinity (JNIEnv *env, jobject obj, jlong affinity)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setAffinity (JNIEnv *env,
+ jobject obj, jlong affinity)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -154,8 +133,8 @@ Java_org_zmq_Socket_setAffinity (JNIEnv *env, jobject obj, jlong affinity)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_setIdentity (JNIEnv *env, jobject obj, jstring identity)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_setIdentity (JNIEnv *env,
+ jobject obj, jstring identity)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -176,8 +155,8 @@ Java_org_zmq_Socket_setIdentity (JNIEnv *env, jobject obj, jstring identity)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj, jstring addr)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj,
+ jstring addr)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -188,8 +167,10 @@ Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj, jstring addr)
}
const char *c_addr = env->GetStringUTFChars (addr, NULL);
- if (c_addr == NULL)
+ if (c_addr == NULL) {
+ raise_exception (env, EINVAL);
return;
+ }
int rc = zmq_bind (s, c_addr);
env->ReleaseStringUTFChars (addr, c_addr);
@@ -198,8 +179,8 @@ Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj, jstring addr)
raise_exception (env, errno);
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_connect (JNIEnv *env, jobject obj, jstring addr)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_connect (JNIEnv *env, jobject obj,
+ jstring addr)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
@@ -210,8 +191,10 @@ Java_org_zmq_Socket_connect (JNIEnv *env, jobject obj, jstring addr)
}
const char *c_addr = env->GetStringUTFChars (addr, NULL);
- if (c_addr == NULL)
+ if (c_addr == NULL) {
+ raise_exception (env, EINVAL);
return;
+ }
int rc = zmq_connect (s, c_addr);
env->ReleaseStringUTFChars (addr, c_addr);
@@ -220,57 +203,80 @@ Java_org_zmq_Socket_connect (JNIEnv *env, jobject obj, jstring addr)
raise_exception (env, errno);
}
-JNIEXPORT jint JNICALL
-Java_org_zmq_Socket_send (JNIEnv *env, jobject obj, jobject msg, jlong flags)
+JNIEXPORT jboolean JNICALL Java_org_zmq_Socket_send (JNIEnv *env, jobject obj,
+ jbyteArray msg, jlong flags)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
- zmq_msg_t *zmq_msg = (zmq_msg_t *)
- env->CallLongMethod (msg, get_msg_handle_mid);
+ jsize size = env->GetArrayLength (msg);
+ jbyte *data = env->GetByteArrayElements (msg, 0);
- if (env->ExceptionCheck ())
- return -1;
+ zmq_msg_t message;
+ int rc = zmq_msg_init_size (&message, size);
+ assert (rc == 0);
+ memcpy (zmq_msg_data (&message), data, size);
- int rc = zmq_send (s, zmq_msg, (int) flags);
+ 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);
- return -1;
+ rc = zmq_msg_close (&message);
+ assert (rc == 0);
+ return JNI_FALSE;
}
- return rc;
+ rc = zmq_msg_close (&message);
+ assert (rc == 0);
+ return JNI_TRUE;
}
-JNIEXPORT void JNICALL
-Java_org_zmq_Socket_flush (JNIEnv *env, jobject obj)
+JNIEXPORT void JNICALL Java_org_zmq_Socket_flush (JNIEnv *env, jobject obj)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
- zmq_flush (s);
+ int rc = zmq_flush (s);
+
+ if (rc == -1) {
+ raise_exception (env, errno);
+ return ;
+ }
}
-JNIEXPORT jobject JNICALL
-Java_org_zmq_Socket_recv (JNIEnv *env, jobject obj, jlong flags)
+JNIEXPORT jbyteArray JNICALL Java_org_zmq_Socket_recv (JNIEnv *env, jobject obj,
+ jlong flags)
{
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
- jobject msg = env->NewObject (msg_class, msg_constructor);
- if (msg == NULL)
- return NULL;
-
- zmq_msg_t *zmq_msg = (zmq_msg_t*)
- env->CallLongMethod (msg, get_msg_handle_mid);
+ zmq_msg_t message;
+ zmq_msg_init (&message);
+ int rc = zmq_recv (s, &message, (int) flags);
- if (env->ExceptionCheck ())
+ if (rc == -1 && errno == EAGAIN) {
+ zmq_msg_close (&message);
return NULL;
+ }
- int rc = zmq_recv (s, zmq_msg, (int) flags);
if (rc == -1) {
raise_exception (env, errno);
+ zmq_msg_close (&message);
return NULL;
}
- return msg;
+ 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/java/org/zmq/Context.java b/java/org/zmq/Context.java
index 8bc4d57..c63ef60 100644
--- a/java/org/zmq/Context.java
+++ b/java/org/zmq/Context.java
@@ -34,6 +34,9 @@ public class Context {
construct (appThreads, ioThreads);
}
+ /**
+ * Internal function. Do not use directly!
+ */
public native long createSocket (int type);
/** Initialize the JNI interface */
diff --git a/java/org/zmq/Message.java b/java/org/zmq/Message.java
deleted file mode 100644
index 3691410..0000000
--- a/java/org/zmq/Message.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- Copyright (c) 2007-2009 FastMQ Inc.
-
- 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 Message {
- static {
- System.loadLibrary("jzmq");
- }
-
- /**
- * Class constructor.
- */
- public Message () {
- construct ();
- }
-
- public Message (byte [] payload) {
- constructWithData (payload);
- }
-
- /**
- * Get message payload.
- */
- public native byte [] getMsgPayload ();
-
- /**
- * Get message type.
- */
- public native int getMsgType ();
-
- /**
- * Get low-level message handler.
- */
- public long getMsgHandle () {
- return msgHandle;
- }
-
- /** Initialize the JNI interface */
- protected native void construct ();
-
- protected native void constructWithData (byte [] payload);
-
- /** Free resources used by JNI driver. */
- protected native void finalize ();
-
- /** Opaque data used by JNI driver. */
- private long msgHandle;
-}
-
diff --git a/java/org/zmq/Socket.java b/java/org/zmq/Socket.java
index 3bfbfec..832467f 100644
--- a/java/org/zmq/Socket.java
+++ b/java/org/zmq/Socket.java
@@ -19,26 +19,26 @@
package org.zmq;
-public class Socket {
+public class Socket
+{
+
static {
System.loadLibrary("jzmq");
}
- public static final int ZMQ_MAX_VSM_SIZE = 30;
-
- public static final int ZMQ_GAP = 1;
+ public static final int NOBLOCK = 1;
- public static final int ZMQ_DELIMITER = 31;
+ public static final int NOFLUSH = 2;
- public static final int ZMQ_NOBLOCK = 1;
+ public static final int P2P = 0;
- public static final int ZMQ_NOFLUSH = 2;
+ public static final int PUB = 1;
- public static final int ZMQ_P2P = 0;
+ public static final int SUB = 2;
- public static final int ZMQ_PUB = 1;
+ public static final int REQ = 3;
- public static final int ZMQ_SUB = 2;
+ public static final int REP = 4;
/**
* Class constructor.
@@ -47,7 +47,6 @@ public class Socket {
* @param type
*/
public Socket (Context context, int type) {
- ctx = context;
construct (context, type);
}
@@ -94,37 +93,39 @@ public class Socket {
public native void setIdentity (String identity);
/**
+ * Bind to network interface. Start listening for new connections.
+ *
* @param addr
*/
public native void bind (String addr);
/**
- * Connect.
+ * Connect to remote application.
*
* @param addr
*/
public native void connect (String addr);
/**
- * Send.
+ * Send the message.
*
- * @param message
- * @param block
+ * @param msg
+ * @param flags
*/
- public native int send (Message msg, long flags);
+ public native boolean send (byte [] msg, long flags);
/**
- * Flush all messages sent with flush flag false down the stream.
+ * Flush the messages down the stream.
*/
public native void flush ();
/**
* Receive message.
*
- * @param block
+ * @param flags
* @return
*/
- public native Message recv (long flags);
+ public native byte [] recv (long flags);
/** Initialize JNI driver */
protected native void construct (Context context, int type);
@@ -132,9 +133,6 @@ public class Socket {
/** Free all resources used by JNI driver. */
protected native void finalize ();
- /** Keep reference to ZMQ context so it is not garbage collected */
- private Context ctx;
-
/** Opaque data used by JNI driver. */
private long socketHandle;