Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafSession.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) 2022- Kontur AS
5 //
6 // GNU Lesser General Public License Usage
7 // This library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
17 // for more details.
18 //
19 #pragma once
20 
21 #include <chrono>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 
26 namespace caffa
27 {
28 class SessionMaintainer;
29 class ConstSessionMaintainer;
34 class Session
35 {
36 public:
37  enum class Type
38  {
39  INVALID = 0x0,
40  REGULAR = 0x1,
41  OBSERVING = 0x2
42  };
43 
44  static std::shared_ptr<Session> create( Type type,
45  std::chrono::milliseconds timeout = std::chrono::milliseconds( 1000 ) );
46 
47  ~Session() = default;
48 
49  const std::string& uuid() const;
50 
51  Type type() const;
52  void setType( Type type );
53 
54  bool isExpired() const;
55  void updateKeepAlive();
56 
57  static Type typeFromUint( unsigned type );
58 
59  std::chrono::milliseconds timeout() const;
60 
61 private:
62  friend class SessionMaintainer;
63  friend class ConstSessionMaintainer;
64 
65  Session( Type type, std::chrono::milliseconds timeout );
66 
67  bool unlockedIsExpired() const;
68  void blockExpiration() const;
69  void unblockExpiration() const;
70 
71  const std::string m_uuid;
72  Type m_type;
73  const std::chrono::milliseconds m_timeOut;
74 
75  mutable std::chrono::steady_clock::time_point m_lastKeepAlive;
76  mutable std::mutex m_mutex;
77  mutable bool m_expirationBlocked;
78 };
79 
81 {
82 public:
83  SessionMaintainer( std::shared_ptr<Session> session = nullptr );
85 
86  std::shared_ptr<Session> operator->();
87  operator bool() const;
88  bool operator!() const;
89  Session* get();
90 
91 private:
92  std::shared_ptr<Session> m_session;
93 };
94 
96 {
97 public:
98  ConstSessionMaintainer( std::shared_ptr<const Session> session = nullptr );
100 
101  std::shared_ptr<const Session> operator->() const;
102  operator bool() const;
103  bool operator!() const;
104  const Session* get() const;
105 
106 private:
107  std::shared_ptr<const Session> m_session;
108 };
109 
110 } // namespace caffa
Definition: cafSession.h:96
Definition: cafSession.h:81
Abstract class representing an application session.
Definition: cafSession.h:35
Main Caffa namespace.
Definition: __init__.py:1