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

import android.util.Base64
import android.util.Log
import org.json.JSONObject
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

data class ModuleResult(val path: String, val contents: String)

class AkonoJni(vararg nodeArgv: String) {
    private var getDataHandler: GetDataHandler? = null
    private var messageHandler: MessageHandler? = null
    private var loadModuleHandler: LoadModuleHandler? = 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 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.
     */
    @Suppress("unused")
    private fun internalOnNotify(type: String, payload: String) {
        Log.i("myapp", "internalOnNotify called")
        Log.i("myapp", "type: $type")
        Log.i("myapp", "payload: $payload")
        messageHandler?.handleMessage(payload)
    }

    /**
     * Called by node/v8 from its thread.
     */
    @Suppress("unused")
    private fun internalOnModuleLoad(loadInfoStr: String): String {
        Log.i("myapp", "internalOnModuleLoad called")
        Log.i("myapp", "loadInfoStr is $loadInfoStr")
        try {
            val loadInfo = JSONObject(loadInfoStr)
            val request: String = loadInfo.getString("request")
            Log.i("myapp", "request is $request")
            val handler = loadModuleHandler
            if (handler != null) {
                val modResult = handler.loadModule(request, arrayOf()) ?: return "null"
                val result = JSONObject()
                result.put("path", modResult.path)
                result.put("content", modResult.contents)
                return result.toString()
            } else {
                Log.v("myapp", "no module load handler registered")
                return "null"
            }
        } catch (e: Exception) {
            Log.v("myapp", "exception during internalOnModuleLoad: $e")
            return "null"
        }
    }

    /**
     * Called by node/v8 from its thread.
     */
    @Suppress("unused")
    private fun internalOnGetData(what: String): String? {
        Log.i("myapp", "internalOnGetData called for $what")
        val data = getDataHandler?.handleGetData(what) ?: return null
        return Base64.encodeToString(data, Base64.NO_WRAP)
    }


    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.__akono_onMessage) {
              const msg = (new Buffer('$encoded', 'base64')).toString('ascii');
              global.__akono_onMessage(msg);
            } else {
                console.log("WARN: no __akono_onMessage defined");
            }
        """.trimIndent()
        evalNodeCode(source)
    }

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

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

    fun setLoadModuleHandler(handler: LoadModuleHandler) {
        this.loadModuleHandler = handler
    }

    fun setGetDataHandler(handler: GetDataHandler) {
        this.getDataHandler = 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)
    }

    interface LoadModuleHandler {
        fun loadModule(name: String, paths: Array<String>): ModuleResult?
    }

    interface GetDataHandler {
        fun handleGetData(what: String): ByteArray?
    }
}