Samchon Framework for CPP  1.0.0
ChatRoom.cpp
1 #include "ChatRoom.hpp"
2 #include "ChatRoomArray.hpp"
3 #include "ChatMessage.hpp"
4 
5 #include "ChatUser.hpp"
6 #include "ChatClient.hpp"
7 #include "ChatService.hpp"
8 
9 #include <samchon/library/XML.hpp>
10 #include <samchon/protocol/Invoke.hpp>
11 
12 using namespace std;
13 using namespace samchon::library;
14 using namespace samchon::protocol;
15 using namespace samchon::example::chat_service;
16 
17 /* -----------------------------------------------------------
18  CONSTRUCTORS
19 ----------------------------------------------------------- */
20 ChatRoom::ChatRoom(ChatRoomArray *roomArray, const string &name, ChatUser *host)
21  : super(),
22  IProtocol()
23 {
24  this->roomArray = roomArray;
25 
26  this->name = name;
27  this->host = host;
28 }
29 
31 {
32  participants.insert(service);
33 
34  sendData(shared_ptr<Invoke>(new Invoke("handleRoom", toXML())));
35  roomArray->notify();
36 }
38 {
39  if (participants.erase(service) == 0)
40  roomArray->erase(this->name);
41  else
42  sendData(shared_ptr<Invoke>(new Invoke("handleRoom", toXML())));
43 
44  roomArray->notify();
45 }
46 
47 /* -----------------------------------------------------------
48  CHAIN OF RESPONSIBILITY
49 ----------------------------------------------------------- */
50 void ChatRoom::replyData(shared_ptr<Invoke> invoke)
51 {
52  if(invoke->getListener() == "sendMessage")
53  {
54  shared_ptr<Invoke> ivk(new Invoke("handleMessage"));
55  ivk->at(0) = invoke->at(0);
56 
57  sendData(ivk);
58  }
59 }
60 void ChatRoom::sendData(shared_ptr<Invoke> invoke)
61 {
62  UniqueReadLock uk(participants.get_allocator().getMutex());
63 
64  if(invoke->getListener() == "handleMessage")
65  {
66  ChatMessage message;
67  message.construct(invoke->at(0)->getValueAsXML());
68 
69  if(message.getListener().empty() == true)
70  for (auto it = participants.begin(); it != participants.end(); it++)
71  (*it)->sendData(invoke);
72  else
73  for (auto it = participants.begin(); it != participants.end(); it++)
74  if((*it)->getClient()->getUser()->getID() == message.getListener())
75  (*it)->sendData(invoke);
76  }
77  else
78  for (auto it = participants.begin(); it != participants.end(); it++)
79  (*it)->sendData(invoke);
80 }
81 
82 /* -----------------------------------------------------------
83  EXPORTS
84 ----------------------------------------------------------- */
85 auto ChatRoom::toXML() const -> shared_ptr<XML>
86 {
87  shared_ptr<XML> &xml = super::toXML();
88  xml->setProperty("name", name);
89  xml->setProperty("host", host->getID());
90 
91  for (auto it = participants.begin(); it != participants.end(); it++)
92  {
93  shared_ptr<XML> participant(new XML());
94  participant->setTag("participant");
95  participant->setValue((*it)->getClient()->getUser()->getID());
96 
97  xml->push_back(participant);
98  }
99  return xml;
100 }
An entity, a standard data class.
Definition: Entity.hpp:48
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the Entity.
Definition: Entity.cpp:30
A chat service inherited from cloud service.
Definition: ChatClient.hpp:8
ChatUser * host
A host, who established the room.
Definition: ChatRoom.hpp:57
auto getID() const -> std::string
Get user&#39;s account id.
Definition: User.cpp:46
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
virtual void construct(std::shared_ptr< library::XML >)
Construct data of the Entity from an XML object.
Definition: ChatMessage.cpp:17
void registerClient(ChatService *)
Register a participant client.
Definition: ChatRoom.cpp:30
library::CriticalSet< ChatService * > participants
Participants, ChatUser(s) in the room.
Definition: ChatRoom.hpp:62
auto getListener() const -> std::string
Get listener.
Definition: ChatMessage.cpp:31
Unique lock for reading.
Package of network protocol and libraries.
Definition: protocol.hpp:185
ChatRoomArray * roomArray
All rooms that the room is belonged to.
Definition: ChatRoom.hpp:40
An interface of Invoke message chain.
Definition: IProtocol.hpp:31
Standard message of network I/O.
Definition: Invoke.hpp:47
virtual auto toXML() const -> std::shared_ptr< library::XML > override
Get an XML object represents the Entity.
Definition: ChatRoom.cpp:85
void notify()
Notify changes of chat rooms.
XML is a class representing xml object.
Definition: XML.hpp:72
std::string name
Name of the room.
Definition: ChatRoom.hpp:52
void eraseClient(ChatService *)
Erase a participant client.
Definition: ChatRoom.cpp:37