From 4e8b04550179377c8905ce071c090dfee8446ca7 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 14 Mar 2019 15:16:15 +0400 Subject: [PATCH] Added bootstrap_success and port forwarding error messages --- .../influence/helpers/MessageActions.java | 2 + .../influence/models/MainModel.java | 48 +++++++++++++++---- .../influence/views/MainActivity.java | 23 ++++++++- build.gradle | 2 +- 4 files changed, 65 insertions(+), 10 deletions(-) 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 index c5da1c3..ab68f13 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/MessageActions.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/MessageActions.java @@ -3,4 +3,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; + public static final int BOOTSTRAP_SUCCESS = 0x2; + public static final int PORT_FORWARDING_ERROR = 0x3; } 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 index d338222..e5134bb 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/MainModel.java @@ -18,6 +18,7 @@ import org.json.JSONObject; import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.UUID; import java.util.regex.Pattern; @@ -35,7 +36,7 @@ public class MainModel implements MainModelContract { public MainModel() { this.context = AppHelper.getContext(); - this.preferences = context.getSharedPreferences("main_config", context.MODE_PRIVATE); + this.preferences = context.getSharedPreferences("io.github.chronosx88.influence_preferences", context.MODE_PRIVATE); } @Override @@ -58,30 +59,61 @@ public class MainModel implements MainModelContract { 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(); + String bootstrapIP = this.preferences.getString("bootstrapAddress", null); + if(bootstrapIP == null) { + throw new NullPointerException(); } - } catch (Exception e) { + bootstrapAddress = Inet4Address.getByName(bootstrapIP); + } catch (NullPointerException e) { try { AppHelper.getObservable().notifyObservers(new JSONObject() .put("action", MessageActions.BOOTSTRAP_NOT_SPECIFIED)); + peerDHT.shutdown(); + return; + } catch (JSONException ex) { + ex.printStackTrace(); + } + } catch (UnknownHostException e) { + try { + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.NETWORK_ERROR)); + peerDHT.shutdown(); + return; + } catch (JSONException ex) { + ex.printStackTrace(); + } + } + + FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start(); + futureDiscover.awaitUninterruptibly(); + if(futureDiscover.isSuccess()) { + Log.d("MainModel", "Success discover! Your IP: " + futureDiscover.externalAddress().toString()); + } else { + try { + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.PORT_FORWARDING_ERROR)); + peerDHT.shutdown(); 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(); + if(futureBootstrap.isSuccess()) { + try { + AppHelper.getObservable().notifyObservers(new JSONObject() + .put("action", MessageActions.BOOTSTRAP_SUCCESS)); + } catch (JSONException ex) { + ex.printStackTrace(); + } + } } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java index 3c5836b..bef2114 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java @@ -96,7 +96,28 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie progressDialog.dismiss(); Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show(); }); - + break; + } + case MessageActions.NETWORK_ERROR: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show(); + }); + break; + } + case MessageActions.BOOTSTRAP_SUCCESS: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show(); + }); + break; + } + case MessageActions.PORT_FORWARDING_ERROR: { + runOnUiThread(() -> { + progressDialog.dismiss(); + Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show(); + }); + break; } } } catch (JSONException e) { diff --git a/build.gradle b/build.gradle index cfb7d1e..c2f4631 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:3.3.1' + classpath 'com.android.tools.build:gradle:3.3.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files