summaryrefslogtreecommitdiff
path: root/java/Socket.cpp
blob: 2274535de8a1feece92d66dc7e6893e794e23811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
    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/>.
*/

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#include "../src/stdint.hpp"

#include "zmq.h"
#include "org_zmq_Socket.h"

static jfieldID socket_handle_fid = NULL;
static jmethodID create_socket_mid = NULL;

static void raise_exception (JNIEnv *env, int err)
{
    //  Get exception class.
    jclass exception_class = env->FindClass ("java/lang/Exception");
    assert (exception_class);

    //  Get text description of the exception.
#if defined _MSC_VER
#pragma warning (push)
#pragma warning (disable:4996)
#endif
    const char *err_msg = strerror (err);
#if defined _MSC_VER
#pragma warning (pop)
#endif

    //  Raise the exception.
    int rc = env->ThrowNew (exception_class, err_msg);
    assert (rc == 0);

    //  Free the local ref.
    env->DeleteLocalRef (exception_class);
}

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);
        assert (cls);
        socket_handle_fid = env->GetFieldID (cls, "socketHandle", "J");
        assert (socket_handle_fid);
        env->DeleteLocalRef (cls);
    }

    if (create_socket_mid == NULL) {
        jclass cls = env->FindClass ("org/zmq/Context");
        assert (cls);
        create_socket_mid = env->GetMethodID (cls, "createSocket", "(I)J");
        assert (create_socket_mid);
        env->DeleteLocalRef (cls);
    }

    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)
{
    void *s = (void*) env->GetLongField (obj, socket_handle_fid);
    assert (s);
    int rc = zmq_close (s);
    assert (rc == 0);
}

JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__IJ (JNIEnv *env,
    jobject obj, jint option, jlong optval)
{
    switch (option) {
    case ZMQ_HWM:
    case ZMQ_LWM:
    case ZMQ_SWAP:
    case ZMQ_AFFINITY:
    case ZMQ_RATE:
    case ZMQ_RECOVERY_IVL:
    case ZMQ_MCAST_LOOP:
        {
            void *s = (void*) env->GetLongField (obj, socket_handle_fid);
            assert (s);

            int64_t value = optval;
            int rc = zmq_setsockopt (s, option, &value, sizeof (value));
            if (rc != 0)
                raise_exception (env, errno);
            return;
        }
    default:
        raise_exception (env, EINVAL);
        return;
    }
}

JNIEXPORT void JNICALL Java_org_zmq_Socket_setsockopt__ILjava_lang_String_2 (
    JNIEnv *env, jobject obj, jint option, jstring optval)
{
    switch (option) {
    case ZMQ_IDENTITY:
    case ZMQ_SUBSCRIBE:
    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);
        return;
    }
}

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);

    if (addr == NULL) {
        raise_exception (env, EINVAL);
        return;
    }

    const char *c_addr = env->GetStringUTFChars (addr, NULL);
    if (c_addr == NULL) {
        raise_exception (env, EINVAL);
        return;
    }

    int rc = zmq_bind (s, c_addr);
    env->ReleaseStringUTFChars (addr, c_addr);

    if (rc == -1)
        raise_exception (env, errno);
}

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);

    if (addr == NULL) {
        raise_exception (env, EINVAL);
        return;
    }

    const char *c_addr = env->GetStringUTFChars (addr, NULL);
    if (c_addr == NULL) {
        raise_exception (env, EINVAL);
        return;
    }

    int rc = zmq_connect (s, c_addr);
    env->ReleaseStringUTFChars (addr, c_addr);

    if (rc == -1)
        raise_exception (env, errno);
}

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);

    jsize size = env->GetArrayLength (msg); 
    jbyte *data = env->GetByteArrayElements (msg, 0);

    zmq_msg_t message;
    int rc = zmq_msg_init_size (&message, size);
    assert (rc == 0);
    memcpy (zmq_msg_data (&message), data, size);

    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);
        rc = zmq_msg_close (&message);
        assert (rc == 0);
        return JNI_FALSE;
    }

    rc = zmq_msg_close (&message);
    assert (rc == 0);
    return JNI_TRUE;
}

JNIEXPORT void JNICALL Java_org_zmq_Socket_flush (JNIEnv *env, jobject obj)
{
    void *s = (void*) env->GetLongField (obj, socket_handle_fid);
    assert (s);

    int rc = zmq_flush (s);

    if (rc == -1) {
        raise_exception (env, errno);
        return ;
    }
}

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);

    zmq_msg_t message;
    zmq_msg_init (&message);
    int rc = zmq_recv (s, &message, (int) flags);

    if (rc == -1 && errno == EAGAIN) {
        zmq_msg_close (&message);
        return NULL;
    }

    if (rc == -1) {
        raise_exception (env, errno);
        zmq_msg_close (&message);
        return NULL;
    }

    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;
}