|
1
2
3
|
#include "chatroom.h"
#include "ui_chatroom.h"
|
|
4
5
6
7
8
9
10
11
12
|
list<QString> sendQueue;
std::mutex myMutex;
std::mutex msgMutex;
unique_lock<std::mutex>* msgLock;
std::condition_variable msgListNotEmpty;
|
|
13
14
15
16
17
|
Chatroom::Chatroom(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Chatroom)
{
ui->setupUi(this);
|
|
18
19
20
21
22
23
24
25
26
|
QList<int> chatSizes;
QList<int> splitSizes;
chatSizes.push_front(ui->chatText->height());
chatSizes.push_front(100);
splitSizes.push_front(ui->chatWindow->width());
splitSizes.push_front(200);
ui->chatSplitter->setSizes(chatSizes);
ui->windowSplitter->setSizes(splitSizes);
connected = false;
|
|
27
28
29
|
msgLock = new unique_lock<std::mutex>(msgMutex,std::defer_lock);
send = NULL;
recv = NULL;
|
|
30
|
|
|
31
32
33
34
35
36
37
|
connect(ui->actionConnect,SIGNAL(triggered()),this,SLOT(startSession()));
connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(finish()));
connect(this,SIGNAL(threadsFinished(bool)),this,SLOT(finishThreads(bool)));
connect(ui->inputText,SIGNAL(msgReady()),this,SLOT(sendMsg()));
connect(ui->actionDisconnect,SIGNAL(triggered()),this,SLOT(disconnectChatroom()));
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMsg()));
connect(this,SIGNAL(messagesToPrint()),this,SLOT(printMsg()));
|
|
38
39
|
}
|
|
40
|
void sendThread(Socket* s, Chatroom* chat)
|
|
41
42
|
{
string send;
|
|
43
|
while(chat->getConnected())
|
|
44
|
{
|
|
45
|
while(sendQueue.empty())
|
|
46
|
{
|
|
47
48
49
50
51
52
53
|
msgLock->lock();
msgListNotEmpty.wait(*msgLock);
if(!chat->getConnected())
{
return;
}
msgLock->unlock();
|
|
54
|
}
|
|
55
56
57
|
msgLock->lock();
send = sendQueue.front().toStdString();
sendQueue.pop_front();
|
|
58
59
|
try
{
|
|
60
61
62
63
64
65
66
67
68
69
70
|
if(send[0] == '@')
{
send = '1' + send;
}
else
{
send = '0' + send;
}
*s << send;
msgLock->unlock();
send = send.substr(1);
|
|
71
72
73
74
|
if(send == "/disconnect" || send == "/exit")
{
break;
}
|
|
75
|
chat->putMsgToPrintQueue(chat->getNickname().toStdString().append(": ").append(send));
|
|
76
77
78
79
80
81
|
}
catch(SocketException& e)
{
cout << e.description() << endl;
}
}
|
|
82
|
cout << "sendThread finished" << endl;
|
|
83
84
|
}
|
|
85
|
void recvThread(Socket* s, Chatroom* chat)
|
|
86
87
88
89
90
91
|
{
string recv;
while(true)
{
try
{
|
|
92
93
94
95
96
97
98
99
100
101
102
|
*s >> recv;
if(recv[0] == '1')
{
recv = recv.substr(1);
chat->relayMsg(recv);
continue;
}
else if(recv[0] == '0')
{
recv = recv.substr(1);
}
|
|
103
104
105
|
}
catch(SocketException &e)
{
|
|
106
|
chat->setConnected(false);
|
|
107
|
cout << e.description() << endl;
|
|
108
109
|
msgListNotEmpty.notify_all();
emit chat->threadsFinished(false);
|
|
110
111
112
113
114
|
break;
}
if(recv == "DISC_OK")
{
cout << "Disconnecting" << endl;
|
|
115
116
117
|
chat->setConnected(false);
msgListNotEmpty.notify_all();
emit chat->threadsFinished(false);
|
|
118
119
120
121
122
|
break;
}
else if(recv == "EXIT_OK")
{
cout << "Exiting" << endl;
|
|
123
124
125
|
chat->setConnected(false);
msgListNotEmpty.notify_all();
emit chat->threadsFinished(true);
|
|
126
127
128
129
|
break;
}
else
{
|
|
130
|
chat->putMsgToPrintQueue(recv);
|
|
131
132
|
}
}
|
|
133
|
cout << "recvThread finished" << endl;
|
|
134
135
|
}
|
|
136
|
bool Chatroom::getConnected()
|
|
137
|
{
|
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
myMutex.lock();
bool val = connected;
myMutex.unlock();
return val;
}
void Chatroom::setConnected(bool status)
{
myMutex.lock();
connected = status;
myMutex.unlock();
}
void Chatroom::putMsgToPrintQueue(string &msg)
{
printQueue.push_back(msg);
emit messagesToPrint();
}
string Chatroom::getSender(string& msg)
{
return msg.substr(5,msg.find(':')-5);
}
void Chatroom::relayMsg(string& msg)
{
string sender = getSender(msg);
map<string,ChatWindow>::iterator it = activeChats.begin();
if(activeChats.find(sender) == activeChats.end())
|
|
167
|
{
|
|
168
|
//LAUNCH new windowchat
|
|
169
|
}
|
|
170
171
172
|
it = activeChats.find(sender);
it->second.printMsg(msg);
}
|
|
173
|
|
|
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
void Chatroom::printMsg()
{
while(!printQueue.empty())
{
string msg = printQueue.front();
ui->chatText->printMsg(msg);
printQueue.pop_front();
}
}
void Chatroom::putMsgToSendQueue(QString& msg)
{
msgMutex.lock();
sendQueue.push_back(msg);
msgMutex.unlock();
msgListNotEmpty.notify_all();
}
void Chatroom::setNickname(QString nick)
{
nickname = nick;
}
QString Chatroom::getNickname()
{
return nickname;
}
void Chatroom::startSession()
{
LoginScreen login(&s,this);
login.exec();
int result = login.result();
if(result == QDialog::Accepted)
{
ui->inputText->setReadOnly(false);
}
else if(result == QDialog::Rejected)
{
return;
}
connected = true;
ui->chatText->printServerMsg("Connected to chatroom");
recv = new std::thread(recvThread,&s,this);
send = new std::thread(sendThread,&s,this);
}
void Chatroom::sendMsg()
{
QString msg = ui->inputText->toPlainText();
ui->inputText->clear();
putMsgToSendQueue(msg);
}
void Chatroom::finish()
{
if(recv == NULL && send == NULL)
{
ui->inputText->setReadOnly(true);
ui->chatText->printServerMsg("Disconnected");
this->close();
}
msgMutex.lock();
sendQueue.clear();
sendQueue.push_back("/exit");
msgMutex.unlock();
msgListNotEmpty.notify_all();
}
void Chatroom::disconnectChatroom()
{
msgMutex.lock();
sendQueue.clear();
sendQueue.push_back("/disconnect");
msgMutex.unlock();
msgListNotEmpty.notify_all();
}
void Chatroom::finishThreads(bool exit)
{
myMutex.lock();
ui->inputText->setReadOnly(true);
recv->join();
send->join();
myMutex.unlock();
ui->chatText->printServerMsg("Disconnected");
send = NULL;
recv = NULL;
if(exit)
{
this->close();
}
|
|
266
267
268
269
|
}
Chatroom::~Chatroom()
{
|
|
270
271
272
|
delete msgLock;
delete recv;
delete send;
|
|
273
274
|
delete ui;
}
|
|
275
276
|
|