blob: be2cf25a12166317e7ec04eed2416e6bd8eeb6bd [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>
Muktadir R Chowdhury5119f3d2015-11-10 15:15:40 -060026#include <ndn-cxx/security/validator-null.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
60 onInvitationPrefixReset();
61
62 void
63 onInvitationPrefixResetFailed(const std::string& failInfo);
64
65 void
66 onInvitationInterest(const ndn::Name& prefix, const ndn::Interest& interest,
67 size_t routingPrefixOffset);
68
69 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080070 onInvitationRequestInterest(const ndn::Name& prefix, const ndn::Interest& interest,
71 size_t routingPrefixOffset);
72
73 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070074 onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo);
75
76 void
77 onInvitationValidated(const shared_ptr<const Interest>& interest);
78
79 void
80 onInvitationValidationFailed(const shared_ptr<const Interest>& interest,
81 std::string failureInfo);
82
83 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080084 onLocalPrefix(const ndn::ConstBufferPtr& data);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070085
86 void
Yingdi Yu7ff31f02015-02-05 11:21:07 -080087 onLocalPrefixError(uint32_t code, const std::string& msg);
Yingdi Yu2c9e7712014-10-20 11:55:05 -070088
89 void
90 updateLocalPrefix(const Name& localPrefix);
91
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080092 void
93 onRequestResponse(const Interest& interest, Data& data);
94
95 void
96 onRequestTimeout(const Interest& interest, int& resendTimes);
97
Yingdi Yu2c9e7712014-10-20 11:55:05 -070098signals:
99 void
100 identityUpdated(const QString& identity);
101
102 void
103 localPrefixUpdated(const QString& localPrefix);
104
105 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800106 invitationValidated(QString alias, QString chatroom, ndn::Name invitationINterest);
107
108 void
109 invitationRequestReceived(QString alias, QString chatroom, ndn::Name invitationRequestInterest);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700110
111 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800112 startChatroomOnInvitation(chronochat::Invitation invitation, bool secured);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700113
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800114 void
115 startChatroom(const QString& chatroomName, bool secured);
116
117 void
118 invitationRequestResult(const std::string& msg);
119
Yingdi Yuf3401182015-02-02 20:21:07 -0800120 void
121 nfdError();
122
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700123public slots:
124 void
125 shutdown();
126
127 void
128 addChatroom(QString chatroom);
129
130 void
131 removeChatroom(QString chatroom);
132
133 void
134 onUpdateLocalPrefixAction();
135
136 void
137 onIdentityChanged(const QString& identity);
138
139 void
140 onInvitationResponded(const ndn::Name& invitationName, bool accepted);
141
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800142 void
143 onInvitationRequestResponded(const ndn::Name& invitationName, bool accepted);
144
145 void
146 onSendInvitationRequest(const QString& chatroomName, const QString& prefix);
147
Yingdi Yuf3401182015-02-02 20:21:07 -0800148 void
149 onNfdReconnect();
150
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700151private slots:
152 void
153 onContactIdListReady(const QStringList& list);
154
155private:
Yingdi Yuf3401182015-02-02 20:21:07 -0800156 bool m_isNfdConnected;
157 bool m_shouldResume;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700158 ndn::Face m_face;
159
160 Name m_identity; //TODO: set/get
161
162 Name m_localPrefix;
163
164 // Contact Manager
165 ContactManager m_contactManager;
166
167 // Security related;
168 ndn::KeyChain m_keyChain;
169 ValidatorInvitation m_validator;
Muktadir R Chowdhury5119f3d2015-11-10 15:15:40 -0600170 ndn::ValidatorNull m_nullValidator;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700171
172 // RegisteredPrefixId
173 const ndn::RegisteredPrefixId* m_invitationListenerId;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800174 const ndn::RegisteredPrefixId* m_requestListenerId;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700175
176 // ChatRoomList
177 QStringList m_chatDialogList;
178
179 QMutex m_mutex;
Yingdi Yuf3401182015-02-02 20:21:07 -0800180 std::mutex m_resumeMutex;
181 std::mutex m_nfdConnectionMutex;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800182
183 ndn::util::InMemoryStoragePersistent m_ims;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700184};
185
Yingdi Yueb692ac2015-02-10 18:46:18 -0800186} // namespace chronochat
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700187
188#endif // CHRONOCHAT_CONTROLLER_BACKEND_HPP