Implement IInstance

This commit is contained in:
Cyberboss
2017-12-08 10:54:37 -05:00
parent ff7a8b83ad
commit 79674a3536
3 changed files with 77 additions and 10 deletions
+12 -10
View File
@@ -9,6 +9,7 @@ using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Description;
using TGS.Interface.Components;
using TGS.Interface.Wrappers;
namespace TGS.Interface
{
@@ -196,23 +197,24 @@ namespace TGS.Interface
/// <inheritdoc />
public T GetComponent<T>()
{
var ToT = typeof(T);
return GetComponentImpl<T>(true);
if(InstanceName == null)
throw new InvalidOperationException("Instance not selected!");
return GetComponent<T>(InstanceName);
}
/// <summary>
/// Returns the requested <see cref="ServerInterface"/> component <see langword="interface"/> for the instance <see cref="InstanceName"/>. This does not guarantee a successful connection. <see cref="ChannelFactory{TChannel}"/>s created this way are recycled for minimum latency and bandwidth usage
/// </summary>
/// <typeparam name="T">The component <see langword="interface"/> to retrieve</typeparam>
/// <param name="useInstanceName">If <see cref="InstanceName"/> should be used to connect</param>
/// <param name="instanceName">The name of the <see cref="IInstance"/> to use</param>
/// <returns>The correct component <see langword="interface"/></returns>
T GetComponentImpl<T>(bool useInstanceName)
internal T GetComponent<T>(string instanceName)
{
if (useInstanceName & InstanceName == null)
throw new Exception("Instance not selected!");
if (disposedValue)
throw new ObjectDisposedException(GetType().Name);
var actualToT = typeof(T);
var tot = actualToT.Name;
if (actualToT == typeof(ITGConnectivity) && !useInstanceName)
if (actualToT == typeof(ITGConnectivity) && instanceName == null)
tot = 'S' + tot;
ChannelFactory<T> cf;
@@ -231,7 +233,7 @@ namespace TGS.Interface
ChannelFactoryCache[tot].Abort();
ChannelFactoryCache.Remove(tot);
}
cf = CreateChannel<T>(useInstanceName ? InstanceName : null);
cf = CreateChannel<T>(instanceName);
ChannelFactoryCache[tot] = cf;
}
return cf.CreateChannel();
@@ -241,7 +243,7 @@ namespace TGS.Interface
public T GetServiceComponent<T>()
{
var ToT = typeof(T);
return GetComponentImpl<T>(false);
return GetComponent<T>(null);
}
/// <summary>
@@ -328,7 +330,7 @@ namespace TGS.Interface
{
try
{
GetComponentImpl<ITGConnectivity>(false).VerifyConnection();
GetComponent<ITGConnectivity>(null).VerifyConnection();
}
catch (Exception e)
{
+1
View File
@@ -67,6 +67,7 @@
<Compile Include="Helpers.cs" />
<Compile Include="Wrappers\IInstance.cs" />
<Compile Include="InstanceMetadata.cs" />
<Compile Include="Wrappers\Instance.cs" />
<Compile Include="Wrappers\IServer.cs" />
<Compile Include="IServerInterface.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+64
View File
@@ -0,0 +1,64 @@
using TGS.Interface.Components;
namespace TGS.Interface.Wrappers
{
/// <inheritdoc />
sealed class Instance : IInstance
{
/// <summary>
/// The backing <see cref="ServerInterface"/>
/// </summary>
readonly ServerInterface serverInterface;
/// <summary>
/// The name of the <see cref="IInstance"/>
/// </summary>
readonly string instanceName;
/// <summary>
/// Construct an <see cref="Instance"/>
/// </summary>
/// <param name="_serverInterface">The <see cref="ServerInterface"/> to use</param>
/// <param name="_instanceName">The <see cref="IInstance"/> name to use</param>
public Instance(ServerInterface _serverInterface, string _instanceName)
{
serverInterface = _serverInterface;
instanceName = _instanceName;
}
/// <inheritdoc />
public ITGAdministration Administration => serverInterface.GetComponent<ITGAdministration>(instanceName);
/// <inheritdoc />
public ITGByond Byond => serverInterface.GetComponent<ITGByond>(instanceName);
/// <inheritdoc />
public ITGChat Chat => serverInterface.GetComponent<ITGChat>(instanceName);
/// <inheritdoc />
public ITGCompiler Compiler => serverInterface.GetComponent<ITGCompiler>(instanceName);
/// <inheritdoc />
public ITGConfig Config => serverInterface.GetComponent<ITGConfig>(instanceName);
/// <inheritdoc />
public ITGDreamDaemon DreamDaemon => serverInterface.GetComponent<ITGDreamDaemon>(instanceName);
/// <inheritdoc />
public ITGInterop Interop => serverInterface.GetComponent<ITGInterop>(instanceName);
/// <inheritdoc />
public ITGRepository Repository => serverInterface.GetComponent<ITGRepository>(instanceName);
/// <inheritdoc />
public string ServerDirectory()
{
return serverInterface.GetComponent<ITGInstance>(instanceName).ServerDirectory();
}
/// <inheritdoc />
public string Version()
{
return serverInterface.GetComponent<ITGInstance>(instanceName).Version();
}
}
}