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 <atomic>
22 #include <chrono>
23 #include <memory>
24 #include <shared_mutex>
25 #include <string>
26 
27 namespace caffa
28 {
29 class SessionMaintainer;
30 class ConstSessionMaintainer;
35 class Session
36 {
37 public:
38  enum class Type
39  {
40  INVALID = 0x0,
41  REGULAR = 0x1,
42  OBSERVING = 0x2
43  };
44 
45  static std::shared_ptr<Session> create( Type type,
46  std::chrono::milliseconds timeout = std::chrono::milliseconds( 1000 ) );
47 
48  ~Session() = default;
49 
50  const std::string& uuid() const;
51 
52  Type type() const;
53  void setType( Type type );
54 
55  bool isExpired() const;
56  void updateKeepAlive() const;
57 
58  static Type typeFromUint( unsigned type );
59 
60  std::chrono::milliseconds timeout() const;
61 
62 private:
63  friend class SessionMaintainer;
64  friend class ConstSessionMaintainer;
65 
66  Session( Type type, std::chrono::milliseconds timeout );
67 
68  bool unlockedIsExpired() const;
69  void blockExpiration() const;
70  void unblockExpiration() const;
71 
72  const std::string m_uuid;
73  std::atomic<Type> m_type;
74  const std::chrono::milliseconds m_timeOut;
75 
76  mutable std::atomic<std::chrono::steady_clock::time_point> m_lastKeepAlive;
77  mutable std::shared_mutex m_expirationBlockedMutex;
78  mutable bool m_expirationBlocked;
79 };
80 
82 {
83 public:
84  SessionMaintainer( std::shared_ptr<Session> session = nullptr );
86 
87  std::shared_ptr<Session> operator->();
88  std::shared_ptr<Session> operator*();
89  operator bool() const;
90  bool operator!() const;
91  Session* get();
92 
93 private:
94  std::shared_ptr<Session> m_session;
95 };
96 
98 {
99 public:
100  ConstSessionMaintainer( std::shared_ptr<const Session> session = nullptr );
102 
103  std::shared_ptr<const Session> operator->() const;
104  std::shared_ptr<const Session> operator*() const;
105  operator bool() const;
106  bool operator!() const;
107  const Session* get() const;
108 
109 private:
110  std::shared_ptr<const Session> m_session;
111 };
112 
113 } // namespace caffa
Definition: cafSession.h:81
Definition: cafSession.h:97
Abstract class representing an application session.
Definition: cafSession.h:35
Main Caffa namespace.
Definition: cafApplication.h:30