blob: f0fead37983275db08fbf1d43a3154ff7236c603 [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
Yingdi Yud45777b2014-10-16 23:54:11 -0700114 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
115
116 void
Qiuhan Ding7a4e7ef2015-02-03 20:25:50 -0800117 messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp,
118 bool addSession);
Yingdi Yud45777b2014-10-16 23:54:11 -0700119
120 void
121 chatPrefixChanged(ndn::Name newChatPrefix);
122
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800123 void
124 eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
125
126 void
127 addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
128
Yingdi Yud45777b2014-10-16 23:54:11 -0700129public slots:
130 void
131 sendChatMessage(QString text, time_t timestamp);
132
133 void
134 updateRoutingPrefix(const QString& localRoutingPrefix);
135
136 void
137 shutdown();
138
139private:
140 typedef std::map<ndn::Name, UserInfo> BackendRoster;
141
Yingdi Yu45da92a2015-02-02 13:17:03 -0800142 shared_ptr<ndn::Face> m_face;
Yingdi Yud45777b2014-10-16 23:54:11 -0700143
144 Name m_localRoutingPrefix; // routable local prefix
145 Name m_chatroomPrefix; // chatroom sync prefix
146 Name m_userChatPrefix; // user chat prefix
147 Name m_routableUserChatPrefix; // routable user chat prefix
148
149 std::string m_chatroomName; // chatroom name
150 std::string m_nick; // user nick
151
Yingdi Yu45da92a2015-02-02 13:17:03 -0800152 Name m_signingId; // signing identity
153 shared_ptr<ndn::Validator> m_validator;// validator
Yingdi Yud45777b2014-10-16 23:54:11 -0700154 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
155
Yingdi Yu4647f022015-02-01 00:26:38 -0800156 unique_ptr<ndn::Scheduler> m_scheduler;// scheduler
Yingdi Yud45777b2014-10-16 23:54:11 -0700157 ndn::EventId m_helloEventId; // event id of timeout
158
Yingdi Yud45777b2014-10-16 23:54:11 -0700159 bool m_joined; // true if in a chatroom
160
161 BackendRoster m_roster; // User roster
Yingdi Yu4647f022015-02-01 00:26:38 -0800162
Qiuhan Dingf22c41b2015-03-11 13:19:01 -0700163 std::mutex m_mutex;
Yingdi Yu4647f022015-02-01 00:26:38 -0800164 bool m_shouldResume;
Yingdi Yud45777b2014-10-16 23:54:11 -0700165};
166
Yingdi Yueb692ac2015-02-10 18:46:18 -0800167} // namespace chronochat
Yingdi Yud45777b2014-10-16 23:54:11 -0700168
169#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP