Samchon Framework for CPP  1.0.0
ChatRoomArray.cpp
1 #include "ChatRoomArray.hpp"
2 #include "ChatRoom.hpp"
3 
4 #include "ChatServer.hpp"
5 #include "ChatUser.hpp"
6 #include "ChatClient.hpp"
7 #include "ListService.hpp"
8 
9 #include <thread>
10 #include <samchon/library/XML.hpp>
11 #include <samchon/protocol/Invoke.hpp>
12 
13 using namespace std;
14 using namespace samchon::library;
15 using namespace samchon::protocol;
16 using namespace samchon::example::chat_service;
17 
18 ChatRoomArray::ChatRoomArray(ChatServer *server)
19  : super(),
20  IEntityGroup()
21 {
22  this->server = server;
23 }
24 
26 {
27  shared_ptr<Invoke> &invoke = this->toInvoke();
28 
29  UniqueReadLock uk(get_allocator().getMutex());
30  for (auto it = server->begin(); it != server->end(); it++)
31  {
32  auto user = it->second;
33 
34  for (auto u_it = user->begin(); u_it != user->end(); u_it++)
35  {
36  auto client = u_it->second;
37  auto service = client->getService();
38 
39  if (dynamic_cast<ListService>(service) != nullptr)
40  thread(&IProtocol::sendData, service, invoke).detach();
41  }
42  }
43 }
44 
45 auto ChatRoomArray::toXML() const -> shared_ptr<XML>
46 {
47  shared_ptr<XML> xml(new XML());
48  xml->setTag(TAG());
49 
50  UniqueReadLock uk(get_allocator().getMutex());
51  for(auto it = begin(); it != end(); it++)
52  xml->push_back(it->second->toXML());
53 
54  return xml;
55 }
56 auto ChatRoomArray::toInvoke() const -> shared_ptr<Invoke>
57 {
58  Invoke *invoke = new Invoke("handleRoomArray", toXML());
59  return shared_ptr<Invoke>(invoke);
60 }
Customized std::map.
Definition: Map.hpp:88
ChatServer * server
A chat server that rooms are belonged to.
A chat service inherited from cloud service.
Definition: ChatClient.hpp:8
auto begin() const -> const_iterator
A const iterator of begin.
Definition: Server.cpp:57
An iternface for entity group.
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
auto end() const -> const_iterator
A const iterator of end.
Definition: Server.cpp:61
Unique lock for reading.
auto toInvoke() const -> std::shared_ptr< protocol::Invoke >
Get an Invoke message used to notifying change.
Package of network protocol and libraries.
Definition: protocol.hpp:185
Standard message of network I/O.
Definition: Invoke.hpp:47
auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object representing the ChatRoomArray.
void notify()
Notify changes of chat rooms.
XML is a class representing xml object.
Definition: XML.hpp:72