Commit 0cad7d0312fbb714182d9e15ab7dcee9d9622b63

Authored by Imanol-Mikel Barba Sabariego
1 parent b23cc15d

git-svn-id: svn://imanolbarba.net/PAD@45 c2ee353e-ed0d-4329-bf56-03aec153487f

JChatServerV2/bin/pad/prac2/JChat$1.class
No preview for this file type
JChatServerV2/bin/pad/prac2/JChat.class
No preview for this file type
JChatServerV2/src/pad/prac2/JChat.java
... ... @@ -8,14 +8,19 @@ public class JChat
8 8 {
9 9 String ip;
10 10 int port, roomSize;
11   - Scanner in = new Scanner(System.in);
  11 + /*Scanner in = new Scanner(System.in);
12 12 System.out.print("IP: ");
13 13 ip = in.nextLine();
14 14 System.out.print("Port: ");
15 15 port = in.nextInt();
16 16 System.out.print("Size of chatroom: ");
17 17 roomSize = in.nextInt();
18   - in.close();
  18 + in.close();*/
  19 +
  20 + ip = "localhost";
  21 + port = 3001;
  22 + roomSize = 5;
  23 +
19 24 final Server serv = new Server(ip,port,roomSize);
20 25 Runtime.getRuntime().addShutdownHook(new Thread()
21 26 {
... ...
QChatClient/chatroom.cpp
... ... @@ -241,6 +241,7 @@ void Chatroom::relayMsg(string msg)
241 241  
242 242 void Chatroom::newChat(QString peerNick)
243 243 {
  244 + freeChats(false);
244 245 ChatWindow* newchat = new ChatWindow(peerNick,this);
245 246 activeChats.insert(std::pair<string,ChatWindow*>(peerNick.toStdString(),newchat));
246 247 newchat->show();
... ... @@ -249,8 +250,8 @@ void Chatroom::newChat(QString peerNick)
249 250  
250 251 void Chatroom::removeChat(QString &nickname)
251 252 {
252   - ChatWindow* chat = (ChatWindow*)activeChats.find(nickname.toStdString())->second;
253   - delete chat;
  253 + ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second;
  254 + inactiveChats.insert(std::pair<string,ChatWindow*>(nickname.toStdString(),chat));
254 255 activeChats.erase(nickname.toStdString());
255 256 }
256 257  
... ... @@ -259,7 +260,7 @@ void Chatroom::launchChatWindow(QModelIndex index)
259 260 QString peerNick = ui->userList->model()->data(index).toString();
260 261 if(activeChats.find(peerNick.toStdString()) == activeChats.end())
261 262 {
262   - emit newChat(peerNick);
  263 + emit createChat(peerNick);
263 264 }
264 265 }
265 266  
... ... @@ -385,18 +386,44 @@ void Chatroom::startSession()
385 386 send = new std::thread(sendThread,&s,this);
386 387 }
387 388  
  389 +void Chatroom::freeChats(bool active)
  390 +{
  391 + int count = 0;
  392 + if(active)
  393 + {
  394 + cout << "FREE ACTIVE" << endl;
  395 + map<string,void*>::iterator it;
  396 + for(it = activeChats.begin(); it != activeChats.end(); it++)
  397 + {
  398 + count++;
  399 + ChatWindow* chat = (ChatWindow*) it->second;
  400 + activeChats.erase(it);
  401 + delete chat;
  402 + }
  403 + }
  404 + else
  405 + {
  406 + cout << "FREE INACTIVE" << endl;
  407 + map<string,void*>::iterator it;
  408 + for(it = inactiveChats.begin(); it != inactiveChats.end(); it++)
  409 + {
  410 + count++;
  411 + ChatWindow* chat = (ChatWindow*) it->second;
  412 + inactiveChats.erase(it);
  413 + delete chat;
  414 + }
  415 + }
  416 + cout << "DELETED " << count << endl << "REMAINING" << endl << activeChats.size() << " ACTIVE" << endl << inactiveChats.size() << " INACTIVE" << endl;
  417 +}
  418 +
388 419 Chatroom::~Chatroom()
389 420 {
390 421 delete msgLock;
391 422 delete chatLock;
392 423 delete recv;
393 424 delete send;
394   - map<string,void*>::iterator it;
395   - for(it = activeChats.begin(); it != activeChats.end(); it++)
396   - {
397   - ChatWindow* chat = (ChatWindow*) it->second;
398   - delete chat;
399   - }
  425 + freeChats(false);
  426 + freeChats(true);
400 427 delete ui;
401 428 }
402 429  
... ...
QChatClient/chatroom.h
... ... @@ -46,10 +46,12 @@ private:
46 46 list<string> printServerQueue;
47 47 list<string> printStatusQueue;
48 48 map<string,void*> activeChats;
  49 + map<string,void*> inactiveChats;
49 50 std::mutex chatMutex;
50 51 unique_lock<std::mutex>* chatLock;
51 52 std::condition_variable chatCreated;
52 53 string getSender(string msg);
  54 + void freeChats(bool active);
53 55  
54 56 protected:
55 57 void closeEvent(QCloseEvent *event);
... ...
build-QChatClient-Desktop-Debug/QChatClient
No preview for this file type
build-QChatClient-Desktop-Debug/main.o
No preview for this file type
build-QChatClient-Desktop-Debug/moc_chatroom.o
No preview for this file type