This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
Worker.cs
132 lines (114 loc) · 4.71 KB
/
Worker.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
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Improbable.Worker.CInterop;
namespace Improbable.Gdk.Core
{
/// <summary>
/// Represents a SpatialOS worker.
/// </summary>
public class Worker : IDisposable
{
/// <summary>
/// The type of the worker.
/// </summary>
public readonly string WorkerType;
/// <summary>
/// The worker ID.
/// </summary>
/// <remarks>
/// Unique for a given SpatialOS deployment.
/// </remarks>
public readonly string WorkerId;
/// <summary>
/// The worker attribute list
/// </summary>
public readonly List<string> Attributes;
/// <summary>
/// The logger for this worker.
/// </summary>
public ILogDispatcher LogDispatcher;
/// <summary>
/// Denotes whether this worker is connected or not.
/// </summary>
public bool IsConnected => ConnectionHandler.IsConnected();
protected IConnectionHandler ConnectionHandler;
// todo replace internal with real apis
internal readonly View View;
internal ViewDiff ViewDiff;
internal MessagesToSend MessagesToSend;
protected Worker(IConnectionHandler connectionHandler, string workerType, ILogDispatcher logDispatcher)
{
ConnectionHandler = connectionHandler;
WorkerType = workerType;
WorkerId = connectionHandler.GetWorkerId();
Attributes = connectionHandler.GetWorkerAttributes();
LogDispatcher = logDispatcher;
logDispatcher.Worker = this;
logDispatcher.WorkerType = workerType;
MessagesToSend = connectionHandler.GetMessagesToSendContainer();
View = new View();
}
/// <summary>
/// Creates a <see cref="Worker"/> object asynchronously.
/// </summary>
/// <param name="connectionHandlerBuilder">
/// A builder which describes how to create the <see cref="IConnectionHandler"/> for this worker.
/// </param>
/// <param name="workerType">The type of worker to connect as.</param>
/// <param name="logDispatcher">The logger to use for this worker.</param>
/// <param name="token">A cancellation token which will cancel this asynchronous operation</param>
/// <returns>A task which represents the asynchronous creation of a worker.</returns>
public static async Task<Worker> CreateWorkerAsync(IConnectionHandlerBuilder connectionHandlerBuilder, string workerType,
ILogDispatcher logDispatcher, CancellationToken? token = null)
{
var handler = await connectionHandlerBuilder.CreateAsync(token);
return new Worker(handler, workerType, logDispatcher);
}
/// <summary>
/// Ticks the worker. Fetches all messages received since the last <see cref="Tick"/> call and
/// applies the diff.
/// </summary>
public void Tick()
{
ConnectionHandler.GetMessagesReceived(ref ViewDiff);
View.ApplyDiff(ViewDiff);
}
public void EnsureMessagesFlushed()
{
ConnectionHandler.PushMessagesToSend(MessagesToSend);
MessagesToSend = ConnectionHandler.GetMessagesToSendContainer();
}
/// <summary>
/// Sends a log message to SpatialOS from this worker.
/// </summary>
/// <param name="logLevel">The log verbosity level.</param>
/// <param name="message">The log message.</param>
/// <param name="loggerName">A name for the sender of the log.</param>
/// <param name="entityId">
/// The <see cref="EntityId"/> to associate with the log message.
/// Set to null for no <see cref="EntityId"/>.
/// </param>
public void SendLogMessage(LogLevel logLevel, string message, string loggerName, EntityId? entityId)
{
MessagesToSend.AddLogMessage(new LogMessageToSend(message, loggerName, logLevel, entityId?.Id));
}
/// <summary>
/// Gets the value for a given worker flag.
/// </summary>
/// <param name="key">The key of the worker flag.</param>
/// <returns>The value of the flag, if it exists, null otherwise.</returns>
public string GetWorkerFlag(string key)
{
return View.GetWorkerFlag(key);
}
public virtual void Dispose()
{
ConnectionHandler?.Dispose();
ConnectionHandler = null;
LogDispatcher?.Dispose();
LogDispatcher = null;
}
}
}