From 0cad7d0312fbb714182d9e15ab7dcee9d9622b63 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Mon, 6 Jan 2014 23:20:02 +0000 Subject: [PATCH] git-svn-id: svn://imanolbarba.net/PAD@45 c2ee353e-ed0d-4329-bf56-03aec153487f --- JChatServerV2/bin/pad/prac2/JChat$1.class | Bin 774 -> 0 bytes JChatServerV2/bin/pad/prac2/JChat.class | Bin 1315 -> 0 bytes JChatServerV2/src/pad/prac2/JChat.java | 9 +++++++-- QChatClient/chatroom.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- QChatClient/chatroom.h | 2 ++ build-QChatClient-Desktop-Debug/QChatClient | Bin 1887357 -> 0 bytes build-QChatClient-Desktop-Debug/main.o | Bin 239728 -> 0 bytes build-QChatClient-Desktop-Debug/moc_chatroom.o | Bin 438168 -> 0 bytes 8 files changed, 45 insertions(+), 11 deletions(-) diff --git a/JChatServerV2/bin/pad/prac2/JChat$1.class b/JChatServerV2/bin/pad/prac2/JChat$1.class index e8fef6f..d203d50 100644 Binary files a/JChatServerV2/bin/pad/prac2/JChat$1.class and b/JChatServerV2/bin/pad/prac2/JChat$1.class differ diff --git a/JChatServerV2/bin/pad/prac2/JChat.class b/JChatServerV2/bin/pad/prac2/JChat.class index 5c8923a..3f22a28 100644 Binary files a/JChatServerV2/bin/pad/prac2/JChat.class and b/JChatServerV2/bin/pad/prac2/JChat.class differ diff --git a/JChatServerV2/src/pad/prac2/JChat.java b/JChatServerV2/src/pad/prac2/JChat.java index a906273..02d7663 100644 --- a/JChatServerV2/src/pad/prac2/JChat.java +++ b/JChatServerV2/src/pad/prac2/JChat.java @@ -8,14 +8,19 @@ public class JChat { String ip; int port, roomSize; - Scanner in = new Scanner(System.in); + /*Scanner in = new Scanner(System.in); System.out.print("IP: "); ip = in.nextLine(); System.out.print("Port: "); port = in.nextInt(); System.out.print("Size of chatroom: "); roomSize = in.nextInt(); - in.close(); + in.close();*/ + + ip = "localhost"; + port = 3001; + roomSize = 5; + final Server serv = new Server(ip,port,roomSize); Runtime.getRuntime().addShutdownHook(new Thread() { diff --git a/QChatClient/chatroom.cpp b/QChatClient/chatroom.cpp index 85e802c..be4b8c5 100644 --- a/QChatClient/chatroom.cpp +++ b/QChatClient/chatroom.cpp @@ -241,6 +241,7 @@ void Chatroom::relayMsg(string msg) void Chatroom::newChat(QString peerNick) { + freeChats(false); ChatWindow* newchat = new ChatWindow(peerNick,this); activeChats.insert(std::pair(peerNick.toStdString(),newchat)); newchat->show(); @@ -249,8 +250,8 @@ void Chatroom::newChat(QString peerNick) void Chatroom::removeChat(QString &nickname) { - ChatWindow* chat = (ChatWindow*)activeChats.find(nickname.toStdString())->second; - delete chat; + ChatWindow* chat = (ChatWindow*) activeChats.find(nickname.toStdString())->second; + inactiveChats.insert(std::pair(nickname.toStdString(),chat)); activeChats.erase(nickname.toStdString()); } @@ -259,7 +260,7 @@ void Chatroom::launchChatWindow(QModelIndex index) QString peerNick = ui->userList->model()->data(index).toString(); if(activeChats.find(peerNick.toStdString()) == activeChats.end()) { - emit newChat(peerNick); + emit createChat(peerNick); } } @@ -385,18 +386,44 @@ void Chatroom::startSession() send = new std::thread(sendThread,&s,this); } +void Chatroom::freeChats(bool active) +{ + int count = 0; + if(active) + { + cout << "FREE ACTIVE" << endl; + map::iterator it; + for(it = activeChats.begin(); it != activeChats.end(); it++) + { + count++; + ChatWindow* chat = (ChatWindow*) it->second; + activeChats.erase(it); + delete chat; + } + } + else + { + cout << "FREE INACTIVE" << endl; + map::iterator it; + for(it = inactiveChats.begin(); it != inactiveChats.end(); it++) + { + count++; + ChatWindow* chat = (ChatWindow*) it->second; + inactiveChats.erase(it); + delete chat; + } + } + cout << "DELETED " << count << endl << "REMAINING" << endl << activeChats.size() << " ACTIVE" << endl << inactiveChats.size() << " INACTIVE" << endl; +} + Chatroom::~Chatroom() { delete msgLock; delete chatLock; delete recv; delete send; - map::iterator it; - for(it = activeChats.begin(); it != activeChats.end(); it++) - { - ChatWindow* chat = (ChatWindow*) it->second; - delete chat; - } + freeChats(false); + freeChats(true); delete ui; } diff --git a/QChatClient/chatroom.h b/QChatClient/chatroom.h index 61c06cf..3701cdd 100644 --- a/QChatClient/chatroom.h +++ b/QChatClient/chatroom.h @@ -46,10 +46,12 @@ private: list printServerQueue; list printStatusQueue; map activeChats; + map inactiveChats; std::mutex chatMutex; unique_lock* chatLock; std::condition_variable chatCreated; string getSender(string msg); + void freeChats(bool active); protected: void closeEvent(QCloseEvent *event); diff --git a/build-QChatClient-Desktop-Debug/QChatClient b/build-QChatClient-Desktop-Debug/QChatClient index 34c8c25..b368172 100755 Binary files a/build-QChatClient-Desktop-Debug/QChatClient and b/build-QChatClient-Desktop-Debug/QChatClient differ diff --git a/build-QChatClient-Desktop-Debug/main.o b/build-QChatClient-Desktop-Debug/main.o index e628880..2769b4a 100644 Binary files a/build-QChatClient-Desktop-Debug/main.o and b/build-QChatClient-Desktop-Debug/main.o differ diff --git a/build-QChatClient-Desktop-Debug/moc_chatroom.o b/build-QChatClient-Desktop-Debug/moc_chatroom.o index ea9df57..c574c1a 100644 Binary files a/build-QChatClient-Desktop-Debug/moc_chatroom.o and b/build-QChatClient-Desktop-Debug/moc_chatroom.o differ -- libgit2 0.22.2