-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFivetService.cs
141 lines (113 loc) · 4.08 KB
/
FivetService.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
133
134
135
136
137
138
139
140
141
using System;
using System.Threading;
using System.Threading.Tasks;
using Fivet.ZeroIce.model;
using Ice;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Fivet.Server
{
/// <summary>
/// The Fivet Service.
/// </summary>
internal class FivetService : IHostedService, IDisposable
{
/// <summary>
/// The Logger.
/// </summary>
private readonly ILogger<FivetService> _logger;
/// <summary>
/// The Port.
/// </summary>
private readonly int _port = 8080;
/// <summary>
/// The Communicator.
/// </summary>
private readonly Communicator _communicator;
/// <summary>
/// The System.
/// </summary>
private readonly TheSystemDisp_ _theSystem;
/// <summary>
/// The Contratos.
/// </summary>
private readonly ContratosDisp_ _contratos;
/// <summary>
/// The FivetService.
/// </summary>
/// <param name="logger">Used to print debug message.</param>
public FivetService(ILogger<FivetService> logger, TheSystemDisp_ theSystem, ContratosDisp_ contratos)
{
_logger = logger;
_logger.LogDebug("Building FivetService..");
_theSystem = theSystem;
_contratos = contratos;
_communicator = buildCommunicator();
}
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogDebug("Starting the FivetService ..");
// The adapter: https://doc.zeroc.com/ice/3.7/client-side-features/proxies/proxy-and-endpoint-syntax
// tcp (protocol) -z (compression) -t 15000 (timeout in ms) -p 8080 (port to bind)
var adapter = _communicator.createObjectAdapterWithEndpoints("TheSystem", "tcp -z -t 15000 -p " + _port);
// Register in the communicator
adapter.add(_theSystem, Util.stringToIdentity("TheSystem"));
// Activation
adapter.activate();
// The Delay call.
_theSystem.getDelay(0);
// All ok
return Task.CompletedTask;
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping the FivetService ..");
_communicator.shutdown();
_logger.LogDebug("Communicator Stopper!");
return Task.CompletedTask;
}
/// <summary>
/// Build the communicator.
/// </summary>
/// <returns>The Communicator</returns>
private Communicator buildCommunicator()
{
_logger.LogDebug("Initializating Communicator v{0} ({1}) ..", Ice.Util.stringVersion(), Ice.Util.intVersion());
// ZeroC properties
Properties properties = Util.createProperties();
// properties.setProperty("Ice.Trace.Network", "3");
InitializationData initializationData = new InitializationData();
initializationData.properties = properties;
return Ice.Util.initialize(initializationData);
}
/// <summary>
/// Clear the memory.
/// </summary>
public void Dispose()
{
_communicator.destroy();
}
}
/// <summary>
/// The Implementation of TheSystem interface.
/// </summary>
public class TheSystemImpl : TheSystemDisp_
{
/// <summary>
/// Return the difference in the time.
/// </summary>
/// <param name="clienTime"></param>
/// <param name="current"></param>
/// <returns>The Delay</returns>
public override long getDelay(long clientTime, Current current = null)
{
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - clientTime;
}
}
}