blob: 0c417c3a521a51b26adec0d0cdcef058dc254ddc [file] [log] [blame]
Yingdi Yud45777b2014-10-16 23:54:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yud45777b2014-10-16 23:54:11 -07004 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yuf3401182015-02-02 20:21:07 -08008 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yud45777b2014-10-16 23:54:11 -07009 */
10
11#ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
12#define CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
13
14#include <QThread>
Yingdi Yud45777b2014-10-16 23:54:11 -070015
16#ifndef Q_MOC_RUN
17#include "common.hpp"
18#include "chatroom-info.hpp"
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080019#include "chat-message.hpp"
Qiuhan Dingf22c41b2015-03-11 13:19:01 -070020#include <mutex>
Varun Patil3d850902020-11-23 12:19:14 +053021#include <ChronoSync/socket.hpp>
Yingdi Yuf3401182015-02-02 20:21:07 -080022#include <boost/thread.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053023#include <ndn-cxx/security/validator-config.hpp>
Yingdi Yud45777b2014-10-16 23:54:11 -070024#endif
25
Yingdi Yueb692ac2015-02-10 18:46:18 -080026namespace chronochat {
Yingdi Yud45777b2014-10-16 23:54:11 -070027
Varun Patila24bd3e2020-11-24 10:08:33 +053028class NodeInfo
29{
Yingdi Yud45777b2014-10-16 23:54:11 -070030public:
31 QString sessionPrefix;
32 chronosync::SeqNo seqNo;
33};
34
Varun Patila24bd3e2020-11-24 10:08:33 +053035class UserInfo
36{
Yingdi Yud45777b2014-10-16 23:54:11 -070037public:
38 ndn::Name sessionPrefix;
39 bool hasNick;
40 std::string userNick;
Varun Patila24bd3e2020-11-24 10:08:33 +053041 ndn::scheduler::ScopedEventId timeoutEventId;
Yingdi Yud45777b2014-10-16 23:54:11 -070042};
43
44class ChatDialogBackend : public QThread
45{
46 Q_OBJECT
47
48public:
49 ChatDialogBackend(const Name& chatroomPrefix,
50 const Name& userChatPrefix,
51 const Name& routingPrefix,
52 const std::string& chatroomName,
53 const std::string& nick,
Yingdi Yu45da92a2015-02-02 13:17:03 -080054 const Name& signingId = Name(),
Yingdi Yuf3401182015-02-02 20:21:07 -080055 QObject* parent = nullptr);
Yingdi Yud45777b2014-10-16 23:54:11 -070056
57 ~ChatDialogBackend();
58
59protected:
60 void
61 run();
62
63private:
64 void
65 initializeSync();
66
67 void
Yingdi Yuf3401182015-02-02 20:21:07 -080068 exitChatroom();
69
70 void
Yingdi Yu4647f022015-02-01 00:26:38 -080071 close();
72
73 void
Yingdi Yud45777b2014-10-16 23:54:11 -070074 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
75
76 void
Varun Patil3d850902020-11-23 12:19:14 +053077 processChatData(const ndn::Data& data,
Yingdi Yu45da92a2015-02-02 13:17:03 -080078 bool needDisplay,
79 bool isValidated);
Yingdi Yud45777b2014-10-16 23:54:11 -070080
81 void
82 remoteSessionTimeout(const Name& sessionPrefix);
83
84 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080085 sendMsg(ChatMessage& msg);
Yingdi Yud45777b2014-10-16 23:54:11 -070086
87 void
88 sendJoin();
89
90 void
91 sendHello();
92
93 void
94 sendLeave();
95
96 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080097 prepareControlMessage(ChatMessage& msg,
98 ChatMessage::ChatMessageType type);
Yingdi Yud45777b2014-10-16 23:54:11 -070099
100 void
101 prepareChatMessage(const QString& text,
102 time_t timestamp,
Varun Patila24bd3e2020-11-24 10:08:33 +0530103 ChatMessage& msg);
Yingdi Yud45777b2014-10-16 23:54:11 -0700104
105 void
106 updatePrefixes();
107
108 std::string
109 getHexEncodedDigest(ndn::ConstBufferPtr digest);
110
111signals:
112 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800113 syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest);
Yingdi Yud45777b2014-10-16 23:54:11 -0700114
115 void
116 chatMessageReceived(QString nick, QString text, time_t timestamp);
117
118 void
Yingdi Yud45777b2014-10-16 23:54:11 -0700119 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
120
121 void
Qiuhan Ding7a4e7ef2015-02-03 20:25:50 -0800122 messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp,
123 bool addSession);
Yingdi Yud45777b2014-10-16 23:54:11 -0700124
125 void
126 chatPrefixChanged(ndn::Name newChatPrefix);
127
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800128 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800129 refreshChatDialog(ndn::Name chatPrefix);
130
131 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800132 eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
133
134 void
135 addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
136
Yingdi Yuf3401182015-02-02 20:21:07 -0800137 void
138 newChatroomForDiscovery(ndn::Name::Component chatroomName);
139
140 void
141 nfdError();
142
Yingdi Yud45777b2014-10-16 23:54:11 -0700143public slots:
144 void
145 sendChatMessage(QString text, time_t timestamp);
146
147 void
148 updateRoutingPrefix(const QString& localRoutingPrefix);
149
150 void
151 shutdown();
152
Yingdi Yuf3401182015-02-02 20:21:07 -0800153 void
154 onNfdReconnect();
155
Yingdi Yud45777b2014-10-16 23:54:11 -0700156private:
157 typedef std::map<ndn::Name, UserInfo> BackendRoster;
158
Yingdi Yuf3401182015-02-02 20:21:07 -0800159 bool m_shouldResume;
160 bool m_isNfdConnected;
Yingdi Yu45da92a2015-02-02 13:17:03 -0800161 shared_ptr<ndn::Face> m_face;
Yingdi Yud45777b2014-10-16 23:54:11 -0700162
Varun Patil3d850902020-11-23 12:19:14 +0530163 Name m_localRoutingPrefix; // routable local prefix
164 Name m_chatroomPrefix; // chatroom sync prefix
165 Name m_userChatPrefix; // user chat prefix
166 Name m_routableUserChatPrefix; // routable user chat prefix
Yingdi Yud45777b2014-10-16 23:54:11 -0700167
Varun Patil3d850902020-11-23 12:19:14 +0530168 std::string m_chatroomName; // chatroom name
169 std::string m_nick; // user nick
Yingdi Yud45777b2014-10-16 23:54:11 -0700170
Varun Patil3d850902020-11-23 12:19:14 +0530171 Name m_signingId; // signing identity
172 shared_ptr<ndn::security::ValidatorConfig> m_validator; // validator
173 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
Yingdi Yud45777b2014-10-16 23:54:11 -0700174
Varun Patil3d850902020-11-23 12:19:14 +0530175 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
176 ndn::scheduler::EventId m_helloEventId; // event id of timeout
Yingdi Yud45777b2014-10-16 23:54:11 -0700177
Varun Patil3d850902020-11-23 12:19:14 +0530178 bool m_joined; // true if in a chatroom
Yingdi Yud45777b2014-10-16 23:54:11 -0700179
Varun Patil3d850902020-11-23 12:19:14 +0530180 BackendRoster m_roster; // User roster
Yingdi Yu4647f022015-02-01 00:26:38 -0800181
Yingdi Yuf3401182015-02-02 20:21:07 -0800182 std::mutex m_resumeMutex;
183 std::mutex m_nfdConnectionMutex;
Yingdi Yud45777b2014-10-16 23:54:11 -0700184};
185
Yingdi Yueb692ac2015-02-10 18:46:18 -0800186} // namespace chronochat
Yingdi Yud45777b2014-10-16 23:54:11 -0700187
188#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP