Added put/get methods in P2PUtils (wrapper for TomP2P). Adapted existing classes to wrapper.

This commit is contained in:
ChronosX88 2019-03-26 17:58:27 +04:00
parent c96231e284
commit 18c06d1b5a
4 changed files with 47 additions and 49 deletions

View File

@ -72,13 +72,9 @@ public class NetworkHandler implements NetworkObserver {
} }
public static void handlePendingChats() { public static void handlePendingChats() {
FutureGet futureGetPendingChats = peerDHT Map<Number640, Data> pendingChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingChats");
.get(Number160.createHash(AppHelper.getPeerID() + "_pendingChats")) if(pendingChats != null) {
.all() for(Map.Entry<Number640, Data> entry : pendingChats.entrySet()) {
.start()
.awaitUninterruptibly();
if(!futureGetPendingChats.isEmpty()) {
for(Map.Entry<Number640, Data> entry : futureGetPendingChats.dataMap().entrySet()) {
NewChatRequestMessage newChatRequestMessage = null; NewChatRequestMessage newChatRequestMessage = null;
try { try {
newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class); newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class);
@ -105,14 +101,10 @@ public class NetworkHandler implements NetworkObserver {
.awaitUninterruptibly(); .awaitUninterruptibly();
} else { } else {
try { try {
FuturePut put = peerDHT.put(Number160.createHash(newChatRequestMessage.getSenderID() + "_pendingAcceptedChats")) if(P2PUtils.put(newChatRequestMessage.getSenderID() + "_pendingAcceptedChats", newChatRequestReply.getChatID(), new Data(gson.toJson(newChatRequestReply)))) {
.data(Number160.createHash(newChatRequestReply.getChatID()), new Data(gson.toJson(newChatRequestReply)))
.start()
.awaitUninterruptibly();
if(put.isSuccess()) {
Log.i(LOG_TAG, "# Successfully put message SUCCESSFULLY_CREATE_CHAT in " + newChatRequestMessage.getSenderID() + "_pendingAcceptedChats, because receiver is offline."); Log.i(LOG_TAG, "# Successfully put message SUCCESSFULLY_CREATE_CHAT in " + newChatRequestMessage.getSenderID() + "_pendingAcceptedChats, because receiver is offline.");
} else { } 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -129,20 +121,14 @@ public class NetworkHandler implements NetworkObserver {
} }
public static void handlePendingAcceptedChats() { public static void handlePendingAcceptedChats() {
FutureGet futureGetPendingAcceptedChats = peerDHT Map<Number640, Data> pendingAcceptedChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingAcceptedChats");
.get(Number160.createHash(AppHelper.getPeerID() + "_pendingAcceptedChats")) if(pendingAcceptedChats != null) {
.all() for(Map.Entry<Number640, Data> entry : pendingAcceptedChats.entrySet()) {
.start()
.awaitUninterruptibly();
if(!futureGetPendingAcceptedChats.isEmpty()) {
for(Map.Entry<Number640, Data> entry : futureGetPendingAcceptedChats.dataMap().entrySet()) {
NewChatRequestMessage newChatRequestMessage = null; NewChatRequestMessage newChatRequestMessage = null;
try { try {
newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class); newChatRequestMessage = gson.fromJson((String) entry.getValue().object(), NewChatRequestMessage.class);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -2,9 +2,16 @@ package io.github.chronosx88.influence.helpers;
import com.google.gson.Gson; import com.google.gson.Gson;
import net.tomp2p.dht.FutureGet;
import net.tomp2p.dht.FuturePut;
import net.tomp2p.dht.PeerDHT; import net.tomp2p.dht.PeerDHT;
import net.tomp2p.futures.FuturePing; import net.tomp2p.futures.FuturePing;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.Number640;
import net.tomp2p.peers.PeerAddress; import net.tomp2p.peers.PeerAddress;
import net.tomp2p.storage.Data;
import java.util.Map;
public class P2PUtils { public class P2PUtils {
private static Gson gson = new Gson(); private static Gson gson = new Gson();
@ -31,4 +38,25 @@ public class P2PUtils {
.awaitUninterruptibly(); .awaitUninterruptibly();
return ping.isSuccess(); 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<Number640, Data> get(String locationKey) {
FutureGet futureGet = peerDHT
.get(Number160.createHash(locationKey))
.all()
.start()
.awaitUninterruptibly();
if(!futureGet.isEmpty()) {
return futureGet.dataMap();
}
return null;
}
} }

View File

@ -38,6 +38,7 @@ import io.github.chronosx88.influence.helpers.AppHelper;
import io.github.chronosx88.influence.helpers.DSAKey; import io.github.chronosx88.influence.helpers.DSAKey;
import io.github.chronosx88.influence.helpers.KeyPairManager; import io.github.chronosx88.influence.helpers.KeyPairManager;
import io.github.chronosx88.influence.helpers.NetworkHandler; 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.StorageMVStore;
import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.helpers.actions.UIActions;
import io.github.chronosx88.influence.models.PublicUserProfile; import io.github.chronosx88.influence.models.PublicUserProfile;
@ -242,10 +243,6 @@ public class MainLogic implements MainLogicContract {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
FuturePut futurePut = peerDHT.put(Number160.createHash(AppHelper.getPeerID() + "_profile")) Log.i(LOG_TAG, P2PUtils.put(AppHelper.getPeerID() + "_profile", null, serializedUserProfile) ? "# Profile successfully published!" : "# Profile publishing failed!");
.data(serializedUserProfile)
.start()
.awaitUninterruptibly();
Log.i(LOG_TAG, futurePut.isSuccess() ? "# Profile successfully published!" : "# Profile publishing failed!");
} }
} }

View File

@ -10,10 +10,12 @@ import net.tomp2p.dht.FuturePut;
import net.tomp2p.dht.PeerDHT; import net.tomp2p.dht.PeerDHT;
import net.tomp2p.futures.FuturePing; import net.tomp2p.futures.FuturePing;
import net.tomp2p.peers.Number160; import net.tomp2p.peers.Number160;
import net.tomp2p.peers.Number640;
import net.tomp2p.peers.PeerAddress; import net.tomp2p.peers.PeerAddress;
import net.tomp2p.storage.Data; import net.tomp2p.storage.Data;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import io.github.chronosx88.influence.contracts.startchat.StartChatLogicContract; 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(); peerDHT.peer().sendDirect(recipientPeerAddress).object(gson.toJson(newChatRequestMessage)).start().awaitUninterruptibly();
} else { } else {
try { try {
FuturePut futurePut = peerDHT if(P2PUtils.put(peerID + "_pendingChats", newChatRequestMessage.getChatID(), new Data(gson.toJson(newChatRequestMessage)))) {
.put(Number160.createHash(peerID + "_pendingChats"))
.data(Number160.createHash(newChatRequestMessage.getChatID()), new Data(gson.toJson(newChatRequestMessage)))
.start()
.awaitUninterruptibly();
if(futurePut.isSuccess()) {
Log.i(LOG_TAG, "# Create new offline chat request is successful! ChatID: " + newChatRequestMessage.getChatID()); Log.i(LOG_TAG, "# Create new offline chat request is successful! ChatID: " + newChatRequestMessage.getChatID());
} else { } 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -75,21 +72,11 @@ public class StartChatLogic implements StartChatLogicContract {
private PublicUserProfile getPublicProfile(String peerID) { private PublicUserProfile getPublicProfile(String peerID) {
PublicUserProfile publicProfile = null; PublicUserProfile publicProfile = null;
FutureGet futureGetProfile = peerDHT.get(Number160.createHash(peerID + "_profile")).start().awaitUninterruptibly(); Map<Number640, Data> data = P2PUtils.get(peerID + "_profile");
if (!futureGetProfile.isEmpty()) { if (data != null && data.size() == 1) {
String jsonString = null;
try { try {
jsonString = (String) futureGetProfile.data().object(); publicProfile = gson.fromJson((String) data.values().iterator().next().object(), PublicUserProfile.class);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException | IOException 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) {
e.printStackTrace(); e.printStackTrace();
} }
return publicProfile; return publicProfile;