blob: 26f588b7f02fae325b96ee8e98efc7fd9949b6c5 [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>
8 */
9
10#ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
11#define CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
12
13#include <QThread>
Yingdi Yud45777b2014-10-16 23:54:11 -070014
15#ifndef Q_MOC_RUN
16#include "common.hpp"
17#include "chatroom-info.hpp"
18#include "chatbuf.pb.h"
Qiuhan Dingf22c41b2015-03-11 13:19:01 -070019#include <mutex>
Yingdi Yud45777b2014-10-16 23:54:11 -070020#include <socket.hpp>
21#endif
22
Yingdi Yueb692ac2015-02-10 18:46:18 -080023namespace chronochat {
Yingdi Yud45777b2014-10-16 23:54:11 -070024
25class NodeInfo {
26public:
27 QString sessionPrefix;
28 chronosync::SeqNo seqNo;
29};
30
31class UserInfo {
32public:
33 ndn::Name sessionPrefix;
34 bool hasNick;
35 std::string userNick;
36 ndn::EventId timeoutEventId;
37};
38
39class ChatDialogBackend : public QThread
40{
41 Q_OBJECT
42
43public:
44 ChatDialogBackend(const Name& chatroomPrefix,
45 const Name& userChatPrefix,
46 const Name& routingPrefix,
47 const std::string& chatroomName,
48 const std::string& nick,
Yingdi Yu45da92a2015-02-02 13:17:03 -080049 const Name& signingId = Name(),
Yingdi Yud45777b2014-10-16 23:54:11 -070050 QObject* parent = 0);
51
52 ~ChatDialogBackend();
53
54protected:
55 void
56 run();
57
58private:
59 void
60 initializeSync();
61
Yingdi Yu45da92a2015-02-02 13:17:03 -080062 shared_ptr<ndn::IdentityCertificate>
63 loadTrustAnchor();
64
Yingdi Yud45777b2014-10-16 23:54:11 -070065 void
Yingdi Yu4647f022015-02-01 00:26:38 -080066 close();
67
68 void
Yingdi Yud45777b2014-10-16 23:54:11 -070069 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
70
71 void
Yingdi Yu45da92a2015-02-02 13:17:03 -080072 processChatData(const ndn::shared_ptr<const ndn::Data>& data,
73 bool needDisplay,
74 bool isValidated);
Yingdi Yud45777b2014-10-16 23:54:11 -070075
76 void
77 remoteSessionTimeout(const Name& sessionPrefix);
78
79 void
80 sendMsg(SyncDemo::ChatMessage& msg);
81
82 void
83 sendJoin();
84
85 void
86 sendHello();
87
88 void
89 sendLeave();
90
91 void
92 prepareControlMessage(SyncDemo::ChatMessage& msg,
93 SyncDemo::ChatMessage::ChatMessageType type);
94
95 void
96 prepareChatMessage(const QString& text,
97 time_t timestamp,
98 SyncDemo::ChatMessage &msg);
99
100 void
101 updatePrefixes();
102
103 std::string
104 getHexEncodedDigest(ndn::ConstBufferPtr digest);
105
106signals:
107 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800108 syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest);
Yingdi Yud45777b2014-10-16 23:54:11 -0700109
110 void
111 chatMessageReceived(QString nick, QString text, time_t timestamp);
112
113 void
114 sessionAdded(QString sessionPrefix, QString nick, time_t timestamp);
115
116 void
117 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
118
119 void
120 nickUpdated(QString sessionPrefix, QString nick);
121
122 void
123 messageReceived(QString sessionPrefix);
124
125 void
126 chatPrefixChanged(ndn::Name newChatPrefix);
127
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800128 void
129 eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
130
131 void
132 addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
133
Yingdi Yud45777b2014-10-16 23:54:11 -0700134public slots:
135 void
136 sendChatMessage(QString text, time_t timestamp);
137
138 void
139 updateRoutingPrefix(const QString& localRoutingPrefix);
140
141 void
142 shutdown();
143
144private:
145 typedef std::map<ndn::Name, UserInfo> BackendRoster;
146
Yingdi Yu45da92a2015-02-02 13:17:03 -0800147 shared_ptr<ndn::Face> m_face;
Yingdi Yud45777b2014-10-16 23:54:11 -0700148
149 Name m_localRoutingPrefix; // routable local prefix
150 Name m_chatroomPrefix; // chatroom sync prefix
151 Name m_userChatPrefix; // user chat prefix
152 Name m_routableUserChatPrefix; // routable user chat prefix
153
154 std::string m_chatroomName; // chatroom name
155 std::string m_nick; // user nick
156
Yingdi Yu45da92a2015-02-02 13:17:03 -0800157 Name m_signingId; // signing identity
158 shared_ptr<ndn::Validator> m_validator;// validator
Yingdi Yud45777b2014-10-16 23:54:11 -0700159 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
160
Yingdi Yu4647f022015-02-01 00:26:38 -0800161 unique_ptr<ndn::Scheduler> m_scheduler;// scheduler
Yingdi Yud45777b2014-10-16 23:54:11 -0700162 ndn::EventId m_helloEventId; // event id of timeout
163
Yingdi Yud45777b2014-10-16 23:54:11 -0700164 bool m_joined; // true if in a chatroom
165
166 BackendRoster m_roster; // User roster
Yingdi Yu4647f022015-02-01 00:26:38 -0800167
Qiuhan Dingf22c41b2015-03-11 13:19:01 -0700168 std::mutex m_mutex;
Yingdi Yu4647f022015-02-01 00:26:38 -0800169 bool m_shouldResume;
Yingdi Yud45777b2014-10-16 23:54:11 -0700170};
171
Yingdi Yueb692ac2015-02-10 18:46:18 -0800172} // namespace chronochat
Yingdi Yud45777b2014-10-16 23:54:11 -0700173
174#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP