aboutsummaryrefslogtreecommitdiff
path: root/src/zencompute/httporchestrator.cpp
blob: 39e7e60d79640863d2d269174af6395ba3f080b9 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zencompute/httporchestrator.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/logging.h>

namespace zen::compute {

HttpOrchestratorService::HttpOrchestratorService() : m_Log(logging::Get("orch"))
{
	m_Router.RegisterRoute(
		"provision",
		[this](HttpRouterRequest& Req) {
			HttpServerRequest& HttpReq = Req.ServerRequest();

			CbObjectWriter Cbo;
			Cbo.BeginArray("workers");

			m_KnownWorkersLock.WithSharedLock([&] {
				for (const auto& [WorkerId, Worker] : m_KnownWorkers)
				{
					Cbo.BeginObject();
					Cbo << "uri" << Worker.BaseUri;
					Cbo << "dt" << Worker.LastSeen.GetElapsedTimeMs();
					Cbo.EndObject();
				}
			});

			Cbo.EndArray();

			HttpReq.WriteResponse(HttpResponseCode::OK, Cbo.Save());
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"announce",
		[this](HttpRouterRequest& Req) {
			HttpServerRequest& HttpReq = Req.ServerRequest();

			CbObject Data = HttpReq.ReadPayloadObject();

			std::string_view WorkerId  = Data["id"].AsString("");
			std::string_view WorkerUri = Data["uri"].AsString("");

			if (WorkerId.empty() || WorkerUri.empty())
			{
				return HttpReq.WriteResponse(HttpResponseCode::BadRequest);
			}

			m_KnownWorkersLock.WithExclusiveLock([&] {
				auto& Worker   = m_KnownWorkers[std::string(WorkerId)];
				Worker.BaseUri = WorkerUri;
				Worker.LastSeen.Reset();
			});

			HttpReq.WriteResponse(HttpResponseCode::OK);
		},
		HttpVerb::kPost);
}

HttpOrchestratorService::~HttpOrchestratorService()
{
}

const char*
HttpOrchestratorService::BaseUri() const
{
	return "/orch/";
}

void
HttpOrchestratorService::HandleRequest(HttpServerRequest& Request)
{
	if (m_Router.HandleRequest(Request) == false)
	{
		ZEN_WARN("No route found for {0}", Request.RelativeUri());
	}
}

}  // namespace zen::compute