DDS  ver. 3.6
SessionIDFile.h
Go to the documentation of this file.
1 // Copyright 2017 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef _DDS_SESSIONIDFILE_
6 #define _DDS_SESSIONIDFILE_
7 // BOOST
8 #include <boost/filesystem.hpp>
9 #include <boost/uuid/uuid.hpp>
10 #include <boost/uuid/uuid_generators.hpp>
11 #include <boost/uuid/uuid_io.hpp>
12 
13 namespace dds::misc
14 {
16  {
17  public:
19  : m_bLocked(false)
20  {
21  }
22  CSessionIDFile(const std::string& _sidFile)
23  : m_pathSIDFile(_sidFile)
24  , m_bLocked(false)
25  , m_sid(boost::uuids::nil_uuid())
26  {
27  }
29  {
30  if (m_bLocked)
31  unlock();
32  }
33 
34  boost::uuids::uuid generate()
35  {
36  m_sid = boost::uuids::random_generator()();
37  return m_sid;
38  }
39 
40  std::string string()
41  {
42  return boost::lexical_cast<std::string>(m_sid);
43  }
44 
45  void lock(const boost::uuids::uuid& _sid, const std::string& _sidFile = "")
46  {
47  if (!_sidFile.empty())
48  m_pathSIDFile = _sidFile;
49 
50  std::ofstream f(m_pathSIDFile.string());
51  f << _sid;
52  f.close();
53  m_bLocked = true;
54  }
55 
56  void unlock()
57  {
58  if (boost::filesystem::is_regular_file(m_pathSIDFile))
59  boost::filesystem::remove(m_pathSIDFile);
60 
61  m_bLocked = false;
62  }
63 
64  std::string getLockedSID()
65  {
66  if (!boost::filesystem::is_regular_file(m_pathSIDFile))
67  return std::string();
68  std::string sid;
69  std::ifstream f(m_pathSIDFile.string());
70  f >> sid;
71 
72  return sid;
73  }
74 
75  private:
76  boost::filesystem::path m_pathSIDFile;
77  bool m_bLocked;
78  boost::uuids::uuid m_sid;
79  };
80 } // namespace dds::misc
81 #endif /* defined(_DDS_SESSIONIDFILE_) */
void unlock()
Definition: SessionIDFile.h:56
~CSessionIDFile()
Definition: SessionIDFile.h:28
Definition: SessionIDFile.h:15
void lock(const boost::uuids::uuid &_sid, const std::string &_sidFile="")
Definition: SessionIDFile.h:45
std::string string()
Definition: SessionIDFile.h:40
boost::uuids::uuid generate()
Definition: SessionIDFile.h:34
CSessionIDFile()
Definition: SessionIDFile.h:18
std::string getLockedSID()
Definition: SessionIDFile.h:64
CSessionIDFile(const std::string &_sidFile)
Definition: SessionIDFile.h:22
Definition: BoostHelper.h:14