Added new message notification when somebody send message

This commit is contained in:
ChronosX88 2019-05-18 17:06:35 +04:00
parent d3ba77fbfd
commit 27939c261f
3 changed files with 43 additions and 18 deletions

View File

@ -62,7 +62,6 @@ dependencies {
implementation 'jdom:jdom:1.1' implementation 'jdom:jdom:1.1'
implementation 'commons-beanutils:commons-beanutils:1.8.3' implementation 'commons-beanutils:commons-beanutils:1.8.3'
implementation 'org.apache.tomcat:servlet-api:6.0.35' 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()

View File

@ -9,7 +9,6 @@ import net.tomp2p.storage.Data;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.UUID; import java.util.UUID;
@ -24,30 +23,33 @@ import io.github.chronosx88.influence.helpers.actions.UIActions;
import io.github.chronosx88.influence.models.JoinChatMessage; import io.github.chronosx88.influence.models.JoinChatMessage;
import io.github.chronosx88.influence.models.NextChunkReference; import io.github.chronosx88.influence.models.NextChunkReference;
import io.github.chronosx88.influence.models.TextMessage; import io.github.chronosx88.influence.models.TextMessage;
import io.github.chronosx88.influence.models.notifications.NewMessageNotification;
import io.github.chronosx88.influence.models.roomEntities.ChatEntity; import io.github.chronosx88.influence.models.roomEntities.ChatEntity;
import io.github.chronosx88.influence.models.roomEntities.MessageEntity; import io.github.chronosx88.influence.models.roomEntities.MessageEntity;
public class ChatLogic implements CoreContracts.IChatLogicContract { public class ChatLogic implements CoreContracts.IChatLogicContract {
private static Gson gson = new Gson(); private static Gson gson = new Gson();
private String chatID; private String chatID;
private volatile String newMessage = ""; //private volatile String newMessage = "";
private ChatEntity chatEntity; private ChatEntity chatEntity;
private Thread checkNewMessagesThread = null; //private Thread checkNewMessagesThread = null;
private KeyPairManager keyPairManager; private KeyPairManager keyPairManager;
private Timer timer;
public ChatLogic(ChatEntity chatEntity) { public ChatLogic(ChatEntity chatEntity) {
this.chatEntity = chatEntity; this.chatEntity = chatEntity;
this.chatID = chatEntity.chatID; this.chatID = chatEntity.chatID;
TimerTask timerTask = new TimerTask() { /*TimerTask timerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
checkForNewMessages(); checkForNewMessages();
} }
}; };*/
this.timer = new Timer(); if(AppHelper.getPeerDHT() != null && AppHelper.getNotificationSystem() != null) {
if(AppHelper.getPeerDHT() != null) { AppHelper.getNotificationSystem().subscribe(chatID, notification -> {
timer.schedule(timerTask, 1, 1000); if(notification instanceof NewMessageNotification) {
handleNewMessages(chatEntity.chunkCursor);
}
});
} }
this.keyPairManager = new KeyPairManager(); this.keyPairManager = new KeyPairManager();
} }
@ -67,15 +69,16 @@ public class ChatLogic implements CoreContracts.IChatLogicContract {
} }
data.protectEntry(keyPairManager.getKeyPair("mainKeyPair")); data.protectEntry(keyPairManager.getKeyPair("mainKeyPair"));
P2PUtils.put(chatID + "_messages" + chatEntity.chunkCursor, message.messageID, data); P2PUtils.put(chatID + "_messages" + chatEntity.chunkCursor, message.messageID, data);
try { /*try {
P2PUtils.put(chatID + "_newMessage", null, new Data(message.messageID)); P2PUtils.put(chatID + "_newMessage", null, new Data(message.messageID));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }*/
AppHelper.getNotificationSystem().publish(chatID, new NewMessageNotification());
}).start(); }).start();
} }
private void checkForNewMessages() { /*private void checkForNewMessages() {
if(checkNewMessagesThread == null) { if(checkNewMessagesThread == null) {
checkNewMessagesThread = new Thread(() -> { checkNewMessagesThread = new Thread(() -> {
Map<Number640, Data> data = P2PUtils.get(chatID + "_newMessage"); Map<Number640, Data> data = P2PUtils.get(chatID + "_newMessage");
@ -120,7 +123,7 @@ public class ChatLogic implements CoreContracts.IChatLogicContract {
}); });
checkNewMessagesThread.start(); checkNewMessagesThread.start();
} }
} }*/
private void handleNewMessages(int chunkID) { private void handleNewMessages(int chunkID) {
new Thread(() -> { new Thread(() -> {
@ -160,8 +163,9 @@ public class ChatLogic implements CoreContracts.IChatLogicContract {
String messageID = UUID.randomUUID().toString(); String messageID = UUID.randomUUID().toString();
try { try {
P2PUtils.put(chatEntity.chatID + "_messages" + chunkID, messageID, new Data(gson.toJson(new NextChunkReference(messageID, AppHelper.getPeerID(), AppHelper.getPeerID(), System.currentTimeMillis(), chatEntity.chunkCursor+1)))); P2PUtils.put(chatEntity.chatID + "_messages" + chunkID, messageID, new Data(gson.toJson(new NextChunkReference(messageID, AppHelper.getPeerID(), AppHelper.getPeerID(), System.currentTimeMillis(), chatEntity.chunkCursor+1))));
P2PUtils.put(chatEntity.chatID + "_newMessage", null, new Data(messageID)); //P2PUtils.put(chatEntity.chatID + "_newMessage", null, new Data(messageID));
LocalDBWrapper.updateChatEntity(chatEntity); //LocalDBWrapper.updateChatEntity(chatEntity);
AppHelper.getNotificationSystem().publish(chatID, new NewMessageNotification());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -177,7 +181,6 @@ public class ChatLogic implements CoreContracts.IChatLogicContract {
@Override @Override
public void stopTrackingForNewMsgs() { public void stopTrackingForNewMsgs() {
timer.cancel(); AppHelper.getNotificationSystem().unsubscribe(chatID);
timer.purge();
} }
} }

View File

@ -0,0 +1,23 @@
/*
* 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 NewMessageNotification implements ScribeContent {
}