Commit c806aabb authored by Martin Sustrik's avatar Martin Sustrik

java binding sets socket options using setsockopt function

parent 36707529
...@@ -44,16 +44,15 @@ extern "C" { ...@@ -44,16 +44,15 @@ extern "C" {
#define ZMQ_VSM 32 #define ZMQ_VSM 32
// Socket options. // Socket options.
#define ZMQ_HWM 1 #define ZMQ_HWM 1 // int64_t
#define ZMQ_LWM 2 #define ZMQ_LWM 2 // int64_t
#define ZMQ_SWAP 3 #define ZMQ_SWAP 3 // int64_t
#define ZMQ_MASK 4 #define ZMQ_AFFINITY 4 // int64_t
#define ZMQ_AFFINITY 5 #define ZMQ_IDENTITY 5 // string
#define ZMQ_IDENTITY 6 #define ZMQ_SUBSCRIBE 6 // string
#define ZMQ_SUBSCRIBE 7 #define ZMQ_UNSUBSCRIBE 7 // string
#define ZMQ_UNSUBSCRIBE 8 #define ZMQ_RATE 8 // int64_t
#define ZMQ_RATE 9 #define ZMQ_RECOVERY_IVL 9 // int64_t
#define ZMQ_RECOVERY_IVL 10
// The operation should be performed in non-blocking mode. I.e. if it cannot // The operation should be performed in non-blocking mode. I.e. if it cannot
// be processed immediately, error should be returned with errno set to EAGAIN. // be processed immediately, error should be returned with errno set to EAGAIN.
......
...@@ -86,80 +86,60 @@ JNIEXPORT void JNICALL Java_org_zmq_Socket_finalize (JNIEnv *env, jobject obj) ...@@ -86,80 +86,60 @@ JNIEXPORT void JNICALL Java_org_zmq_Socket_finalize (JNIEnv *env, jobject obj)
assert (rc == 0); assert (rc == 0);
} }
JNIEXPORT void JNICALL Java_org_zmq_Socket_setHwm (JNIEnv *env, jobject obj, JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__IJ (JNIEnv *env,
jlong hwm) jobject obj, jint option, jlong optval)
{ {
void *s = (void*) env->GetLongField (obj, socket_handle_fid); switch (option) {
assert (s); case ZMQ_HWM:
int rc = zmq_setsockopt (s, ZMQ_HWM, &hwm, sizeof hwm); case ZMQ_LWM:
if (rc == -1) case ZMQ_SWAP:
raise_exception (env, errno); case ZMQ_AFFINITY:
} case ZMQ_RATE:
case ZMQ_RECOVERY_IVL:
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);
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s); int64_t value = optval;
int rc = zmq_setsockopt (s, option, &value, sizeof (value));
int rc = zmq_setsockopt (s, ZMQ_LWM, &lwm, sizeof lwm); if (rc != 0)
if (rc == -1) raise_exception (env, errno);
raise_exception (env, errno); return;
} }
default:
JNIEXPORT void JNICALL Java_org_zmq_Socket_setSwap (JNIEnv *env, jobject obj, raise_exception (env, EINVAL);
jlong swap_size) return;
{ }
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
assert (s);
int rc = zmq_setsockopt (s, ZMQ_SWAP, &swap_size, sizeof swap_size);
if (rc == -1)
raise_exception (env, errno);
}
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);
int rc = zmq_setsockopt (s, ZMQ_MASK, &mask, sizeof mask);
if (rc == -1)
raise_exception (env, errno);
}
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);
int rc = zmq_setsockopt (s, ZMQ_AFFINITY, &affinity, sizeof affinity);
if (rc == -1)
raise_exception (env, errno);
} }
JNIEXPORT void JNICALL Java_org_zmq_Socket_setIdentity (JNIEnv *env, JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__ILjava_lang_String_2 (
jobject obj, jstring identity) JNIEnv *env, jobject obj, jint option, jstring optval)
{ {
void *s = (void*) env->GetLongField (obj, socket_handle_fid); switch (option) {
assert (s); case ZMQ_IDENTITY:
case ZMQ_SUBSCRIBE:
if (identity == NULL) { case ZMQ_UNSUBSCRIBE:
{
if (optval == NULL) {
raise_exception (env, EINVAL);
return;
}
void *s = (void*) env->GetLongField (obj, socket_handle_fid);
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); raise_exception (env, EINVAL);
return; return;
} }
const char *c_identity = env->GetStringUTFChars (identity, NULL);
if (c_identity == NULL)
return;
int rc = zmq_setsockopt (s, ZMQ_IDENTITY, c_identity, sizeof c_identity);
env->ReleaseStringUTFChars (identity, c_identity);
if (rc == -1)
raise_exception (env, errno);
} }
JNIEXPORT void JNICALL Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj, JNIEXPORT void JNICALL Java_org_zmq_Socket_bind (JNIEnv *env, jobject obj,
......
...@@ -27,19 +27,24 @@ public class Socket ...@@ -27,19 +27,24 @@ public class Socket
} }
public static final int NOBLOCK = 1; public static final int NOBLOCK = 1;
public static final int NOFLUSH = 2; public static final int NOFLUSH = 2;
public static final int P2P = 0; public static final int P2P = 0;
public static final int PUB = 1; public static final int PUB = 1;
public static final int SUB = 2; public static final int SUB = 2;
public static final int REQ = 3; public static final int REQ = 3;
public static final int REP = 4; public static final int REP = 4;
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;
/** /**
* Class constructor. * Class constructor.
* *
...@@ -51,46 +56,13 @@ public class Socket ...@@ -51,46 +56,13 @@ public class Socket
} }
/** /**
* Set the high watermark on the socket. * Set the socket option value.
*
* @param hwm high watermark.
*/
public native void setHwm (long hwm);
/**
* Set the low watermark on the socket.
*
* @param lwm low watermark.
*/
public native void setLwm (long lwm);
/**
* Set swap size.
*
* @param swap_size swap size.
*/
public native void setSwap (long swap_size);
/**
* Set reception mask.
*
* @param mask mask.
*/
public native void setMask (long mask);
/**
* Set affinity.
*
* @param affinity
*/
public native void setAffinity (long affinity);
/**
* Set identity.
* *
* @param identity * @param option ID of the option to set
* @param optval value to set the option to
*/ */
public native void setIdentity (String identity); public native void setsockopt (int option, long optval);
public native void setsockopt (int option, String optval);
/** /**
* Bind to network interface. Start listening for new connections. * Bind to network interface. Start listening for new connections.
......
...@@ -23,9 +23,8 @@ zmq::options_t::options_t () : ...@@ -23,9 +23,8 @@ zmq::options_t::options_t () :
hwm (0), hwm (0),
lwm (0), lwm (0),
swap (0), swap (0),
mask (0),
affinity (0), affinity (0),
rate (0), rate (100),
recovery_ivl (0) recovery_ivl (10)
{ {
} }
...@@ -34,7 +34,6 @@ namespace zmq ...@@ -34,7 +34,6 @@ namespace zmq
int64_t hwm; int64_t hwm;
int64_t lwm; int64_t lwm;
int64_t swap; int64_t swap;
uint64_t mask;
uint64_t affinity; uint64_t affinity;
std::string identity; std::string identity;
......
...@@ -125,14 +125,6 @@ int zmq::socket_base_t::setsockopt (int option_, const void *optval_, ...@@ -125,14 +125,6 @@ int zmq::socket_base_t::setsockopt (int option_, const void *optval_,
options.swap = *((int64_t*) optval_); options.swap = *((int64_t*) optval_);
return 0; return 0;
case ZMQ_MASK:
if (optvallen_ != sizeof (int64_t)) {
errno = EINVAL;
return -1;
}
options.mask = (uint64_t) *((int64_t*) optval_);
return 0;
case ZMQ_AFFINITY: case ZMQ_AFFINITY:
if (optvallen_ != sizeof (int64_t)) { if (optvallen_ != sizeof (int64_t)) {
errno = EINVAL; errno = EINVAL;
...@@ -151,19 +143,19 @@ int zmq::socket_base_t::setsockopt (int option_, const void *optval_, ...@@ -151,19 +143,19 @@ int zmq::socket_base_t::setsockopt (int option_, const void *optval_,
return -1; return -1;
case ZMQ_RATE: case ZMQ_RATE:
if (optvallen_ != sizeof (uint32_t)) { if (optvallen_ != sizeof (int64_t)) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
options.rate = *((int32_t*) optval_); options.rate = (uint32_t) *((int64_t*) optval_);
return 0; return 0;
case ZMQ_RECOVERY_IVL: case ZMQ_RECOVERY_IVL:
if (optvallen_ != sizeof (uint32_t)) { if (optvallen_ != sizeof (int64_t)) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
options.recovery_ivl = *((int32_t*) optval_); options.recovery_ivl = (uint32_t) *((int64_t*) optval_);
return 0; return 0;
default: default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment