Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. // Daniel Depaolo
  2. // 10-25-10
  3. // CS 450
  4. // ChatRoom.h
  5.  
  6. #ifndef CHATROOM_H
  7. #define CHATROOM_H
  8.  
  9. #include <string>
  10. #include <vector>
  11. #include <list>
  12. #include "Client.h"
  13.  
  14. class ChatRoom {
  15. public:
  16.     ChatRoom( std::string roomName, std::string roomTitle );
  17.     void setRoomName( std::string name ) { _name = name; }
  18.     void setRoomTitle( std::string title ) { _title = title; }
  19.     std::string getRoomName() { return _name; }
  20.     std::string getRoomTitle() { return _title; }
  21.  
  22.     // writer thread:
  23.     void addClient( ChatRoom *client );
  24.  
  25.     // writer thread:
  26.     bool removeClient( std::string clientToRemove );
  27.     int numClients();
  28.  
  29.     // writer thread:
  30.     // Add a message to the list of messages to be sent to all clients
  31.     // who have subscribed to this chat-room.
  32.     void postMessage( std::string message );
  33.  
  34.     // reader thread:
  35.     // Get a message from those deposted to the list of message for
  36.     // distribution by "postMessage" and deliver it to all clients who
  37.     // have subscribed to this chat-room.
  38.     void distributeMessages();
  39.    
  40. private:
  41.     std::string _name;
  42.     std::string _title;
  43.     std::vector< Client *> clients;
  44.     std::list< std::string > messages; // a FCFS queue
  45. };
  46.  
  47. #endif
  48.  
  49. /////////////////////////////////////
  50.  
  51. // Daniel DePaolo
  52. // 11-2-10
  53. // Collections.h
  54. // instances of the reader/writer problem.
  55.  
  56. #ifndef COLLECTIONS_H
  57. #define COLLECTIONS_H
  58.  
  59. #include <vector>
  60. #include <string>
  61. #include <algorithm>
  62. #include "Client.h"
  63.  
  64. class ChatRoomCollection {
  65. public:
  66.     void addChatRoom(ChatRoom * cr)
  67.     {
  68.         CRList.push_back(cr);
  69.     }
  70.     void deleteChatRoom(ChatRoom * cr)
  71.     {
  72.         if (findChatRoom(cr->getRoomName()))
  73.         {
  74.             ChatRoom * tmp = getChatRoom(cr->getRoomName());
  75.             delete tmp;
  76.         }
  77.         else return;
  78.     }
  79.     int numChatRooms() { return CRList.size(); }
  80.     ChatRoom* getChatRoom(std::string name)
  81.     {
  82.         int i;
  83.         for (i = 0; i < CRList.size(); i++)
  84.             if (CRList[i]->getRoomName() == name)
  85.                 return CRList[i];
  86.         return CRList[0];
  87.     }
  88.     bool findChatRoom(std::string name)
  89.     {
  90.         int i;
  91.         for (i = 0; i < CRList.size(); i++)
  92.             if (CRList[i]->getRoomName() == name)
  93.                 return CRList[i];
  94.         return CRList[0];
  95.     }
  96.  
  97. private:
  98.     std::vector<ChatRoom*> CRList;
  99. };
  100.  
  101. // instances of the reader/writer problem.
  102. class ClientCollection {
  103. public:
  104.     void addClient( Client *client )
  105.     {
  106.         clientList.push_back(client);
  107.     }
  108.     void deleteClient( Client *client )
  109.     {
  110.         if (findClient(client->getID()))
  111.         {
  112.             Client * tmp = getClient(client->getID());
  113.             delete tmp;
  114.         }
  115.         else return;
  116.     }
  117.     int numClients() { return clientList.size(); }
  118.     Client * getClient(std::string name)
  119.     {
  120.         int i;
  121.         for (i = 0; i < clientList.size(); i++)
  122.             if (clientList[i]->getID() == name)
  123.                 return clientList[i];
  124.         return clientList[0];
  125.     }
  126.     bool findClient(std::string name)
  127.     {
  128.         int i;
  129.         for (i = 0; i < clientList.size(); i++)
  130.             if (clientList[i]->getID() == name)
  131.                 return true;
  132.         return false;
  133.     }
  134.  
  135. private:
  136.     std::vector<Client*> clientList;
  137. };
  138.  
  139. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement