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

View File

@ -18,18 +18,9 @@
package org.kiwix.kiwixmobile.nav.destination.library
import android.util.Log
import org.kiwix.kiwixlib.Library
import javax.inject.Inject
private const val TAG = "LibraryFactory"
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
class LibraryFactory @Inject constructor() : Library() {
fun createKiwixLibrary(): Library = Library()
}

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.Library;
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 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 {
private static final String TAG = "WebServerHelper";
//private Library kiwixLibrary;
private LibraryFactory kiwixLibraryFactory;
private JNIKiwixServer kiwixServer;
private KiwixServer kiwixServer;
private IpAddressCallbacks ipAddressCallbacks;
private boolean isServerStarted;
private ServiceComponent serviceComponent;
@Inject public WebServerHelper(@NonNull LibraryFactory kiwixLibraryFactory,
@NonNull JNIKiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) {
this.kiwixLibraryFactory = kiwixLibraryFactory;
@Inject public WebServerHelper(@NonNull KiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) {
this.kiwixServer = kiwixServer;
this.ipAddressCallbacks = ipAddressCallbacks;
}
private JNIKiwixServer server;
public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {
String ip = ServerUtils.getIpAddress();
server = kiwixServer.createKiwixServer();
if (ip.length() == 0) {
return false;
} else if (startAndroidWebServer(selectedBooksPath)) {
@ -68,7 +63,7 @@ private ServiceComponent serviceComponent;
public void stopAndroidWebServer() {
if (isServerStarted) {
kiwixServer.stop();
server.stop();
updateServerState(false);
}
}
@ -76,23 +71,24 @@ private ServiceComponent serviceComponent;
private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
if (!isServerStarted) {
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,
"startAndroidWebServer: LibName101: { " + kiwixLibraryFactory + " }");
"startAndroidWebServer: LibName101: { " + kiwixLibrary + " }");
ServerUtils.port = DEFAULT_PORT;
for (String path : selectedBooksPath) {
try {
boolean isBookAdded = kiwixLibraryFactory.addBook(path);
boolean isBookAdded = kiwixLibrary.addBook(path);
Log.d(TAG, "startAndroidWebServer: LibName102: { "
+ kiwixLibraryFactory
+ kiwixLibrary
+ " }");
Log.v(TAG, "isBookAdded: " + isBookAdded + path);
} catch (JNIKiwixException e) {
Log.v(TAG, "Couldn't add book " + path);
}
}
kiwixServer.setPort(ServerUtils.port);
updateServerState(kiwixServer.start());
server.setPort(ServerUtils.port);
updateServerState(server.start());
Log.v(TAG, "Server status" + isServerStarted);
}
return isServerStarted;