mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-22 07:12:19 +00:00
Refactored basic peer init in MVP architecture
This commit is contained in:
parent
8194b27c2e
commit
53f7453f12
@ -10,7 +10,7 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
android:name=".AppHelper">
|
||||
android:name=".helpers.AppHelper">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package io.github.chronosx88.influence.contracts;
|
||||
|
||||
public interface MainActivityContract {
|
||||
//
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package io.github.chronosx88.influence.contracts;
|
||||
|
||||
public interface MainActivityModelContact {
|
||||
public interface MainModelContract {
|
||||
void initPeer();
|
||||
void shutdownPeer();
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package io.github.chronosx88.influence.contracts;
|
||||
|
||||
public interface MainActivityPresenterContract {
|
||||
public interface MainPresenterContract {
|
||||
void initPeer();
|
||||
void onDestroy();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package io.github.chronosx88.influence.contracts;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
public interface MainViewContract {
|
||||
//
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package io.github.chronosx88.influence;
|
||||
package io.github.chronosx88.influence.helpers;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
@ -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;
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user