aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/sessions/sessions.cpp
blob: d919db6e996a08117fac84f5be5186f18b9d1a4c (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "sessions.h"

#include <zencore/basicfile.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>

namespace zen {
using namespace std::literals;

class SessionLog : public TRefCounted<SessionLog>
{
public:
	SessionLog(std::filesystem::path LogFilePath) { m_LogFile.Open(LogFilePath, BasicFile::Mode::kWrite); }

private:
	BasicFile m_LogFile;
};

class SessionLogStore
{
public:
	SessionLogStore(std::filesystem::path StoragePath) : m_StoragePath(std::move(StoragePath)) {}

	~SessionLogStore() = default;

	Ref<SessionLog> GetLogForSession(const Oid& SessionId)
	{
		// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
		return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
	}

	Ref<SessionLog> CreateLogForSession(const Oid& SessionId)
	{
		// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
		return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
	}

private:
	std::filesystem::path m_StoragePath;
};

SessionsService::Session::Session(const SessionInfo& Info) : m_Info(Info)
{
}
SessionsService::Session::~Session() = default;

//////////////////////////////////////////////////////////////////////////

SessionsService::SessionsService() : m_Log(logging::Get("sessions"))
{
}

SessionsService::~SessionsService() = default;

bool
SessionsService::RegisterSession(const Oid& SessionId, std::string AppName, const Oid& JobId, CbObjectView Metadata)
{
	RwLock::ExclusiveLockScope Lock(m_Lock);

	if (m_Sessions.contains(SessionId))
	{
		return false;
	}

	ZEN_INFO("Session {} registered (AppName: {}, JobId: {})", SessionId, AppName, JobId);

	const DateTime Now = DateTime::Now();
	m_Sessions.emplace(SessionId,
					   Ref(new Session(SessionInfo{.Id		  = SessionId,
												   .AppName	  = std::move(AppName),
												   .JobId	  = JobId,
												   .Metadata  = CbObject::Clone(Metadata),
												   .CreatedAt = Now,
												   .UpdatedAt = Now})));
	return true;
}

bool
SessionsService::UpdateSession(const Oid& SessionId, CbObjectView Metadata)
{
	RwLock::ExclusiveLockScope Lock(m_Lock);

	auto It = m_Sessions.find(SessionId);
	if (It == m_Sessions.end())
	{
		return false;
	}

	It.value()->UpdateMetadata(Metadata);

	const SessionInfo& Info = It.value()->Info();
	ZEN_DEBUG("Session {} updated (AppName: {}, JobId: {})", SessionId, Info.AppName, Info.JobId);
	return true;
}

Ref<SessionsService::Session>
SessionsService::GetSession(const Oid& SessionId) const
{
	RwLock::SharedLockScope Lock(m_Lock);

	auto It = m_Sessions.find(SessionId);
	if (It == m_Sessions.end())
	{
		return {};
	}

	return It->second;
}

std::vector<Ref<SessionsService::Session>>
SessionsService::GetSessions() const
{
	RwLock::SharedLockScope Lock(m_Lock);

	std::vector<Ref<Session>> Result;
	Result.reserve(m_Sessions.size());
	for (const auto& [Id, SessionRef] : m_Sessions)
	{
		Result.push_back(SessionRef);
	}
	return Result;
}

bool
SessionsService::RemoveSession(const Oid& SessionId)
{
	RwLock::ExclusiveLockScope Lock(m_Lock);

	auto It = m_Sessions.find(SessionId);
	if (It == m_Sessions.end())
	{
		return false;
	}

	ZEN_INFO("Session {} removed (AppName: {}, JobId: {})", SessionId, It.value()->Info().AppName, It.value()->Info().JobId);

	m_Sessions.erase(It);
	return true;
}

uint64_t
SessionsService::GetSessionCount() const
{
	RwLock::SharedLockScope Lock(m_Lock);
	return m_Sessions.size();
}

}  // namespace zen