0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-26 23:01:04 +08:00

Java Poller patch

This commit is contained in:
Martin Sustrik 2010-02-26 17:28:09 +01:00
parent 4a1a83887d
commit deda7ca54a
4 changed files with 21 additions and 128 deletions

3
.gitignore vendored
View File

@ -44,9 +44,12 @@ bindings/python/setup.py
bindings/java/org_zmq_*.h
src/libzmq.pc
lib/
builds/msvc/*.suo
builds/msvc/*/*.user
builds/msvc/*/Debug
builds/msvc/*/Release
foreign/openpgm/*
!foreign/openpgm/*.tar.bz2
!foreign/openpgm/*.tar.gz
classdist_noinst.stamp
bindings/java/Zmq.jar

View File

@ -24,8 +24,6 @@
#include "org_zmq_Context.h"
static void *fetch_socket (JNIEnv *env, jobject socket);
/** Handle to Java's Context::contextHandle. */
static jfieldID ctx_handle_fid = NULL;
@ -112,107 +110,3 @@ JNIEXPORT void JNICALL Java_org_zmq_Context_finalize (JNIEnv *env,
put_context (env, obj, NULL);
assert (rc == 0);
}
JNIEXPORT jlong JNICALL Java_org_zmq_Context_poll (JNIEnv *env,
jobject obj,
jobjectArray socket_0mq,
jshortArray event_0mq,
jshortArray revent_0mq,
jlong timeout)
{
jsize ls_0mq = 0;
jsize le_0mq = 0;
jsize 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_0mq != le_0mq || ls_0mq != lr_0mq)
return 0;
jsize ls = ls_0mq;
if (ls <= 0)
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);
int err = 0;
const char *msg = "";
if (rc < 0) {
err = errno;
msg = zmq_strerror (err);
}
}
// 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.
*/
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;
}

View File

@ -12,15 +12,18 @@ jar_DATA = $(jarfile)
dist_noinst_JAVA = \
org/zmq/Context.java \
org/zmq/Socket.java
org/zmq/Socket.java \
org/zmq/Poller.java
lib_LTLIBRARIES = libjzmq.la
libjzmq_la_SOURCES = \
Context.cpp \
Socket.cpp
Socket.cpp \
Poller.cpp
nodist_libjzmq_la_SOURCES = \
org_zmq_Context.h \
org_zmq_Socket.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@
@ -30,13 +33,17 @@ BUILT_SOURCES = \
org/zmq/Context.class \
org_zmq_Context.h \
org/zmq/Socket.class \
org_zmq_Socket.h
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
@ -53,6 +60,13 @@ org_zmq_Socket.h: org/zmq/Socket.class
./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

18
bindings/java/org/zmq/Context.java Normal file → Executable file
View File

@ -26,10 +26,6 @@ public class Context {
public static final int POLL = 1;
public static final int POLLIN = 1;
public static final int POLLOUT = 2;
public static final int POLLERR = 4;
/**
* Class constructor.
*
@ -40,20 +36,6 @@ public class Context {
construct (appThreads, ioThreads, flags);
}
/**
* Issue a poll call on the specified 0MQ sockets.
* This function is experimental and may change in the future.
*
* @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.
*/
public native long poll (Socket[] socket,
short[] event,
short[] revent,
long timeout);
/** Initialize the JNI interface */
protected native void construct (int appThreads, int ioThreads, int flags);