Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Create our log wrapper and events
            ILogger logger = new Logger();

            logger.OnError += OnError;
            logger.OnDebug += OnDebug;
            logger.OnWarn  += OnWarn;
            logger.OnInfo  += OnInfo;

            //Create our dependency utility and register the logger
            IDependencyUtility dependencyUtility = new DependencyUtility();

            dependencyUtility.Register <ILogger>(logger);

            //Set port and host
            int       port = Convert.ToInt32(1337);
            IPAddress host = IPAddress.Parse("0.0.0.0");

            //Start the server
            using (IServer server = new WebSocketServer("MyServerName", host, port, dependencyUtility))
            {
                //Start listening and blocking the main thread
                server.Run();
            }
        }
Ejemplo n.º 2
0
        public void DependencyUtilityRegistersAndResolvesInterfaces()
        {
            Guid id = Guid.NewGuid();
            IDependencyUtility utility = new DependencyUtility();

            utility.Register <ITestInterface>(new TestClass(id));

            ITestInterface resolvedObject = utility.Resolve <ITestInterface>();

            Assert.Equal(id, resolvedObject.Id);
        }
Ejemplo n.º 3
0
        public void ServerRunningFlagReturnsCorrectStatus()
        {
            IDependencyUtility dependencyUtility = new DependencyUtility();

            dependencyUtility.Register <ILogger>(new Mock <ILogger>().Object);
            dependencyUtility.Register <IRouteTable>(new Mock <IRouteTable>().Object);
            dependencyUtility.Register <ISecurityContainer>(new Mock <ISecurityContainer>().Object);
            dependencyUtility.Register <ITcpHandler>(new Mock <ITcpHandler>().Object);
            dependencyUtility.Register <IThreadHelper>(new Mock <IThreadHelper>().Object);

            using (IServer server = new Server.WebSocketServer(string.Empty, string.Empty, 0, dependencyUtility, 1, 3))
            {
                server.Run(false);

                Assert.True(server.IsRunning);

                server.Stop();

                Assert.False(server.IsRunning);
            }
        }
Ejemplo n.º 4
0
        public void ExtensionLoaderThrowsExceptionWhenFileDoesNotExist()
        {
            string json = "{\"host\":\"0.0.0.0\",\"port\":1337,\"extensions\":{\"extension1\":{\"path\":\"\",\"config\":{\"key\":\"value\"}}}}";
            IConfigurationHandler config = GetConfigurationHandler(json, out string path);

            IDependencyUtility dependencyUtility = new DependencyUtility();

            dependencyUtility.Register <ILogger>(new Mock <ILogger>().Object);
            dependencyUtility.Register <IConfigurationHandler>(config);

            IExtensionLoader loader = new ExtensionLoader(dependencyUtility);

            try
            {
                Assert.Throws(typeof(ConfigurationItemMissingException), () => loader.LoadExtensions());
            }
            finally
            {
                File.Delete(path);
            }
        }
Ejemplo n.º 5
0
        public void ServerRunningSpawnsCorrectAmountOfThreads()
        {
            IDependencyUtility dependencyUtility = new DependencyUtility();

            Mock <IThreadHelper> threadHelper = new Mock <IThreadHelper>();

            dependencyUtility.Register <ILogger>(new Mock <ILogger>().Object);
            dependencyUtility.Register <IRouteTable>(new Mock <IRouteTable>().Object);
            dependencyUtility.Register <ISecurityContainer>(new Mock <ISecurityContainer>().Object);
            dependencyUtility.Register <ITcpHandler>(new Mock <ITcpHandler>().Object);
            dependencyUtility.Register <IThreadHelper>(threadHelper.Object);

            using (IServer server = new Server.WebSocketServer(string.Empty, string.Empty, 0, dependencyUtility, 2, 4))
            {
                server.Run(false);

                //Listener threads: 2
                //Client threads: 2 * 4 (multiplicand)
                //Dead Connection thread: 1
                // = 11
                threadHelper.Verify(x => x.CreateAndRun(It.IsAny <ThreadStart>()), Times.Exactly(11));
                server.Stop();
            }
        }
Ejemplo n.º 6
0
        public void RemoveDeadConnectionsCleansUpCorrectly()
        {
            Mock <IThreadHelper> threadHelper = new Mock <IThreadHelper>();
            Mock <TcpClient>     client       = new Mock <TcpClient>();

            IDependencyUtility utility = new DependencyUtility();

            utility.Register <IThreadHelper>(threadHelper.Object);

            IWebSocketConnectionManager connectionManager = new WebSocketConnectionManager(utility);

            connectionManager.RegisterConnection(client.Object);
            connectionManager.RemoveDeadConnections();

            Assert.Equal(0, connectionManager.TotalConnections);
        }
Ejemplo n.º 7
0
        public void ConnectionManagerRegistersAndStartsClientThread()
        {
            Mock <IThreadHelper> threadHelper = new Mock <IThreadHelper>();
            Mock <TcpClient>     client       = new Mock <TcpClient>();

            IDependencyUtility utility = new DependencyUtility();

            utility.Register <IThreadHelper>(threadHelper.Object);

            IWebSocketConnectionManager connectionManager = new WebSocketConnectionManager(utility);

            connectionManager.RegisterConnection(client.Object);

            threadHelper.Verify(x => x.CreateAndRun(It.IsAny <ThreadStart>()), Times.Once);
            Assert.Equal(1, connectionManager.TotalConnections);
        }
Ejemplo n.º 8
0
        private void WriteExecuteDelete(Action <IConfigurationHandler> action, string json)
        {
            IDependencyUtility dependencyUtility = new DependencyUtility();

            dependencyUtility.Register(new Mock <ILogger>().Object);

            string path = $"{Path.GetTempPath()}/{DateTime.UtcNow.Ticks}.json";

            File.WriteAllText(path, json, Encoding.UTF8);

            IConfigurationHandler config = new ConfigurationHandler(null, path);

            try
            {
                action.Invoke(config);
            }
            finally
            {
                File.Delete(path);
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            //Create our dependency utility
            IDependencyUtility dependencyUtility = new DependencyUtility();

            //Create our security container
            ISecurityContainer securityContainer = GenerateSecurityContainer();

            //Register dependencies
            dependencyUtility.Register <ISecurityContainer>(securityContainer);

            //Set port and host
            int       port = Convert.ToInt32(1337);
            IPAddress host = IPAddress.Parse("0.0.0.0");

            //Start the server
            using (IServer server = new Server.WebSocketServer("MyServerName", host, port, dependencyUtility))
            {
                //Start listening and blocking the main thread
                server.Run();
            }
        }