#2447 Separated and Optimizedd the KiwixServer and Factory Functionality, works now

This commit is contained in:
s-ayush2903 2020-12-14 15:55:51 +05:30
parent 0ea288d314
commit 7faf13ee3a
No known key found for this signature in database
GPG Key ID: B4341DD08B2371CB
3 changed files with 33 additions and 14 deletions

View File

@ -23,6 +23,8 @@ import android.app.Service
import android.content.Context import android.content.Context
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import org.kiwix.kiwixlib.JNIKiwixServer
import org.kiwix.kiwixlib.Library
import org.kiwix.kiwixmobile.di.ServiceScope import org.kiwix.kiwixmobile.di.ServiceScope
import org.kiwix.kiwixmobile.webserver.KiwixServer import org.kiwix.kiwixmobile.webserver.KiwixServer
import org.kiwix.kiwixmobile.webserver.WebServerHelper import org.kiwix.kiwixmobile.webserver.WebServerHelper
@ -46,6 +48,15 @@ class ServiceModule {
fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks =
service as IpAddressCallbacks service as IpAddressCallbacks
@Provides
@ServiceScope
fun providesLibrary(): Library = Library()
@Provides
@ServiceScope
fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer =
JNIKiwixServer(jniKiwixLibrary)
@Provides @Provides
@ServiceScope @ServiceScope
fun providesHotspotNotificationManager( fun providesHotspotNotificationManager(

View File

@ -26,17 +26,26 @@ import javax.inject.Inject
private const val TAG = "KiwixServer" private const val TAG = "KiwixServer"
class KiwixServer @Inject constructor() { class KiwixServer @Inject constructor(private val jniKiwixServer: JNIKiwixServer) {
fun createKiwixServer(selectedBooksPath: ArrayList<String>): JNIKiwixServer? { class Factory {
val kiwixLibrary = Library() fun createKiwixServer(selectedBooksPath: ArrayList<String>): KiwixServer {
selectedBooksPath.forEach { path -> val kiwixLibrary = Library()
try { selectedBooksPath.forEach { path ->
kiwixLibrary.addBook(path) try {
} catch (e: JNIKiwixException) { kiwixLibrary.addBook(path)
Log.v(TAG, "Couldn't add book with path:{ $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()
} }

View File

@ -26,7 +26,6 @@ import java.util.ArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import org.kiwix.kiwixlib.JNIKiwixServer; import org.kiwix.kiwixlib.JNIKiwixServer;
import org.kiwix.kiwixlib.Library;
import org.kiwix.kiwixmobile.core.utils.ServerUtils; import org.kiwix.kiwixmobile.core.utils.ServerUtils;
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks; import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks;
@ -63,7 +62,7 @@ public class WebServerHelper {
public void stopAndroidWebServer() { public void stopAndroidWebServer() {
if (isServerStarted) { if (isServerStarted) {
server.stop(); kiwixServer.stopServer();
updateServerState(false); updateServerState(false);
} }
} }
@ -72,9 +71,9 @@ public class WebServerHelper {
if (!isServerStarted) { if (!isServerStarted) {
int DEFAULT_PORT = 8080; int DEFAULT_PORT = 8080;
ServerUtils.port = DEFAULT_PORT; ServerUtils.port = DEFAULT_PORT;
server = kiwixServer.createKiwixServer(selectedBooksPath); KiwixServer.Factory factory = new KiwixServer.Factory();
server.setPort(ServerUtils.port); kiwixServer = factory.createKiwixServer(selectedBooksPath);
updateServerState(server.start()); updateServerState(kiwixServer.startServer(ServerUtils.port));
Log.d(TAG, "Server status" + isServerStarted); Log.d(TAG, "Server status" + isServerStarted);
} }
return isServerStarted; return isServerStarted;