blob: 78865efcef4f118c6c6efda5c54a5eec22006985 [file] [log] [blame]
Yingdi Yu2c9e7712014-10-20 11:55:05 -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 Yu2c9e7712014-10-20 11:55:05 -07009 */
10
11#ifndef CHRONOCHAT_CONTROLLER_BACKEND_HPP
12#define CHRONOCHAT_CONTROLLER_BACKEND_HPP
13
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080014#include <QString>
Yingdi Yu2c9e7712014-10-20 11:55:05 -070015#include <QThread>
16#include <QStringList>
17#include <QMutex>
18
19#ifndef Q_MOC_RUN
20#include "common.hpp"
21#include "contact-manager.hpp"
22#include "invitation.hpp"
23#include "validator-invitation.hpp"
24#include <ndn-cxx/security/key-chain.hpp>
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080025#include <ndn-cxx/util/in-memory-storage-persistent.hpp>
Yingdi Yuf3401182015-02-02 20:21:07 -080026#include <boost/thread.hpp>
27#include <mutex>
Yingdi Yu2c9e7712014-10-20 11:55:05 -070028#endif
29
Yingdi Yueb692ac2015-02-10 18:46:18 -080030namespace chronochat {
Yingdi Yu2c9e7712014-10-20 11:55:05 -070031
32class ControllerBackend : public QThread
33{
34 Q_OBJECT
35
36public:
Yingdi Yuf3401182015-02-02 20:21:07 -080037 ControllerBackend(QObject* parent = nullptr);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070038
39 ~ControllerBackend();
40
41 ContactManager*
42 getContactManager()
43 {
44 return &m_contactManager;
45 }
46
47protected:
48 void
49 run();
50
51private:
52 void
53 setInvitationListener();
54
55 ndn::Name
56 getInvitationRoutingPrefix();
57
58 void
59 onInvitationPrefixReset();
60
61 void
62 onInvitationPrefixResetFailed(const std::string& failInfo);
63
64 void
65 onInvitationInterest(const ndn::Name& prefix, const ndn::Interest& interest,
66 size_t routingPrefixOffset);
67
68 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080069 onInvitationRequestInterest(const ndn::Name& prefix, const ndn::Interest& interest,
70 size_t routingPrefixOffset);
71
72 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070073 onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo);
74
75 void
76 onInvitationValidated(const shared_ptr<const Interest>& interest);
77
78 void
79 onInvitationValidationFailed(const shared_ptr<const Interest>& interest,
80 std::string failureInfo);
81
82 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080083 onLocalPrefix(const ndn::ConstBufferPtr& data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070084
85 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080086 onLocalPrefixError(uint32_t code, const std::string& msg);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070087
88 void
89 updateLocalPrefix(const Name& localPrefix);
90
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080091 void
92 onRequestResponse(const Interest& interest, Data& data);
93
94 void
95 onRequestTimeout(const Interest& interest, int& resendTimes);
96
Yingdi Yu2c9e7712014-10-20 11:55:05 -070097signals:
98 void
99 identityUpdated(const QString& identity);
100
101 void
102 localPrefixUpdated(const QString& localPrefix);
103
104 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800105 invitationValidated(QString alias, QString chatroom, ndn::Name invitationINterest);
106
107 void
108 invitationRequestReceived(QString alias, QString chatroom, ndn::Name invitationRequestInterest);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700109
110 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800111 startChatroomOnInvitation(chronochat::Invitation invitation, bool secured);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700112
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800113 void
114 startChatroom(const QString& chatroomName, bool secured);
115
116 void
117 invitationRequestResult(const std::string& msg);
118
Yingdi Yuf3401182015-02-02 20:21:07 -0800119 void
120 nfdError();
121
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700122public slots:
123 void
124 shutdown();
125
126 void
127 addChatroom(QString chatroom);
128
129 void
130 removeChatroom(QString chatroom);
131
132 void
133 onUpdateLocalPrefixAction();
134
135 void
136 onIdentityChanged(const QString& identity);
137
138 void
139 onInvitationResponded(const ndn::Name& invitationName, bool accepted);
140
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800141 void
142 onInvitationRequestResponded(const ndn::Name& invitationName, bool accepted);
143
144 void
145 onSendInvitationRequest(const QString& chatroomName, const QString& prefix);
146
Yingdi Yuf3401182015-02-02 20:21:07 -0800147 void
148 onNfdReconnect();
149
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700150private slots:
151 void
152 onContactIdListReady(const QStringList& list);
153
154private:
Yingdi Yuf3401182015-02-02 20:21:07 -0800155 bool m_isNfdConnected;
156 bool m_shouldResume;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700157 ndn::Face m_face;
158
159 Name m_identity; //TODO: set/get
160
161 Name m_localPrefix;
162
163 // Contact Manager
164 ContactManager m_contactManager;
165
166 // Security related;
167 ndn::KeyChain m_keyChain;
168 ValidatorInvitation m_validator;
169
170 // RegisteredPrefixId
171 const ndn::RegisteredPrefixId* m_invitationListenerId;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800172 const ndn::RegisteredPrefixId* m_requestListenerId;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700173
174 // ChatRoomList
175 QStringList m_chatDialogList;
176
177 QMutex m_mutex;
Yingdi Yuf3401182015-02-02 20:21:07 -0800178 std::mutex m_resumeMutex;
179 std::mutex m_nfdConnectionMutex;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800180
181 ndn::util::InMemoryStoragePersistent m_ims;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700182};
183
Yingdi Yueb692ac2015-02-10 18:46:18 -0800184} // namespace chronochat
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700185
186#endif // CHRONOCHAT_CONTROLLER_BACKEND_HPP