blob: ae7b3da15057ab517eec5ac617ef26a2ad626b24 [file] [log] [blame]
Yingdi Yud45777b2014-10-16 23:54:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 *
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>
Yingdi Yud45777b2014-10-16 23:54:11 -070021#include <socket.hpp>
Yingdi Yuf3401182015-02-02 20:21:07 -080022#include <boost/thread.hpp>
Yingdi Yud45777b2014-10-16 23:54:11 -070023#endif
24
Yingdi Yueb692ac2015-02-10 18:46:18 -080025namespace chronochat {
Yingdi Yud45777b2014-10-16 23:54:11 -070026
27class NodeInfo {
28public:
29 QString sessionPrefix;
30 chronosync::SeqNo seqNo;
31};
32
33class UserInfo {
34public:
35 ndn::Name sessionPrefix;
36 bool hasNick;
37 std::string userNick;
38 ndn::EventId timeoutEventId;
39};
40
41class ChatDialogBackend : public QThread
42{
43 Q_OBJECT
44
45public:
46 ChatDialogBackend(const Name& chatroomPrefix,
47 const Name& userChatPrefix,
48 const Name& routingPrefix,
49 const std::string& chatroomName,
50 const std::string& nick,
Yingdi Yu45da92a2015-02-02 13:17:03 -080051 const Name& signingId = Name(),
Yingdi Yuf3401182015-02-02 20:21:07 -080052 QObject* parent = nullptr);
Yingdi Yud45777b2014-10-16 23:54:11 -070053
54 ~ChatDialogBackend();
55
56protected:
57 void
58 run();
59
60private:
61 void
62 initializeSync();
63
Yingdi Yu45da92a2015-02-02 13:17:03 -080064 shared_ptr<ndn::IdentityCertificate>
65 loadTrustAnchor();
66
Yingdi Yud45777b2014-10-16 23:54:11 -070067 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
Yingdi Yu45da92a2015-02-02 13:17:03 -080077 processChatData(const ndn::shared_ptr<const ndn::Data>& data,
78 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,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800103 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
163 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
167
168 std::string m_chatroomName; // chatroom name
169 std::string m_nick; // user nick
170
Yingdi Yu45da92a2015-02-02 13:17:03 -0800171 Name m_signingId; // signing identity
172 shared_ptr<ndn::Validator> m_validator;// validator
Yingdi Yud45777b2014-10-16 23:54:11 -0700173 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
174
Yingdi Yu4647f022015-02-01 00:26:38 -0800175 unique_ptr<ndn::Scheduler> m_scheduler;// scheduler
Yingdi Yud45777b2014-10-16 23:54:11 -0700176 ndn::EventId m_helloEventId; // event id of timeout
177
Yingdi Yud45777b2014-10-16 23:54:11 -0700178 bool m_joined; // true if in a chatroom
179
180 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