From 18c06d1b5a540951b21c88ae9c2ae93ad00e9f15 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 26 Mar 2019 17:58:27 +0400 Subject: [PATCH] Added put/get methods in P2PUtils (wrapper for TomP2P). Adapted existing classes to wrapper. --- .../influence/helpers/NetworkHandler.java | 32 ++++++------------- .../influence/helpers/P2PUtils.java | 28 ++++++++++++++++ .../chronosx88/influence/logic/MainLogic.java | 7 ++-- .../influence/logic/StartChatLogic.java | 29 +++++------------ 4 files changed, 47 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java index 9dc4c3b..c8ae371 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/NetworkHandler.java @@ -72,13 +72,9 @@ public class NetworkHandler implements NetworkObserver { } public static void handlePendingChats() { - FutureGet futureGetPendingChats = peerDHT - .get(Number160.createHash(AppHelper.getPeerID() + "_pendingChats")) - .all() - .start() - .awaitUninterruptibly(); - if(!futureGetPendingChats.isEmpty()) { - for(Map.Entry entry : futureGetPendingChats.dataMap().entrySet()) { + Map pendingChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingChats"); + if(pendingChats != null) { + for(Map.Entry entry : pendingChats.entrySet()) { NewChatRequestMessage newChatRequestMessage = null; try { newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class); @@ -105,14 +101,10 @@ public class NetworkHandler implements NetworkObserver { .awaitUninterruptibly(); } else { try { - FuturePut put = peerDHT.put(Number160.createHash(newChatRequestMessage.getSenderID() + "_pendingAcceptedChats")) - .data(Number160.createHash(newChatRequestReply.getChatID()), new Data(gson.toJson(newChatRequestReply))) - .start() - .awaitUninterruptibly(); - if(put.isSuccess()) { + if(P2PUtils.put(newChatRequestMessage.getSenderID() + "_pendingAcceptedChats", newChatRequestReply.getChatID(), new Data(gson.toJson(newChatRequestReply)))) { Log.i(LOG_TAG, "# Successfully put message SUCCESSFULLY_CREATE_CHAT in " + newChatRequestMessage.getSenderID() + "_pendingAcceptedChats, because receiver is offline."); } else { - Log.e(LOG_TAG, "# Failed to put message SUCCESSFULLY_CREATE_CHAT in " + newChatRequestMessage.getSenderID() + "_pendingAcceptedChats. Reason: " + put.failedReason()); + Log.e(LOG_TAG, "# Failed to put message SUCCESSFULLY_CREATE_CHAT in " + newChatRequestMessage.getSenderID() + "_pendingAcceptedChats."); } } catch (IOException e) { e.printStackTrace(); @@ -129,20 +121,14 @@ public class NetworkHandler implements NetworkObserver { } public static void handlePendingAcceptedChats() { - FutureGet futureGetPendingAcceptedChats = peerDHT - .get(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats")) - .all() - .start() - .awaitUninterruptibly(); - if(!futureGetPendingAcceptedChats.isEmpty()) { - for(Map.Entry entry : futureGetPendingAcceptedChats.dataMap().entrySet()) { + Map pendingAcceptedChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingAcceptedChats"); + if(pendingAcceptedChats != null) { + for(Map.Entry entry : pendingAcceptedChats.entrySet()) { NewChatRequestMessage newChatRequestMessage = null; try { newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java b/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java index c0b2c36..adcfb1b 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java @@ -2,9 +2,16 @@ package io.github.chronosx88.influence.helpers; import com.google.gson.Gson; +import net.tomp2p.dht.FutureGet; +import net.tomp2p.dht.FuturePut; import net.tomp2p.dht.PeerDHT; import net.tomp2p.futures.FuturePing; +import net.tomp2p.peers.Number160; +import net.tomp2p.peers.Number640; import net.tomp2p.peers.PeerAddress; +import net.tomp2p.storage.Data; + +import java.util.Map; public class P2PUtils { private static Gson gson = new Gson(); @@ -31,4 +38,25 @@ public class P2PUtils { .awaitUninterruptibly(); return ping.isSuccess(); } + + public static boolean put(String locationKey, String contentKey, Data data) { + FuturePut futurePut = peerDHT + .put(Number160.createHash(locationKey)) + .data(contentKey == null ? Number160.ZERO : Number160.createHash(contentKey), data) + .start() + .awaitUninterruptibly(); + return futurePut.isSuccess(); + } + + public static Map get(String locationKey) { + FutureGet futureGet = peerDHT + .get(Number160.createHash(locationKey)) + .all() + .start() + .awaitUninterruptibly(); + if(!futureGet.isEmpty()) { + return futureGet.dataMap(); + } + return null; + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java index 1103d24..4e955be 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java @@ -38,6 +38,7 @@ import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.DSAKey; import io.github.chronosx88.influence.helpers.KeyPairManager; import io.github.chronosx88.influence.helpers.NetworkHandler; +import io.github.chronosx88.influence.helpers.P2PUtils; import io.github.chronosx88.influence.helpers.StorageMVStore; import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.models.PublicUserProfile; @@ -242,10 +243,6 @@ public class MainLogic implements MainLogicContract { } catch (IOException e) { e.printStackTrace(); } - FuturePut futurePut = peerDHT.put(Number160.createHash(AppHelper.getPeerID() + "_profile")) - .data(serializedUserProfile) - .start() - .awaitUninterruptibly(); - Log.i(LOG_TAG, futurePut.isSuccess() ? "# Profile successfully published!" : "# Profile publishing failed!"); + Log.i(LOG_TAG, P2PUtils.put(AppHelper.getPeerID() + "_profile", null, serializedUserProfile) ? "# Profile successfully published!" : "# Profile publishing failed!"); } } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java index 1c8b5db..8a00a84 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/StartChatLogic.java @@ -10,10 +10,12 @@ import net.tomp2p.dht.FuturePut; import net.tomp2p.dht.PeerDHT; import net.tomp2p.futures.FuturePing; import net.tomp2p.peers.Number160; +import net.tomp2p.peers.Number640; import net.tomp2p.peers.PeerAddress; import net.tomp2p.storage.Data; import java.io.IOException; +import java.util.Map; import java.util.UUID; import io.github.chronosx88.influence.contracts.startchat.StartChatLogicContract; @@ -54,15 +56,10 @@ public class StartChatLogic implements StartChatLogicContract { peerDHT.peer().sendDirect(recipientPeerAddress).object(gson.toJson(newChatRequestMessage)).start().awaitUninterruptibly(); } else { try { - FuturePut futurePut = peerDHT - .put(Number160.createHash(peerID + "_pendingChats")) - .data(Number160.createHash(newChatRequestMessage.getChatID()), new Data(gson.toJson(newChatRequestMessage))) - .start() - .awaitUninterruptibly(); - if(futurePut.isSuccess()) { + if(P2PUtils.put(peerID + "_pendingChats", newChatRequestMessage.getChatID(), new Data(gson.toJson(newChatRequestMessage)))) { Log.i(LOG_TAG, "# Create new offline chat request is successful! ChatID: " + newChatRequestMessage.getChatID()); } else { - Log.e(LOG_TAG, "# Failed to create chat: " + futurePut.failedReason()); + Log.e(LOG_TAG, "# Failed to create offline chat request. ChatID: " + newChatRequestMessage.getChatID()); } } catch (IOException e) { e.printStackTrace(); @@ -75,21 +72,11 @@ public class StartChatLogic implements StartChatLogicContract { private PublicUserProfile getPublicProfile(String peerID) { PublicUserProfile publicProfile = null; - FutureGet futureGetProfile = peerDHT.get(Number160.createHash(peerID + "_profile")).start().awaitUninterruptibly(); - if (!futureGetProfile.isEmpty()) { - String jsonString = null; + Map data = P2PUtils.get(peerID + "_profile"); + if (data != null && data.size() == 1) { try { - jsonString = (String) futureGetProfile.data().object(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - try { - publicProfile = gson.fromJson((String) futureGetProfile.data().object(), PublicUserProfile.class); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + publicProfile = gson.fromJson((String) data.values().iterator().next().object(), PublicUserProfile.class); + } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } return publicProfile;