Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Daniel Depaolo
- // 10-25-10
- // CS 450
- // ChatRoom.h
- #ifndef CHATROOM_H
- #define CHATROOM_H
- #include <string>
- #include <vector>
- #include <list>
- #include "Client.h"
- class ChatRoom {
- public:
- ChatRoom( std::string roomName, std::string roomTitle );
- void setRoomName( std::string name ) { _name = name; }
- void setRoomTitle( std::string title ) { _title = title; }
- std::string getRoomName() { return _name; }
- std::string getRoomTitle() { return _title; }
- // writer thread:
- void addClient( ChatRoom *client );
- // writer thread:
- bool removeClient( std::string clientToRemove );
- int numClients();
- // writer thread:
- // Add a message to the list of messages to be sent to all clients
- // who have subscribed to this chat-room.
- void postMessage( std::string message );
- // reader thread:
- // Get a message from those deposted to the list of message for
- // distribution by "postMessage" and deliver it to all clients who
- // have subscribed to this chat-room.
- void distributeMessages();
- private:
- std::string _name;
- std::string _title;
- std::vector< Client *> clients;
- std::list< std::string > messages; // a FCFS queue
- };
- #endif
- /////////////////////////////////////
- // Daniel DePaolo
- // 11-2-10
- // Collections.h
- // instances of the reader/writer problem.
- #ifndef COLLECTIONS_H
- #define COLLECTIONS_H
- #include <vector>
- #include <string>
- #include <algorithm>
- #include "Client.h"
- class ChatRoomCollection {
- public:
- void addChatRoom(ChatRoom * cr)
- {
- CRList.push_back(cr);
- }
- void deleteChatRoom(ChatRoom * cr)
- {
- if (findChatRoom(cr->getRoomName()))
- {
- ChatRoom * tmp = getChatRoom(cr->getRoomName());
- delete tmp;
- }
- else return;
- }
- int numChatRooms() { return CRList.size(); }
- ChatRoom* getChatRoom(std::string name)
- {
- int i;
- for (i = 0; i < CRList.size(); i++)
- if (CRList[i]->getRoomName() == name)
- return CRList[i];
- return CRList[0];
- }
- bool findChatRoom(std::string name)
- {
- int i;
- for (i = 0; i < CRList.size(); i++)
- if (CRList[i]->getRoomName() == name)
- return CRList[i];
- return CRList[0];
- }
- private:
- std::vector<ChatRoom*> CRList;
- };
- // instances of the reader/writer problem.
- class ClientCollection {
- public:
- void addClient( Client *client )
- {
- clientList.push_back(client);
- }
- void deleteClient( Client *client )
- {
- if (findClient(client->getID()))
- {
- Client * tmp = getClient(client->getID());
- delete tmp;
- }
- else return;
- }
- int numClients() { return clientList.size(); }
- Client * getClient(std::string name)
- {
- int i;
- for (i = 0; i < clientList.size(); i++)
- if (clientList[i]->getID() == name)
- return clientList[i];
- return clientList[0];
- }
- bool findClient(std::string name)
- {
- int i;
- for (i = 0; i < clientList.size(); i++)
- if (clientList[i]->getID() == name)
- return true;
- return false;
- }
- private:
- std::vector<Client*> clientList;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement