1 //
2 // Copyright (c) 2010-2024 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.Collections.Generic;
8 using Antmicro.Renode.Core;
9 using Antmicro.Renode.Utilities;
10 
11 namespace Antmicro.Renode.Network.ExternalControl
12 {
13     public class GetMachine : BaseCommand, IMachineContainer
14     {
GetMachine(ExternalControlServer parent)15         public GetMachine(ExternalControlServer parent)
16             : base(parent)
17         {
18             machines = new InstanceCollection<IMachine>();
19         }
20 
TryGetMachine(int id, out IMachine machine)21         public bool TryGetMachine(int id, out IMachine machine) => machines.TryGet(id, out machine);
22 
Invoke(List<byte> data)23         public override Response Invoke(List<byte> data)
24         {
25             if(!IInstanceBasedCommandExtensions.TryGetName(Identifier, data, 0, out var name, out var response))
26             {
27                 return response;
28             }
29 
30             if(!EmulationManager.Instance.CurrentEmulation.TryGetMachineByName(name, out var machine))
31             {
32                 return Response.CommandFailed(Identifier, "Machine not found");
33             }
34 
35             machines.TryAdd(machine, out var id);
36             return Response.Success(Identifier, id.AsRawBytes());
37         }
38 
39         public override Command Identifier => Command.GetMachine;
40         public override byte Version => 0x0;
41 
42         private readonly InstanceCollection<IMachine> machines;
43     }
44 
45     public interface IMachineContainer
46     {
TryGetMachine(int id, out IMachine machine)47         bool TryGetMachine(int id, out IMachine machine);
48     }
49 }
50