Skip to content

Commit d381d45

Browse files
committed
Android: Convert Server.java to Kotlin
1 parent 2ecc799 commit d381d45

File tree

3 files changed

+87
-105
lines changed

3 files changed

+87
-105
lines changed

android/src/main/java/com/drpogodin/reactnativestaticserver/ReactNativeStaticServerModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
6565
@ReactMethod
6666
override fun start(
6767
id: Double, // Server ID for backward communication with JS layer.
68-
configPath: String?,
69-
errlogPath: String?,
70-
promise: Promise?
68+
configPath: String,
69+
errlogPath: String,
70+
promise: Promise
7171
) {
7272
Log.i(LOGTAG, "Starting...")
7373
try {

android/src/main/java/com/lighttpd/Server.java

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.lighttpd
2+
3+
import android.util.Log
4+
import com.drpogodin.reactnativestaticserver.Errors
5+
import java.util.function.BiConsumer
6+
7+
/**
8+
* Java interface for native Lighttpd server running in a dedicated Thread.
9+
* Use Thread methods to operate the server:
10+
* .start() - To launch it;
11+
* .isActive() - To check its current status;
12+
* .interrupt() - To gracefully terminate it.
13+
* Also, `signalConsumer` callback provided to Server instance upon construction
14+
* will provide you with server state change Signals.
15+
*
16+
* As Java Thread instances may be executed only once, to restart the server
17+
* you should create and launch a new instance of Server object.
18+
*
19+
* BEWARE: With the current Lighttpd implementation,
20+
* and the way it is integrated into this library, it is not safe to run
21+
* multiple server instances in parallel! Be sure the previous server instance,
22+
* if any, has terminated or crashed before launching a new one!
23+
*/
24+
class Server(
25+
var configPath: String,
26+
var errlogPath: String,
27+
private val signalConsumer: BiConsumer<String, String?>
28+
) : Thread() {
29+
override fun interrupt() {
30+
Log.i(LOGTAG, "Server.interrupt() triggered")
31+
gracefulShutdown()
32+
// No need to call super.interrupt() here, the native this.shutdown()
33+
// method will set within the native layer necessary flags that will
34+
// cause graceful termination of the thread.
35+
}
36+
37+
private external fun gracefulShutdown()
38+
external fun launch(configPath: String, errlogPath: String): Int
39+
override fun run() {
40+
Log.i(LOGTAG, "Server.run() triggered")
41+
if (activeServer != null) {
42+
val msg = "Another Server instance is active"
43+
Log.e(LOGTAG, msg)
44+
signalConsumer.accept(CRASHED, msg)
45+
return
46+
}
47+
try {
48+
activeServer = this
49+
val res = launch(configPath, errlogPath)
50+
if (res != 0) {
51+
throw Exception("Native server exited with status $res")
52+
}
53+
54+
// NOTE: It MUST BE set "null" prior to sending out TERMINATED or CRASHED
55+
// signals.
56+
activeServer = null
57+
Log.i(LOGTAG, "Server terminated gracefully")
58+
signalConsumer.accept(TERMINATED, null)
59+
} catch (error: Exception) {
60+
activeServer = null
61+
Log.e(LOGTAG, "Server crashed", error)
62+
signalConsumer.accept(CRASHED, error.message)
63+
}
64+
}
65+
66+
companion object {
67+
init {
68+
System.loadLibrary("lighttpd")
69+
}
70+
71+
// NOTE: Tried to use enum, but was not able to make it work with JNI.
72+
const val CRASHED = "CRASHED"
73+
const val LAUNCHED = "LAUNCHED"
74+
const val TERMINATED = "TERMINATED"
75+
private var activeServer: Server? = null
76+
private const val LOGTAG = Errors.LOGTAG
77+
78+
// NOTE: @JvmStatic annotation is needed to make this function
79+
// visible via JNI in C code.
80+
@JvmStatic fun onLaunchedCallback() {
81+
activeServer!!.signalConsumer.accept(LAUNCHED, null)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)