// Author: Gockner, Simon // Created: 2020-02-10 // Copyright(c) 2020 SimonG. All Rights Reserved. using System.ServiceModel; using System.Threading.Tasks; using GBase.Api.Services; using GBase.Client.Interfaces; namespace GBase.Client { public class GBaseClient : IGBaseClient //TODO: Add logging? { public GBaseClient(IGBaseClientSettings settings) { GBaseServiceFactory = OpenFactory(settings.ServerProtocolGBaseEndpointAddress); GBaseTableServiceFactory = OpenFactory(settings.ServerProtocolGBaseTableEndpointAddress); GBaseEntryServiceFactory = OpenFactory(settings.ServerProtocolGBaseEntryEndpointAddress); } private ChannelFactory GBaseServiceFactory { get; set; } private ChannelFactory GBaseTableServiceFactory { get; set; } private ChannelFactory GBaseEntryServiceFactory { get; set; } public bool AddTable() //TODO: Implement this function correctly { IGBaseService channel = OpenChannel(GBaseServiceFactory); bool success = channel.AddTable(); CloseChannel(channel); return success; } public bool RemoveTable() //TODO: Implement this function correctly { IGBaseService channel = OpenChannel(GBaseServiceFactory); bool success = channel.RemoveTable(); CloseChannel(channel); return success; } private ChannelFactory OpenFactory(string endpoint) { ChannelFactory factory = new ChannelFactory(new BasicHttpBinding(), new EndpointAddress(endpoint)); factory.Open(); return factory; } private TService OpenChannel(ChannelFactory factory) { TService channel = factory.CreateChannel(); ((IClientChannel) channel).Open(); return channel; } private void CloseChannel(TService service) { ((IClientChannel) service).Close(); } public async ValueTask DisposeAsync() { GBaseServiceFactory.Close(); GBaseTableServiceFactory.Close(); GBaseEntryServiceFactory.Close(); } } }