Webdar 1.0.0
Web user interface to libdar
session.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef SESSION_HPP
25#define SESSION_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31}
32
33 // C++ system header files
34#include <list>
35#include <libthreadar/libthreadar.hpp>
36
37 // webdar headers
38#include "user_interface.hpp"
39#include "events.hpp"
40
42
49
50class session : public responder, public actor
51{
52public:
53
55 // Object methods
56 //
57
58 // constructor of the class are left private intentionnaly
59 // this class provides a global table of session one can create, lookup or destroy objects in/from this table
60
62 virtual answer give_answer(const request & req) override;
63
64 bool has_waiting_threads() const { return lock_wui.waiting_thread(); };
65 bool has_working_server() const { return lock_wui.working_thread(); };
66 std::string get_session_ID() const { return session_ID; };
67 bool disconnection_requested() const;
68
69
71 virtual void on_event(const std::string & event_name) override;
72
73
75 // Class types and methods
76 //
77
80 {
81 std::string owner;
82 std::string session_ID;
83 std::string session_name;
84 bool locked;
86 bool closing;
87 void clear() { owner = session_ID = ""; locked = libdar_running = closing = false; };
88 };
89
90
92
99 static bool create_new_session(const std::string & user, bool initial, const request & req, answer & ret);
100
102 static unsigned int get_num_session();
103
105 static unsigned int get_num_session(const std::string & user);
106
108 static std::vector<session_summary> get_summary();
109
111 static bool get_session_info(const std::string & session_ID, session_summary & val);
112
114
119 static session *acquire_session(const std::string & session_ID);
120
122
126 static void release_session(session *sess);
127
129
132 static bool close_session(const std::string & session_ID);
133
134private:
136 session();
137 session(const session & ref) = delete;
138 session(session && ref) = delete;
139 session & operator = (const session & ref) = delete;
140 session & operator = (session && ref) = delete;
141 ~session() = default;
142
144 void set_session_id(const std::string & sessid);
145
146 libthreadar::semaphore lock_wui;
147 user_interface wui;
148 pthread_t tid;
149 std::string session_ID;
150
151 void check_caller() const; //< test whether the caller has properly acquired the lock on this object
152
153
155 // static types, variables and methods
156 //
157
159 struct table
160 {
161 std::string owner;
163 unsigned int ref_given;
164 bool closing;
165 void clear() { owner = ""; reference = nullptr; ref_given = 0; closing = false; };
166 };
167
168 static libthreadar::mutex lock_running;
169 static std::map<std::string, table> running_session;
170 static session_summary publish(std::map<std::string, table>::iterator it);
171 static std::string create_new(const std::string & owner);
172};
173
174#endif
class of object that are pointed/triggered to by others
Definition: actor.hpp:55
class answer provides easy means to set an HTTP answer and means to sent it back to a proto_connexion...
Definition: answer.hpp:49
class reference gives a mean to link objects by a peering method
Definition: reference.hpp:47
reference()
usual constructor
Definition: reference.hpp:51
class holding fields of an HTTP request (method, URI, header, cookies, and so on)
Definition: request.hpp:45
pure virtual class, base class for all classes that can generate an answer to a request
Definition: responder.hpp:46
class session - holds information about a current user session
Definition: session.hpp:51
static unsigned int get_num_session()
get the total number of session (all users)
Definition: session.cpp:101
virtual void on_event(const std::string &event_name) override
inherited from actor parent class
Definition: session.cpp:86
static session * acquire_session(const std::string &session_ID)
acquire the excusivity use of the session which id is provided in argument
Definition: session.cpp:263
static bool get_session_info(const std::string &session_ID, session_summary &val)
get a summary description of the session known from its session_ID
Definition: session.cpp:174
static std::vector< session_summary > get_summary()
get a summary description vector of all sessions
Definition: session.cpp:145
virtual answer give_answer(const request &req) override
inherited from responder
Definition: session.cpp:74
static bool close_session(const std::string &session_ID)
request the session to be tear down and destoyed
Definition: session.cpp:340
static bool create_new_session(const std::string &user, bool initial, const request &req, answer &ret)
create a new session
Definition: session.cpp:410
static void release_session(session *sess)
release the session object from our exclusive access
Definition: session.cpp:298
main webdar html components that defines for a given session the type of output (config pages,...
Definition: user_interface.hpp:68
defines the event class
summary information of sessions
Definition: session.hpp:80
std::string session_name
name given to that session by the user
Definition: session.hpp:83
std::string session_ID
session ID: internal unique identifier for that session
Definition: session.hpp:82
std::string owner
owner of the session
Definition: session.hpp:81
bool closing
whether the session is pending for closure
Definition: session.hpp:86
bool libdar_running
whether a libdar job is running
Definition: session.hpp:85
bool locked
whether the session is locked by a thread
Definition: session.hpp:84