Samchon Framework for CPP  1.0.0
samchon::protocol::service::Client Class Referenceabstract

A network boundary with the client in an User. More...

#include <Client.hpp>

Collaboration diagram for samchon::protocol::service::Client:

Public Member Functions

 Client (User *)
 Construct from User, No and socket. More...
 
auto getUser () const -> User *
 Get User. More...
 
auto getService () const -> Service *
 Get Service. More...
 
auto getNo () const -> size_t
 Get no. More...
 
void sendData (std::shared_ptr< Invoke >)
 Send Invoke message to (physical) client. More...
 
void replyData (std::shared_ptr< Invoke >)
 Reply Invoke message from (physical) client. More...
 
- Public Member Functions inherited from samchon::protocol::IClient
 IClient ()
 Default Constructor. More...
 
virtual void listen ()
 Listens message from a related system. More...
 
- Public Member Functions inherited from samchon::protocol::IProtocol
 IProtocol ()
 Default Constructor. More...
 

Protected Member Functions

virtual auto createService (const std::string &) -> Service *=0
 Factory method of Service. More...
 
- Protected Member Functions inherited from samchon::protocol::IClient
virtual auto BUFFER_SIZE () const -> size_t
 Buffer size of network I/O. More...
 
virtual void _replyData (std::shared_ptr< Invoke >)
 A method for pre-processing replied Invoke message. More...
 

Protected Attributes

Useruser
 An User of the Client. More...
 
size_t no
 A sequence number of the Client in an User. More...
 
Serviceservice
 A Service belongs to the Client. More...
 
- Protected Attributes inherited from samchon::protocol::IClient
Socket * socket
 Socket for network I/O. More...
 
std::mutex * sendMtx
 A mutex for sending message. More...
 

Private Member Functions

void constructService (const std::string &)
 Construct Service. More...
 

Detailed Description

A network boundary with the client in an User.

Client is an object interacting with physical client by socket.

The Client is correspond with a Window in JS (UI) 1:1

  • A Window, the object in UI
  • A Window, matching with window of a internet browser
protocol_service.png
Note
Method to override
  • createUser()
See also
samchon::protocol
samchon::protocol::service
Author
Jeongho Nam http://samchon.org

Definition at line 46 of file Client.hpp.

Constructor & Destructor Documentation

Client::Client ( User user)

Construct from User, No and socket.

Parameters
userAn User containing the Client

Definition at line 30 of file Client.cpp.

References service, and user.

Member Function Documentation

virtual auto samchon::protocol::service::Client::createService ( const std::string &  ) -> Service *
protectedpure virtual

Factory method of Service.

Parameters
nameRequested name representing a Service from the (physical) client
Returns
A new Service belongs the the Client

Implemented in samchon::example::chat_service::ChatClient.

Referenced by constructService().

Here is the caller graph for this function:

auto Client::getUser ( ) const -> User*

Get User.

Definition at line 48 of file Client.cpp.

References user.

auto Client::getService ( ) const -> Service*

Get Service.

Definition at line 52 of file Client.cpp.

References service.

auto Client::getNo ( ) const -> size_t

Get no.

Definition at line 56 of file Client.cpp.

References no.

void Client::sendData ( std::shared_ptr< Invoke )
virtual

Send Invoke message to (physical) client.

Method sendData of Client does not only send Invoke message to client system, but also archives the Invoke message to database system as a historical-log.

Note

Method sendData monopolies a critical section.

How many threads have called that sendData, the Invoke messages are delivered sequentially.

  • Using sendData with a new thread is not recommended
Warning
You can't override this method, sendData.
Parameters
invokeInvoke message to send

Reimplemented from samchon::protocol::IClient.

Definition at line 64 of file Client.cpp.

References constructService(), samchon::protocol::service::User::getSemaphore(), samchon::protocol::IProtocol::replyData(), replyData(), samchon::protocol::service::User::replyData(), samchon::protocol::IClient::sendMtx, service, samchon::protocol::IClient::socket, user, and samchon::ByteArray::writeReversely().

Referenced by constructService(), samchon::protocol::service::Service::sendData(), and samchon::protocol::service::User::sendData().

Here is the call graph for this function:

Here is the caller graph for this function:

void samchon::protocol::service::Client::replyData ( std::shared_ptr< Invoke )
virtual

Reply Invoke message from (physical) client.

Handles replied Invoke message from client system.

  1. Constructs Service
    • Constructs Service by requested service name.
    • Notifies client system whether the user satisfies the authority.
  2. Shifts responsibility to related chain.
    • Invoke message is about member (join, login, etc.), shift to User.
    • Except that all, shifts to Service.
  3. Archives the Invoke message to Database system as a historical-log.
    • Invoke to HISTORY_INVOKE table, with User's session and Client's sequence id.
    • InvokeParameters(s) to HISTORY_INVOKE_PARAMETER with key of HISTORY_INVOKE as foriegn key.
Note
Historical-log

HISTORY_INVOKE and HISTORY_INVOKE_PARAMETER has a 1:N relationship

If you want to modify archiving method, override method of Server::archiveReplyData()

Shifting responsibility to Service

Service::replyData will be called by a new thread. The thread will acquire an admission from semaphore in User to avoid exhausted allocation of threads.

Exception handling

If an std::exception has thrown from Service::replyData, the exception will archived in Database (HISTORY_INVOKE_EXCEPTION)

The most famous thrown parameters like std::exception, std::invalid_argument and std::runtime_error, those are all derived from std::exception.
     -> http://www.cplusplus.com/reference/stdexcept/

  • std::exception

    • std::logic_error
      • std::domain_error
      • std::invalid_argument
      • std::length_error
      • std::out_of_range
      • std::future_error
    • std::runtime_error
      • std::range_error
      • std::overflow_error
      • std::underflow_error
      • std::system_error
    Warning

    You can't override this method, replyData.

    If you want to modify replyData's rule, override related objects' replyData

    Parameters
    invokeInvoke message replied from client system

Reimplemented from samchon::protocol::IProtocol.

Referenced by sendData().

Here is the caller graph for this function:

void Client::constructService ( const std::string &  name)
private

Construct Service.

Constructs Service by requested name from client and notify whether the User is satisfying authority.

Parameters
nameA name representing type of Service

Definition at line 123 of file Client.cpp.

References createService(), samchon::protocol::service::User::getAuthority(), samchon::protocol::service::Service::name, samchon::protocol::service::Service::REQUIRE_AUTHORITY(), sendData(), service, and user.

Referenced by sendData().

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

User* samchon::protocol::service::Client::user
protected

An User of the Client.

Definition at line 59 of file Client.hpp.

Referenced by Client(), constructService(), getUser(), and sendData().

size_t samchon::protocol::service::Client::no
protected

A sequence number of the Client in an User.

Definition at line 64 of file Client.hpp.

Referenced by getNo().

Service* samchon::protocol::service::Client::service
protected

A Service belongs to the Client.

Definition at line 69 of file Client.hpp.

Referenced by Client(), constructService(), getService(), and sendData().


The documentation for this class was generated from the following files: