diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d2eb58a..e791df6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" - android:name=".AppHelper"> + android:name=".helpers.AppHelper"> diff --git a/app/src/main/java/io/github/chronosx88/influence/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/MainActivity.java index 86ad52a..70295d3 100644 --- a/app/src/main/java/io/github/chronosx88/influence/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/MainActivity.java @@ -1,80 +1,61 @@ package io.github.chronosx88.influence; -import android.content.SharedPreferences; +import android.app.ProgressDialog; import android.os.Bundle; -import android.util.Log; +import android.widget.Toast; -import net.tomp2p.dht.PeerBuilderDHT; -import net.tomp2p.dht.PeerDHT; -import net.tomp2p.futures.FutureBootstrap; -import net.tomp2p.futures.FutureDiscover; -import net.tomp2p.p2p.PeerBuilder; -import net.tomp2p.peers.Number160; - -import java.io.IOException; -import java.net.Inet4Address; -import java.net.InetAddress; -import java.util.UUID; +import org.json.JSONException; +import org.json.JSONObject; import androidx.appcompat.app.AppCompatActivity; -import io.github.chronosx88.influence.helpers.StorageMVStore; +import io.github.chronosx88.influence.contracts.MainPresenterContract; +import io.github.chronosx88.influence.contracts.MainViewContract; +import io.github.chronosx88.influence.contracts.observer.Observer; +import io.github.chronosx88.influence.helpers.AppHelper; +import io.github.chronosx88.influence.helpers.MessageActions; +import io.github.chronosx88.influence.presenters.MainPresenter; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements Observer, MainViewContract { - private PeerDHT peerDHT; - private Number160 peerID; - private SharedPreferences preferences; + private MainPresenterContract presenter; + private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - org.apache.log4j.BasicConfigurator.configure(); setContentView(R.layout.activity_main); + presenter = new MainPresenter(this); + AppHelper.getObservable().register(this); - preferences = getSharedPreferences("main_config", MODE_PRIVATE); - - if(checkFirstRun()) { - SharedPreferences.Editor editor = preferences.edit(); - String uuid = UUID.randomUUID().toString(); - editor.putString("peerID", uuid); - editor.apply(); - } - - peerID = Number160.createHash(preferences.getString("peerID", "0")); - - try { - peerDHT = new PeerBuilderDHT( - new PeerBuilder(peerID) - .ports(7243) - .behindFirewall(true) - .start() - ) - .storage(new StorageMVStore(peerID, getFilesDir())) - .start(); - InetAddress address = Inet4Address.getByName("*IP*"); - FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress( address ).ports( 7243 ).start(); - futureDiscover.awaitUninterruptibly(); - FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress( address ).ports( 7243 ).start(); - futureBootstrap.awaitUninterruptibly(); - Log.d("", futureBootstrap.failedReason()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public boolean checkFirstRun() { - if (preferences.getBoolean("firstRun", true)) { - SharedPreferences.Editor editor = preferences.edit(); - editor.putBoolean("firstRun", false); - editor.apply(); - return true; - } - return false; + progressDialog = new ProgressDialog(MainActivity.this, R.style.AlertDialogTheme); + progressDialog.setCancelable(false); + progressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small); + progressDialog.show(); + presenter.initPeer(); } @Override protected void onDestroy() { super.onDestroy(); - peerDHT.shutdown(); + presenter.onDestroy(); + AppHelper.getObservable().unregister(this); + } + + @Override + public void handleEvent(JSONObject object) { + try { + switch ((int) object.get("action")) { + case MessageActions.BOOTSTRAP_NOT_SPECIFIED: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show(); + }); + + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityContract.java deleted file mode 100644 index 556db9d..0000000 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityContract.java +++ /dev/null @@ -1,5 +0,0 @@ -package io.github.chronosx88.influence.contracts; - -public interface MainActivityContract { - // -} diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityModelContact.java b/app/src/main/java/io/github/chronosx88/influence/contracts/MainModelContract.java similarity index 54% rename from app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityModelContact.java rename to app/src/main/java/io/github/chronosx88/influence/contracts/MainModelContract.java index 273621d..18f7ac0 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityModelContact.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/MainModelContract.java @@ -1,5 +1,6 @@ package io.github.chronosx88.influence.contracts; -public interface MainActivityModelContact { +public interface MainModelContract { void initPeer(); + void shutdownPeer(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityPresenterContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/MainPresenterContract.java similarity index 54% rename from app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityPresenterContract.java rename to app/src/main/java/io/github/chronosx88/influence/contracts/MainPresenterContract.java index 72e3d77..cca16f2 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/MainActivityPresenterContract.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/MainPresenterContract.java @@ -1,5 +1,6 @@ package io.github.chronosx88.influence.contracts; -public interface MainActivityPresenterContract { +public interface MainPresenterContract { void initPeer(); + void onDestroy(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/MainViewContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/MainViewContract.java new file mode 100644 index 0000000..9c44961 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/MainViewContract.java @@ -0,0 +1,7 @@ +package io.github.chronosx88.influence.contracts; + +import android.content.SharedPreferences; + +public interface MainViewContract { + // +} diff --git a/app/src/main/java/io/github/chronosx88/influence/AppHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java similarity index 93% rename from app/src/main/java/io/github/chronosx88/influence/AppHelper.java rename to app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java index 29f81fb..ec22ae9 100644 --- a/app/src/main/java/io/github/chronosx88/influence/AppHelper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java @@ -1,4 +1,4 @@ -package io.github.chronosx88.influence; +package io.github.chronosx88.influence.helpers; import android.app.Application; import android.content.Context; diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/MessageActions.java b/app/src/main/java/io/github/chronosx88/influence/helpers/MessageActions.java new file mode 100644 index 0000000..c5da1c3 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/MessageActions.java @@ -0,0 +1,6 @@ +package io.github.chronosx88.influence.helpers; + +public class MessageActions { + public static final int BOOTSTRAP_NOT_SPECIFIED = 0x0; + public static final int NETWORK_ERROR = 0x1; +} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/MainActivityModel.java b/app/src/main/java/io/github/chronosx88/influence/models/MainActivityModel.java deleted file mode 100644 index 1a8aedc..0000000 --- a/app/src/main/java/io/github/chronosx88/influence/models/MainActivityModel.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.github.chronosx88.influence.models; - -import io.github.chronosx88.influence.contracts.MainActivityModelContact; - -public class MainActivityModel implements MainActivityModelContact { - @Override - public void initPeer() { - // TODO - } -} diff --git a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java new file mode 100644 index 0000000..d338222 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java @@ -0,0 +1,105 @@ +package io.github.chronosx88.influence.models; + +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; +import android.util.Patterns; + +import net.tomp2p.dht.PeerBuilderDHT; +import net.tomp2p.dht.PeerDHT; +import net.tomp2p.futures.FutureBootstrap; +import net.tomp2p.futures.FutureDiscover; +import net.tomp2p.p2p.PeerBuilder; +import net.tomp2p.peers.Number160; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.util.UUID; +import java.util.regex.Pattern; + +import io.github.chronosx88.influence.helpers.AppHelper; +import io.github.chronosx88.influence.contracts.MainModelContract; +import io.github.chronosx88.influence.contracts.MainViewContract; +import io.github.chronosx88.influence.helpers.MessageActions; +import io.github.chronosx88.influence.helpers.StorageMVStore; + +public class MainModel implements MainModelContract { + private SharedPreferences preferences; + private Number160 peerID; + private PeerDHT peerDHT; + private Context context; + + public MainModel() { + this.context = AppHelper.getContext(); + this.preferences = context.getSharedPreferences("main_config", context.MODE_PRIVATE); + } + + @Override + public void initPeer() { + org.apache.log4j.BasicConfigurator.configure(); + + if(checkFirstRun()) { + SharedPreferences.Editor editor = preferences.edit(); + String uuid = UUID.randomUUID().toString(); + editor.putString("peerID", uuid); + editor.apply(); + } + + peerID = Number160.createHash(preferences.getString("peerID", null)); + + new Thread(() -> { + try { + InetAddress bootstrapAddress = null; + + peerDHT = new PeerBuilderDHT( + new PeerBuilder(peerID) + .ports(7243) + .behindFirewall(true) + .start() + ) + .storage(new StorageMVStore(peerID, context.getFilesDir())) + .start(); + try { + String bootstrapIP = preferences.getString("bootstrapIP", null); + bootstrapAddress = bootstrapIP == null ? null : Inet4Address.getByName(bootstrapIP); + if(bootstrapAddress == null) { + throw new Exception(); + } + } catch (Exception e) { + try { + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.BOOTSTRAP_NOT_SPECIFIED)); + return; + } catch (JSONException ex) { + ex.printStackTrace(); + } + } + + FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start().awaitUninterruptibly(); + FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress(bootstrapAddress).ports(7243).start(); + futureBootstrap.awaitUninterruptibly(); + } catch (IOException e) { + e.printStackTrace(); + } + }).start(); + } + + @Override + public void shutdownPeer() { + peerDHT.shutdown(); + } + + private boolean checkFirstRun() { + if (preferences.getBoolean("firstRun", true)) { + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("firstRun", false); + editor.apply(); + return true; + } + return false; + } +} diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/MainActivityPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/MainActivityPresenter.java deleted file mode 100644 index d46ac4f..0000000 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/MainActivityPresenter.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.github.chronosx88.influence.presenters; - -import io.github.chronosx88.influence.contracts.MainActivityPresenterContract; - -public class MainActivityPresenter implements MainActivityPresenterContract { - @Override - public void initPeer() { - //TODO - } -} diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java new file mode 100644 index 0000000..a6cdfe9 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java @@ -0,0 +1,26 @@ +package io.github.chronosx88.influence.presenters; + +import io.github.chronosx88.influence.contracts.MainModelContract; +import io.github.chronosx88.influence.contracts.MainPresenterContract; +import io.github.chronosx88.influence.contracts.MainViewContract; +import io.github.chronosx88.influence.models.MainModel; + +public class MainPresenter implements MainPresenterContract { + private MainModelContract model; + private MainViewContract view; + + public MainPresenter(MainViewContract view) { + this.view = view; + model = new MainModel(); + } + + @Override + public void initPeer() { + model.initPeer(); + } + + @Override + public void onDestroy() { + model.shutdownPeer(); + } +}