0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-08 13:25:53 +08:00
libzmq/bindings/java/Context.cpp

113 lines
2.9 KiB
C++
Raw Normal View History

2009-08-31 13:11:46 +02:00
/*
Copyright (c) 2007-2010 iMatix Corporation
2009-08-31 13:11:46 +02:00
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"
2009-08-31 13:11:46 +02:00
#include "org_zmq_Context.h"
/** Handle to Java's Context::contextHandle. */
2009-08-31 13:11:46 +02:00
static jfieldID ctx_handle_fid = NULL;
/**
* Make sure we have a valid pointer to Java's Context::contextHandle.
*/
2010-02-25 16:29:17 +01:00
static void ensure_context (JNIEnv *env, jobject obj)
{
if (ctx_handle_fid == NULL) {
jclass cls = env->GetObjectClass (obj);
2010-02-25 16:29:17 +01:00
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.
*/
2010-02-25 16:29:17 +01:00
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.
*/
2010-02-25 16:29:17 +01:00
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.
*/
2010-02-25 16:29:17 +01:00
static void raise_exception (JNIEnv *env, int err)
2009-08-31 13:11:46 +02:00
{
// 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);
2009-08-31 13:11:46 +02:00
// Raise the exception.
int rc = env->ThrowNew (exception_class, err_msg);
env->DeleteLocalRef (exception_class);
assert (rc == 0);
2009-08-31 13:11:46 +02:00
}
/**
* Called to construct a Java Context object.
*/
JNIEXPORT void JNICALL Java_org_zmq_Context_construct (JNIEnv *env,
2010-02-25 16:29:17 +01:00
jobject obj, jint app_threads, jint io_threads, jint flags)
2009-08-31 13:11:46 +02:00
{
void *c = get_context (env, obj);
2010-02-25 16:29:17 +01:00
assert (!c);
c = zmq_init (app_threads, io_threads, flags);
2010-02-25 16:29:17 +01:00
put_context (env, obj, c);
2009-08-31 13:11:46 +02:00
2010-02-25 16:29:17 +01:00
if (!c) {
2009-08-31 13:11:46 +02:00
raise_exception (env, errno);
return;
}
}
/**
* Called to destroy a Java Context object.
*/
JNIEXPORT void JNICALL Java_org_zmq_Context_finalize (JNIEnv *env,
2010-02-25 16:29:17 +01:00
jobject obj)
2009-08-31 13:11:46 +02:00
{
void *c = get_context (env, obj);
assert (c);
2009-08-31 13:11:46 +02:00
int rc = zmq_term (c);
put_context (env, obj, NULL);
2009-08-31 13:11:46 +02:00
assert (rc == 0);
}