blob: eda4094484b7c47b9accfa2d3a8e8ee7e152a0a8 [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>
14#include <QMutex>
15
16#ifndef Q_MOC_RUN
17#include "common.hpp"
18#include "chatroom-info.hpp"
19#include "chatbuf.pb.h"
20#include <socket.hpp>
21#endif
22
23namespace chronos {
24
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,
49 QObject* parent = 0);
50
51 ~ChatDialogBackend();
52
53protected:
54 void
55 run();
56
57private:
58 void
59 initializeSync();
60
61 void
62 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
63
64 void
65 processChatData(const ndn::shared_ptr<const ndn::Data>& data, bool needDisplay);
66
67 void
68 remoteSessionTimeout(const Name& sessionPrefix);
69
70 void
71 sendMsg(SyncDemo::ChatMessage& msg);
72
73 void
74 sendJoin();
75
76 void
77 sendHello();
78
79 void
80 sendLeave();
81
82 void
83 prepareControlMessage(SyncDemo::ChatMessage& msg,
84 SyncDemo::ChatMessage::ChatMessageType type);
85
86 void
87 prepareChatMessage(const QString& text,
88 time_t timestamp,
89 SyncDemo::ChatMessage &msg);
90
91 void
92 updatePrefixes();
93
94 std::string
95 getHexEncodedDigest(ndn::ConstBufferPtr digest);
96
97signals:
98 void
99 syncTreeUpdated(std::vector<chronos::NodeInfo> updates, QString digest);
100
101 void
102 chatMessageReceived(QString nick, QString text, time_t timestamp);
103
104 void
105 sessionAdded(QString sessionPrefix, QString nick, time_t timestamp);
106
107 void
108 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
109
110 void
111 nickUpdated(QString sessionPrefix, QString nick);
112
113 void
114 messageReceived(QString sessionPrefix);
115
116 void
117 chatPrefixChanged(ndn::Name newChatPrefix);
118
119public slots:
120 void
121 sendChatMessage(QString text, time_t timestamp);
122
123 void
124 updateRoutingPrefix(const QString& localRoutingPrefix);
125
126 void
127 shutdown();
128
129private:
130 typedef std::map<ndn::Name, UserInfo> BackendRoster;
131
132 ndn::Face m_face;
133
134 Name m_localRoutingPrefix; // routable local prefix
135 Name m_chatroomPrefix; // chatroom sync prefix
136 Name m_userChatPrefix; // user chat prefix
137 Name m_routableUserChatPrefix; // routable user chat prefix
138
139 std::string m_chatroomName; // chatroom name
140 std::string m_nick; // user nick
141
142 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
143
144 ndn::Scheduler m_scheduler; // scheduler
145 ndn::EventId m_helloEventId; // event id of timeout
146
147 QMutex mutex; // mutex used for prefix updates
148
149 bool m_joined; // true if in a chatroom
150
151 BackendRoster m_roster; // User roster
152};
153
154} // namespace chronos
155
156#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP