Example #1
0
        public void OctopusCanSendMessagesToWebSocketPollingTentacle()
        {
            const int octopusPort = 8450;

            AddSslCertToLocalStoreAndRegisterFor("0.0.0.0:" + octopusPort);

            try
            {
                using (var octopus = new HalibutRuntime(services, Certificates.Octopus))
                    using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling))
                    {
                        octopus.ListenWebSocket($"https://+:{octopusPort}/Halibut");
                        octopus.Trust(Certificates.TentaclePollingPublicThumbprint);

                        tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri($"wss://localhost:{octopusPort}/Halibut"), Certificates.SslThumbprint));

                        var echo = octopus.CreateClient <IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint);
                        for (var i = 0; i < 2000; i++)
                        {
                            echo.SayHello("Deploy package A" + i).Should().Be("Deploy package A" + i + "...");
                        }
                    }
            }
            finally
            {
                RemoveSslCertBindingFor("0.0.0.0:" + octopusPort);
            }
        }
Example #2
0
        public void OctopusCanSendMessagesToWebSocketPollingTentacle()
        {
            var       services    = GetDelegateServiceFactory();
            const int octopusPort = 8450;

            AddSslCertToLocalStoreAndRegisterFor("0.0.0.0:" + octopusPort);

            try
            {
                using (var octopus = new HalibutRuntime(services, Certificates.Octopus))
                    using (var tentaclePolling = new HalibutRuntime(services, Certificates.TentaclePolling))
                    {
                        octopus.ListenWebSocket($"https://+:{octopusPort}/Halibut");
                        octopus.Trust(Certificates.TentaclePollingPublicThumbprint);

                        tentaclePolling.Poll(new Uri("poll://SQ-TENTAPOLL"), new ServiceEndPoint(new Uri($"wss://localhost:{octopusPort}/Halibut"), Certificates.SslThumbprint));

                        var echo = octopus.CreateClient <IEchoService>("poll://SQ-TENTAPOLL", Certificates.TentaclePollingPublicThumbprint);
                        for (var i = 0; i < 2000; i++)
                        {
                            echo.SayHello("Deploy package A" + i).Should().Be("Deploy package A" + i + "...");
                        }
                    }
            }
            catch (NotSupportedException nse) when(nse.Message == "The netstandard build of this library cannot act as the client in a WebSocket polling setup")
            {
                Assert.Inconclusive("This test cannot run on the netstandard build");
            }
            finally
            {
                RemoveSslCertBindingFor("0.0.0.0:" + octopusPort);
            }
        }
Example #3
0
        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;
        }
Example #4
0
        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);
        }
 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;
 }
Example #6
0
        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"));
                }
        }
Example #7
0
        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 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"));
            }
        }
Example #9
0
        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 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;
        }
Example #11
0
        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
            };
        }
Example #12
0
        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");
                }
        }
Example #13
0
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.ColoredConsole()
                         .CreateLogger();

            Console.Title = "Halibut Client";
            var certificate = new X509Certificate2("HalibutClient.pfx");

            var hostName = args.FirstOrDefault() ?? "localhost";
            var port     = args.Skip(1).FirstOrDefault() ?? "8433";

            using (var runtime = new HalibutRuntime(certificate))
            {
                //Begin make request of Listening server
                //var calculator = runtime.CreateClient<ICalculatorService>("https://" + hostName + ":" + port + "/", "EF3A7A69AFE0D13130370B44A228F5CD15C069BC");
                //End make request of Listening server

                //Begin make request of Polling server
                //var endPoint = new IPEndPoint(IPAddress.IPv6Any, 8433);
                //runtime.Listen(endPoint);
                //runtime.Trust("EF3A7A69AFE0D13130370B44A228F5CD15C069BC");
                //var calculator = runtime.CreateClient<ICalculatorService>("poll://SQ-TENTAPOLL", "2074529C99D93D5955FEECA859AEAC6092741205");
                //End make request of Polling server

                //Begin make request of WebSocket Polling server
                AddSslCertToLocalStoreAndRegisterFor("0.0.0.0:8433");
                runtime.ListenWebSocket("https://+:8433/Halibut");
                runtime.Trust("EF3A7A69AFE0D13130370B44A228F5CD15C069BC");
                var calculator = runtime.CreateClient <ICalculatorService>("poll://SQ-TENTAPOLL", "2074529C99D93D5955FEECA859AEAC6092741205");
                //End make request of WebSocket Polling server

                while (true)
                {
                    try
                    {
                        var result = calculator.Add(12, 18);
                        Console.WriteLine("12 + 18 = " + result);
                        Console.ReadKey();
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                }
            }
        }
Example #14
0
        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);
        }
Example #15
0
        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 + "..."));
                }
            }
        }
Example #16
0
        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 + "..."));
                    }
                }
        }
Example #17
0
        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.");
                }
            }
        }
Example #18
0
        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);
            }
        }
Example #19
0
        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);
                }
        }
Example #20
0
        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));
                    }
                }
        }
Example #21
0
        static void RunWebSocketPollingClient(HalibutRuntime server, X509Certificate2 clientCertificate, string remoteThumbprint, string trustedCertificate, bool expectSuccess = true)
        {
            using (var runtime = new HalibutRuntime(clientCertificate))
            {
                runtime.ListenWebSocket("https://+:8434/Halibut");
                runtime.Trust(trustedCertificate);

                var serverEndpoint = new ServiceEndPoint(new Uri("wss://localhost:8434/Halibut"), Certificates.SslThumbprint)
                {
                    TcpClientConnectTimeout = TimeSpan.FromSeconds(5)
                };
                server.Poll(new Uri("poll://SQ-WEBSOCKETPOLL"), serverEndpoint);

                var clientEndpoint = new ServiceEndPoint("poll://SQ-WEBSOCKETPOLL", remoteThumbprint);
                var calculator     = runtime.CreateClient <ICalculatorService>(clientEndpoint);

                MakeRequest(calculator, "websocket polling", expectSuccess);

                runtime.Disconnect(clientEndpoint);
            }
        }
Example #22
0
        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.");
                }
            }
        }
Example #23
0
        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);
                }
        }
Example #24
0
        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);
                    }
                }
        }
Example #25
0
        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");
                }
        }
Example #26
0
        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);
            }
        }
Example #27
0
        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();
        }
Example #28
0
        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));
                }
        }
Example #29
0
        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"));
            }
        }
Example #30
0
        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
        }
Example #31
0
        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);
        }
Example #32
0
        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));
                }
            }
        }
Example #33
0
        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 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 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"));
            }
        }