-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainerNode.cs
172 lines (137 loc) · 4.04 KB
/
ContainerNode.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.IO;
using Godot;
using Directory = System.IO.Directory;
using Path = System.IO.Path;
using Thread = System.Threading.Thread;
public class ContainerNode : Spatial
{
private Dictionary<string, StreamWriter> _peripheralWriters = new Dictionary<string, StreamWriter>();
private readonly Dictionary<string, Node> _peripheralNodes = new Dictionary<string, Node>();
private readonly List<string> _peripherals = new List<string>();
private bool _running;
[Export(PropertyHint.MultilineText)] public string Dockerfile = "";
[Export]
public string WorkingDirectoryName = "";
private string _workingDirectory;
[Export] public string Id = "";
[Export] public NodePath Peripheral1 = null;
[Export] public NodePath Peripheral2 = null;
[Export] public NodePath Peripheral3 = null;
public override void _EnterTree()
{
base._EnterTree();
// Generate custom ID, if not provided
if (string.IsNullOrEmpty(Id))
Id = Math.Abs(GetHashCode()).ToString();
if (!string.IsNullOrEmpty(WorkingDirectoryName))
{
_workingDirectory = Path.Combine(GameFiles.UserDirectoryPath, "workingd", WorkingDirectoryName);
if (!Directory.Exists(_workingDirectory))
Directory.CreateDirectory(_workingDirectory);
}
// Register peripherals
foreach (var nodePath in new[]{Peripheral1, Peripheral2, Peripheral3})
{
if (nodePath == null)
continue;
var node = GetNode(nodePath);
var id = node.Name;
_peripherals.Add(id);
_peripheralNodes.Add(id, node);
}
// Boot VM if not booted already
if (BridgeNode.DryMode == false && BridgeNode.Attached == false)
{
GetTree().Root.CallDeferred("add_child", new BridgeNode());
BridgeNode.PreStart();
BridgeNode.Attached = true;
}
}
public override void _Ready()
{
if (BridgeNode.DryMode)
{
// Fake initialize peripherals
foreach (var pair in _peripheralNodes)
{
var node = pair.Value;
var id = pair.Key;
node.Call("peripheral_initialization", id, GD.FuncRef(this, "_sendData"));
}
return;
}
var status = BridgeNode.ContainerApi.CreateContainer(Id, _workingDirectory, Dockerfile, _peripherals);
if (status != 0)
{
GD.PrintErr($"Container failed to create! - {(Errors) status}");
}
status = BridgeNode.ContainerApi.StartContainer(Id);
if (status != 0)
{
GD.PrintErr($"Failed to start container! - {(Errors) status}");
}
foreach (var pair in _peripheralNodes)
{
var node = pair.Value;
var id = pair.Key;
var writer = BridgeNode.ContainerApi.GetPeripheralOutgoingStream(Id, id);
_peripheralWriters.Add(id, writer);
node.Call("peripheral_initialization", id, GD.FuncRef(this, "_sendData"));
}
_startStreams();
}
public override void _ExitTree()
{
_running = false;
}
/// <summary>
/// Sends a singular command to a container
/// </summary>
/// <param name="command"></param>
public void HotCode(string command)
{
if (BridgeNode.DryMode)
{
GD.PrintErr("Dry Mode active, refusing to execute HotCode.");
return;
}
BridgeNode.ContainerApi.CreateTTY(Id, out var stdin, out var stdout);
stdin.WriteLine(command);
stdin.Close();
stdout.Close();
}
// --------------------
// THIS mess below is taking care of input and output streams, do not know how to make it better
// --------------------
private void _sendData(string id, string message)
{
if (BridgeNode.DryMode)
return;
_peripheralWriters[id].WriteLine(message);
}
private void _startStreams()
{
_running = true;
foreach (var peripheral in _peripherals)
{
var thread = new Thread(() => _streamRead(peripheral));
thread.Start();
}
}
private void _streamRead(string peripheral)
{
while (_running)
{
var ingoing = BridgeNode.ContainerApi.GetPeripheralIngoingStream(Id, peripheral);
while (ingoing != null && !ingoing.EndOfStream)
{
var i = ingoing.Read().ToString();
GD.Print("[Peripheral] Sending " + i);
_peripheralNodes[peripheral].Call("peripheral_receive", i);
}
GD.Print("[Peripheral] Lost stream, recreating...");
}
}
}