Skip to content

Commit 38a8928

Browse files
committed
Merge branch 'dev-0.10'
2 parents ad90a44 + 19436e9 commit 38a8928

File tree

9 files changed

+597
-942
lines changed

9 files changed

+597
-942
lines changed

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ package com.drpogodin.reactnativestaticserver
33
import android.util.Log
44
import com.facebook.react.bridge.Promise
55

6-
enum class Errors(val message: String) {
7-
ANOTHER_INSTANCE_IS_ACTIVE(
8-
"Failed to launch, another server instance is active."),
9-
FAIL_GET_LOCAL_IP_ADDRESS("Failed to get local IP adddress"),
10-
FAIL_GET_OPEN_PORT("Failed to get an open port"),
11-
INTERNAL_ERROR("Internal error"),
12-
SERVER_CRASHED("Server crashed"),
13-
STOP_FAILURE("Failed to gracefully shutdown the server");
14-
6+
class Errors(val name: String, val message: String) {
157
val error: Error
168
get() = Error(message)
179
val exception: Exception
@@ -45,5 +37,40 @@ enum class Errors(val message: String) {
4537

4638
companion object {
4739
const val LOGTAG = "RN_STATIC_SERVER"
40+
41+
fun ANOTHER_INSTANCE_IS_ACTIVE(
42+
activeServerId: Double,
43+
failedToLaunchServerId: Double
44+
): Errors {
45+
return Errors(
46+
"ANOTHER_INSTANCE_IS_ACTIVE",
47+
"Failed to launch server #$failedToLaunchServerId, another server instance (#$activeServerId) is active.")
48+
}
49+
50+
fun FAIL_GET_LOCAL_IP_ADDRESS(): Errors {
51+
return Errors(
52+
"FAIL_GET_LOCAL_IP_ADDRESS",
53+
"Failed to get local IP adddress"
54+
)
55+
}
56+
57+
fun FAIL_GET_OPEN_PORT(): Errors {
58+
return Errors(
59+
"FAIL_GET_OPEN_PORT",
60+
"Failed to get an open port"
61+
)
62+
}
63+
64+
fun INTERNAL_ERROR(serverId: Double): Errors {
65+
return Errors("INTERNAL_ERROR", "Internal error (server #$serverId)")
66+
}
67+
68+
fun SERVER_CRASHED(serverId: Double): Errors {
69+
return Errors("SERVER_CRASHED", "Server #$serverId crashed")
70+
}
71+
72+
fun STOP_FAILURE(serverId: Double): Errors {
73+
return Errors("STOP_FAILURE", "Failed to gracefully shutdown the server #$serverId")
74+
}
4875
}
4976
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
5959
}
6060
promise.resolve("127.0.0.1")
6161
} catch (e: Exception) {
62-
Errors.FAIL_GET_LOCAL_IP_ADDRESS.reject(promise)
62+
Errors.FAIL_GET_LOCAL_IP_ADDRESS().reject(promise)
6363
}
6464
}
6565

@@ -78,17 +78,19 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
7878
try {
7979
sem.acquire()
8080
} catch (e: Exception) {
81-
Errors.INTERNAL_ERROR.log(e)
81+
Errors.INTERNAL_ERROR(id).log(e)
8282
.reject(promise, "Failed to acquire a semaphore")
8383
return
8484
}
85-
if (server != null) {
86-
Errors.ANOTHER_INSTANCE_IS_ACTIVE.log().reject(promise)
85+
86+
val activeServerId = server?.id;
87+
if (activeServerId != null) {
88+
Errors.ANOTHER_INSTANCE_IS_ACTIVE(activeServerId, id).log().reject(promise)
8789
sem.release()
8890
return
8991
}
9092
if (pendingPromise != null) {
91-
Errors.INTERNAL_ERROR.log().reject(promise, "Unexpected pending promise")
93+
Errors.INTERNAL_ERROR(id).log().reject(promise, "Unexpected pending promise")
9294
sem.release()
9395
return
9496
}
@@ -106,7 +108,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
106108
emitter.emit("RNStaticServer", event)
107109
} else {
108110
if (signal === Server.CRASHED) {
109-
Errors.SERVER_CRASHED.reject(pendingPromise, details)
111+
Errors.SERVER_CRASHED(id).reject(pendingPromise, details)
110112
} else pendingPromise!!.resolve(details)
111113
pendingPromise = null
112114
sem.release()
@@ -124,7 +126,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
124126
socket.close()
125127
promise.resolve(port)
126128
} catch (e: Exception) {
127-
Errors.FAIL_GET_OPEN_PORT.log(e).reject(promise)
129+
Errors.FAIL_GET_OPEN_PORT().log(e).reject(promise)
128130
}
129131
}
130132

@@ -134,12 +136,12 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
134136
try {
135137
sem.acquire()
136138
} catch (e: Exception) {
137-
Errors.INTERNAL_ERROR.log(e)
139+
Errors.INTERNAL_ERROR(server!!.id).log(e)
138140
.reject(promise, "Failed to acquire a semaphore")
139141
return
140142
}
141143
if (pendingPromise != null) {
142-
Errors.INTERNAL_ERROR
144+
Errors.INTERNAL_ERROR(server!!.id)
143145
.reject(pendingPromise, "Unexpected pending promise")
144146
sem.release()
145147
return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.util.function.BiConsumer
2222
* if any, has terminated or crashed before launching a new one!
2323
*/
2424
class Server(
25-
var id: Double,
25+
val id: Double,
2626
var configPath: String,
2727
var errlogPath: String,
2828
private val signalConsumer: BiConsumer<String, String?>

0 commit comments

Comments
 (0)