Commit d41aecd4 authored by legoc's avatar legoc

Updated SessionManager: login with user and loginRefused function

parent 2f499868
......@@ -27,12 +27,13 @@ message Message {
enum Type {
Login = 1;
EnableRemoteControl = 2;
DisableRemoteControl = 3;
GetToken = 4;
Sync = 5;
ReleaseToken = 6;
Chat = 7;
LoginRefused = 2;
EnableRemoteControl = 3;
DisableRemoteControl = 4;
GetToken = 5;
Sync = 6;
ReleaseToken = 7;
Chat = 8;
}
required Type type = 1;
......@@ -41,6 +42,13 @@ message Message {
message LoginRequest {
required bool standAlone = 1;
optional string user = 2;
}
message LoginRefusedRequest {
required string user = 1;
required string reason = 2;
}
message LoginResponse {
......
......@@ -177,7 +177,7 @@ void SessionManager::checkRequesterApplications() {
if (state == cameo::application::UNKNOWN) {
// Get the last state: should be a terminal state.
cout << "client " << a->instance->getId() << " has finished with last state " << cameo::application::toString(a->instance->getLastState()) << endl;
//cout << "client " << a->instance->getId() << " has finished with last state " << cameo::application::toString(a->instance->getLastState()) << endl;
// Release the token if necessary.
if (a->server->getEndpoint() == m_tokenOwner) {
......@@ -221,7 +221,7 @@ std::chrono::system_clock::time_point SessionManager::lastTokenActivityDate() {
return m_lastTokenActivityDate;
}
int SessionManager::login(cameo::application::Request * request) {
int SessionManager::login(cameo::application::Request * request, const std::string& user) {
// Check the requester applications.
checkRequesterApplications();
......@@ -266,7 +266,7 @@ int SessionManager::login(cameo::application::Request * request) {
// Add the app in the list of client apps.
m_applications.push_back(std::move(application));
LogHandler::getInstance()->write(SimpleLog(common::LOG_DEBUG, "Client " + hostname(applicationEndpoint) + " logged in"));
LogHandler::getInstance()->write(SimpleLog(common::LOG_DEBUG, "User " + user + " logged from client " + hostname(applicationEndpoint)));
// Assign the next id.
return nextId();
......@@ -275,6 +275,17 @@ int SessionManager::login(cameo::application::Request * request) {
return -1;
}
void SessionManager::loginRefused(cameo::application::Request * request, const std::string& reason, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
// Get the requester application endpoint.
string applicationEndpoint = application.instance->getEndpoint();
LogHandler::getInstance()->write(SimpleLog(common::LOG_DEBUG, "User " + user + " refused from client " + hostname(applicationEndpoint)));
}
void SessionManager::enableRemoteControl(cameo::application::Request * request, uint32 clientID, const std::string& clientType) {
// Get the application.
......
......@@ -86,7 +86,12 @@ public:
/**
* Logins a session. Returns the id of the session if accepted, -1 otherwise.
*/
int login(cameo::application::Request * request);
int login(cameo::application::Request * request, const std::string& user = "?");
/**
* Logs the refusal of login from the client.
*/
void loginRefused(cameo::application::Request * request, const std::string& reason, const std::string& user = "?");
/**
* Enables the remote control depending on conditions.
......
......@@ -58,6 +58,7 @@ void SessionResponder::stop() {
void SessionResponder::initProcessFunctions() {
m_processFunctions[session::Message::Login] = &SessionResponder::processLogin;
m_processFunctions[session::Message::LoginRefused] = &SessionResponder::processLoginRefused;
m_processFunctions[session::Message::EnableRemoteControl] = &SessionResponder::processEnableRemoteControlRequest;
m_processFunctions[session::Message::DisableRemoteControl] = &SessionResponder::processDisableRemoteControlRequest;
m_processFunctions[session::Message::GetToken] = &SessionResponder::processGetTokenRequest;
......@@ -77,8 +78,16 @@ void SessionResponder::processLogin(cameo::application::Request * request, const
responseMessage.set_value(0);
}
else {
// Forwards to the session manager.
int result = SessionManager::getInstance()->login(request);
// The request should contain a user but it is optional.
// Forwards the call to the session manager.
int result = 0;
if (messageRequest.has_user()) {
result = SessionManager::getInstance()->login(request, messageRequest.user());
}
else {
result = SessionManager::getInstance()->login(request);
}
// Add the application if it is accepted.
if (result > 0) {
......@@ -94,6 +103,28 @@ void SessionResponder::processLogin(cameo::application::Request * request, const
responseMessage.SerializeToString(&response);
}
void SessionResponder::processLoginRefused(cameo::application::Request * request, const std::string & message, std::string & response) {
session::LoginRefusedRequest messageRequest;
messageRequest.ParseFromString(message);
common::BooleanResponse responseMessage;
// Forwards to the session manager.
if (messageRequest.has_user()) {
SessionManager::getInstance()->loginRefused(request, messageRequest.reason(), messageRequest.user());
}
else {
SessionManager::getInstance()->loginRefused(request, messageRequest.reason());
}
responseMessage.set_value(true);
// Serialize the response.
responseMessage.SerializeToString(&response);
}
void SessionResponder::processEnableRemoteControlRequest(cameo::application::Request * request, const std::string & message, std::string & response) {
common::BooleanResponse responseMessage;
......
......@@ -57,6 +57,7 @@ private:
void initProcessFunctions();
void processLogin(cameo::application::Request * request, const std::string & message, std::string & response);
void processLoginRefused(cameo::application::Request * request, const std::string & message, std::string & response);
void processEnableRemoteControlRequest(cameo::application::Request * request, const std::string & message, std::string & response);
void processDisableRemoteControlRequest(cameo::application::Request * request, const std::string & message, std::string & response);
void processGetTokenRequest(cameo::application::Request * request, const std::string & message, std::string & response);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment