...
 
Commits (3)
......@@ -34,6 +34,7 @@ message Message {
Sync = 6;
ReleaseToken = 7;
Chat = 8;
CrashMail = 9;
}
required Type type = 1;
......@@ -86,3 +87,8 @@ message ChatMessage {
required string sender = 2;
required string message = 3;
}
message CrashMailRequest {
required string content = 1;
}
......@@ -44,7 +44,7 @@ SessionManager* SessionManager::m_instance = nullptr;
RequesterApplication::RequesterApplication() {}
RequesterApplication::RequesterApplication(RequesterApplication && application)
: server(std::move(application.server)), instance(std::move(application.instance)) {
: server(std::move(application.server)), instance(std::move(application.instance)), user(application.user) {
}
void RequesterApplication::reset() {
......@@ -55,12 +55,13 @@ void RequesterApplication::reset() {
RequesterApplication & RequesterApplication::operator= (RequesterApplication && application) {
server = std::move(application.server);
instance = std::move(application.instance);
user = application.user;
return *this;
}
SessionManager::SessionManager() :
m_tokenTimeout(30), m_running(false), m_multipleClients(0), m_currentId(0), m_tokenClientID(0) {
m_notificationsEnabled(true), m_tokenTimeout(30), m_running(false), m_multipleClients(0), m_currentId(0), m_tokenClientID(0) {
m_stateAccessor = MultiThreadCommunication::getInstance()->getStateAccessor();
}
......@@ -111,6 +112,12 @@ void SessionManager::init() {
err << "unable to find property 'tokenTimeout', setting to default value 30s" << eol;
}
// Test environment variable.
string notificationsString = ServerProperties::getInstance()->getNomadNotifications();
if ((notificationsString == "0") || (notificationsString == "-1")) {
m_notificationsEnabled = false;
}
m_running.store(true);
// Start the thread.
......@@ -146,7 +153,7 @@ void SessionManager::init() {
}));
}
RequesterApplication SessionManager::getRequesterApplication(cameo::application::Request * request) {
RequesterApplication SessionManager::getRequesterApplication(cameo::application::Request * request, const std::string& user) {
RequesterApplication application;
......@@ -156,6 +163,9 @@ RequesterApplication SessionManager::getRequesterApplication(cameo::application:
// Need to get the server, otherwise the request cannot be destroyed.
application.server = request->getServer();
// Store the user. Used for remote applications.
application.user = user;
// Return the application.
return application;
}
......@@ -228,7 +238,7 @@ int SessionManager::login(cameo::application::Request * request, const std::stri
checkRequesterApplications();
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Check the current applications.
bool accepted = true;
......@@ -279,7 +289,7 @@ int SessionManager::login(cameo::application::Request * request, const std::stri
void SessionManager::loginRefused(cameo::application::Request * request, const std::string& reason, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Get the requester application endpoint.
string applicationEndpoint = application.instance->getEndpoint();
......@@ -290,7 +300,7 @@ void SessionManager::loginRefused(cameo::application::Request * request, const s
void SessionManager::enableRemoteControl(cameo::application::Request * request, uint32 clientID, const std::string& clientType, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Get the endpoint.
string endpoint = application.server->getEndpoint();
......@@ -320,7 +330,7 @@ void SessionManager::enableRemoteControl(cameo::application::Request * request,
void SessionManager::disableRemoteControl(cameo::application::Request * request, uint32 clientID, const std::string& clientType, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Get the endpoint.
string endpoint = application.server->getEndpoint();
......@@ -350,7 +360,7 @@ void SessionManager::disableRemoteControl(cameo::application::Request * request,
void SessionManager::requestToken(cameo::application::Request * request, uint32 clientID, const std::string& clientType, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Get the endpoint.
string endpoint = application.server->getEndpoint();
......@@ -371,7 +381,7 @@ void SessionManager::requestToken(cameo::application::Request * request, uint32
notifyTokenState();
updateTokenActivity();
LogHandler::getInstance()->write(SimpleLog(common::LOG_DEBUG, "User " + user + " on Client " + hostname(endpoint) + " has the token"));
LogHandler::getInstance()->write(SimpleLog(common::LOG_DEBUG, "User " + user + " on client " + hostname(endpoint) + " has the token"));
}
else {
// It is not possible to request the token when remote control is disabled.
......@@ -401,7 +411,7 @@ void SessionManager::requestToken(cameo::application::Request * request, uint32
void SessionManager::releaseToken(cameo::application::Request * request, uint32 clientID, const std::string& user) {
// Get the application.
RequesterApplication application = getRequesterApplication(request);
RequesterApplication application = getRequesterApplication(request, user);
// Get the endpoint.
string endpoint = application.server->getEndpoint();
......@@ -471,4 +481,19 @@ void SessionManager::sendChatMessage(cameo::application::Request * request, uint
m_stateAccessor->notifyChatMessage(clientID, seconds, sender, message);
}
void SessionManager::sendCrashMail(cameo::application::Request * request, const std::string& content) {
string mailCommand("echo \"");
mailCommand += content + "\" | mail -r sci@ill.fr -s \"";
mailCommand += "[`hostname --long`][remote client]\" ";
string recipients = "nomad_l@ill.fr";
mailCommand += recipients;
cout << "crash mail command = " << mailCommand << endl;
if (m_notificationsEnabled) {
::system(mailCommand.c_str());
}
}
}
......@@ -40,6 +40,7 @@ struct RequesterApplication {
std::unique_ptr<cameo::Server> server;
std::unique_ptr<cameo::application::Instance> instance;
std::string user;
/**
* Constructor.
......@@ -128,13 +129,18 @@ public:
*/
void sendChatMessage(cameo::application::Request * request, uint32 clientID, const std::string& sender, const std::string& message);
/**
* Sends a mail of crash.
*/
void sendCrashMail(cameo::application::Request * request, const std::string& content);
private:
SessionManager();
~SessionManager();
void init();
RequesterApplication getRequesterApplication(cameo::application::Request * request);
RequesterApplication getRequesterApplication(cameo::application::Request * request, const std::string& user);
void checkRequesterApplications();
int nextId();
std::chrono::system_clock::time_point lastTokenActivityDate();
......@@ -142,6 +148,7 @@ private:
static SessionManager* m_instance;
bool m_notificationsEnabled;
int m_tokenTimeout;
std::atomic_bool m_running;
std::unique_ptr<std::thread> m_checkThread;
......
......@@ -76,6 +76,7 @@ void SessionResponder::initProcessFunctions() {
m_processFunctions[session::Message::ReleaseToken] = &SessionResponder::processReleaseTokenRequest;
m_processFunctions[session::Message::Sync] = &SessionResponder::processSync;
m_processFunctions[session::Message::Chat] = &SessionResponder::processChatMessage;
m_processFunctions[session::Message::CrashMail] = &SessionResponder::processCrashMailRequest;
}
void SessionResponder::processLogin(cameo::application::Request * request, const std::string & message, std::string & response) {
......@@ -229,6 +230,22 @@ void SessionResponder::processChatMessage(cameo::application::Request * request,
responseMessage.SerializeToString(&response);
}
void SessionResponder::processCrashMailRequest(cameo::application::Request * request, const std::string & message, std::string & response) {
common::BooleanResponse responseMessage;
session::CrashMailRequest messageRequest;
messageRequest.ParseFromString(message);
// Forwards to the session manager.
SessionManager::getInstance()->sendCrashMail(request, messageRequest.content());
responseMessage.set_value(true);
// Serialize the response.
responseMessage.SerializeToString(&response);
}
void SessionResponder::loop() {
// Initialize the map of functions.
......
......@@ -64,6 +64,7 @@ private:
void processReleaseTokenRequest(cameo::application::Request * request, const std::string & message, std::string & response);
void processSync(cameo::application::Request * request, const std::string & message, std::string & response);
void processChatMessage(cameo::application::Request * request, const std::string & message, std::string & response);
void processCrashMailRequest(cameo::application::Request * request, const std::string & message, std::string & response);
typedef void (SessionResponder:: *ProcessFunctionType)(cameo::application::Request *, const std::string &, std::string &);
......