1 //
2 // Copyright (c) 2010-2023 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using System.Threading.Tasks;
9 using Antmicro.Renode;
10 using Antmicro.Renode.Core;
11 using Antmicro.Renode.UserInterface;
12 using Antmicro.Renode.Utilities;
13 
14 namespace Antmicro.Renode.RobotFramework
15 {
16     public class RobotFrameworkEngine
17     {
RobotFrameworkEngine()18         public RobotFrameworkEngine()
19         {
20             keywordManager = new KeywordManager();
21             TypeManager.Instance.AutoLoadedType += keywordManager.Register;
22 
23             var processor = new XmlRpcServer(keywordManager);
24             server = new HttpServer(processor);
25         }
26 
Start(int port)27         public void Start(int port)
28         {
29             Task.Run(() =>
30             {
31                 try
32                 {
33                     server.Run(port);
34                     server.Dispose();
35                 }
36                 finally
37                 {
38                     Emulator.Exit();
39                     Emulator.FinishExecutionAsMainThread();
40                 }
41             });
42         }
43 
ExecuteKeyword(string name, object[] arguments)44         public void ExecuteKeyword(string name, object[] arguments)
45         {
46             if(keywordManager.TryExecuteKeyword(name, arguments, out var _) != KeywordManager.KeywordLookupResult.Success)
47             {
48                 throw new ArgumentException($"Could not find the '{name}' keyword with matching arguments, although it was used previously. It might indicate an internal error.");
49             }
50         }
51 
Shutdown()52         public void Shutdown()
53         {
54             server.Shutdown();
55         }
56 
57         private readonly HttpServer server;
58         private readonly KeywordManager keywordManager;
59     }
60 }
61