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 
8 using System;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Exceptions;
11 using Antmicro.Renode.Utilities;
12 
13 namespace Antmicro.Renode.Peripherals.Network
14 {
15     public static class EmulatedNetworkServiceExtensions
16     {
CreateEmulatedNetworkService(this Emulation emulation, string name, string typeName, string host, ushort port, string args = R)17         public static void CreateEmulatedNetworkService(this Emulation emulation, string name, string typeName, string host, ushort port, string args = "")
18         {
19             var type = TypeManager.Instance.TryGetTypeByName(typeName);
20             if(type == null)
21             {
22                 throw new RecoverableException($"Type '{typeName}' not found when creating emulated network service");
23             }
24 
25             try
26             {
27                 var service = (IEmulatedNetworkService)Activator.CreateInstance(type, host, port, args);
28                 emulation.ExternalsManager.AddExternal(service, name);
29             }
30             catch(Exception e)
31             {
32                 throw new RecoverableException("Failed to create emulated network service", e);
33             }
34         }
35     }
36 }
37