Create KiwixServer & Factory, and set the hosting working

This commit is contained in:
s-ayush2903 2020-12-11 03:13:08 +05:30
parent b9028dd630
commit f0e874ea94
No known key found for this signature in database
GPG Key ID: B4341DD08B2371CB
4 changed files with 57 additions and 44 deletions

View File

@ -23,10 +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.nav.destination.library.LibraryFactory import org.kiwix.kiwixmobile.webserver.KiwixServer
import org.kiwix.kiwixmobile.webserver.WebServerHelper import org.kiwix.kiwixmobile.webserver.WebServerHelper
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver
@ -39,29 +37,28 @@ class ServiceModule {
@Provides @Provides
@ServiceScope @ServiceScope
fun providesWebServerHelper( fun providesWebServerHelper(
jniKiwixLibrary: LibraryFactory, kiwixServer: KiwixServer,
kiwixServer: JNIKiwixServer,
ipAddressCallbacks: IpAddressCallbacks ipAddressCallbacks: IpAddressCallbacks
): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer, ipAddressCallbacks) ): WebServerHelper = WebServerHelper(kiwixServer, ipAddressCallbacks)
@Provides @Provides
@ServiceScope @ServiceScope
fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks =
service as IpAddressCallbacks service as IpAddressCallbacks
@Provides // @Provides
@ServiceScope // @ServiceScope
fun providesLibrary(): Library = Library() // fun providesLibrary(): Library = Library()
@Provides // @Provides
@ServiceScope // @ServiceScope
fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer = // fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer =
JNIKiwixServer(jniKiwixLibrary) // KiwixServer(jniKiwixLibrary)
@Provides // @Provides
@ServiceScope // @ServiceScope
fun providesKiwixLibraryFactory(kiwixLibrary: Library): Library = // fun providesKiwixLibraryFactory(): Library =
LibraryFactory(kiwixLibrary) // LibraryFactory()
@Provides @Provides
@ServiceScope @ServiceScope

View File

@ -18,18 +18,9 @@
package org.kiwix.kiwixmobile.nav.destination.library package org.kiwix.kiwixmobile.nav.destination.library
import android.util.Log
import org.kiwix.kiwixlib.Library import org.kiwix.kiwixlib.Library
import javax.inject.Inject import javax.inject.Inject
private const val TAG = "LibraryFactory" class LibraryFactory @Inject constructor() : Library() {
fun createKiwixLibrary(): Library = Library()
class LibraryFactory @Inject constructor(libr: Library) : Library() {
private val lib = libr
fun createKiwixLibrary(): Library {
Log.d(TAG, "createKiwixLibrary: instance check of lib: $lib")
return lib
}
// works as previous one, nothing special also, ammend in this commit
} }

View File

@ -0,0 +1,29 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.webserver
import org.kiwix.kiwixlib.JNIKiwixServer
import org.kiwix.kiwixmobile.nav.destination.library.LibraryFactory
import javax.inject.Inject
class KiwixServer @Inject constructor(kiwixLibraryFactory: LibraryFactory) {
val kiwixLibrary = kiwixLibraryFactory.createKiwixLibrary()
fun createKiwixServer(): JNIKiwixServer? = JNIKiwixServer(kiwixLibrary)
}

View File

@ -29,8 +29,6 @@ import org.kiwix.kiwixlib.JNIKiwixException;
import org.kiwix.kiwixlib.JNIKiwixServer; import org.kiwix.kiwixlib.JNIKiwixServer;
import org.kiwix.kiwixlib.Library; import org.kiwix.kiwixlib.Library;
import org.kiwix.kiwixmobile.core.utils.ServerUtils; import org.kiwix.kiwixmobile.core.utils.ServerUtils;
import org.kiwix.kiwixmobile.di.components.ServiceComponent;
import org.kiwix.kiwixmobile.nav.destination.library.LibraryFactory;
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks; import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks;
import static org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP; import static org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP;
@ -44,20 +42,17 @@ import static org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP;
public class WebServerHelper { public class WebServerHelper {
private static final String TAG = "WebServerHelper"; private static final String TAG = "WebServerHelper";
//private Library kiwixLibrary; //private Library kiwixLibrary;
private LibraryFactory kiwixLibraryFactory; private KiwixServer kiwixServer;
private JNIKiwixServer kiwixServer;
private IpAddressCallbacks ipAddressCallbacks; private IpAddressCallbacks ipAddressCallbacks;
private boolean isServerStarted; private boolean isServerStarted;
private ServiceComponent serviceComponent; @Inject public WebServerHelper(@NonNull KiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) {
@Inject public WebServerHelper(@NonNull LibraryFactory kiwixLibraryFactory,
@NonNull JNIKiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) {
this.kiwixLibraryFactory = kiwixLibraryFactory;
this.kiwixServer = kiwixServer; this.kiwixServer = kiwixServer;
this.ipAddressCallbacks = ipAddressCallbacks; this.ipAddressCallbacks = ipAddressCallbacks;
} }
private JNIKiwixServer server;
public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) { public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {
String ip = ServerUtils.getIpAddress(); String ip = ServerUtils.getIpAddress();
server = kiwixServer.createKiwixServer();
if (ip.length() == 0) { if (ip.length() == 0) {
return false; return false;
} else if (startAndroidWebServer(selectedBooksPath)) { } else if (startAndroidWebServer(selectedBooksPath)) {
@ -68,7 +63,7 @@ private ServiceComponent serviceComponent;
public void stopAndroidWebServer() { public void stopAndroidWebServer() {
if (isServerStarted) { if (isServerStarted) {
kiwixServer.stop(); server.stop();
updateServerState(false); updateServerState(false);
} }
} }
@ -76,23 +71,24 @@ private ServiceComponent serviceComponent;
private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) { private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
if (!isServerStarted) { if (!isServerStarted) {
int DEFAULT_PORT = 8080; int DEFAULT_PORT = 8080;
Log.d(TAG, "startAndroidWebServer: KiwixLibFactory check: { " + kiwixLibraryFactory + " }"); Library kiwixLibrary = kiwixServer.getKiwixLibrary();
Log.d(TAG, "startAndroidWebServer: KiwixLibFactory check: { " + kiwixLibrary + " }");
Log.d(TAG, Log.d(TAG,
"startAndroidWebServer: LibName101: { " + kiwixLibraryFactory + " }"); "startAndroidWebServer: LibName101: { " + kiwixLibrary + " }");
ServerUtils.port = DEFAULT_PORT; ServerUtils.port = DEFAULT_PORT;
for (String path : selectedBooksPath) { for (String path : selectedBooksPath) {
try { try {
boolean isBookAdded = kiwixLibraryFactory.addBook(path); boolean isBookAdded = kiwixLibrary.addBook(path);
Log.d(TAG, "startAndroidWebServer: LibName102: { " Log.d(TAG, "startAndroidWebServer: LibName102: { "
+ kiwixLibraryFactory + kiwixLibrary
+ " }"); + " }");
Log.v(TAG, "isBookAdded: " + isBookAdded + path); Log.v(TAG, "isBookAdded: " + isBookAdded + path);
} catch (JNIKiwixException e) { } catch (JNIKiwixException e) {
Log.v(TAG, "Couldn't add book " + path); Log.v(TAG, "Couldn't add book " + path);
} }
} }
kiwixServer.setPort(ServerUtils.port); server.setPort(ServerUtils.port);
updateServerState(kiwixServer.start()); updateServerState(server.start());
Log.v(TAG, "Server status" + isServerStarted); Log.v(TAG, "Server status" + isServerStarted);
} }
return isServerStarted; return isServerStarted;