/// <summary> /// Initializes common resources. /// </summary> /// <param name="container">The composition container.</param> protected void EtpSetUp(IContainer container) { _container = container; // Clean up any remaining resources _client?.Dispose(); _server?.Dispose(); // Get next available port number var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); var port = ((IPEndPoint)listener.LocalEndpoint).Port; listener.Stop(); // Update EtpServerUrl setting var uri = new Uri(TestSettings.EtpServerUrl); var url = TestSettings.EtpServerUrl.Replace($":{uri.Port}", $":{port}"); // Create server and client instances _server = CreateServer(port); _client = InitClient(CreateClient(url)); // Resolve dependencies early to avoid object disposed var streaming = container.Resolve <IChannelStreamingProducer>(); var discovery = container.Resolve <IDiscoveryStore>(); var store = container.Resolve <IStoreStore>(); // Register server handlers _server.Register(() => streaming); _server.Register(() => discovery); _server.Register(() => store); }
private void RegisterProtocolHandlers(EtpClient client) { if (Requesting(Protocols.ChannelStreaming, "producer")) { client.Register <IChannelStreamingConsumer, ChannelStreamingConsumerHandler>(); RegisterEventHandlers(client.Handler <IChannelStreamingConsumer>()); } if (Requesting(Protocols.ChannelStreaming, "consumer")) { client.Register <IChannelStreamingProducer, ChannelStreamingProducerHandler>(); RegisterEventHandlers(client.Handler <IChannelStreamingProducer>()); } if (Requesting(Protocols.ChannelDataFrame, "producer")) { client.Register <IChannelDataFrameConsumer, ChannelDataFrameConsumerHandler>(); RegisterEventHandlers(client.Handler <IChannelDataFrameConsumer>()); } if (Requesting(Protocols.ChannelDataFrame, "consumer")) { client.Register <IChannelDataFrameProducer, ChannelDataFrameProducerHandler>(); RegisterEventHandlers(client.Handler <IChannelDataFrameProducer>()); } if (Requesting(Protocols.Discovery, "store")) { client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); RegisterEventHandlers(client.Handler <IDiscoveryCustomer>(), x => x.OnGetResourcesResponse += OnGetResourcesResponse); } if (Requesting(Protocols.Store, "store")) { client.Register <IStoreCustomer, StoreCustomerHandler>(); RegisterEventHandlers(client.Handler <IStoreCustomer>(), x => x.OnObject += OnObject); } if (Requesting(Protocols.StoreNotification, "store")) { client.Register <IStoreNotificationCustomer, StoreNotificationCustomerHandler>(); RegisterEventHandlers(client.Handler <IStoreNotificationCustomer>()); } if (Requesting(Protocols.GrowingObject, "store")) { client.Register <IGrowingObjectCustomer, GrowingObjectCustomerHandler>(); RegisterEventHandlers(client.Handler <IGrowingObjectCustomer>(), x => x.OnObjectFragment += OnObjectFragment); } if (Requesting(Protocols.DataArray, "store")) { client.Register <IDataArrayCustomer, DataArrayCustomerHandler>(); RegisterEventHandlers(client.Handler <IDataArrayCustomer>()); } if (Requesting(Protocols.WitsmlSoap, "store")) { //client.Register<IWitsmlSoapCustomer, WitsmlSoapCustomerHandler>(); //RegisterEventHandlers(client.Handler<IWitsmlSoapCustomer>()); } }
/// <summary> /// Opens a WebSocket connection and waits for the SocketOpened event to be called. /// </summary> /// <param name="client">The client.</param> /// <param name="milliseconds">The timeout, in milliseconds.</param> /// <returns>An awaitable task.</returns> public static async Task <bool> OpenAsync(this EtpClient client, int?milliseconds = null) { var task = new Task <bool>(() => client.IsOpen); client.SocketOpened += (s, e) => task.Start(); client.Open(); return(await task.WaitAsync(milliseconds)); }
/// <summary> /// Initializes the client. /// </summary> /// <param name="client">The ETP client.</param> /// <returns>The ETP client.</returns> protected EtpClient InitClient(EtpClient client) { // Register client handlers client.Register <IChannelStreamingConsumer, ChannelStreamingConsumerHandler>(); client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); client.Register <IStoreCustomer, StoreCustomerHandler>(); return(client); }
/// <summary> /// Creates an <see cref="EtpClient"/> instance configurated with the /// current connection and authorization parameters. /// </summary> /// <param name="url">The WebSocket URL.</param> /// <returns>A new <see cref="EtpClient"/> instance.</returns> protected EtpClient CreateClient(string url) { var version = GetType().Assembly.GetName().Version.ToString(); var headers = Energistics.Security.Authorization.Basic(TestSettings.Username, TestSettings.Password); var client = new EtpClient(url ?? TestSettings.EtpServerUrl, GetType().AssemblyQualifiedName, version, headers); client.Output = client.Logger.Info; return(client); }
private void Connect2Server_Button_Click(object sender, RoutedEventArgs e) { Client = new EtpClient(ServerUri_TextBox.Text, AppName_TextBox.Text, Version_TextBox.Text); Client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); Client.Output = LogClientOutput; Client.Open(); //System.Windows.MessageBox.Show(Proto_ListBox.SelectedIndex.ToString()); //foreach (var item in Proto_ListBox.SelectedItems) //{ // System.Windows.MessageBox.Show(item.ToString()); //} }
private void Button_Click(object sender, RoutedEventArgs e) { Client = new EtpClient(ServerUrl, "MyPDS", "1.0.0.0"); Client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); RegisterEventHandlers(Client.Handler <IDiscoveryCustomer>(), x => x.OnGetResourcesResponse += OnGetResourcesResponse); Client.Output = LogClientOutput; Client.Open(); //System.Windows.MessageBox.Show(Client.IsOpen.ToString()); }
/// <summary> /// Requests a new session and asserts. /// </summary> /// <param name="retries">The number of retries.</param> /// <returns>The <see cref="OpenSession" /> message args.</returns> protected async Task <ProtocolEventArgs <OpenSession> > RequestSessionAndAssert(int retries = 10) { try { var client = _client; // Register event handler for OpenSession response var onOpenSession = HandleAsync <OpenSession>( x => client.Handler <ICoreClient>().OnOpenSession += x); // Wait for Open connection var isOpen = await _client.OpenAsync(); Assert.IsTrue(isOpen); // Wait for OpenSession var openArgs = await onOpenSession; // Verify OpenSession and Supported Protocols VerifySessionWithProtcols(openArgs, Protocols.ChannelStreaming, Protocols.Discovery, Protocols.Store); return(openArgs); } catch (TimeoutException) { if (retries < 1) { throw; } await Task.Delay(TestSettings.DefaultTimeoutInMilliseconds); Logger.Warn("Retrying connection attempt after timeout"); if (retries == 1) { _client?.Dispose(); _client = InitClient(CreateClient(TestSettings.FallbackServerUrl)); } else { EtpSetUp(_container); _server.Start(); } return(await RequestSessionAndAssert(retries - 1)); } }
/// <summary> /// Executes an action and waits the specified timeout for an error to occur. /// </summary> /// <param name="client">The client.</param> /// <param name="action">The action to execute.</param> /// <param name="milliseconds">The timeout, in milliseconds.</param> /// <returns>An awaitable task.</returns> public static async Task <bool> ErrorAsync(this EtpClient client, Action action, int?milliseconds = null) { var task = new Task <bool>(() => true); client.SocketError += (s, e) => { if (task.Status == TaskStatus.Created) { task.Start(); } }; action(); return(await task.WaitAsync()); }
/// <summary> /// Creates an ETP client for the current connection /// </summary> /// <param name="connection">The connection.</param> /// <param name="applicationName">Name of the application.</param> /// <param name="applicationVersion">The application version.</param> /// <returns>An <see cref="Energistics.EtpClient"/> instance.</returns> public static EtpClient CreateEtpClient(this Connection connection, string applicationName, string applicationVersion) { var headers = connection.GetAuthorizationHeader(); connection.UpdateEtpSettings(headers); connection.SetServerCertificateValidation(); var client = new EtpClient(connection.Uri, applicationName, applicationVersion, headers); if (!string.IsNullOrWhiteSpace(connection.ProxyHost)) { client.SetProxy(connection.ProxyHost, connection.ProxyPort, connection.ProxyUsername, connection.ProxyPassword); } return(client); }
private async Task <bool> CanConnect(Connection connection, IDictionary <string, string> headers) { try { var applicationName = GetType().Assembly.FullName; var applicationVersion = GetType().GetAssemblyVersion(); connection.UpdateEtpSettings(headers); connection.SetServerCertificateValidation(); using (var client = new EtpClient(connection.Uri, applicationName, applicationVersion, headers)) { client.Register <IChannelStreamingConsumer, ChannelStreamingConsumerHandler>(); client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); client.Register <IStoreCustomer, StoreCustomerHandler>(); var count = 0; client.Open(); while (string.IsNullOrWhiteSpace(client.SessionId) && count < 10) { await Task.Delay(1000); count++; } var result = !string.IsNullOrWhiteSpace(client.SessionId); _log.DebugFormat("ETP connection test {0}", result ? "passed" : "failed"); return(result); } } catch (Exception ex) { _log.Error("ETP connection test failed", ex); return(false); } }