mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-25 00:22:17 +00:00
Added put/get methods in P2PUtils (wrapper for TomP2P). Adapted existing classes to wrapper.
This commit is contained in:
parent
c96231e284
commit
18c06d1b5a
@ -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<Number640, Data> entry : futureGetPendingChats.dataMap().entrySet()) {
|
||||
Map<Number640, Data> pendingChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingChats");
|
||||
if(pendingChats != null) {
|
||||
for(Map.Entry<Number640, Data> 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<Number640, Data> entry : futureGetPendingAcceptedChats.dataMap().entrySet()) {
|
||||
Map<Number640, Data> pendingAcceptedChats = P2PUtils.get(AppHelper.getPeerID() + "_pendingAcceptedChats");
|
||||
if(pendingAcceptedChats != null) {
|
||||
for(Map.Entry<Number640, Data> 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();
|
||||
}
|
||||
|
||||
|
@ -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<Number640, Data> get(String locationKey) {
|
||||
FutureGet futureGet = peerDHT
|
||||
.get(Number160.createHash(locationKey))
|
||||
.all()
|
||||
.start()
|
||||
.awaitUninterruptibly();
|
||||
if(!futureGet.isEmpty()) {
|
||||
return futureGet.dataMap();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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!");
|
||||
}
|
||||
}
|
||||
|
@ -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<Number640, Data> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user