/soc/2012/michael/android: 100fc68d310b: Put callbacks from the ...

Michael Zangl michael at soc.pidgin.im
Tue Aug 7 05:59:27 EDT 2012


Changeset: 100fc68d310b42c9bc82f20ddb8eef8e04df590f
Author:	 Michael Zangl <michael at soc.pidgin.im>
Date:	 2012-08-07 01:06 +0200
Branch:	 soc.2012.android
URL: http://hg.pidgin.im/soc/2012/michael/android/rev/100fc68d310b

Description:

Put callbacks from the input_add function and from ssl to the main purple thread.

diffstat:

 android/workspace/im.pidgin.libpurple/native/EventLoop.c                                             |  47 +++++++--
 android/workspace/im.pidgin.libpurple/native/SslOps.c                                                |  13 +-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java                  |   2 +-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/EventLoop.java                    |  26 +++++
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java             |   5 +
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryFailedCallback.java   |   5 +-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryResolvedCallback.java |   5 +-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java            |  15 ++-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslOps.java                   |  14 ++-
 9 files changed, 109 insertions(+), 23 deletions(-)

diffs (truncated from 326 to 300 lines):

diff --git a/android/workspace/im.pidgin.libpurple/native/EventLoop.c b/android/workspace/im.pidgin.libpurple/native/EventLoop.c
--- a/android/workspace/im.pidgin.libpurple/native/EventLoop.c
+++ b/android/workspace/im.pidgin.libpurple/native/EventLoop.c
@@ -56,6 +56,24 @@ eventloop_timeout_remove(guint handle)
 
 }
 
+/*
+ * Class:     im_pidgin_libpurple_core_EventLoop_IoInvokeRunner
+ * Method:    ioInvoke_native
+ * Signature: (JJII)V
+ */JNIEXPORT void JNICALL
+Java_im_pidgin_libpurple_core_EventLoop_00024IoInvokeRunner_ioInvoke_1native(
+		JNIEnv *env, jobject obj, jlong function, jlong data, jint fd,
+		jint cond)
+{
+	PurpleInputFunction function_native;
+	gpointer data_native;
+
+	function_native = longToP(function);
+	data_native = longToP(data);
+
+	function_native(data_native, (gint) fd, (PurpleInputCondition) cond);
+}
+
 #define ANDROID_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
 #define ANDROID_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
 
@@ -63,14 +81,20 @@ typedef struct _AndroidIOClosure {
 	PurpleInputFunction function;
 	guint result;
 	gpointer data;
-} PidginIOClosure;
+} AndroidIOClosure;
 
 static gboolean
 android_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
 {
-	PidginIOClosure *closure = data;
+	static JavaMethodIDCache methodCache = METHOD_CACHE("ioInvoke", "(JJII)V");
+
+	jmethodID mid;
+	AndroidIOClosure *closure;
 	PurpleInputCondition purple_cond = 0;
 
+	CALLBACK_START(&eventloop, 0);
+
+	closure = data;
 	if (condition & ANDROID_READ_COND)
 		purple_cond |= PURPLE_INPUT_READ;
 	if (condition & ANDROID_WRITE_COND)
@@ -79,9 +103,12 @@ android_io_invoke(GIOChannel *source, GI
 	purple_debug_misc("eventloop", "CLOSURE: callback for %d, fd is %d\n",
 			closure->result, g_io_channel_unix_get_fd(source));
 
-	// TODO: Sync
-	closure->function(closure->data, g_io_channel_unix_get_fd(source),
-			purple_cond);
+	mid = getMethodIDCachedReferenced(env, eventloop.java_class, &methodCache);
+	if (mid != NULL) {
+		(*env)->CallVoidMethod(env, eventloop.handlerObject, mid,
+				pToLong(closure->function), pToLong(closure->data),
+				(jint) g_io_channel_unix_get_fd(source), (jint) purple_cond);
+	}
 
 	return TRUE;
 }
@@ -90,7 +117,7 @@ static guint
 eventloop_input_add(gint fd, PurpleInputCondition condition,
 		PurpleInputFunction function, gpointer data)
 {
-	PidginIOClosure *closure = g_new0(PidginIOClosure, 1);
+	AndroidIOClosure *closure = g_new0(AndroidIOClosure, 1);
 	GIOChannel *channel;
 	GIOCondition cond = 0;
 
@@ -120,8 +147,8 @@ eventloop_input_remove(guint handle)
 }
 
 static PurpleEventLoopUiOps eventloopUiOps = { eventloop_timeout_add,
-		eventloop_timeout_remove, eventloop_input_add, eventloop_input_remove, NULL,
-		NULL, NULL, NULL, NULL };
+		eventloop_timeout_remove, eventloop_input_add, eventloop_input_remove,
+		NULL, NULL, NULL, NULL, NULL };
 
 JNIEXPORT void JNICALL
 Java_im_pidgin_libpurple_core_EventLoop_init_1native(JNIEnv *env, jobject obj)
@@ -137,8 +164,8 @@ Java_im_pidgin_libpurple_core_EventLoop_
 Java_im_pidgin_libpurple_core_EventLoop_runGLibLoop_1native(JNIEnv *env,
 		jobject obj)
 {
-	 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
-	 g_main_loop_run(loop);
+	GMainLoop *loop = g_main_loop_new(NULL, FALSE);
+	g_main_loop_run(loop);
 }
 
 JNIEXPORT jboolean JNICALL
diff --git a/android/workspace/im.pidgin.libpurple/native/SslOps.c b/android/workspace/im.pidgin.libpurple/native/SslOps.c
--- a/android/workspace/im.pidgin.libpurple/native/SslOps.c
+++ b/android/workspace/im.pidgin.libpurple/native/SslOps.c
@@ -5,6 +5,7 @@
 #include <sys/socket.h>
 
 static JavaObjectReference ssl = JAVA_NULL_OBJECT_REF;
+static jobject thread = NULL;
 
 static JavaObjectReference*
 getJavaObject(PurpleSslConnection* gsc)
@@ -143,7 +144,7 @@ connection_register(PurpleSslConnection*
 {
 	g_log("jni", G_LOG_LEVEL_DEBUG, "ssl registerConnection");
 	static JavaMethodIDCache connectionConstructor =
-			METHOD_CACHE("<init>", "(JLjava/net/Socket;)V");
+			METHOD_CACHE("<init>", "(JLjava/net/Socket;Lim/pidgin/libpurple/core/thread/PurpleThread;)V");
 	static JavaMethodIDCache connectMethod = METHOD_CACHE("connect", "()Z");
 
 	CALLBACK_START_VOID(&ssl);
@@ -166,7 +167,7 @@ connection_register(PurpleSslConnection*
 			&connectionConstructor);
 	g_return_if_fail(connectionConstructorMid != NULL);
 	connection = (*env)->NewObject(env, connectionClass,
-			connectionConstructorMid, pToLong(gsc), socket);
+			connectionConstructorMid, pToLong(gsc), socket, thread);
 
 	setJavaObject(getJavaObject(gsc), env, connection);
 
@@ -197,8 +198,9 @@ closeConnection(PurpleSslConnection* gsc
 		(*env)->CallVoidMethod(env, connection->handlerObject, mid);
 	}
 
-	unsetJavaObject(connection, env);
-	g_free(connection);
+	// TODO: closeConnection gets called twice
+	//unsetJavaObject(connection, env);
+	//g_free(connection);
 }
 
 static size_t
@@ -277,8 +279,9 @@ PurpleSslOps sslOps =
  * Signature: ()V
  */JNIEXPORT void JNICALL
 Java_im_pidgin_libpurple_core_ssl_SslOps_register_1native(JNIEnv *env,
-		jobject object)
+		jobject object, jobject thread2)
 {
 	setJavaObject(&ssl, env, object);
+	thread = (*env)->NewGlobalRef(env, thread2);
 	purple_ssl_set_ops(&sslOps);
 }
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/CoreManager.java
@@ -63,7 +63,7 @@ public class CoreManager {
 		accountList.register();
 		blist.register();
 		conversationManager.register();
-		new SslOps().register();
+		new SslOps(getThread()).register();
 		new DnsQueryUiOps().register();
 		new RequestUiOps().register();
 	}
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/EventLoop.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/EventLoop.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/EventLoop.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/EventLoop.java
@@ -147,4 +147,30 @@ public class EventLoop extends AbstractP
 
 	private native boolean exeucte_native(long function, long data);
 
+	public void ioInvoke(long function, long data, int fd, int cond) {
+		getManager().getThread().schedule(new IoInvokeRunner(function, data, fd, cond));
+	}
+	
+	private static class IoInvokeRunner extends AbstractWaitableRunnable {
+		private final long function;
+		private final long data;
+		private final int fd;
+		private final int cond;
+
+		public IoInvokeRunner(long function, long data, int fd, int cond) {
+			this.function = function;
+			this.data = data;
+			this.fd = fd;
+			this.cond = cond;
+		}
+
+		@Override
+		protected void execute() {
+			ioInvoke_native(function, data, fd, cond);
+		}
+
+		private native void ioInvoke_native(long function2, long data2, int fd2,
+				int cond2);
+		
+	}
 }
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryData.java
@@ -27,6 +27,11 @@ public class DnsQueryData extends Peered
 	protected synchronized void nativeObjectFreed() {
 		super.nativeObjectFreed();
 	}
+	
+	@Override
+	protected void ensureNotDestroyed() {
+		super.ensureNotDestroyed();
+	}
 
 	@Override
 	public String toString() {
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryFailedCallback.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryFailedCallback.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryFailedCallback.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryFailedCallback.java
@@ -18,7 +18,10 @@ public class DnsQueryFailedCallback exte
 	}
 
 	public void call(String errorMessage) {
-		call_native(data, errorMessage);
+		synchronized (data) {
+			data.ensureNotDestroyed();
+			call_native(data, errorMessage);
+		}
 	}
 
 	private native void call_native(DnsQueryData data2, String errorMessage);
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryResolvedCallback.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryResolvedCallback.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryResolvedCallback.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/DnsQueryResolvedCallback.java
@@ -25,7 +25,10 @@ public class DnsQueryResolvedCallback ex
 		for (int i = 0; i < allByName.length; i++) {
 			addresses[i] = allByName[i].getAddress();
 		}
-		call_native(data, addresses);
+		synchronized (data) {
+			data.ensureNotDestroyed();
+			call_native(data, addresses);
+		}
 	}
 
 	private native void call_native(DnsQueryData data2, byte[][] addresses);
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/ssl/SslConnection.java
@@ -1,5 +1,7 @@
 package im.pidgin.libpurple.core.ssl;
 
+import im.pidgin.libpurple.core.thread.AbstractWaitableRunnable;
+import im.pidgin.libpurple.core.thread.PurpleThread;
 import im.pidgin.libpurple.peering.Peered;
 
 import java.io.IOException;
@@ -26,6 +28,7 @@ public final class SslConnection extends
 
 	private Socket sslsocket;
 	private final Socket socket;
+	private final PurpleThread thread;
 
 	/**
 	 * Create a new ssl connection object for the given native pointer.
@@ -33,9 +36,10 @@ public final class SslConnection extends
 	 * @param nativePointer
 	 * @param socket
 	 */
-	protected SslConnection(long nativePointer, Socket socket) {
+	protected SslConnection(long nativePointer, Socket socket, PurpleThread thread) {
 		super(nativePointer);
 		this.socket = socket;
+		this.thread = thread;
 	}
 
 	/**
@@ -116,6 +120,7 @@ public final class SslConnection extends
 		try {
 			return sslsocket.getInputStream().read(data, 0, length);
 		} catch (IOException e) {
+			e.printStackTrace();
 			return -1;
 		}
 	}
@@ -129,13 +134,19 @@ public final class SslConnection extends
 			sslsocket.getOutputStream().write(data, 0, length);
 			return length;
 		} catch (IOException e) {
+			e.printStackTrace();
 			return -1;
 		}
 	}
 
 	@Override
 	public void handshakeCompleted(HandshakeCompletedEvent event) {
-		handshakeCompleted_native();
+		thread.schedule(new AbstractWaitableRunnable() {
+			@Override
+			protected void execute() {
+				handshakeCompleted_native();
+			}
+		});
 	}
 



More information about the Commits mailing list