Skip to content

Instantly share code, notes, and snippets.

@sonOfRa
Created September 19, 2013 15:08
Show Gist options
  • Save sonOfRa/6624934 to your computer and use it in GitHub Desktop.
Save sonOfRa/6624934 to your computer and use it in GitHub Desktop.
Mysterious failure to call methods
JNIEXPORT void JNICALL Java_im_tox_jtoxcore_JTox_tox_1onfriendrequest(
JNIEnv * env, jobject obj, jlong messenger, jobject callback) {
tox_jni_globals_t *_messenger = (tox_jni_globals_t *) messenger;
if (_messenger->frqc) {
if (_messenger->frqc->jobj) {
(*env)->DeleteGlobalRef(env, _messenger->frqc->jobj);
}
free(_messenger->frqc);
}
friendrequest_callback_t *data = malloc(sizeof(friendrequest_callback_t));
data->env = env;
data->jobj = (*env)->NewGlobalRef(env, callback);
(*env)->DeleteLocalRef(env, callback);
_messenger->frqc = data;
tox_callback_friendrequest(_messenger->tox, (void *) callback_friendrequest,
data);
}
static void callback_friendrequest(uint8_t *pubkey, uint8_t *message,
uint16_t length, void *ptr) {
friendrequest_callback_t *data = ptr;
jclass class = (*data->env)->GetObjectClass(data->env, data->jobj);
jmethodID meth = (*data->env)->GetMethodID(data->env, class, "execute",
"(Ljava/lang/String;[B)V");
char buf[ADDR_SIZE_HEX] = { 0 };
addr_to_hex(pubkey, buf);
jstring _pubkey = (*data->env)->NewStringUTF(data->env, buf);
jbyteArray _message = (*data->env)->NewByteArray(data->env, length);
(*data->env)->SetByteArrayRegion(data->env, _message, 0, length, message);
printf("definitely happening");
fflush(stdout);
(*data->env)->CallVoidMethod(data->env, data->jobj, meth, _pubkey,
_message);
}
/**
* Native call to tox_callback_friendrequest
*
* @param messengerPointer
* pointer to the internal messenger struct
* @param callback
* the callback to set for receiving friend requests
*/
private native void tox_onfriendrequest(long messengerPointer,
OnFriendRequestCallback callback);
/**
* Method used to set a callback method for receiving friend requests. Any
* time a friend request is received on this Tox instance, the
* {@link OnFriendRequestCallback#execute(String, String)} method will be
* executed.
*
* @param callback
* the callback to set for receiving friend requests
* @throws ToxException
* if the instance has been killed
*/
public void setOnFriendRequestCallback(OnFriendRequestCallback callback)
throws ToxException {
lock.lock();
try {
checkPointer();
tox_onfriendrequest(this.messengerPointer, callback);
} finally {
lock.unlock();
}
}
package im.tox.jtoxcore.test;
import im.tox.jtoxcore.JTox;
import im.tox.jtoxcore.ToxException;
import im.tox.jtoxcore.ToxWorker;
import java.net.UnknownHostException;
public class JToxTest {
public static void main(String[] args) throws UnknownHostException {
try {
JTox jtox = new JTox();
jtox.bootstrap("66.175.223.88", 33445,
"AC4112C975240CAD260BB2FCD134266521FAAF0A5D159C5FD3201196191E4F5D");
System.out.println(jtox.getAddress());
jtox.setOnActionCallback(new TestOnActionCallback(jtox));
jtox.setOnConnectionStatusCallback(new TestOnConnectionStatusCallback(
jtox));
jtox.setOnFriendRequestCallback(new TestOnFriendRequestCallback(
jtox));
jtox.setOnMessageCallback(new TestOnMessageCallback(jtox));
jtox.setOnNameChangeCallback(new TestOnNameChangeCallback(jtox));
jtox.setOnReadReceiptCallback(new TestOnReadReceiptCallback(jtox));
jtox.setOnStatusMessageCallback(new TestOnStatusMessageCallback(
jtox));
jtox.setOnUserStatusCallback(new TestOnUserStatusCallback(jtox));
Thread worker = new Thread(new ToxWorker(jtox));
worker.start();
} catch (ToxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3A9236B1D04B0BC45330E8E3FC3CBB5D640531D7196FCF2424D163F3FCC3E57CEC0A431D590D
definitely happening
package im.tox.jtoxcore.test;
import im.tox.jtoxcore.JTox;
import im.tox.jtoxcore.ToxException;
import im.tox.jtoxcore.callbacks.OnFriendRequestCallback;
public class TestOnFriendRequestCallback extends OnFriendRequestCallback {
public TestOnFriendRequestCallback(JTox jtox) {
super(jtox);
}
@Override
public void execute(String publicKey, byte[] message) {
String msg;
if (message == null) {
msg = "No mesage";
} else {
try {
msg = JTox.getByteString(message);
} catch (ToxException e) {
msg = "ERROR: Unable to get message";
}
}
System.out.println("We received a friend request from " + publicKey
+ " with the following message: " + msg);
try {
jtox.confirmRequest(publicKey);
System.out.println("confirmed!");
} catch (ToxException e) {
System.out.println("Unable to confirm friend request");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment