1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zennet/beacon.h>
#include <zencore/basicfile.h>
#include <zencore/compactbinary.h>
#include <zencore/compactbinaryfile.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/session.h>
#include <zencore/uid.h>
#include <fmt/format.h>
#include <asio.hpp>
#include <map>
namespace zen {
//////////////////////////////////////////////////////////////////////////
struct FsBeacon::Impl
{
Impl(std::filesystem::path ShareRoot);
~Impl();
void EnsureValid();
void AddGroup(std::string_view GroupId, CbObject Metadata);
void ScanGroup(std::string_view GroupId, std::vector<Oid>& OutSessions);
void ReadMetadata(std::string_view GroupId, const std::vector<Oid>& InSessions, std::vector<CbObject>& OutMetadata);
private:
std::filesystem::path m_ShareRoot;
zen::Oid m_SessionId;
struct GroupData
{
CbObject Metadata;
BasicFile LockFile;
};
std::map<std::string, GroupData> m_Registration;
std::filesystem::path GetSessionMarkerPath(std::string_view GroupId, const Oid& SessionId)
{
Oid::String_t SessionIdString;
SessionId.ToString(SessionIdString);
return m_ShareRoot / GroupId / SessionIdString;
}
};
FsBeacon::Impl::Impl(std::filesystem::path ShareRoot) : m_ShareRoot(ShareRoot), m_SessionId(GetSessionId())
{
}
FsBeacon::Impl::~Impl()
{
}
void
FsBeacon::Impl::EnsureValid()
{
}
void
FsBeacon::Impl::AddGroup(std::string_view GroupId, CbObject Metadata)
{
zen::CreateDirectories(m_ShareRoot / GroupId);
std::filesystem::path MarkerFile = GetSessionMarkerPath(GroupId, m_SessionId);
GroupData& Group = m_Registration[std::string(GroupId)];
Group.Metadata = Metadata;
std::error_code Ec;
Group.LockFile.Open(MarkerFile,
BasicFile::Mode::kTruncate | BasicFile::Mode::kPreventDelete |
BasicFile::Mode::kPreventWrite /* | BasicFile::Mode::kDeleteOnClose */,
Ec);
if (Ec)
{
throw std::system_error(Ec, fmt::format("failed to open beacon marker file '{}' for write", MarkerFile));
}
Group.LockFile.WriteAll(Metadata.GetBuffer().AsIoBuffer(), Ec);
if (Ec)
{
throw std::system_error(Ec, fmt::format("failed to write to beacon marker file '{}'", MarkerFile));
}
Group.LockFile.Flush();
}
void
FsBeacon::Impl::ScanGroup(std::string_view GroupId, std::vector<Oid>& OutSessions)
{
DirectoryContent Dc;
zen::GetDirectoryContent(m_ShareRoot / GroupId, zen::DirectoryContentFlags::IncludeFiles, /* out */ Dc);
for (const std::filesystem::path& FilePath : Dc.Files)
{
std::filesystem::path File = FilePath.filename();
std::error_code Ec;
if (std::filesystem::remove(FilePath, Ec) == false)
{
auto FileString = File.generic_string();
if (FileString.length() != Oid::StringLength)
continue;
if (const Oid SessionId = Oid::FromHexString(FileString))
{
if (std::filesystem::file_size(File, Ec) > 0)
{
OutSessions.push_back(SessionId);
}
}
}
}
}
void
FsBeacon::Impl::ReadMetadata(std::string_view GroupId, const std::vector<Oid>& InSessions, std::vector<CbObject>& OutMetadata)
{
for (const Oid& SessionId : InSessions)
{
const std::filesystem::path MarkerFile = GetSessionMarkerPath(GroupId, SessionId);
if (CbObject Metadata = LoadCompactBinaryObject(MarkerFile).Object)
{
OutMetadata.push_back(std::move(Metadata));
}
}
}
//////////////////////////////////////////////////////////////////////////
FsBeacon::FsBeacon(std::filesystem::path ShareRoot) : m_Impl(std::make_unique<Impl>(ShareRoot))
{
}
FsBeacon::~FsBeacon()
{
}
void
FsBeacon::AddGroup(std::string_view GroupId, CbObject Metadata)
{
m_Impl->AddGroup(GroupId, Metadata);
}
void
FsBeacon::ScanGroup(std::string_view GroupId, std::vector<Oid>& OutSessions)
{
m_Impl->ScanGroup(GroupId, OutSessions);
}
void
FsBeacon::ReadMetadata(std::string_view GroupId, const std::vector<Oid>& InSessions, std::vector<CbObject>& OutMetadata)
{
m_Impl->ReadMetadata(GroupId, InSessions, OutMetadata);
}
//////////////////////////////////////////////////////////////////////////
} // namespace zen
|