diff --git a/JChatServerV2/bin/pad/prac2/JChat$1.class b/JChatServerV2/bin/pad/prac2/JChat$1.class index e8fef6f..d203d50 100644 --- a/JChatServerV2/bin/pad/prac2/JChat$1.class +++ b/JChatServerV2/bin/pad/prac2/JChat$1.class diff --git a/JChatServerV2/bin/pad/prac2/JChat.class b/JChatServerV2/bin/pad/prac2/JChat.class index 5c8923a..3f22a28 100644 --- a/JChatServerV2/bin/pad/prac2/JChat.class +++ b/JChatServerV2/bin/pad/prac2/JChat.class 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 --- a/build-QChatClient-Desktop-Debug/QChatClient +++ b/build-QChatClient-Desktop-Debug/QChatClient diff --git a/build-QChatClient-Desktop-Debug/main.o b/build-QChatClient-Desktop-Debug/main.o index e628880..2769b4a 100644 --- a/build-QChatClient-Desktop-Debug/main.o +++ b/build-QChatClient-Desktop-Debug/main.o diff --git a/build-QChatClient-Desktop-Debug/moc_chatroom.o b/build-QChatClient-Desktop-Debug/moc_chatroom.o index ea9df57..c574c1a 100644 --- a/build-QChatClient-Desktop-Debug/moc_chatroom.o +++ b/build-QChatClient-Desktop-Debug/moc_chatroom.o