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 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;
}

View File

@ -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();
}

View File

@ -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) {

View File

@ -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