Added bootstrap_success and port forwarding error messages

This commit is contained in:
ChronosX88 2019-03-14 15:16:15 +04:00
parent a8fb301965
commit 4e8b045501
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 65 additions and 10 deletions

View File

@ -3,4 +3,6 @@ package io.github.chronosx88.influence.helpers;
public class MessageActions { public class MessageActions {
public static final int BOOTSTRAP_NOT_SPECIFIED = 0x0; public static final int BOOTSTRAP_NOT_SPECIFIED = 0x0;
public static final int NETWORK_ERROR = 0x1; public static final int NETWORK_ERROR = 0x1;
public static final int BOOTSTRAP_SUCCESS = 0x2;
public static final int PORT_FORWARDING_ERROR = 0x3;
} }

View File

@ -18,6 +18,7 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -35,7 +36,7 @@ public class MainModel implements MainModelContract {
public MainModel() { public MainModel() {
this.context = AppHelper.getContext(); 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 @Override
@ -58,30 +59,61 @@ public class MainModel implements MainModelContract {
peerDHT = new PeerBuilderDHT( peerDHT = new PeerBuilderDHT(
new PeerBuilder(peerID) new PeerBuilder(peerID)
.ports(7243) .ports(7243)
.behindFirewall(true)
.start() .start()
) )
.storage(new StorageMVStore(peerID, context.getFilesDir())) .storage(new StorageMVStore(peerID, context.getFilesDir()))
.start(); .start();
try { try {
String bootstrapIP = preferences.getString("bootstrapIP", null); String bootstrapIP = this.preferences.getString("bootstrapAddress", null);
bootstrapAddress = bootstrapIP == null ? null : Inet4Address.getByName(bootstrapIP); if(bootstrapIP == null) {
if(bootstrapAddress == null) { throw new NullPointerException();
throw new Exception();
} }
} catch (Exception e) { bootstrapAddress = Inet4Address.getByName(bootstrapIP);
} catch (NullPointerException e) {
try { try {
AppHelper.getObservable().notifyObservers(new JSONObject() AppHelper.getObservable().notifyObservers(new JSONObject()
.put("action", MessageActions.BOOTSTRAP_NOT_SPECIFIED)); .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; return;
} catch (JSONException ex) { } catch (JSONException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
FutureDiscover futureDiscover = peerDHT.peer().discover().inetAddress(bootstrapAddress).ports(7243).start().awaitUninterruptibly();
FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress(bootstrapAddress).ports(7243).start(); FutureBootstrap futureBootstrap = peerDHT.peer().bootstrap().inetAddress(bootstrapAddress).ports(7243).start();
futureBootstrap.awaitUninterruptibly(); futureBootstrap.awaitUninterruptibly();
if(futureBootstrap.isSuccess()) {
try {
AppHelper.getObservable().notifyObservers(new JSONObject()
.put("action", MessageActions.BOOTSTRAP_SUCCESS));
} catch (JSONException ex) {
ex.printStackTrace();
}
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -96,7 +96,28 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie
progressDialog.dismiss(); progressDialog.dismiss();
Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show(); 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) { } catch (JSONException e) {

View File

@ -9,7 +9,7 @@ buildscript {
} }
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files