Eile Mit Weile
Loading...
Searching...
No Matches
ServerResponse Class Reference

Base class for all messages sent from the server to the client. More...

#include <ServerResponse.hpp>

Inheritance diagram for ServerResponse:
Serializable FullStateResponse RequestResponse

Classes

struct  BaseClassProperties
 Contains the properties that are common to all ServerResponses. More...
 

Public Member Functions

ResponseType GetType () const
 
std::string GetGameId () const
 
void WriteIntoJson (rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) const override
 
virtual void Process () const
 
- Public Member Functions inherited from Serializable
virtual rapidjson::DocumentToJson () const
 

Static Public Member Functions

static ServerResponseFromJson (const rapidjson::Value &json)
 

Protected Member Functions

 ServerResponse (const BaseClassProperties &)
 

Static Protected Member Functions

static BaseClassProperties CreateBaseClassProperties (ResponseType type, const std::string &game_id)
 
static BaseClassProperties ExtractBaseClassProperties (const rapidjson::Value &json)
 

Protected Attributes

std::string _game_id
 
ResponseType _type
 

Static Private Attributes

static const std::unordered_map< std::string, ResponseType_string_to_response_type
 
static const std::unordered_map< ResponseType, std::string > _response_type_to_string
 

Detailed Description

Base class for all messages sent from the server to the client.

It offers a function to deserialize a ServerResponse subclass from a valid json. Note: This class design is taken from https://gitlab.ethz.ch/hraymond/cse-lama-example-project. RequestResponse and FullStateResponse are derivatives of ServerResponse. Due to static allocation, this base class can call its derivatives' FromJson methods. We think this is very bad design, but changing it would require a lot of refactoring so we decided to leave it as it is.

Constructor & Destructor Documentation

◆ ServerResponse()

ServerResponse::ServerResponse ( const BaseClassProperties params)
protected

Constructor that assigns BaseClassProperties.

Member Function Documentation

◆ CreateBaseClassProperties()

ServerResponse::BaseClassProperties ServerResponse::CreateBaseClassProperties ( ResponseType  type,
const std::string &  game_id 
)
staticprotected

Creates a BaseClassProperties object with the given parameters.

◆ ExtractBaseClassProperties()

ServerResponse::BaseClassProperties ServerResponse::ExtractBaseClassProperties ( const rapidjson::Value json)
staticprotected

Extracts the BaseClassProperties from the given json.

◆ FromJson()

ServerResponse * ServerResponse::FromJson ( const rapidjson::Value json)
static

Tries to create the specific server_response from the provided json (see RequestResponse::FromJson and FullStateResponse::FromJson). Note**: RequestResponse and FullStateResponse are derivatives of ServerResponse. Due to static allocation, this base class can call its derivatives' FromJson methods. We think this is very bad design, but changing it would require a lot of refactoring so we decided to leave it as it is.

Throws exception if parsing fails.

◆ GetGameId()

std::string ServerResponse::GetGameId ( ) const

Returns the id of the game this ServerResponse is related to. See GameInstance::id.

◆ GetType()

ResponseType ServerResponse::GetType ( ) const

Returns the ResponseType of this ServerResponse.

◆ WriteIntoJson()

void ServerResponse::WriteIntoJson ( rapidjson::Value json,
rapidjson::Document::AllocatorType &  allocator 
) const
overridevirtual

Serializes the ServerResponse into a json object that can be sent over the network.

Implements Serializable.

Member Data Documentation

◆ _game_id

std::string ServerResponse::_game_id
protected

The id of the game this ServerResponse is related to. See GameInstance::id.

◆ _response_type_to_string

const std::unordered_map< ResponseType, std::string > ServerResponse::_response_type_to_string
staticprivate
Initial value:
= {
{ ResponseType::req_response, "req_response" },
{ ResponseType::state_diff_msg, "state_diff_msg"},
{ ResponseType::full_state_msg, "full_state_msg"}
}

Maps a ResponseType to a string. Used for serialization.

◆ _string_to_response_type

const std::unordered_map< std::string, ResponseType > ServerResponse::_string_to_response_type
staticprivate
Initial value:
= {
{"req_response", ResponseType::req_response },
{"state_diff_msg", ResponseType::state_diff_msg},
{"full_state_msg", ResponseType::full_state_msg}
}

Maps a string to a ResponseType. Used for deserialization.

◆ _type

ResponseType ServerResponse::_type
protected

The type of this ServerResponse. See ResponseType.


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