-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
188 lines (167 loc) · 5.35 KB
/
Program.cs
File metadata and controls
188 lines (167 loc) · 5.35 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using Rcl;
using TlarcKernel.Init;
using TlarcKernel.IO;
using TlarcKernel.IO.ProcessCommunicateInterfaces;
using TlarcKernel.IO.TlarcMsgs;
namespace TlarcKernel
{
static class Program
{
static Dictionary<uint, Process> Processes = [];
static Dictionary<uint, ComponentCell> Components = [];
static Dictionary<string, IPublisher> CommunicatorInterface = [];
static Dictionary<Type, uint> LastInstance = [];
static void Main(string[] args)
{
ThreadPool.SetMinThreads(10, 10);
GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
Ros2Def.context = new RclContext(args);
Ros2Def.node = Ros2Def.context.CreateNode("tlarc");
#if DEBUG
Console.WriteLine(
@"
________ ___ ____ ______
/_ __/ / / | / __ \/ ____/
/ / / / / /| | / /_/ / /
/ / / /___/ ___ |/ _, _/ /___
/_/ /_____/_/ |_/_/ |_|\____/
================================ @Alray"
);
Console.WriteLine(
"[Enter] to use debug.yaml, or input your config files name, split with [Space]:"
);
Console.WriteLine("Usage: [Option] filename1.yaml [filenames2.yaml ...]");
Console.WriteLine("\t Option: --debug (default) : log info,warning,errors in 1k hz");
Console.WriteLine("\t Option: --profile : log times in each process or user log in 1 hz");
args = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
#endif
bool profile = false;
if (args[0] == "--profile")
{
profile = true;
ProcessInit.Init(args[1..], ref Processes, ref Components, ref LastInstance);
}
else
ProcessInit.Init(args, ref Processes, ref Components, ref LastInstance);
StageConstruct();
Awake();
BuildInterfaceTable();
BindInterface();
Start();
while (true)
{
#if DEBUG
if (!profile)
{
#endif
if (TlarcSystem.TryGetPrint(out var a))
a();
Thread.Sleep(1);
#if DEBUG
}
else
{
string times = "";
foreach (var process in Processes.Values)
{
times += $"\n======================\n Process@ {process.Pid}\n";
foreach (var (name, time) in process.Times)
{
times += $"\t\"{name}\":".PadRight(100, ' ') + $"{time}\n";
}
}
foreach (var (name, timers) in TlarcSystem.TryGetTimer())
{
times += $"\n======================\n Component@ {name}\n";
foreach (var (desc, time) in timers)
{
times += $"\t\"{desc}\":".PadRight(100, ' ') + $"{time}\n";
}
}
Console.Clear();
Console.WriteLine(times);
Thread.Sleep(1000);
}
#endif
}
}
static void Awake()
{
foreach (var i in Components.Values)
i.Awake();
}
static void BindInterface()
{
foreach (var i in Components.Values)
i.BindInterface();
}
static void Start()
{
foreach (var i in Processes.Values)
i.Start();
}
public static void BuildInterfaceTable()
{
foreach (var c in Components.Values)
foreach (
var p in c
.Component.GetType()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
)
if (p.FieldType.GetInterfaces().Any(x => typeof(IPublisher).IsAssignableFrom(x)))
CommunicatorInterface[(p.GetValue(c.Component) as ICommunicateInterface).InterfaceName] =
p.GetValue(c.Component) as IPublisher;
}
public static void StageConstruct()
{
foreach (var c in Components.Values)
{
c.AutoSetReceiveID();
foreach (var id in c.Component.ReceiveID.Values)
{
if (Processes[c.Component.ProcessID].Components.Keys.Contains(id))
continue;
var componentC =
Components[id]
.Component.GetType()
.Assembly.CreateInstance(Components[id].Component.GetType().FullName)
?? throw new Exception();
var componentI =
Components[id]
.Component.GetType()
.Assembly.CreateInstance(Components[id].Component.GetType().FullName)
?? throw new Exception();
var change1 = new CopyWithExpressions();
change1.Copy(componentC, componentI);
change1.NewCopy(componentI, componentC);
Components[id].Component.IOManager.CopyForOut.Add((Components[id].Component, change1));
c.Component.IOManager.CopyForIn.Add((componentI, change1));
Processes[c.Component.ProcessID]
.Components.Add(id, new(componentI as Component) { Image = true });
}
}
}
//=====================
public static Process GetProcessWithPID(uint id)
{
return Processes[id];
}
public static uint GetInstanceWithType(Type type)
{
if (!LastInstance.TryGetValue(type, out var publisher))
LastInstance.TryGetValue(
LastInstance.Keys.FirstOrDefault(type.IsAssignableFrom),
out publisher
);
return publisher;
}
public static IPublisher? GetInterfaceWithName(string name)
{
CommunicatorInterface.TryGetValue(name, out var publisher);
return publisher;
}
}
}