summaryrefslogtreecommitdiff
path: root/akono/src/main/java/akono/AkonoJni.kt
blob: 85ceefa8466937746e3b6bda2b99f56505a63306 (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
package akono

import android.util.Base64
import android.util.Log
import java.lang.Exception
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.LinkedBlockingDeque
import kotlin.concurrent.thread

typealias AkonoNativePointer = ByteBuffer

private val TAG = "AkonoJni"

class AkonoJni(vararg nodeArgv: String) {
    private var messageHandler: MessageHandler? = null
    private val initializedLatch = CountDownLatch(1)

    private val workQueue = LinkedBlockingDeque<() -> Unit>()

    private external fun evalJs(source: String, p: AkonoNativePointer): String
    private external fun runNode(p: AkonoNativePointer)

    private external fun makeCallbackNative(source: String, p: AkonoNativePointer)
    private external fun putModuleCodeNative(key: String, source: String)

    private external fun destroyNative(b: AkonoNativePointer)
    private external fun initNative(nodeArgv: Array<out String>): AkonoNativePointer
    private external fun notifyNative(b: AkonoNativePointer)

    private lateinit var internalNativePointer: AkonoNativePointer

    private val jniThread: Thread

    private var stopped = false

    /**
     * Schedule a block do be executed in the node thread.
     */
    private fun scheduleNodeThread(b: () -> Unit) {
        initializedLatch.await()
        workQueue.put(b)
        notifyNative()
    }

    /**
     * Called by node/v8 from its thread.
     */
    private fun internalOnNotify(payload: String) {
        messageHandler?.handleMessage(payload)
    }


    fun notifyNative() {
        initializedLatch.await()
        notifyNative(internalNativePointer)
    }

    /**
     * Schedule Node.JS to be run.
     */
    fun evalSimpleJs(source: String): String {
        val latch = CountDownLatch(1)
        var result: String? = null
        scheduleNodeThread {
            result = evalJs(source, internalNativePointer)
            latch.countDown()
        }
        latch.await()
        return result ?: throw Exception("invariant failed")
    }

    fun evalNodeCode(source: String) {
        scheduleNodeThread {
            makeCallbackNative(source, internalNativePointer)
        }
    }

    /**
     * Send a message to node, calling global.__akono_onMessage.
     */
    fun sendMessage(message: String) {
        val encoded = Base64.encodeToString(message.toByteArray(), Base64.NO_WRAP)
        val source = """
            if (global.__native_onMessage) {
              const msg = (new Buffer('$encoded', 'base64')).toString('ascii');
              global.__native_onMessage(msg);
            } else {
                console.log("WARN: no __native_onMessage defined");
            }
        """.trimIndent()
        evalNodeCode(source)
    }

    fun waitStopped(): Unit {
        Log.i(TAG, "waiting for stop")
        scheduleNodeThread {
            stopped = true
        }
        jniThread.join()
        return
    }

    fun putModuleCode(modName: String, code: String) {
        Log.v(TAG, "putting module code (kotlin)")
        putModuleCodeNative(modName, code)
    }

    /**
     * Register a message handler that is called when the JavaScript code
     * running in [runNodeJs] calls __native_sendMessage
     *
     * Does not block.
     */
    fun setMessageHandler(handler: MessageHandler) {
        this.messageHandler = handler
    }


    @Override
    protected fun finalize() {
        destroyNative(internalNativePointer)
    }

    init {
        jniThread = thread {
            internalNativePointer = initNative(nodeArgv)
            initializedLatch.countDown()
            while (true) {
                runNode(internalNativePointer)
                while (true) {
                    val w = workQueue.poll() ?: break
                    w()
                }
                if (stopped) {
                    break
                }
            }
        }
    }

    companion object {
        init {
            System.loadLibrary("akono-jni")
        }
    }

    interface MessageHandler {
        fun handleMessage(message: String)
    }
}