public Task StartAsync(CancellationToken cancellationToken) { var certificate = new X509Certificate2(_options.CertificateFile); _runtime = new HalibutRuntime(_serviceFactory, certificate, _trustProvider); if (_options.Trust.Count > 0) { foreach (var item in _options.Trust) { _trustProvider.Add(item); } } _runtime.OnUnauthorizedClientConnect = _options.OnUnauthorizedClientConnect; switch (_options.Mode) { case HalibutServerMode.Listening: _runtime.Listen(_options.Listen.EndPoint); break; case HalibutServerMode.Polling: _runtime.Poll(new Uri($"poll://{_options.Polling.Subscription}"), new ServiceEndPoint(new Uri(_options.Polling.BaseUri), _options.Polling.RemoteThumbPrint, _options.ProxyDetails)); break; case HalibutServerMode.WebSocketPolling: _runtime.Poll(new Uri($"poll://{_options.Polling.Subscription}"), new ServiceEndPoint(new Uri(_options.Polling.BaseUri), _options.Polling.RemoteThumbPrint, _options.ProxyDetails)); break; } return(Task.CompletedTask); }
public void ConnectingOverHttpShouldFailQuickly() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); Assert.Throws<WebException>(() => DownloadStringIgnoringCertificateValidation("http://localhost:" + listenPort)); } }
public void StartServer() { var services = new DelegateServiceFactory(); services.Register <ITestService>(() => new TestService()); var DaServer = new HalibutRuntime(services, Configuration.ServerCertificate); DaServer.Listen(1337); DaServer.Trust(Configuration.ClientCertificate.Thumbprint); }
static int RunServer() { var services = new DelegateServiceFactory(); services.Register<ICalculatorService>(() => new CalculatorService()); var server = new HalibutRuntime(services, ServerCertificate); server.Trust("2074529C99D93D5955FEECA859AEAC6092741205"); var port = server.Listen(); return port; }
void DoConnectingOverHttpShouldFailQuickly() { var services = GetDelegateServiceFactory(); using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); Assert.ThrowsAsync <HttpRequestException>(() => DownloadStringIgnoringCertificateValidation("http://localhost:" + listenPort)); } }
public void OctopusCanDiscoverTentacle() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); var info = octopus.Discover(new Uri("https://localhost:" + tentaclePort)); Assert.That(info.RemoteThumbprint, Is.EqualTo(Certificates.TentacleListeningPublicThumbprint)); } }
public void SetUp() { var services = new DelegateServiceFactory(); services.Register<IEchoService>(() => new EchoService()); tentacle = new HalibutRuntime(services, Certificates.TentacleListening); var tentaclePort = tentacle.Listen(); tentacle.Trust(Certificates.OctopusPublicThumbprint); endpoint = new ServiceEndPoint("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); log = new InMemoryConnectionLog(endpoint.ToString()); HalibutLimits.ConnectionErrorRetryTimeout = TimeSpan.MaxValue; }
public void SupportsHttpsGet(string uriFormat) { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); var uri = uriFormat.Replace("{machine}", Environment.MachineName).Replace("{port}", listenPort.ToString()); var result = DownloadStringIgnoringCertificateValidation(uri); result.Should().Be("<html><body><p>Hello!</p></body></html>"); } }
void DoConnectingOverHttpShouldFailQuickly() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); #if NET40 Assert.Throws <WebException>(() => DownloadStringIgnoringCertificateValidation("http://localhost:" + listenPort)); #else Assert.Throws <HttpRequestException>(() => DownloadStringIgnoringCertificateValidation("http://localhost:" + listenPort)); #endif } }
public void OctopusCanDiscoverTentacle() { var services = GetDelegateServiceFactory(); using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); var info = octopus.Discover(new Uri("https://localhost:" + tentaclePort)); info.RemoteThumbprint.Should().Be(Certificates.TentacleListeningPublicThumbprint); } }
public void FailWhenListeningServerPresentsWrongCertificate() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient<IEchoService>("https://localhost:" + tentaclePort, Certificates.TentaclePollingPublicThumbprint); var ex = Assert.Throws<HalibutClientException>(() => echo.SayHello("World")); } }
public void FailWhenServerThrowsAnException() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); var ex = Assert.Throws <HalibutClientException>(() => echo.Crash()); ex.Message.Should().Contain("at Halibut.Tests.TestServices.EchoService.Crash()").And.Contain("divide by zero"); } }
public SecureClientFixture() { var services = new DelegateServiceFactory(); services.Register <IEchoService>(() => new EchoService()); tentacle = new HalibutRuntime(services, Certificates.TentacleListening); var tentaclePort = tentacle.Listen(); tentacle.Trust(Certificates.OctopusPublicThumbprint); endpoint = new ServiceEndPoint("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); log = new InMemoryConnectionLog(endpoint.ToString()); HalibutLimits.ConnectionErrorRetryTimeout = TimeSpan.MaxValue; }
public void FailWhenListeningServerPresentsWrongCertificate() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentaclePollingPublicThumbprint); var ex = Assert.Throws <HalibutClientException>(() => echo.SayHello("World")); } }
static int RunServer() { var services = new DelegateServiceFactory(); services.Register <ICalculatorService>(() => new CalculatorService()); var server = new HalibutRuntime(services, ServerCertificate); server.Trust("2074529C99D93D5955FEECA859AEAC6092741205"); var port = server.Listen(); return(port); }
public void CanSetCustomFriendlyHtmlPage(string html, string expectedResult = null) { expectedResult = expectedResult ?? html; // Handle the null case which reverts to default html using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { octopus.SetFriendlyHtmlPageContent(html); var listenPort = octopus.Listen(); var result = DownloadStringIgnoringCertificateValidation("https://localhost:" + listenPort); result.Should().Be(expectedResult); } }
public void CanSetCustomFriendlyHtmlPage(string html, string expectedResult = null) { expectedResult = expectedResult ?? html; // Handle the null case which reverts to default html using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { octopus.SetFriendlyHtmlPageContent(html); var listenPort = octopus.Listen(); var result = DownloadStringIgnoringCertificateValidation("https://localhost:" + listenPort); Assert.That(result, Is.EqualTo(expectedResult)); } }
public void SetUp() { var services = new DelegateServiceFactory(); services.Register <IEchoService>(() => new EchoService()); tentacle = new HalibutRuntime(services, Certificates.TentacleListening); var tentaclePort = tentacle.Listen(); tentacle.Trust(Certificates.OctopusPublicThumbprint); endpoint = new ServiceEndPoint("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint) { ConnectionErrorRetryTimeout = TimeSpan.MaxValue }; }
public void CanSetCustomFriendlyHtmlPageHeaders() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { octopus.SetFriendlyHtmlPageHeaders(new Dictionary <string, string> { { "X-Content-Type-Options", "nosniff" }, { "X-Frame-Options", "DENY" } }); var listenPort = octopus.Listen(); var result = GetHeadersIgnoringCertificateValidation("https://localhost:" + listenPort).ToList(); result.Should().Contain(x => x.Key == "X-Content-Type-Options" && x.Value == "nosniff"); result.Should().Contain(x => x.Key == "X-Frame-Options" && x.Value == "DENY"); } }
public void FailWhenServerThrowsAnExceptionOnPolling() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling)) { var octopusPort = octopus.Listen(); octopus.Trust(Certificates.TentaclePollingPublicThumbprint); tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), Certificates.OctopusPublicThumbprint)); var echo = octopus.CreateClient <IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint); var ex = Assert.Throws <HalibutClientException>(() => echo.Crash()); ex.Message.Should().Contain("at Halibut.Tests.TestServices.EchoService.Crash()").And.Contain("divide by zero"); } }
static HalibutRuntime RunServer(X509Certificate2 serverCertificate, out int port) { var services = new DelegateServiceFactory(); services.Register <ICalculatorService>(() => new CalculatorService()); var server = new HalibutRuntime(services, serverCertificate); //set up listening server.Trust(Certificates.TentacleListeningPublicThumbprint); port = server.Listen(); //setup polling websocket AddSslCertToLocalStoreAndRegisterFor("0.0.0.0:8434"); return(server); }
public void OctopusCanSendMessagesToPollingTentacle() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling)) { var octopusPort = octopus.Listen(); octopus.Trust(Certificates.TentaclePollingPublicThumbprint); tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), Certificates.OctopusPublicThumbprint)); var echo = octopus.CreateClient <IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint); for (var i = 0; i < 2000; i++) { Assert.That(echo.SayHello("Deploy package A" + i), Is.EqualTo("Deploy package A" + i + "...")); } } }
public void OctopusCanSendMessagesToPollingTentacle() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling)) { var octopusPort = octopus.Listen(); octopus.Trust(Certificates.TentaclePollingPublicThumbprint); tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), Certificates.OctopusPublicThumbprint)); var echo = octopus.CreateClient<IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint); for (var i = 0; i < 2000; i++) { Assert.That(echo.SayHello("Deploy package A" + i), Is.EqualTo("Deploy package A" + i + "...")); } } }
public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .CreateLogger(); Console.Title = "Halibut Server"; var certificate = new X509Certificate2("HalibutServer.pfx"); var endPoint = new IPEndPoint(IPAddress.IPv6Any, 8433); var services = new DelegateServiceFactory(); services.Register <ICalculatorService>(() => new CalculatorService()); using (var server = new HalibutRuntime(services, certificate)) { server.Listen(endPoint); server.Trust("2074529C99D93D5955FEECA859AEAC6092741205"); Console.WriteLine("Server listening on port 8433. Type 'exit' to quit, or 'cls' to clear..."); while (true) { var line = Console.ReadLine(); if (string.Equals("cls", line, StringComparison.OrdinalIgnoreCase)) { Console.Clear(); continue; } if (string.Equals("q", line, StringComparison.OrdinalIgnoreCase)) { return; } if (string.Equals("exit", line, StringComparison.OrdinalIgnoreCase)) { return; } Console.WriteLine("Unknown command. Enter 'q' to quit."); } } }
public void OctopusCanSendMessagesToListeningTentacle() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient<IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); Assert.That(echo.SayHello("Deploy package A"), Is.EqualTo("Deploy package A...")); var watch = Stopwatch.StartNew(); for (var i = 0; i < 2000; i++) { Assert.That(echo.SayHello("Deploy package A"), Is.EqualTo("Deploy package A...")); } Console.WriteLine("Complete in {0:n0}ms", watch.ElapsedMilliseconds); } }
public void OctopusCanSendMessagesToListeningTentacle() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); Assert.That(echo.SayHello("Deploy package A"), Is.EqualTo("Deploy package A...")); var watch = Stopwatch.StartNew(); for (var i = 0; i < 2000; i++) { Assert.That(echo.SayHello("Deploy package A"), Is.EqualTo("Deploy package A...")); } Console.WriteLine("Complete in {0:n0}ms", watch.ElapsedMilliseconds); } }
public void StreamsCanBeSentToListening() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var data = new byte[1024 * 1024 + 15]; new Random().NextBytes(data); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); for (var i = 0; i < 100; i++) { var count = echo.CountBytes(DataStream.FromBytes(data)); Assert.That(count, Is.EqualTo(1024 * 1024 + 15)); } } }
public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .CreateLogger(); Console.Title = "Halibut Server"; var certificate = new X509Certificate2("HalibutServer.pfx"); var endPoint = new IPEndPoint(IPAddress.IPv6Any, 8433); var services = new DelegateServiceFactory(); services.Register<ICalculatorService>(() => new CalculatorService()); using (var server = new HalibutRuntime(services, certificate)) { server.Listen(endPoint); server.Trust("2074529C99D93D5955FEECA859AEAC6092741205"); Console.WriteLine("Server listening on port 8433. Type 'exit' to quit, or 'cls' to clear..."); while (true) { var line = Console.ReadLine(); if (string.Equals("cls", line, StringComparison.OrdinalIgnoreCase)) { Console.Clear(); continue; } if (string.Equals("q", line, StringComparison.OrdinalIgnoreCase)) return; if (string.Equals("exit", line, StringComparison.OrdinalIgnoreCase)) return; Console.WriteLine("Unknown command. Enter 'q' to quit."); } } }
public void StreamsCanBeSentToPolling() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling)) { var octopusPort = octopus.Listen(); octopus.Trust(Certificates.TentaclePollingPublicThumbprint); tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), Certificates.OctopusPublicThumbprint)); var echo = octopus.CreateClient <IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint); var data = new byte[1024 * 1024 + 15]; new Random().NextBytes(data); for (var i = 0; i < 100; i++) { var count = echo.CountBytes(DataStream.FromBytes(data)); count.Should().Be(1024 * 1024 + 15); } } }
public void StreamsCanBeSentToListeningWithProgressReporting() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var progressReported = new List <int>(); var data = new byte[1024 * 1024 * 16 + 15]; new Random().NextBytes(data); var stream = new MemoryStream(data); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); var count = echo.CountBytes(DataStream.FromStream(stream, progressReported.Add)); Assert.That(count, Is.EqualTo(1024 * 1024 * 16 + 15)); CollectionAssert.AreEqual(Enumerable.Range(1, 100).ToList(), progressReported); } }
public void SupportsDifferentServiceContractMethods() { var services = GetDelegateServiceFactory(); services.Register <ISupportedServices>(() => new SupportedServices()); using (var octopus = new HalibutRuntime(Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var tentaclePort = tentacleListening.Listen(); var echo = octopus.CreateClient <ISupportedServices>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); echo.MethodReturningVoid(12, 14); echo.Hello().Should().Be("Hello"); echo.Hello("a").Should().Be("Hello a"); echo.Hello("a", "b").Should().Be("Hello a b"); echo.Hello("a", "b", "c").Should().Be("Hello a b c"); echo.Hello("a", "b", "c", "d").Should().Be("Hello a b c d"); echo.Hello("a", "b", "c", "d", "e").Should().Be("Hello a b c d e"); echo.Hello("a", "b", "c", "d", "e", "f").Should().Be("Hello a b c d e f"); echo.Hello("a", "b", "c", "d", "e", "f", "g").Should().Be("Hello a b c d e f g"); echo.Hello("a", "b", "c", "d", "e", "f", "g", "h").Should().Be("Hello a b c d e f g h"); echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i").Should().Be("Hello a b c d e f g h i"); echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").Should().Be("Hello a b c d e f g h i j"); echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k").Should().Be("Hello a b c d e f g h i j k"); echo.Add(1, 2).Should().Be(3); echo.Add(1.00, 2.00).Should().Be(3.00); echo.Add(1.10M, 2.10M).Should().Be(3.20M); echo.Ambiguous("a", "b").Should().Be("Hello string"); echo.Ambiguous("a", new Tuple <string, string>("a", "b")).Should().Be("Hello tuple"); var ex = Assert.Throws <HalibutClientException>(() => echo.Ambiguous("a", (string)null)); ex.Message.Should().Contain("Ambiguous"); } }
static void RunPollingClient(HalibutRuntime server, X509Certificate2 clientCertificate, string remoteThumbprint, bool expectSuccess = true) { using (var runtime = new HalibutRuntime(clientCertificate)) { runtime.Listen(new IPEndPoint(IPAddress.IPv6Any, 8433)); runtime.Trust(Certificates.OctopusPublicThumbprint); //setup polling var serverEndpoint = new ServiceEndPoint(new Uri("https://localhost:8433"), Certificates.TentaclePollingPublicThumbprint) { TcpClientConnectTimeout = TimeSpan.FromSeconds(5) }; server.Poll(new Uri("poll://SQ-TENTAPOLL"), serverEndpoint); var clientEndpoint = new ServiceEndPoint("poll://SQ-TENTAPOLL", remoteThumbprint); var calculator = runtime.CreateClient <ICalculatorService>(clientEndpoint); MakeRequest(calculator, "polling", expectSuccess); runtime.Disconnect(clientEndpoint); } }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .CreateLogger(); var services = new DelegateServiceFactory(); services.Register <ICalculatorService>(() => new CalculatorService()); using (var client = new HalibutRuntime(services, ClientCertificate)) using (var server = new HalibutRuntime(services, ServerCertificate)) { var octopusPort = client.Listen(); client.Trust(ServerCertificate.Thumbprint); server.Poll(new Uri(PollUrl), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), ClientCertificate.Thumbprint)); var calculator = client.CreateClient <ICalculatorService>(PollUrl, ServerCertificate.Thumbprint); var result = calculator.Add(12, 18); Debug.Assert(result == 30); } Console.ReadKey(); }
public void StreamsCanBeSentToListeningWithProgressReporting() { var services = GetDelegateServiceFactory(); using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var progressReported = new List <int>(); var data = new byte[1024 * 1024 * 16 + 15]; new Random().NextBytes(data); var stream = new MemoryStream(data); var echo = octopus.CreateClient <IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); var count = echo.CountBytes(DataStream.FromStream(stream, progressReported.Add)); count.Should().Be(1024 * 1024 * 16 + 15); progressReported.Should().ContainInOrder(Enumerable.Range(1, 100)); } }
public void SupportsHttpsGet(string uriFormat) { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); using (var webClient = new WebClient()) { try { // We need to ignore server certificate validation errors - the server certificate is self-signed ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; var result = webClient.DownloadString(uriFormat.Replace("{machine}", Environment.MachineName).Replace("{port}", listenPort.ToString())); Assert.That(result, Is.EqualTo("<html><body><p>Hello!</p></body></html>")); } finally { // And restore it back to default behaviour ServicePointManager.ServerCertificateValidationCallback = null; } } } }
public void FailWhenServerThrowsAnExceptionOnPolling() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling)) { var octopusPort = octopus.Listen(); octopus.Trust(Certificates.TentaclePollingPublicThumbprint); tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri("https://localhost:" + octopusPort), Certificates.OctopusPublicThumbprint)); var echo = octopus.CreateClient<IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint); var ex = Assert.Throws<HalibutClientException>(() => echo.Crash()); Assert.That(ex.Message, Does.Contain("at Halibut.Tests.TestServices.EchoService.Crash()").And.Contains("divide by zero")); } }
public void Start() { if (!Directory.Exists(SourcesPath)) { Log.Fatal("{@SourcesPath} doesn't exist; service has nothing to do without sources", SourcesPath); Stop(); return; } if (!Directory.Exists(SinksPath)) { Log.Warning("{@SinksPath} doesn't exist; service has nothing to do without sinks", SinksPath); } _childDevices.CollectionChanged += (sender, args) => { switch (args.Action) { case NotifyCollectionChangedAction.Add: foreach (IDualShockDevice item in args.NewItems) { Log.Information("Device {Device} got attached via {ConnectionType}", item, item.ConnectionType); foreach (var plugin in SinkPlugins.Select(p => p.Value)) { plugin.DeviceArrived(item); } } break; case NotifyCollectionChangedAction.Remove: foreach (IDualShockDevice item in args.OldItems) { Log.Information("Device {Device} got removed via {ConnectionType}", item, item.ConnectionType); foreach (var plugin in SinkPlugins.Select(p => p.Value)) { plugin.DeviceRemoved(item); } } break; } }; #region MEF //Creating an instance of aggregate catalog. It aggregates other catalogs var aggregateCatalog = new AggregateCatalog(); //Load parts from the current assembly if available var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); //Add to the aggregate catalog aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SourcesPath, "*.dll")); aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SinksPath, "*.dll")); aggregateCatalog.Catalogs.Add(asmCatalog); //Crete the composition container var container = new CompositionContainer(aggregateCatalog); // Composable parts are created here i.e. // the Import and Export components assembles here container.ComposeParts(this); #endregion // Log loaded sink plugins foreach (var plugin in SinkPlugins) { Log.Information("Loaded sink plugin {Plugin}", plugin.Metadata["Name"]); plugin.Value.RumbleRequestReceived += (sender, args) => _childDevices[(IDualShockDevice)sender].Rumble(args.LargeMotor, args.SmallMotor); } // Log and enable sources foreach (var emulator in BusEmulators) { Log.Information("Loaded bus emulator {Emulator}", emulator.Metadata["Name"]); emulator.Value.ChildDeviceAttached += (sender, args) => _childDevices.Add(args.Device); emulator.Value.ChildDeviceRemoved += (sender, args) => _childDevices.Remove(args.Device); emulator.Value.InputReportReceived += EmulatorOnInputReportReceived; try { Log.Information("Starting bus emulator {Emulator}", emulator.Metadata["Name"]); emulator.Value.Start(); Log.Information("Bus emulator {Emulator} started successfully", emulator.Metadata["Name"]); } catch (Exception ex) { Log.Error("Failed to start {@emulator}: {@ex}", emulator.Metadata["Name"], ex); } } #region IPC var services = new DelegateServiceFactory(); services.Register <IPairingService>(() => { var service = new PairingService(); service.DeviceListRequested += (sender, args) => _childDevices .Where(d => d.ConnectionType.Equals(DualShockConnectionType.USB)) .Select(d => new DualShockDeviceDescriptor { ClientAddress = new UniqueAddress(d.ClientAddress), ConnectionType = d.ConnectionType, DeviceType = d.DeviceType, HostAddress = new UniqueAddress(d.HostAddress) }).ToList(); service.DevicePairingRequested += (device, args) => _childDevices[device.ClientAddress].PairTo(new PhysicalAddress(args.HostAddress.AddressBytes)); return(service); }); _ipcServer = new HalibutRuntime(services, Configuration.ServerCertificate); _ipcServer.Listen(Configuration.ServerEndpoint); _ipcServer.Trust(Configuration.ClientCertificate.Thumbprint); #endregion }
public void Start() { if (!Directory.Exists(SourcesPath)) { Log.Fatal("{@SourcesPath} doesn't exist; service has nothing to do without sources", SourcesPath); Stop(); return; } if (!Directory.Exists(SinksPath)) { Log.Warning("{@SinksPath} doesn't exist; service has nothing to do without sinks", SinksPath); } _childDevices.CollectionChanged += (sender, args) => { switch (args.Action) { case NotifyCollectionChangedAction.Add: foreach (IDualShockDevice item in args.NewItems) { Log.Information("Device {Device} got attached via {ConnectionType}", item, item.ConnectionType); // Crude auto-pairing mechanism in case BthPS3 is present if (item.ConnectionType.Equals(DualShockConnectionType.USB) && BusEmulators.Select(be => be.Value).Any(b => b.Name == "BthPS3BusEmulator") && BluetoothAdapter.GetDefault() != null) { // Check if newly connected USB device is already connected via Bluetooth if (item.ConnectionType.Equals(DualShockConnectionType.USB)) { var emulator = BusEmulators.Select(be => be.Value).Where(b => b.Name == "BthPS3BusEmulator").First(); foreach (var otherItem in _childDevices) { if (otherItem.ConnectionType.Equals(DualShockConnectionType.Bluetooth)) { if (otherItem.ClientAddress.Equals(item.ClientAddress)) // If found remove bluetooth device { emulator.RemoveDevice(otherItem); break; } } } } // Get address of detected primary radio var hostAddress = new PhysicalAddress(BitConverter .GetBytes(BluetoothAdapter.GetDefault().BluetoothAddress).Take(6).Reverse() .ToArray()); if (!item.HostAddress.Equals(hostAddress)) { Log.Information("Auto-pairing device {Device} to {HostAddress}", item, hostAddress.AsFriendlyName()); // Pair USB device item.PairTo(hostAddress); } else { Log.Information("Device {Device} already paired to this radio {HostAddress}.", item, hostAddress.AsFriendlyName()); } } else { Log.Warning("Auto-pairing not supported as BthPS3 and/or Bluetooth Host Radio not found"); } foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled).Select(p => p.Value)) { plugin.DeviceArrived(item); } } break; case NotifyCollectionChangedAction.Remove: foreach (IDualShockDevice item in args.OldItems) { Log.Information("Device {Device} got removed via {ConnectionType}", item, item.ConnectionType); foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled).Select(p => p.Value)) { plugin.DeviceRemoved(item); } } break; } UpdateLEDS(); }; #region MEF //Creating an instance of aggregate catalog. It aggregates other catalogs var aggregateCatalog = new AggregateCatalog(); //Load parts from the current assembly if available var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); //Add to the aggregate catalog aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SourcesPath, "*.dll")); aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SinksPath, "*.dll")); aggregateCatalog.Catalogs.Add(asmCatalog); //Crete the composition container var container = new CompositionContainer(aggregateCatalog); // Composable parts are created here i.e. // the Import and Export components assembles here container.ComposeParts(this); #endregion // Log loaded sink plugins foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled)) { Log.Information("Loaded sink plugin {Plugin}", plugin.Metadata["Name"]); plugin.Value.RumbleRequestReceived += (sender, args) => _childDevices[(IDualShockDevice)sender].Rumble(args.LargeMotor, args.SmallMotor); } // Log and enable sources foreach (var emulator in BusEmulators.Where(p => p.Value.IsEnabled)) { Log.Information("Loaded bus emulator {Emulator}", emulator.Metadata["Name"]); emulator.Value.ChildDeviceAttached += (sender, args) => _childDevices.Add(args.Device); emulator.Value.ChildDeviceRemoved += (sender, args) => _childDevices.Remove(args.Device); emulator.Value.InputReportReceived += EmulatorOnInputReportReceived; try { Log.Information("Starting bus emulator {Emulator}", emulator.Metadata["Name"]); emulator.Value.Start(); Log.Information("Bus emulator {Emulator} started successfully", emulator.Metadata["Name"]); } catch (Exception ex) { Log.Error("Failed to start {@emulator}: {@ex}", emulator.Metadata["Name"], ex); } } #region IPC if (Config.Global.Core.Halibut.IsEnabled) { var services = new DelegateServiceFactory(); services.Register <IPairingService>(() => { var service = new PairingService(); service.DeviceListRequested += (sender, args) => _childDevices .Where(d => d.ConnectionType.Equals(DualShockConnectionType.USB)) .Select(d => new DualShockDeviceDescriptor { ClientAddress = new UniqueAddress(d.ClientAddress), ConnectionType = d.ConnectionType, DeviceType = d.DeviceType, HostAddress = new UniqueAddress(d.HostAddress) }).ToList(); service.DevicePairingRequested += (device, args) => _childDevices[device.ClientAddress].PairTo(new PhysicalAddress(args.HostAddress.AddressBytes)); return(service); }); _ipcServer = new HalibutRuntime(services, Configuration.ServerCertificate); _ipcServer.Listen(Configuration.ServerEndpoint); _ipcServer.Trust(Configuration.ClientCertificate.Thumbprint); } #endregion Task.Factory.StartNew(PollBatteryLevel, _inputCancellationTokenSource.Token); }
public void SupportsHttpsGet(string uriFormat) { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) { var listenPort = octopus.Listen(); var uri = uriFormat.Replace("{machine}", Environment.MachineName).Replace("{port}", listenPort.ToString()); var result = DownloadStringIgnoringCertificateValidation(uri); Assert.That(result, Is.EqualTo("<html><body><p>Hello!</p></body></html>")); } }
public void StreamsCanBeSentToListeningWithProgressReporting() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var progressReported = new List<int>(); var data = new byte[1024 * 1024 * 16 + 15]; new Random().NextBytes(data); var stream = new MemoryStream(data); var echo = octopus.CreateClient<IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); var count = echo.CountBytes(DataStream.FromStream(stream, progressReported.Add)); Assert.That(count, Is.EqualTo(1024 * 1024 * 16 + 15)); CollectionAssert.AreEqual(Enumerable.Range(1, 100).ToList(), progressReported); } }
public void SupportsDifferentServiceContractMethods() { services.Register<ISupportedServices>(() => new SupportedServices()); using (var octopus = new HalibutRuntime(Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var tentaclePort = tentacleListening.Listen(); var echo = octopus.CreateClient<ISupportedServices>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); echo.MethodReturningVoid(12, 14); Assert.That(echo.Hello(), Is.EqualTo("Hello")); Assert.That(echo.Hello("a"), Is.EqualTo("Hello a")); Assert.That(echo.Hello("a", "b"), Is.EqualTo("Hello a b")); Assert.That(echo.Hello("a", "b", "c"), Is.EqualTo("Hello a b c")); Assert.That(echo.Hello("a", "b", "c", "d"), Is.EqualTo("Hello a b c d")); Assert.That(echo.Hello("a", "b", "c", "d", "e"), Is.EqualTo("Hello a b c d e")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f"), Is.EqualTo("Hello a b c d e f")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g"), Is.EqualTo("Hello a b c d e f g")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h"), Is.EqualTo("Hello a b c d e f g h")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i"), Is.EqualTo("Hello a b c d e f g h i")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), Is.EqualTo("Hello a b c d e f g h i j")); Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), Is.EqualTo("Hello a b c d e f g h i j k")); Assert.That(echo.Add(1, 2), Is.EqualTo(3)); Assert.That(echo.Add(1.00, 2.00), Is.EqualTo(3.00)); Assert.That(echo.Add(1.10M, 2.10M), Is.EqualTo(3.20M)); Assert.That(echo.Ambiguous("a", "b"), Is.EqualTo("Hello string")); Assert.That(echo.Ambiguous("a", new Tuple<string, string>("a", "b")), Is.EqualTo("Hello tuple")); var ex = Assert.Throws<HalibutClientException>(() => echo.Ambiguous("a", (string)null)); Assert.That(ex.Message, Is.StringContaining("Ambiguous")); } }
public void FailWhenServerThrowsAnException() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var echo = octopus.CreateClient<IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); var ex = Assert.Throws<HalibutClientException>(() => echo.Crash()); Assert.That(ex.Message, Is.StringContaining("at Halibut.Tests.TestServices.EchoService.Crash()").And.StringContaining("divide by zero")); } }
public void StreamsCanBeSentToListening() { using (var octopus = new HalibutRuntime(services, Certificates.Octopus)) using (var tentacleListening = new HalibutRuntime(services, Certificates.TentacleListening)) { var tentaclePort = tentacleListening.Listen(); tentacleListening.Trust(Certificates.OctopusPublicThumbprint); var data = new byte[1024 * 1024 + 15]; new Random().NextBytes(data); var echo = octopus.CreateClient<IEchoService>("https://localhost:" + tentaclePort, Certificates.TentacleListeningPublicThumbprint); for (var i = 0; i < 100; i++) { var count = echo.CountBytes(DataStream.FromBytes(data)); Assert.That(count, Is.EqualTo(1024 * 1024 + 15)); } } }