blob: adf60a8cc333e3d97f60b8b1e131b832f7204809 [file] [log] [blame]
Yingdi Yu2c9e7712014-10-20 11:55:05 -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 Yu2c9e7712014-10-20 11:55:05 -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 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"
Yingdi Yu2c9e7712014-10-20 11:55:05 -070023#include <ndn-cxx/security/key-chain.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053024#include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
Muktadir R Chowdhury5119f3d2015-11-10 15:15:40 -060025#include <ndn-cxx/security/validator-null.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053026#include <ndn-cxx/face.hpp>
Yingdi Yuf3401182015-02-02 20:21:07 -080027#include <boost/thread.hpp>
28#include <mutex>
Yingdi Yu2c9e7712014-10-20 11:55:05 -070029#endif
30
Yingdi Yueb692ac2015-02-10 18:46:18 -080031namespace chronochat {
Yingdi Yu2c9e7712014-10-20 11:55:05 -070032
33class ControllerBackend : public QThread
34{
35 Q_OBJECT
36
37public:
Yingdi Yuf3401182015-02-02 20:21:07 -080038 ControllerBackend(QObject* parent = nullptr);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070039
40 ~ControllerBackend();
41
42 ContactManager*
43 getContactManager()
44 {
45 return &m_contactManager;
46 }
47
48protected:
49 void
50 run();
51
52private:
53 void
54 setInvitationListener();
55
56 ndn::Name
57 getInvitationRoutingPrefix();
58
59 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070060 onInvitationInterest(const ndn::Name& prefix, const ndn::Interest& interest,
61 size_t routingPrefixOffset);
62
63 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080064 onInvitationRequestInterest(const ndn::Name& prefix, const ndn::Interest& interest,
65 size_t routingPrefixOffset);
66
67 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070068 onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo);
69
70 void
Varun Patil3d850902020-11-23 12:19:14 +053071 onInvitationValidated(const Interest& interest);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070072
73 void
Varun Patil3d850902020-11-23 12:19:14 +053074 onInvitationValidationFailed(const Interest& interest,
75 const ndn::security::ValidationError& failureInfo);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070076
77 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080078 onLocalPrefix(const ndn::ConstBufferPtr& data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070079
80 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080081 onLocalPrefixError(uint32_t code, const std::string& msg);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070082
83 void
84 updateLocalPrefix(const Name& localPrefix);
85
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080086 void
Varun Patil3d850902020-11-23 12:19:14 +053087 onRequestResponse(const Interest& interest, const Data& data);
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080088
89 void
90 onRequestTimeout(const Interest& interest, int& resendTimes);
91
Yingdi Yu2c9e7712014-10-20 11:55:05 -070092signals:
93 void
94 identityUpdated(const QString& identity);
95
96 void
97 localPrefixUpdated(const QString& localPrefix);
98
99 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800100 invitationValidated(QString alias, QString chatroom, ndn::Name invitationINterest);
101
102 void
103 invitationRequestReceived(QString alias, QString chatroom, ndn::Name invitationRequestInterest);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700104
105 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800106 startChatroomOnInvitation(chronochat::Invitation invitation, bool secured);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700107
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800108 void
109 startChatroom(const QString& chatroomName, bool secured);
110
111 void
112 invitationRequestResult(const std::string& msg);
113
Yingdi Yuf3401182015-02-02 20:21:07 -0800114 void
115 nfdError();
116
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700117public slots:
118 void
119 shutdown();
120
121 void
122 addChatroom(QString chatroom);
123
124 void
125 removeChatroom(QString chatroom);
126
127 void
128 onUpdateLocalPrefixAction();
129
130 void
131 onIdentityChanged(const QString& identity);
132
133 void
134 onInvitationResponded(const ndn::Name& invitationName, bool accepted);
135
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800136 void
137 onInvitationRequestResponded(const ndn::Name& invitationName, bool accepted);
138
139 void
140 onSendInvitationRequest(const QString& chatroomName, const QString& prefix);
141
Yingdi Yuf3401182015-02-02 20:21:07 -0800142 void
143 onNfdReconnect();
144
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700145private slots:
146 void
147 onContactIdListReady(const QStringList& list);
148
149private:
Yingdi Yuf3401182015-02-02 20:21:07 -0800150 bool m_isNfdConnected;
151 bool m_shouldResume;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700152
153 Name m_identity; //TODO: set/get
154
155 Name m_localPrefix;
156
Varun Patil074e38d2021-01-05 17:38:53 +0530157 ndn::KeyChain m_keyChain;
158 ndn::Face m_face;
159
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700160 // Contact Manager
161 ContactManager m_contactManager;
162
Varun Patil3d850902020-11-23 12:19:14 +0530163 shared_ptr<ndn::security::Validator> m_validator;
164 ndn::security::ValidatorNull m_nullValidator;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700165
166 // RegisteredPrefixId
Varun Patila24bd3e2020-11-24 10:08:33 +0530167 ndn::ScopedRegisteredPrefixHandle m_invitationListenerHandle;
168 ndn::ScopedRegisteredPrefixHandle m_requestListenerHandle;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700169
170 // ChatRoomList
171 QStringList m_chatDialogList;
172
173 QMutex m_mutex;
Yingdi Yuf3401182015-02-02 20:21:07 -0800174 std::mutex m_resumeMutex;
175 std::mutex m_nfdConnectionMutex;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800176
Varun Patil3d850902020-11-23 12:19:14 +0530177 ndn::InMemoryStoragePersistent m_ims;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700178};
179
Yingdi Yueb692ac2015-02-10 18:46:18 -0800180} // namespace chronochat
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700181
182#endif // CHRONOCHAT_CONTROLLER_BACKEND_HPP