Skip to content

Commit 47f3b23

Browse files
[#78775] Add WebSocket API
1 parent 772aa97 commit 47f3b23

File tree

3 files changed

+393
-0
lines changed

3 files changed

+393
-0
lines changed

src/Renode/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ This circular dependency seems to be a problem.
7373
context.RegisterSurrogate(typeof(RobotFrameworkEngine), rf);
7474
rf.Start(options.RobotFrameworkRemoteServerPort);
7575
}
76+
#if NET
77+
if(options.ServerMode)
78+
{
79+
var wsAPI = new WebSockets.WebSocketAPI(options.ServerModeWorkDir);
80+
Emulator.BeforeExit += wsAPI.Dispose;
81+
}
82+
#endif
7683
});
7784
}
7885
}
@@ -82,6 +89,7 @@ This circular dependency seems to be a problem.
8289
}
8390
});
8491
thread.Start();
92+
8593
Emulator.ExecuteAsMainThread();
8694
}
8795

src/Renode/WebSockets/Utils.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
4+
using Antmicro.Renode.Utilities;
5+
6+
namespace Antmicro.Renode.WebSockets
7+
{
8+
public class WebSocketAPIBaseAttribute : Attribute
9+
{
10+
public WebSocketAPIBaseAttribute(string name, string version)
11+
{
12+
Name = name;
13+
Version = new Version(version);
14+
}
15+
16+
public string Name;
17+
public Version Version;
18+
}
19+
20+
public class WebSocketAPIActionAttribute : WebSocketAPIBaseAttribute
21+
{
22+
public WebSocketAPIActionAttribute(string action, string version) : base(action, version)
23+
{
24+
}
25+
}
26+
27+
public class WebSocketAPIEventAttribute : WebSocketAPIBaseAttribute
28+
{
29+
public WebSocketAPIEventAttribute(string eventName, string version) : base(eventName, version)
30+
{
31+
}
32+
}
33+
34+
public enum WebSocketAPIResponseStatus
35+
{
36+
SUCCESS,
37+
FAIL
38+
}
39+
40+
public class WebSocketAPIResponse
41+
{
42+
public object data;
43+
public string error;
44+
}
45+
46+
public interface IWebSocketAPIProvider : IAutoLoadType
47+
{
48+
49+
}
50+
51+
public delegate void WebSocketAPIEventHandler(object eventData);
52+
53+
public static class WebSocketAPIUtils
54+
{
55+
public static WebSocketAPIResponse CreateActionResponse(object response, string errorMessage = null)
56+
{
57+
return new WebSocketAPIResponse
58+
{
59+
data = response,
60+
error = errorMessage
61+
};
62+
}
63+
64+
public static WebSocketAPIResponse CreateEmptyActionResponse(string errorMessage = null)
65+
{
66+
return new WebSocketAPIResponse
67+
{
68+
data = new object(),
69+
error = errorMessage
70+
};
71+
}
72+
73+
public static void RaiseEvent(this WebSocketAPIEventHandler webSocketEvent, object data)
74+
{
75+
webSocketEvent.Invoke(data);
76+
}
77+
78+
public static void RaiseEventWithoutBody(this WebSocketAPIEventHandler webSocketEvent)
79+
{
80+
webSocketEvent.Invoke(new object());
81+
}
82+
}
83+
84+
public class WebSocketAPISharedData
85+
{
86+
public void SetDefaults()
87+
{
88+
cwd = Path.Combine(Environment.CurrentDirectory, "working-dir");
89+
}
90+
91+
public string cwd;
92+
}
93+
}

0 commit comments

Comments
 (0)