Package of cloud service as a server.
In Samchon Framework, realizing cloud service, there're two classes representing physical server and client as Server and Client. Between the two classes representing boundaries, there is a class representing logical conception user as User. In the terminal node under the Client, Service class represents a control.
The default protocol of the service package is not 'web socket'. The default only has message protocol of Invoke, does not have handshake or another pre-defined protocol. By the default protocol, you can connect to the cloud server (built from c++, by service package) as a client by a program or plug-in like Flex or C#. However you can't connect by web-browser only allowing socket protocol as web-socket.
To build a cloud service to follow web-socket protocol, inherits derviced classes not from Server, Client, but from WebServer, WebClient or implements IWebServer, IWebClient which are derived from Server and Client.
Class Diagram
Sequence Diagram - A client's connection
Sequence Diagram - Network communication
Simple example - Chat service
- example/chat_service/ChatServer.hpp
2 #include <samchon/protocol/service/Server.hpp> 23 :
public protocol::service::Server
26 typedef protocol::service::Server super;
28 virtual auto PORT() const ->
int override 37 ChatRoomArray *roomArray;
44 virtual ~ChatServer();
47 virtual auto createUser() -> protocol::service::User*
override;
53 auto getChatRoomArray() const -> ChatRoomArray*;
Top level namespace of products built from samchon.
- example/chat_service/ChatUser.hpp
2 #include <samchon/protocol/service/User.hpp> 20 :
public protocol::service::User
26 ChatUser(protocol::service::Server*);
27 virtual ~ChatUser() =
default;
30 virtual auto createClient() -> protocol::service::Client*
override;
Top level namespace of products built from samchon.
- example/chat_service/ChatClient.hpp
2 #include <samchon/protocol/service/Client.hpp> 20 :
public protocol::service::Client
26 ChatClient(protocol::service::User*);
27 virtual ~ChatClient() =
default;
30 auto createService(
const std::string &) -> protocol::service::Service*
override;
Top level namespace of products built from samchon.
- example/chat_service/ChatService.hpp
2 #include <samchon/protocol/service/Service.hpp> 23 :
public protocol::service::Service
26 typedef protocol::service::Service super;
38 ChatService(protocol::service::Client*);
39 virtual ~ChatService();
41 virtual void replyData(std::shared_ptr<protocol::Invoke>)
override;
Top level namespace of products built from samchon.
- example/chat_service/ListService.hpp
2 #include <samchon/protocol/service/Service.hpp> 22 :
public protocol::service::Service
25 typedef protocol::service::Service super;
31 ListService(protocol::service::Client*);
32 virtual ~ListService() =
default;
34 virtual void replyData(std::shared_ptr<protocol::Invoke> invoke)
override;
42 void createRoom(
const std::string &);
44 void handleRoomArray(std::shared_ptr<library::XML>);
45 void handleMakeRoom(
bool);
46 void handleRoomArray(
bool);
Top level namespace of products built from samchon.
- example/chat_service/ChatRoomArray.hpp
2 #include <samchon/library/CriticalDictionary.hpp> 3 #include <samchon/protocol/IEntityGroup.hpp> 7 namespace library {
class XML; };
8 namespace protocol {
class Invoke; };
12 namespace chat_service
33 public protocol::IEntityGroup
36 typedef CriticalDictionary<std::shared_ptr<ChatRoom>> super;
38 virtual auto TAG() const ->
std::
string {
return "roomArray"; };
39 virtual auto CHILD_TAG() const ->
std::
string {
return "room"; };
54 ChatRoomArray(ChatServer*);
55 virtual ~ChatRoomArray() =
default;
73 auto toXML() const ->
std::shared_ptr<library::XML>;
78 auto toInvoke() const ->
std::shared_ptr<protocol::Invoke>;
CriticalMap< std::string, T, _Pr > CriticalDictionary
A Dictionary ensures concurrency.
Top level namespace of products built from samchon.
- example/chat_service/ChatRoom.hpp
2 #include <samchon/protocol/Entity.hpp> 3 #include <samchon/protocol/IProtocol.hpp> 6 #include <samchon/library/CriticalSet.hpp> 13 namespace chat_service
34 :
public protocol::Entity,
35 public protocol::IProtocol
38 typedef protocol::Entity super;
40 virtual auto TAG() const ->
std::
string {
return "room"; };
46 ChatRoomArray *roomArray;
62 library::CriticalSet<ChatService*> participants;
75 ChatRoom(ChatRoomArray*,
const std::string &, ChatUser*);
76 virtual ~ChatRoom() =
default;
82 void registerClient(ChatService*);
88 void eraseClient(ChatService*);
93 virtual void replyData(std::shared_ptr<protocol::Invoke>)
override;
94 virtual void sendData(std::shared_ptr<protocol::Invoke>)
override;
100 virtual auto toXML() const ->
std::shared_ptr<library::XML> override;
Top level namespace of products built from samchon.
- example/chat_service/ChatMessage.hpp
5 #include <samchon/protocol/Entity.hpp> 11 namespace chat_service
25 :
public protocol::Entity
28 typedef protocol::Entity super;
30 virtual auto TAG() const ->
std::
string 60 virtual ~ChatMessage() =
default;
62 virtual void construct(std::shared_ptr<library::XML>);
71 auto getListener() const ->
std::
string;
73 virtual auto toXML() const ->
std::shared_ptr<library::XML>;
Top level namespace of products built from samchon.
- example/chat_service/ChatServer.cpp
-
- example/chat_service/ChatUser.cpp
-
- example/chat_service/ChatClient.cpp
-
- example/chat_service/ChatService.cpp
1 #include "ChatService.hpp" 2 #include "ChatRoom.hpp" 4 #include <samchon/protocol/Invoke.hpp> 11 ChatService::ChatService(
Client *client)
16 ChatService::~ChatService()
21 room->eraseClient(
this);
24 void ChatService::replyData(shared_ptr<Invoke> invoke)
26 if(invoke->getListener() ==
"sendMessage")
27 room->replyData(invoke);
A chat service inherited from cloud service.
Package of cloud service as a server.
Package of network protocol and libraries.
A network boundary with the client in an User.
- example/chat_service/ListService.cpp
- example/chat_service/ChatRoomArray.cpp
1 #include "ChatRoomArray.hpp" 2 #include "ChatRoom.hpp" 4 #include "ChatServer.hpp" 5 #include "ChatUser.hpp" 6 #include "ChatClient.hpp" 7 #include "ListService.hpp" 10 #include <samchon/library/XML.hpp> 11 #include <samchon/protocol/Invoke.hpp> 18 ChatRoomArray::ChatRoomArray(
ChatServer *server)
22 this->server = server;
25 void ChatRoomArray::notify()
27 shared_ptr<Invoke> &invoke = this->toInvoke();
30 for (
auto it = server->begin(); it != server->end(); it++)
32 auto user = it->second;
34 for (
auto u_it = user->begin(); u_it != user->end(); u_it++)
36 auto client = u_it->second;
37 auto service = client->getService();
39 if (dynamic_cast<ListService>(service) !=
nullptr)
40 thread(&IProtocol::sendData, service, invoke).detach();
45 auto ChatRoomArray::toXML() const -> shared_ptr<XML>
47 shared_ptr<XML> xml(
new XML());
51 for(
auto it = begin(); it != end(); it++)
52 xml->push_back(it->second->toXML());
56 auto ChatRoomArray::toInvoke() const -> shared_ptr<Invoke>
58 Invoke *invoke =
new Invoke(
"handleRoomArray", toXML());
59 return shared_ptr<Invoke>(invoke);
A chat service inherited from cloud service.
An iternface for entity group.
Package of network protocol and libraries.
- example/chat_service/ChatRoom.cpp
1 #include "ChatRoom.hpp" 2 #include "ChatRoomArray.hpp" 3 #include "ChatMessage.hpp" 5 #include "ChatUser.hpp" 6 #include "ChatClient.hpp" 7 #include "ChatService.hpp" 9 #include <samchon/library/XML.hpp> 10 #include <samchon/protocol/Invoke.hpp> 24 this->roomArray = roomArray;
32 participants.insert(service);
34 sendData(shared_ptr<Invoke>(
new Invoke(
"handleRoom", toXML())));
39 if (participants.erase(service) == 0)
40 roomArray->erase(this->name);
42 sendData(shared_ptr<Invoke>(
new Invoke(
"handleRoom", toXML())));
50 void ChatRoom::replyData(shared_ptr<Invoke> invoke)
52 if(invoke->getListener() ==
"sendMessage")
54 shared_ptr<Invoke> ivk(
new Invoke(
"handleMessage"));
55 ivk->at(0) = invoke->at(0);
60 void ChatRoom::sendData(shared_ptr<Invoke> invoke)
64 if(invoke->getListener() ==
"handleMessage")
67 message.construct(invoke->at(0)->getValueAsXML());
69 if(message.getListener().empty() ==
true)
70 for (
auto it = participants.begin(); it != participants.end(); it++)
71 (*it)->sendData(invoke);
73 for (
auto it = participants.begin(); it != participants.end(); it++)
74 if((*it)->getClient()->getUser()->getID() == message.getListener())
75 (*it)->sendData(invoke);
78 for (
auto it = participants.begin(); it != participants.end(); it++)
79 (*it)->sendData(invoke);
85 auto ChatRoom::toXML() const -> shared_ptr<XML>
87 shared_ptr<XML> &xml = super::toXML();
88 xml->setProperty(
"name", name);
89 xml->setProperty(
"host", host->getID());
91 for (
auto it = participants.begin(); it != participants.end(); it++)
93 shared_ptr<XML> participant(
new XML());
94 participant->setTag(
"participant");
95 participant->setValue((*it)->getClient()->getUser()->getID());
97 xml->push_back(participant);
A chat service inherited from cloud service.
Package of network protocol and libraries.
An interface of Invoke message chain.
- example/chat_service/ChatMessage.cpp
1 #include "ChatMessage.hpp" 3 #include <samchon/library/XML.hpp> 13 ChatMessage::ChatMessage()
17 void ChatMessage::construct(shared_ptr<XML> xml)
19 this->orator = xml->getProperty(
"orator");
20 this->message = xml->getProperty(
"message");
22 if(xml->hasProperty(
"listener") ==
true)
23 this->listener = xml->getProperty(
"listener");
25 this->listener.clear();
31 auto ChatMessage::getListener() const ->
string 35 auto ChatMessage::toXML() const -> shared_ptr<XML>
37 shared_ptr<XML> &xml = super::toXML();
38 xml->setProperty(
"orator", orator);
39 xml->setProperty(
"message", message);
41 if(listener.empty() ==
false)
42 xml->setProperty(
"listener", listener);
A chat service inherited from cloud service.
Package of network protocol and libraries.
- Author
- Jeongho Nam http://samchon.org