aboutsummaryrefslogtreecommitdiff
path: root/src/zen/zenserviceclient.cpp
blob: 255a028becceda7c60bc94a5fcf640aa47e47ab4 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenserviceclient.h"

#include "zen.h"

#include <zencore/logging.h>
#include <zencore/logging/broadcastsink.h>
#include <zencore/session.h>
#include <zenutil/logging.h>

namespace zen {

ZenServiceClient::ZenServiceClient(Options Opts)
: m_HostSpec(ZenCmdBase::ResolveTargetHostSpec(Opts.HostSpec))
, m_Http(ZenCmdBase::CreateHttpClient(m_HostSpec, Opts.HttpSettings))
{
	if (m_HostSpec.empty())
	{
		throw OptionParseException("Unable to resolve server specification", {});
	}

	SessionsServiceClient::Options SessionOpts{
		.TargetUrl		 = m_HostSpec,
		.AppName		 = "zen",
		.Mode			 = std::move(Opts.CommandName),
		.SessionId		 = GetSessionId(),
		.ParentSessionId = GetParentSessionId(),
	};

	// For unix socket connections, forward the socket path to the sessions client
	if (ZenCmdBase::IsUnixSocketSpec(m_HostSpec))
	{
		SessionOpts.TargetUrl					  = "http://localhost";
		SessionOpts.ClientSettings.UnixSocketPath = m_HostSpec.substr(7);  // strip "unix://"
	}

	m_Sessions = std::make_unique<SessionsServiceClient>(std::move(SessionOpts));
	m_Sessions->Announce();

	m_LogSink = m_Sessions->CreateLogSink();
	GetDefaultBroadcastSink()->AddSink(m_LogSink);
}

ZenServiceClient::~ZenServiceClient()
{
	if (m_LogSink)
	{
		if (Ref<logging::BroadcastSink> Broadcast = GetDefaultBroadcastSink())
		{
			Broadcast->RemoveSink(m_LogSink);
		}
	}
}

bool
ZenServiceClient::IsUnixSocket() const
{
	return ZenCmdBase::IsUnixSocketSpec(m_HostSpec);
}

}  // namespace zen