-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (102 loc) · 4.78 KB
/
Copy pathProgram.cs
File metadata and controls
112 lines (102 loc) · 4.78 KB
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
using ShapeDiver.SDK.Stargate;
using ShapeDiver.SDK.Stargate.Client.Interfaces;
using ShapeDiver.SDK.Stargate.Commands;
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace StargateDotNetClientExample
{
internal class Program
{
static async Task Main(string[] args)
{
// Create services (SDK, logging, utils, etc.)
var services = new Services();
if (!await services.Init())
return;
// Create Stargate client, register command handlers
using (var client = await CreateStargateClient(services))
{
// wait for user hitting ESC
Console.WriteLine("Press ESC to stop");
do
{
while (!Console.KeyAvailable)
{
Thread.Sleep(100);
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
}
}
/// <summary>
/// Create Stargate client and register command handlers.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
static async Task<IStargateClient> CreateStargateClient(IServices services)
{
try
{
// get the Stargate endpoint to use
var stargateConfig = await services.Sdk.PlatformClient.StargateApi.GetConfig();
// Note: The following selection logic could be improved by
// choosing based on geolocation.
var endpoint = stargateConfig.Endpoint.Values.FirstOrDefault();
if (endpoint == null)
{
throw new StargateException("Could not determine endpoint of Stargate service.");
}
// register with Stargate
var client = new StargateClient(endpoint, services.Sdk.PlatformClient.AuthenticationClient /*, 10 */ /* specify a low keepalive timeout for testing OnDisconnect */);
await client.InitAsync();
var clientName = "StargateDotNetClientExample";
var clientVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
var hostOs = Environment.OSVersion.VersionString;
var hostName = System.Net.Dns.GetHostName();
var userName = Environment.UserName;
var clientId = (await client.RegisterAsync(
clientName,
clientVersion,
hostOs,
hostName,
userName
)).Id;
services.LogMessage($"Registered with ShapeDiver Stargate as {clientName} {clientVersion}, host OS {hostOs}, hostname {hostName}, username {userName}.");
services.LogMessage($"Stargate client id: {clientId}.");
// some debug output to the Rhino console
client.OnDisconnect += (IStargateClient cl, StargateDisconnectEvent ev) =>
{
services.LogMessage($"StargateDisconnectEvent: {ev.Message}");
// TODO maybe automatically try to re-connect
};
client.OnError += (IStargateClient cl, StargateErrorEvent ev) =>
{
services.LogMessage($"StargateErrorEvent: {ev.Message}");
};
client.OnMessageReceived += (IStargateClient cl, StargateMessageEvent ev) =>
{
services.LogMessage($"StargateMessageEvent: {ev}");
};
// register command implementations
client.AddCommand(new StatusCommand(client, new StatusCommandHandler(services)));
client.AddCommand(new GetSupportedDataCommand(client, new GetSupportedDataCommandHandler(services)));
client.AddCommand(new PrepareModelCommand(client, new PrepareModelCommandHandler(services)));
client.AddCommand(new GetDataCommand(client, new GetDataCommandHandler(services)));
client.AddCommand(new BakeDataCommand(client, new BakeDataCommandHandler(services)));
client.AddCommand(new ExportFileCommand(client, new ExportFileCommandHandler(services)));
return client;
}
catch (StargateException ex)
{
services.LogMessage($"CreateStargateClient: A Stargate exception happened: {ex.Message}");
}
catch (Exception ex)
{
services.LogMessage($"CreateStargateClient: A generic exception happened: {ex.Message}");
}
return null;
}
}
}