Fixed issues with starting Pastry node on Android. Added Notification about new chat

This commit is contained in:
ChronosX88 2019-05-16 19:07:23 +04:00
parent 3d2230bea4
commit d3ba77fbfd
9 changed files with 113 additions and 18 deletions

View File

@ -58,6 +58,11 @@ dependencies {
implementation 'com.esotericsoftware:kryo:5.0.0-RC1' implementation 'com.esotericsoftware:kryo:5.0.0-RC1'
implementation 'com.github.instacart.truetime-android:library:3.4' implementation 'com.github.instacart.truetime-android:library:3.4'
implementation 'jdom:jdom:1.1'
implementation 'commons-beanutils:commons-beanutils:1.8.3'
implementation 'org.apache.tomcat:servlet-api:6.0.35'
//implementation group: 'org.eclipse.jetty.orbit', name: 'javax.servlet.jsp', version: '2.2.0.v201112011158'
} }
repositories { repositories {
mavenCentral() mavenCentral()

Binary file not shown.

Binary file not shown.

BIN
app/libs/openbeans-1.0.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@ -75,4 +75,45 @@ public class NetworkHandler implements INetworkObserver {
} }
} }
} }
public static void handleNewChat(String chatID) {
Data newChat = P2PUtils.get(AppHelper.getPeerID() + "_pendingChats", chatID);
if (newChat != null) {
NewChatRequestMessage newChatRequestMessage = null;
try {
newChatRequestMessage = gson.fromJson((String) newChat.object(), NewChatRequestMessage.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ChatMember chatMember = new ChatMember(AppHelper.getPeerID(), AppHelper.getPeerID());
Data putData = null;
try {
putData = new Data(gson.toJson(chatMember)).protectEntry(keyPairManager.openMainKeyPair());
} catch (IOException e) {
e.printStackTrace();
}
P2PUtils.put(newChatRequestMessage.getChatID() + "_members", AppHelper.getPeerID(), putData);
LocalDBWrapper.createChatEntry(
newChatRequestMessage.getChatID(),
newChatRequestMessage.getUsername(),
newChatRequestMessage.getChatID() + "_metadata",
newChatRequestMessage.getChatID() + "_members",
newChatRequestMessage.getChunkID()
);
P2PUtils.remove(AppHelper.getPeerID() + "_pendingChats", newChatRequestMessage.getChatID());
String messageID = UUID.randomUUID().toString();
try {
P2PUtils.put(newChatRequestMessage.getChatID() + "_messages", messageID, new Data(gson.toJson(new JoinChatMessage(AppHelper.getPeerID(), AppHelper.getUsername() == null ? AppHelper.getPeerID() : AppHelper.getUsername(), newChatRequestMessage.getChatID(), System.currentTimeMillis()))).protectEntry(keyPairManager.openMainKeyPair()));
} catch (IOException e) {
e.printStackTrace();
}
ObservableUtils.notifyUI(UIActions.SUCCESSFUL_CREATE_CHAT);
}
}
} }

View File

@ -53,6 +53,20 @@ public class P2PUtils {
return null; return null;
} }
public static Data get(String locationKey, String contentKey) {
FutureGet futureGet = peerDHT
.get(Number160.createHash(locationKey))
.contentKey(Number160.createHash(contentKey))
.start()
.awaitUninterruptibly();
if(futureGet != null) {
if(!futureGet.isEmpty()) {
return futureGet.data();
}
}
return null;
}
public static boolean remove(String locationKey, String contentKey) { public static boolean remove(String locationKey, String contentKey) {
FutureRemove futureRemove = peerDHT FutureRemove futureRemove = peerDHT
.remove(Number160.createHash(locationKey)) .remove(Number160.createHash(locationKey))

View File

@ -7,10 +7,8 @@ import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.tomp2p.connection.Bindings;
import net.tomp2p.connection.ChannelClientConfiguration; import net.tomp2p.connection.ChannelClientConfiguration;
import net.tomp2p.connection.ChannelServerConfiguration; import net.tomp2p.connection.ChannelServerConfiguration;
import net.tomp2p.connection.Ports;
import net.tomp2p.connection.RSASignatureFactory; import net.tomp2p.connection.RSASignatureFactory;
import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.PeerDHT; import net.tomp2p.dht.PeerDHT;
@ -39,8 +37,6 @@ import java.net.UnknownHostException;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID; import java.util.UUID;
import io.github.chronosx88.influence.contracts.CoreContracts; import io.github.chronosx88.influence.contracts.CoreContracts;
@ -56,11 +52,9 @@ import io.github.chronosx88.influence.helpers.actions.UIActions;
import io.github.chronosx88.influence.models.ChatMetadata; import io.github.chronosx88.influence.models.ChatMetadata;
import io.github.chronosx88.influence.models.NewChatRequestMessage; import io.github.chronosx88.influence.models.NewChatRequestMessage;
import io.github.chronosx88.influence.models.PublicUserProfile; import io.github.chronosx88.influence.models.PublicUserProfile;
import io.github.chronosx88.influence.notificationSystem.NotificationHandler; import io.github.chronosx88.influence.models.notifications.NewChatNotification;
import io.github.chronosx88.influence.notificationSystem.NotificationSystem; import io.github.chronosx88.influence.notificationSystem.NotificationSystem;
import rice.environment.Environment; import rice.environment.Environment;
import rice.p2p.scribe.ScribeContent;
import rice.pastry.NodeHandle;
import rice.pastry.NodeIdFactory; import rice.pastry.NodeIdFactory;
import rice.pastry.PastryNode; import rice.pastry.PastryNode;
import rice.pastry.socket.internet.InternetPastryNodeFactory; import rice.pastry.socket.internet.InternetPastryNodeFactory;
@ -188,7 +182,11 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
publicProfileToDHT(); publicProfileToDHT();
SettingsLogic.Companion.publishUsername(AppHelper.getUsername(), AppHelper.getUsername()); SettingsLogic.Companion.publishUsername(AppHelper.getUsername(), AppHelper.getUsername());
NetworkHandler.handlePendingChatRequests(); NetworkHandler.handlePendingChatRequests();
AppHelper.getNotificationSystem().subscribe(AppHelper.getPeerID() + "_newChats", notification -> NetworkHandler.handlePendingChatRequests()); AppHelper.getNotificationSystem().subscribe(AppHelper.getPeerID() + "_newChats", notification -> {
if(notification instanceof NewChatNotification) {
NetworkHandler.handleNewChat(((NewChatNotification) notification).getChatID());
}
});
/*TimerTask timerTask = new TimerTask() { /*TimerTask timerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -263,7 +261,9 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
peerDHT.peer().announceShutdown().start().awaitUninterruptibly(); peerDHT.peer().announceShutdown().start().awaitUninterruptibly();
peerDHT.peer().shutdown().awaitUninterruptibly(); peerDHT.peer().shutdown().awaitUninterruptibly();
} }
storage.close(); if(storage != null) {
storage.close();
}
System.exit(0); System.exit(0);
}).start(); }).start();
} }
@ -321,7 +321,7 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
return; return;
} }
NewChatRequestMessage newChatRequestMessage = new NewChatRequestMessage(UUID.randomUUID().toString(), UUID.randomUUID().toString(), AppHelper.getPeerID(), AppHelper.getUsername(), System.currentTimeMillis(), 0); NewChatRequestMessage newChatRequestMessage = new NewChatRequestMessage(UUID.randomUUID().toString(), UUID.randomUUID().toString(), AppHelper.getPeerID(), AppHelper.getUsername() == null ? AppHelper.getPeerID() : AppHelper.getUsername(), System.currentTimeMillis(), 0);
try { try {
if(P2PUtils.put(companionPeerID + "_pendingChats", newChatRequestMessage.getChatID(), new Data(gson.toJson(newChatRequestMessage)))) { if(P2PUtils.put(companionPeerID + "_pendingChats", newChatRequestMessage.getChatID(), new Data(gson.toJson(newChatRequestMessage)))) {
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());
@ -344,6 +344,7 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
P2PUtils.put(newChatRequestMessage.getChatID() + "_metadata", null, data); P2PUtils.put(newChatRequestMessage.getChatID() + "_metadata", null, data);
LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), username, newChatRequestMessage.getChatID() + "_metadata", newChatRequestMessage.getChatID() + "_members", 0); LocalDBWrapper.createChatEntry(newChatRequestMessage.getChatID(), username, newChatRequestMessage.getChatID() + "_metadata", newChatRequestMessage.getChatID() + "_members", 0);
ObservableUtils.notifyUI(UIActions.NEW_CHAT); ObservableUtils.notifyUI(UIActions.NEW_CHAT);
AppHelper.getNotificationSystem().publish(companionPeerID + "_newChats", new NewChatNotification(newChatRequestMessage.getChatID()));
} }
private PublicUserProfile getPublicProfile(String peerID) { private PublicUserProfile getPublicProfile(String peerID) {
@ -376,20 +377,18 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
private boolean createPastryNode(int bindPort, InetSocketAddress bootAddress) throws Exception { private boolean createPastryNode(int bindPort, InetSocketAddress bootAddress) throws Exception {
Environment env = new Environment(); Environment env = new Environment();
env.getParameters().setString("probe_for_external_address","true"); env.getParameters().setString("probe_for_external_address","true");
env.getParameters().setString("nat_search_policy", "never");
AppHelper.storePastryEnvironment(env); AppHelper.storePastryEnvironment(env);
NodeIdFactory nidFactory = new RandomNodeIdFactory(env); NodeIdFactory nidFactory = new RandomNodeIdFactory(env);
InternetPastryNodeFactory factory = new InternetPastryNodeFactory(nidFactory, bindPort, env); InternetPastryNodeFactory factory = new InternetPastryNodeFactory(nidFactory, bindPort, env);
NodeHandle bootHandle = factory.getNodeHandle(bootAddress); PastryNode node = factory.newNode();
PastryNode node;
if(bootHandle != null) { node.boot(bootAddress);
node = factory.newNode(bootHandle);
} else {
return false;
}
// the node may require sending several messages to fully boot into the ring // the node may require sending several messages to fully boot into the ring
synchronized(node) { synchronized(node) {
while(!node.isReady() && !node.joinFailed()) { while(!node.isReady() && !node.joinFailed()) {
node.wait(100); node.wait(500);
if (node.joinFailed()) { if (node.joinFailed()) {
return false; return false;
} }

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 ChronosX88
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.chronosx88.influence.models.notifications;
import rice.p2p.scribe.ScribeContent;
public class NewChatNotification implements ScribeContent {
private String chatID;
public NewChatNotification(String chatID) {
this.chatID = chatID;
}
public String getChatID() {
return chatID;
}
public void setChatID(String chatID) {
this.chatID = chatID;
}
}