Skip to content

Commit

Permalink
add getter and setter methods for keepalive options
Browse files Browse the repository at this point in the history
Implemented Java methods to get and set following keepalive options:
ZMQ_TCP_KEEPALIVE
ZMQ_TCP_KEEPALIVE_CNT
ZMQ_TCP_KEEPALIVE_IDLE
ZMQ_TCP_KEEPALIVE_INTVL
The appropriate C++ functionality is already implemented and availbale
with version >= 3.2
jira issue LIBZMQ-435
  • Loading branch information
sammedic committed Sep 28, 2012
1 parent 3c43094 commit 00b1df4
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/Socket.cpp
Expand Up @@ -124,6 +124,12 @@ JNIEXPORT jlong JNICALL Java_org_zeromq_ZMQ_00024Socket_getLongSockopt (JNIEnv *
case ZMQ_FD:
case ZMQ_EVENTS:
case ZMQ_LINGER:
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,2,0)
case ZMQ_TCP_KEEPALIVE:
case ZMQ_TCP_KEEPALIVE_IDLE:
case ZMQ_TCP_KEEPALIVE_CNT:
case ZMQ_TCP_KEEPALIVE_INTVL:
#endif
case ZMQ_AFFINITY:
case ZMQ_RATE:
Expand All @@ -136,12 +142,26 @@ JNIEXPORT jlong JNICALL Java_org_zeromq_ZMQ_00024Socket_getLongSockopt (JNIEnv *
jlong ret = 0;
int rc = 0;
int err = 0;

uint64_t optval = 0;
size_t optvallen = sizeof(optval);
rc = zmq_getsockopt (s, option, &optval, &optvallen);
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,2,0)
if(
(option == ZMQ_TCP_KEEPALIVE)
|| (option == ZMQ_TCP_KEEPALIVE_IDLE)
|| (option == ZMQ_TCP_KEEPALIVE_CNT)
|| (option == ZMQ_TCP_KEEPALIVE_INTVL)
) {
int optval = 0;
size_t optvallen = sizeof(optval);
rc = zmq_getsockopt (s, option, &optval, &optvallen);
ret = (jlong) optval;
} else
#endif
{
uint64_t optval = 0;
size_t optvallen = sizeof(optval);
rc = zmq_getsockopt (s, option, &optval, &optvallen);
ret = (jlong) optval;
}
err = zmq_errno();
ret = (jlong) optval;

if (rc != 0) {
raise_exception (env, err);
Expand Down Expand Up @@ -224,6 +244,12 @@ JNIEXPORT void JNICALL Java_org_zeromq_ZMQ_00024Socket_setLongSockopt (JNIEnv *e
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(2,1,0)
case ZMQ_LINGER:
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,2,0)
case ZMQ_TCP_KEEPALIVE:
case ZMQ_TCP_KEEPALIVE_IDLE:
case ZMQ_TCP_KEEPALIVE_CNT:
case ZMQ_TCP_KEEPALIVE_INTVL:
#endif
case ZMQ_AFFINITY:
case ZMQ_RATE:
Expand All @@ -234,7 +260,6 @@ JNIEXPORT void JNICALL Java_org_zeromq_ZMQ_00024Socket_setLongSockopt (JNIEnv *e
void *s = get_socket (env, obj, 1);
int rc = 0;
int err = 0;
uint64_t optval = (uint64_t) value;

#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(2,1,0)
if(
Expand All @@ -248,14 +273,21 @@ JNIEXPORT void JNICALL Java_org_zeromq_ZMQ_00024Socket_setLongSockopt (JNIEnv *e
|| (option == ZMQ_SNDTIMEO)
|| (option == ZMQ_RCVTIMEO)
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,2,0)
|| (option == ZMQ_TCP_KEEPALIVE)
|| (option == ZMQ_TCP_KEEPALIVE_IDLE)
|| (option == ZMQ_TCP_KEEPALIVE_CNT)
|| (option == ZMQ_TCP_KEEPALIVE_INTVL)
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(2,1,0)
) {
int ival = (int) optval;
int ival = (int) value;
size_t optvallen = sizeof(ival);
rc = zmq_setsockopt (s, option, &ival, optvallen);
} else
#endif
{
uint64_t optval = (uint64_t) value;
size_t optvallen = sizeof(optval);
rc = zmq_setsockopt (s, option, &optval, optvallen);
}
Expand Down
105 changes: 105 additions & 0 deletions src/org/zeromq/ZMQ.java
Expand Up @@ -510,6 +510,54 @@ public long getAffinity () {
return getLongSockopt (AFFINITY);
}

/**
* @see #setTCPKeepAlive(long)
*
* @return the keep alive setting.
*/
public long getTCPKeepAliveSetting () {
if (ZMQ.version_full() < ZMQ.make_version(3, 2, 0))
return -1;

return getLongSockopt (KEEPALIVE);
}

/**
* @see #setTCPKeepAliveIdle(long)
*
* @return the keep alive idle value.
*/
public long getTCPKeepAliveIdle () {
if (ZMQ.version_full() < ZMQ.make_version(3, 2, 0))
return -1;

return getLongSockopt (KEEPALIVEIDLE);
}

/**
* @see #setTCPKeepAliveInterval(long)
*
* @return the keep alive interval.
*/
public long getTCPKeepAliveInterval () {
if (ZMQ.version_full() < ZMQ.make_version(3, 2, 0))
return -1;

return getLongSockopt (KEEPALIVEINTVL);
}

/**
* @see #setTCPKeepAliveCount(long)
*
* @return the keep alive count.
*/
public long getTCPKeepAliveCount () {
if (ZMQ.version_full() < ZMQ.make_version(3, 2, 0))
return -1;

return getLongSockopt (KEEPALIVECNT);
}

/**
* @see #setIdentity(byte[])
*
Expand Down Expand Up @@ -828,6 +876,59 @@ public void setAffinity (long affinity) {
setLongSockopt (AFFINITY, affinity);
}

/**
* Override SO_KEEPALIVE socket option (where supported by OS) to enable keep-alive
* packets for a socket connection.
* Possible values are -1, 0, 1.
* The default value -1 will skip all overrides and do the OS default.
*
* @param optVal
* The value of 'ZMQ_TCP_KEEPALIVE' to turn TCP keepalives on (1) or off (0).
*/
public void setTCPKeepAlive (long optVal) {
if (ZMQ.version_full() >= ZMQ.make_version(3, 2, 0))
setLongSockopt (KEEPALIVE, optVal);
}

/**
* Override TCP_KEEPCNT socket option (where supported by OS).
* The default value -1 will skip all overrides and do the OS default.
*
* @param optVal
* The value of 'ZMQ_TCP_KEEPALIVE_CNT' defines the number of keepalives
* before death.
*/
public void setTCPKeepAliveCount (long optVal) {
if (ZMQ.version_full() >= ZMQ.make_version(3, 2, 0))
setLongSockopt (KEEPALIVECNT, optVal);
}

/**
* Override TCP_KEEPINTVL socket option (where supported by OS).
* The default value -1 will skip all overrides and do the OS default.
*
* @param optVal
* The value of 'ZMQ_TCP_KEEPALIVE_INTVL' defines the interval between
* keepalives in ms.
*/
public void setTCPKeepAliveInterval (long optVal) {
if (ZMQ.version_full() >= ZMQ.make_version(3, 2, 0))
setLongSockopt (KEEPALIVEINTVL, optVal);
}

/**
* Override TCP_KEEPCNT (or TCP_KEEPALIVE on some OS) socket option (where supported by OS).
* The default value -1 will skip all overrides and do the OS default.
*
* @param optVal
* The value of 'ZMQ_TCP_KEEPALIVE_IDLE' defines the interval between the last
* data packet sent over the socket and the first keepalive probe in ms.
*/
public void setTCPKeepAliveIdle (long optVal) {
if (ZMQ.version_full() >= ZMQ.make_version(3, 2, 0))
setLongSockopt (KEEPALIVEIDLE, optVal);
}

/**
* The 'ZMQ_IDENTITY' option shall set the identity of the specified 'socket'. Socket
* identity determines if existing 0MQ infastructure (_message queues_, _forwarding
Expand Down Expand Up @@ -1098,6 +1199,10 @@ private long getSocketHandle () {
private static final int MULTICAST_HOPS = 25;
private static final int RCVTIMEO = 27;
private static final int SNDTIMEO = 28;
private static final int KEEPALIVE = 34;
private static final int KEEPALIVECNT = 35;
private static final int KEEPALIVEIDLE = 36;
private static final int KEEPALIVEINTVL = 37;
}

/**
Expand Down

0 comments on commit 00b1df4

Please sign in to comment.