mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-25 16:32:19 +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 'com.google.android.material:material:1.1.0-alpha04'
|
||||
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;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public interface Observable {
|
||||
void register(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;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
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 NEW_MESSAGE = 0x1;
|
||||
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;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -24,17 +23,15 @@ public class ChatListLogic implements ChatListLogicContract, Observer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(JSONObject object) {
|
||||
try {
|
||||
switch ((int) object.get("action")) {
|
||||
case NetworkActions.START_CHAT: {
|
||||
createChatBySender(new ChatEntity(object.getString("chatID"), object.getString("name"), ""));
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject().put("action", UIActions.NEW_CHAT), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
break;
|
||||
}
|
||||
public void handleEvent(JsonObject object) {
|
||||
switch (object.get("action").getAsInt()) {
|
||||
case NetworkActions.START_CHAT: {
|
||||
createChatBySender(new ChatEntity(object.get("chatID").getAsString(), object.get("name").getAsString(), ""));
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.NEW_CHAT);
|
||||
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.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.PeerDHT;
|
||||
import net.tomp2p.futures.FutureBootstrap;
|
||||
@ -16,7 +20,6 @@ import net.tomp2p.peers.Number160;
|
||||
import net.tomp2p.peers.PeerAddress;
|
||||
import net.tomp2p.relay.tcp.TCPRelayClientConfig;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
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.helpers.AppHelper;
|
||||
import io.github.chronosx88.influence.helpers.NetworkActions;
|
||||
import io.github.chronosx88.influence.helpers.StorageMVStore;
|
||||
import io.github.chronosx88.influence.helpers.UIActions;
|
||||
import io.github.chronosx88.influence.observable.MainObservable;
|
||||
@ -40,10 +44,12 @@ public class MainLogic implements MainLogicContract {
|
||||
private Context context;
|
||||
private InetAddress bootstrapAddress = null;
|
||||
private PeerAddress bootstrapPeerAddress = null;
|
||||
private Gson gson;
|
||||
|
||||
public MainLogic() {
|
||||
this.context = AppHelper.getContext();
|
||||
this.preferences = context.getSharedPreferences("io.github.chronosx88.influence_preferences", context.MODE_PRIVATE);
|
||||
gson = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,60 +81,42 @@ public class MainLogic implements MainLogicContract {
|
||||
}
|
||||
bootstrapAddress = Inet4Address.getByName(bootstrapIP);
|
||||
} catch (NullPointerException e) {
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.BOOTSTRAP_NOT_SPECIFIED), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
peerDHT.shutdown();
|
||||
return;
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.BOOTSTRAP_NOT_SPECIFIED);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
peerDHT.shutdown();
|
||||
return;
|
||||
} catch (UnknownHostException e) {
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.NETWORK_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
peerDHT.shutdown();
|
||||
return;
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.NETWORK_ERROR);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
peerDHT.shutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
if(!discoverExternalAddress()) {
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.PORT_FORWARDING_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.PORT_FORWARDING_ERROR);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
}
|
||||
|
||||
if(!setupConnectionToRelay()) {
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.RELAY_CONNECTION_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
return;
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.RELAY_CONNECTION_ERROR);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!bootstrapPeer()) {
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.BOOTSTRAP_ERROR), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
return;
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.BOOTSTRAP_ERROR);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
AppHelper.getObservable().notifyObservers(new JSONObject()
|
||||
.put("action", UIActions.BOOTSTRAP_SUCCESS), MainObservable.UI_ACTIONS_CHANNEL);
|
||||
} catch (JSONException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", UIActions.BOOTSTRAP_SUCCESS);
|
||||
AppHelper.getObservable().notifyObservers(jsonObject, MainObservable.UI_ACTIONS_CHANNEL);
|
||||
AppHelper.storePeerID(preferences.getString("peerID", null));
|
||||
AppHelper.storePeerDHT(peerDHT);
|
||||
setReceiveHandler();
|
||||
@ -183,7 +171,13 @@ public class MainLogic implements MainLogicContract {
|
||||
private void setReceiveHandler() {
|
||||
AppHelper.getPeerDHT().peer().objectDataReply((s, 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;
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package io.github.chronosx88.influence.observable;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -55,7 +55,7 @@ public class MainObservable implements Observable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObservers(JSONObject jsonObject, int channelID) {
|
||||
public void notifyObservers(JsonObject jsonObject, int channelID) {
|
||||
switch (channelID) {
|
||||
case UI_ACTIONS_CHANNEL: {
|
||||
for (Observer observer : uiObservers) {
|
||||
|
@ -6,9 +6,7 @@ import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
@ -89,54 +87,49 @@ public class MainActivity extends AppCompatActivity implements Observer, MainVie
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(JSONObject object) {
|
||||
try {
|
||||
switch ((int) object.get("action")) {
|
||||
case UIActions.BOOTSTRAP_NOT_SPECIFIED: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.NETWORK_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.BOOTSTRAP_SUCCESS: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.PORT_FORWARDING_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.BOOTSTRAP_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.RELAY_CONNECTION_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
public void handleEvent(JsonObject object) {
|
||||
switch (object.get("action").getAsInt()) {
|
||||
case UIActions.BOOTSTRAP_NOT_SPECIFIED: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Bootstrap-нода не указана. Прерываю подключение к сети...", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.NETWORK_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Ошибка сети. Возможно, нода недоступна, или у вас отсутствует Интернет.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.BOOTSTRAP_SUCCESS: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Нода успешно запущена!", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.PORT_FORWARDING_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
Toast.makeText(this, "Проблемы с пробросом портов. Возможно, у вас не настроен uPnP.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.BOOTSTRAP_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Не удалось подключиться к бутстрап-ноде.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case UIActions.RELAY_CONNECTION_ERROR: {
|
||||
runOnUiThread(() -> {
|
||||
progressDialog.dismiss();
|
||||
Toast.makeText(this, "Не удалось подключиться к relay-ноде.", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@ -58,15 +57,11 @@ public class ChatListFragment extends Fragment implements ChatListViewContract,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(JSONObject object) {
|
||||
try {
|
||||
switch (object.getInt("action")) {
|
||||
case UIActions.NEW_CHAT: {
|
||||
presenter.updateChatList();
|
||||
}
|
||||
public void handleEvent(JsonObject object) {
|
||||
switch (object.get("action").getAsInt()) {
|
||||
case UIActions.NEW_CHAT: {
|
||||
presenter.updateChatList();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user