Skip to content

Commit

Permalink
kiwix#2447 Separated and Optimizedd the KiwixServer and Factory Funct…
Browse files Browse the repository at this point in the history
…ionality, works now
  • Loading branch information
s-ayush2903 committed Dec 14, 2020
1 parent 0ea288d commit 7faf13e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import android.app.Service
import android.content.Context
import dagger.Module
import dagger.Provides
import org.kiwix.kiwixlib.JNIKiwixServer
import org.kiwix.kiwixlib.Library
import org.kiwix.kiwixmobile.di.ServiceScope
import org.kiwix.kiwixmobile.webserver.KiwixServer
import org.kiwix.kiwixmobile.webserver.WebServerHelper
Expand All @@ -46,6 +48,15 @@ class ServiceModule {
fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks =
service as IpAddressCallbacks

@Provides
@ServiceScope
fun providesLibrary(): Library = Library()

@Provides
@ServiceScope
fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer =
JNIKiwixServer(jniKiwixLibrary)

@Provides
@ServiceScope
fun providesHotspotNotificationManager(
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ import javax.inject.Inject

private const val TAG = "KiwixServer"

class KiwixServer @Inject constructor() {
class KiwixServer @Inject constructor(private val jniKiwixServer: JNIKiwixServer) {

fun createKiwixServer(selectedBooksPath: ArrayList<String>): JNIKiwixServer? {
val kiwixLibrary = Library()
selectedBooksPath.forEach { path ->
try {
kiwixLibrary.addBook(path)
} catch (e: JNIKiwixException) {
Log.v(TAG, "Couldn't add book with path:{ $path }")
class Factory {
fun createKiwixServer(selectedBooksPath: ArrayList<String>): KiwixServer {
val kiwixLibrary = Library()
selectedBooksPath.forEach { path ->
try {
kiwixLibrary.addBook(path)
} catch (e: JNIKiwixException) {
Log.v(TAG, "Couldn't add book with path:{ $path }")
}
}
return KiwixServer(JNIKiwixServer(kiwixLibrary))
}
return JNIKiwixServer(kiwixLibrary)
}

fun startServer(port: Int): Boolean {
jniKiwixServer.setPort(port)
return jniKiwixServer.start()
}

fun stopServer() = jniKiwixServer.stop()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import org.kiwix.kiwixlib.JNIKiwixServer;
import org.kiwix.kiwixlib.Library;
import org.kiwix.kiwixmobile.core.utils.ServerUtils;
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks;

Expand Down Expand Up @@ -63,7 +62,7 @@ public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {

public void stopAndroidWebServer() {
if (isServerStarted) {
server.stop();
kiwixServer.stopServer();
updateServerState(false);
}
}
Expand All @@ -72,9 +71,9 @@ private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
if (!isServerStarted) {
int DEFAULT_PORT = 8080;
ServerUtils.port = DEFAULT_PORT;
server = kiwixServer.createKiwixServer(selectedBooksPath);
server.setPort(ServerUtils.port);
updateServerState(server.start());
KiwixServer.Factory factory = new KiwixServer.Factory();
kiwixServer = factory.createKiwixServer(selectedBooksPath);
updateServerState(kiwixServer.startServer(ServerUtils.port));
Log.d(TAG, "Server status" + isServerStarted);
}
return isServerStarted;
Expand Down

0 comments on commit 7faf13e

Please sign in to comment.