This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMatcher.cs
63 lines (56 loc) · 2.52 KB
/
Matcher.cs
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
using System;
using Grpc.Core;
using Improbable.OnlineServices.Proto.Gateway;
using Improbable.SpatialOS.Deployment.V1Alpha1;
using Improbable.SpatialOS.Platform.Common;
namespace Improbable.OnlineServices.Base.Matcher
{
public abstract class Matcher
{
private const string SpatialRefreshTokenEnvironmentVariable = "SPATIAL_REFRESH_TOKEN";
private const string GatewayServiceTargetEnvironmentVariable = "GATEWAY_SERVICE_TARGET";
private readonly GatewayInternalService.GatewayInternalServiceClient _gatewayClient;
private readonly DeploymentServiceClient _spatialDeploymentClient;
private volatile bool _running;
protected Matcher()
{
var spatialRefreshToken = Environment.GetEnvironmentVariable(SpatialRefreshTokenEnvironmentVariable) ??
throw new Exception(
$"{SpatialRefreshTokenEnvironmentVariable} environment variable is required.");
_spatialDeploymentClient =
DeploymentServiceClient.Create(credentials: new PlatformRefreshTokenCredential(spatialRefreshToken));
_gatewayClient =
new GatewayInternalService.GatewayInternalServiceClient(new Channel(
Environment.GetEnvironmentVariable(GatewayServiceTargetEnvironmentVariable)
?? throw new Exception(
$"Environment variable {GatewayServiceTargetEnvironmentVariable} is required."),
ChannelCredentials.Insecure));
}
public void Start()
{
_running = true;
while (_running)
{
try
{
DoMatch(_gatewayClient, _spatialDeploymentClient);
}
catch (Exception e)
{
Console.WriteLine($"Uncaught exception, shutting down: {e.Message}:\n{e.StackTrace}");
_running = false;
break;
}
}
DoShutdown(_gatewayClient, _spatialDeploymentClient);
}
public void Stop()
{
_running = false;
}
protected abstract void DoMatch(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
DeploymentServiceClient deploymentServiceClient);
protected abstract void DoShutdown(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
DeploymentServiceClient deploymentServiceClient);
}
}