-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathProgram.cs
148 lines (125 loc) · 5.25 KB
/
Program.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
142
143
144
145
146
147
148
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Iviz.Msgs.IvizMsgs;
using Iviz.Roslib;
using Iviz.Roslib.Utils;
using Iviz.Tools;
namespace Iviz.ModelService
{
public static class Program
{
static async Task Main(string[] args)
{
try
{
await MainImpl(args);
}
catch (Exception e)
{
Console.Error.WriteLine("EE Fatal error: " + Logger.ExceptionToString(e));
}
}
static async Task MainImpl(string[] args)
{
Console.WriteLine("** Starting Iviz.ModelService...");
var masterUri = RosClient.EnvironmentMasterUri;
if (masterUri is null)
{
Console.Error.WriteLine("EE Fatal error: Failed to determine master uri. " +
"Try setting ROS_MASTER_URI to the address of the master.");
return;
}
bool enableFileSchema = false;
bool verbose = false;
var additionalPaths = new StringBuilder();
foreach (string arg in args)
{
if (arg == "--enable-file-schema" && !enableFileSchema)
{
enableFileSchema = true;
Console.Error.WriteLine("WW Uris starting with 'file://' are now accepted. " +
"This makes all your files available to the outside.");
}
else if (arg is "--verbose" or "-v")
{
verbose = true;
}
else
{
additionalPaths.Append(arg);
}
}
if (await GetPathExtras() is { } paths)
{
additionalPaths.Append(additionalPaths.Length == 0 ? paths : (":" + paths));
}
using var modelServer = new ModelServer(additionalPaths.ToString(), enableFileSchema, verbose);
if (modelServer.NumPackages == 0)
{
return;
}
Console.WriteLine($"** Found {modelServer.NumPackages} ROS packages. Starting node...");
var myUri = RosClient.TryGetCallerUriFor(masterUri) ?? RosClient.TryGetCallerUri();
await using RosClient client = await RosClient.CreateAsync(masterUri, "iviz_model_service", myUri);
Console.WriteLine($"** Started node at URI {client.CallerUri}");
//Console.WriteLine("** Starting service {0} [{1}].", ModelServer.ModelServiceName,
// GetModelResource.ServiceType);
//Console.WriteLine("** Starting service {0} [{1}].", ModelServer.ModelServiceName,
// GetModelResource.ServiceType);
await client.AdvertiseServiceAsync<GetModelResource>(ModelServer.ModelServiceName,
modelServer.ModelCallback);
//Console.WriteLine("** Starting service {0} [{1}].", ModelServer.TextureServiceName,
// GetModelTexture.ServiceType);
await client.AdvertiseServiceAsync<GetModelTexture>(ModelServer.TextureServiceName,
modelServer.TextureCallback);
//Console.WriteLine("** Starting service {0} [{1}].", ModelServer.FileServiceName, GetFile.ServiceType);
await client.AdvertiseServiceAsync<GetFile>(ModelServer.FileServiceName, modelServer.FileCallback);
//Console.WriteLine("** Starting service {0} [{1}].", ModelServer.SdfServiceName, GetSdf.ServiceType);
await client.AdvertiseServiceAsync<GetSdf>(ModelServer.SdfServiceName, modelServer.SdfCallback);
Console.WriteLine("** Iviz.ModelService started. Standing by for requests...");
await RosEventHandler.WaitForCancelAsync();
Console.WriteLine();
}
static async Task<string?> GetPathExtras()
{
string? homeFolder;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
case PlatformID.MacOSX:
homeFolder = Environment.GetEnvironmentVariable("HOME");
break;
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
homeFolder = Environment.GetEnvironmentVariable("%HOMEDRIVE%%HOMEPATH%");
break;
default:
return null;
}
if (homeFolder == null)
{
return null;
}
string extrasPath = homeFolder + "/.iviz/ros_package_path";
if (!File.Exists(extrasPath))
{
return null;
}
try
{
Console.WriteLine($"** Checking file '{extrasPath}' for additional ROS paths...");
return (await File.ReadAllTextAsync(extrasPath)).Trim();
}
catch (IOException e)
{
Logger.LogErrorFormat("EE ROS package file '{0}' exists but could not be read. Reason: {1}",
extrasPath, e.Message);
return null;
}
}
}
}