mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-22 15:22:18 +00:00
Changed JSON formatting (from org.json to Gson), added ping handler (sends pong in reply)
This commit is contained in:
parent
055f62bfec
commit
34fe3715ac
@ -43,4 +43,5 @@ dependencies {
|
|||||||
implementation group: 'com.h2database', name: 'h2-mvstore', version: '1.4.197'
|
implementation group: 'com.h2database', name: 'h2-mvstore', version: '1.4.197'
|
||||||
implementation 'com.google.android.material:material:1.1.0-alpha04'
|
implementation 'com.google.android.material:material:1.1.0-alpha04'
|
||||||
implementation 'androidx.preference:preference:1.1.0-alpha03'
|
implementation 'androidx.preference:preference:1.1.0-alpha03'
|
||||||
|
implementation 'com.google.code.gson:gson:2.8.5'
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package io.github.chronosx88.influence.contracts.observer;
|
package io.github.chronosx88.influence.contracts.observer;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
public interface Observable {
|
public interface Observable {
|
||||||
void register(Observer observer, int channelID);
|
void register(Observer observer, int channelID);
|
||||||
void unregister(Observer observer, int channelID);
|
void unregister(Observer observer, int channelID);
|
||||||
void notifyObservers(JSONObject jsonObject, int channelID);
|
void notifyObservers(JsonObject jsonObject, int channelID);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package io.github.chronosx88.influence.contracts.observer;
|
package io.github.chronosx88.influence.contracts.observer;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
public interface Observer {
|
public interface Observer {
|
||||||
void handleEvent(JSONObject object);
|
void handleEvent(JsonObject object);
|
||||||
}
|
}
|
@ -4,4 +4,6 @@ public class NetworkActions {
|
|||||||
public static final int START_CHAT = 0x0;
|
public static final int START_CHAT = 0x0;
|
||||||
public static final int NEW_MESSAGE = 0x1;
|
public static final int NEW_MESSAGE = 0x1;
|
||||||
public static final int MESSAGE_SENT = 0x2;
|
public static final int MESSAGE_SENT = 0x2;
|
||||||
|
public static final int PING = 0x3;
|
||||||
|
public static final int PONG = 0x4;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package io.github.chronosx88.influence.logic;
|
package io.github.chronosx88.influence.logic;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import com.google.gson.JsonObject;
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -24,17 +23,15 @@ public class ChatListLogic implements ChatListLogicContract, Observer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(JSONObject object) {
|
public void handleEvent(JsonObject object) {
|
||||||
try {
|
switch (object.get("action").getAsInt()) {
|
||||||
switch ((int) object.get("action")) {
|
case NetworkActions.START_CHAT: {
|
||||||
case NetworkActions.START_CHAT: {
|
createChatBySender(new ChatEntity(object.get("chatID").getAsString(), object.get("name").getAsString(), ""));
|
||||||
createChatBySender(new ChatEntity(object.getString("chatID"), object.getString("name"), ""));
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject().put("action", UIActions.NEW_CHAT), MainObservable.UI_ACTIONS_CHANNEL);
|
jsonObject.addProperty("action", UIActions.NEW_CHAT);
|
||||||
break;
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ import android.content.Context;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import net.tomp2p.dht.PeerBuilderDHT;
|
import net.tomp2p.dht.PeerBuilderDHT;
|
||||||
import net.tomp2p.dht.PeerDHT;
|
import net.tomp2p.dht.PeerDHT;
|
||||||
import net.tomp2p.futures.FutureBootstrap;
|
import net.tomp2p.futures.FutureBootstrap;
|
||||||
@ -16,7 +20,6 @@ import net.tomp2p.peers.Number160;
|
|||||||
import net.tomp2p.peers.PeerAddress;
|
import net.tomp2p.peers.PeerAddress;
|
||||||
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
||||||
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -27,6 +30,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
import io.github.chronosx88.influence.contracts.main.MainLogicContract;
|
import io.github.chronosx88.influence.contracts.main.MainLogicContract;
|
||||||
import io.github.chronosx88.influence.helpers.AppHelper;
|
import io.github.chronosx88.influence.helpers.AppHelper;
|
||||||
|
import io.github.chronosx88.influence.helpers.NetworkActions;
|
||||||
import io.github.chronosx88.influence.helpers.StorageMVStore;
|
import io.github.chronosx88.influence.helpers.StorageMVStore;
|
||||||
import io.github.chronosx88.influence.helpers.UIActions;
|
import io.github.chronosx88.influence.helpers.UIActions;
|
||||||
import io.github.chronosx88.influence.observable.MainObservable;
|
import io.github.chronosx88.influence.observable.MainObservable;
|
||||||
@ -40,10 +44,12 @@ public class MainLogic implements MainLogicContract {
|
|||||||
private Context context;
|
private Context context;
|
||||||
private InetAddress bootstrapAddress = null;
|
private InetAddress bootstrapAddress = null;
|
||||||
private PeerAddress bootstrapPeerAddress = null;
|
private PeerAddress bootstrapPeerAddress = null;
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
public MainLogic() {
|
public MainLogic() {
|
||||||
this.context = AppHelper.getContext();
|
this.context = AppHelper.getContext();
|
||||||
this.preferences = context.getSharedPreferences("io.github.chronosx88.influence_preferences", context.MODE_PRIVATE);
|
this.preferences = context.getSharedPreferences("io.github.chronosx88.influence_preferences", context.MODE_PRIVATE);
|
||||||
|
gson = new Gson();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,60 +81,42 @@ public class MainLogic implements MainLogicContract {
|
|||||||
}
|
}
|
||||||
bootstrapAddress = Inet4Address.getByName(bootstrapIP);
|
bootstrapAddress = Inet4Address.getByName(bootstrapIP);
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.BOOTSTRAP_NOT_SPECIFIED);
|
||||||
.put("action", UIActions.BOOTSTRAP_NOT_SPECIFIED), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
peerDHT.shutdown();
|
peerDHT.shutdown();
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.NETWORK_ERROR);
|
||||||
.put("action", UIActions.NETWORK_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
peerDHT.shutdown();
|
peerDHT.shutdown();
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!discoverExternalAddress()) {
|
if(!discoverExternalAddress()) {
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.PORT_FORWARDING_ERROR);
|
||||||
.put("action", UIActions.PORT_FORWARDING_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!setupConnectionToRelay()) {
|
if(!setupConnectionToRelay()) {
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.RELAY_CONNECTION_ERROR);
|
||||||
.put("action", UIActions.RELAY_CONNECTION_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!bootstrapPeer()) {
|
if(!bootstrapPeer()) {
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.BOOTSTRAP_ERROR);
|
||||||
.put("action", UIActions.BOOTSTRAP_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
return;
|
return;
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
JsonObject jsonObject = new JsonObject();
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
jsonObject.addProperty("action", UIActions.BOOTSTRAP_SUCCESS);
|
||||||
.put("action", UIActions.BOOTSTRAP_SUCCESS), MainObservable.UI_ACTIONS_CHANNEL);
|
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||||
} catch (JSONException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
AppHelper.storePeerID(preferences.getString("peerID", null));
|
AppHelper.storePeerID(preferences.getString("peerID", null));
|
||||||
AppHelper.storePeerDHT(peerDHT);
|
AppHelper.storePeerDHT(peerDHT);
|
||||||
setReceiveHandler();
|
setReceiveHandler();
|
||||||
@ -183,7 +171,13 @@ public class MainLogic implements MainLogicContract {
|
|||||||
private void setReceiveHandler() {
|
private void setReceiveHandler() {
|
||||||
AppHelper.getPeerDHT().peer().objectDataReply((s, r) -> {
|
AppHelper.getPeerDHT().peer().objectDataReply((s, r) -> {
|
||||||
Log.i(LOG_TAG, "# Incoming message: " + r);
|
Log.i(LOG_TAG, "# Incoming message: " + r);
|
||||||
AppHelper.getObservable().notifyObservers(new JSONObject((String) r), MainObservable.OTHER_ACTIONS_CHANNEL);
|
JSONObject incomingObject = new JSONObject((String) r);
|
||||||
|
if(incomingObject.getInt("action") == NetworkActions.PING) {
|
||||||
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
jsonObject.addProperty("action", NetworkActions.PONG);
|
||||||
|
return gson.toJson(jsonObject);
|
||||||
|
}
|
||||||
|
AppHelper.getObservable().notifyObservers(new JsonParser().parse((String) r).getAsJsonObject(), MainObservable.OTHER_ACTIONS_CHANNEL);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package io.github.chronosx88.influence.observable;
|
package io.github.chronosx88.influence.observable;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ public class MainObservable implements Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void notifyObservers(JSONObject jsonObject, int channelID) {
|
public void notifyObservers(JsonObject jsonObject, int channelID) {
|
||||||
switch (channelID) {
|
switch (channelID) {
|
||||||
case UI_ACTIONS_CHANNEL: {
|
case UI_ACTIONS_CHANNEL: {
|
||||||
for (Observer observer : uiObservers) {
|
for (Observer observer : uiObservers) {
|
||||||
|
@ -6,9 +6,7 @@ import android.view.MenuItem;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
@ -89,54 +87,49 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(JSONObject object) {
|
public void handleEvent(JsonObject object) {
|
||||||
try {
|
switch (object.get("action").getAsInt()) {
|
||||||
switch ((int) object.get("action")) {
|
case UIActions.BOOTSTRAP_NOT_SPECIFIED: {
|
||||||
case UIActions.BOOTSTRAP_NOT_SPECIFIED: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
progressDialog.dismiss();
|
||||||
progressDialog.dismiss();
|
Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case UIActions.NETWORK_ERROR: {
|
||||||
case UIActions.NETWORK_ERROR: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
progressDialog.dismiss();
|
||||||
progressDialog.dismiss();
|
Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case UIActions.BOOTSTRAP_SUCCESS: {
|
||||||
case UIActions.BOOTSTRAP_SUCCESS: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
progressDialog.dismiss();
|
||||||
progressDialog.dismiss();
|
Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case UIActions.PORT_FORWARDING_ERROR: {
|
||||||
case UIActions.PORT_FORWARDING_ERROR: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case UIActions.BOOTSTRAP_ERROR: {
|
||||||
case UIActions.BOOTSTRAP_ERROR: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
progressDialog.dismiss();
|
||||||
progressDialog.dismiss();
|
Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
}
|
||||||
}
|
case UIActions.RELAY_CONNECTION_ERROR: {
|
||||||
case UIActions.RELAY_CONNECTION_ERROR: {
|
runOnUiThread(() -> {
|
||||||
runOnUiThread(() -> {
|
progressDialog.dismiss();
|
||||||
progressDialog.dismiss();
|
Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show();
|
||||||
Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show();
|
});
|
||||||
});
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,7 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import com.google.gson.JsonObject;
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@ -58,15 +57,11 @@ public class ChatListFragment extends Fragment implements ChatListViewContract,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(JSONObject object) {
|
public void handleEvent(JsonObject object) {
|
||||||
try {
|
switch (object.get("action").getAsInt()) {
|
||||||
switch (object.getInt("action")) {
|
case UIActions.NEW_CHAT: {
|
||||||
case UIActions.NEW_CHAT: {
|
presenter.updateChatList();
|
||||||
presenter.updateChatList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user