Пример #1
0
        public void Start()
        {
            try
            {
                ConsoleSampleServer().Wait();
                Console.WriteLine("Server started. Press any key to exit...");
            }
            catch (Exception ex)
            {
                Utils.Trace("ServiceResultException:" + ex.Message);
                Console.WriteLine("Exception: {0}", ex.Message);
            }

            //try
            //{
            //    Console.ReadKey(true);
            //}
            //catch
            //{
            //    // wait forever if there is no console
            //    Thread.Sleep(Timeout.Infinite);
            //}

            if (server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                server.Dispose();
                server = null;

                status.Wait();
            }
        }
Пример #2
0
        public void Stop()
        {
            if (server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = server)
                {
                    // Stop status thread
                    server = null;
                    if (status != null)
                    {
                        status.Wait();
                    }
                    // Stop server and dispose
                    if (_server != null)
                    {
                        _server.Stop();
                    }

                    FinallyStopped = AllowFinallyStopped;

                    Log.Info("End of Server stopping!");
                }
            }
        }
Пример #3
0
        public void FilteredEventHandlerUsingCombinedFilter_TransformsEventsLocally()
        {
            // prepare event handler
            var handledValue = default(SampleEventArgs);
            var handler      = new EventHandler <SampleEventArgs>((sender, args) => handledValue = args);

            // attach client-side event filter
            var sample = new SampleServer();

            sample.SampleEvent += handler
                                  .AddFilter(new SampleEventFilter(321, 123, 111))
                                  .AddFilter(new SampleEventFilter(333, 123, 222)); // 123 will pass both filters

            // raise events, check results
            sample.RaiseSampleEvent(321);             // filtered out
            Assert.IsNull(handledValue);

            sample.RaiseSampleEvent(123);
            Assert.IsNotNull(handledValue);
            Assert.AreEqual(123, handledValue.Value);
            Assert.IsTrue(handledValue.IsTransformed);

            handledValue.Value = 111;
            sample.RaiseSampleEvent(456);             // filtered out
            Assert.AreEqual(111, handledValue.Value);
        }
Пример #4
0
        public void FilteredCustomHandlerUsingCombinedFilter_FiltersEventsLocally()
        {
            // prepare event handler
            var firstArgument  = 0;
            var secondArgument = string.Empty;
            var handled        = false;
            var handler        = new CustomEventType((first, second) =>
            {
                firstArgument  = first;
                secondArgument = second;
                handled        = true;
            });

            // initialize event filter
            handler = FilteredEventHandler.Create(handler, new CustomEventFilter("2.718", "1.618"));
            handler = FilteredEventHandler.Create(handler, new CustomEventFilter("3.14", "2.718"));

            // attach client-side event filter
            var sample = new SampleServer();

            sample.CustomEvent += handler;

            // raise events, check results
            sample.RaiseCustomEvent(3, ".14");             // filtered out
            Assert.IsFalse(handled);

            sample.RaiseCustomEvent(2, ".718");
            Assert.IsTrue(handled);
            Assert.AreEqual(2, firstArgument);
            Assert.AreEqual(".718", secondArgument);

            handled = false;
            sample.RaiseCustomEvent(1, ".618");             // filtered out
            Assert.IsFalse(handled);
        }
Пример #5
0
 public Engine()
 {
     Instance          = this;
     EncryptionManager = new EncryptionManager();
     LicenseManager    = new LicenseManager();
     PacketManager     = new PacketManager();
     SampleServer      = new SampleServer();
     SampleServer.Start();
 }
Пример #6
0
        public void Run()
        {
            try
            {
                ExitCode = ExitCode.ErrorServerNotStarted;
                ConsoleSampleServer().Wait();
                Console.WriteLine("Server started. Press Ctrl-C to exit...");
                ExitCode = ExitCode.ErrorServerRunning;
            }
            catch (Exception ex)
            {
                Utils.Trace("ServiceResultException:" + ex.Message);
                Console.WriteLine("Exception: {0}", ex.Message);
                ExitCode = ExitCode.ErrorServerException;
                return;
            }

            ManualResetEvent quitEvent = new ManualResetEvent(false);

            try
            {
                Console.CancelKeyPress += (sender, eArgs) => {
                    quitEvent.Set();
                    eArgs.Cancel = true;
                };
            }
            catch
            {
            }

            // wait for timeout or Ctrl-C
            quitEvent.WaitOne(ServerRunTime);

            if (Server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = Server)
                {
                    Server.CurrentInstance.SessionManager.SessionActivated -= EventStatus;
                    Server.CurrentInstance.SessionManager.SessionClosing   -= EventStatus;
                    Server.CurrentInstance.SessionManager.SessionCreated   -= EventStatus;

                    // Stop status thread
                    Server = null;
                    Status.Wait();
                    // Stop server and dispose
                    _server.Stop();
                }
            }

            ExitCode = ExitCode.Ok;
        }
Пример #7
0
        public void FilteredEventHandlerUsingFactorySyntax_FiltersEventsLocally()
        {
            // prepare event handler, attach event filter
            var handledValue = 0;
            var sample       = new SampleServer();

            sample.SampleEvent += FilteredEventHandler.Create((object sender, SampleEventArgs args) => handledValue = args.Value, new SampleEventFilter(321));

            // raise events, check results
            sample.RaiseSampleEvent(123);             // filtered out
            Assert.AreEqual(0, handledValue);

            sample.RaiseSampleEvent(321);
            Assert.AreEqual(321, handledValue);

            handledValue = 111;
            sample.RaiseSampleEvent(456);             // filtered out
            Assert.AreEqual(111, handledValue);
        }
Пример #8
0
        public void FilteredEventHandlerOfTypeEventHandler_FiltersEventsLocally()
        {
            // prepare event handler
            var handled = false;
            var handler = new EventHandler((sender, args) => handled = true);

            // attach client-side event filter
            var sample = new SampleServer();

            sample.TestEvent += handler.AddFilter(new TestEventFilter());

            // raise events, check results
            sample.RaiseTestEvent(EventArgs.Empty);             // filtered out
            Assert.IsFalse(handled);

            sample.RaiseTestEvent(null);
            Assert.IsTrue(handled);

            handled = false;
            sample.RaiseTestEvent(new EventArgs());             // filtered out
            Assert.IsFalse(handled);
        }
Пример #9
0
        public void FilteredEventHandlerUsingFlexibleFilter_FiltersEventsLocally()
        {
            // prepare event handler
            var handledValue = 0;
            var handler      = new EventHandler <SampleEventArgs>((sender, args) => handledValue = args.Value);

            // attach client-side event filter
            var sample = new SampleServer();

            sample.SampleEvent += handler.AddFilter((f, args) => args.Value == 123);

            // raise events, check results
            sample.RaiseSampleEvent(321);             // filtered out
            Assert.AreEqual(0, handledValue);

            sample.RaiseSampleEvent(123);
            Assert.AreEqual(123, handledValue);

            handledValue = 111;
            sample.RaiseSampleEvent(456);             // filtered out
            Assert.AreEqual(111, handledValue);
        }
Пример #10
0
        private async Task ConsoleSampleServer()
        {
            ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
            ApplicationInstance application = new ApplicationInstance();

            application.ApplicationName   = "UA Core Sample Server";
            application.ApplicationType   = ApplicationType.Server;
            application.ConfigSectionName = "Opc.Ua.SampleServer";

            // load the application configuration.
            ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);

            // check the application certificate.
            bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);

            if (!haveAppCertificate)
            {
                throw new Exception("Application instance certificate invalid!");
            }

            if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
            {
                config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
            }

            // start the server.
            server = new SampleServer();
            await application.Start(server);

            // start the status thread
            status = Task.Run(new Action(StatusThread));

            // print notification on session events
            server.CurrentInstance.SessionManager.SessionActivated += EventStatus;
            server.CurrentInstance.SessionManager.SessionClosing   += EventStatus;
            server.CurrentInstance.SessionManager.SessionCreated   += EventStatus;
        }
        public void Run()
        {
            try
            {
                ConsoleSampleServer().Wait();
                Console.WriteLine("Server started. Press any key to exit...");
            }
            catch (Exception ex)
            {
                Utils.Trace("ServiceResultException:" + ex.Message);
                Console.WriteLine("Exception: {0}", ex.Message);
            }

            try
            {
                Console.ReadKey(true);
            }
            catch
            {
                // wait forever if there is no console
                Thread.Sleep(Timeout.Infinite);
            }

            if (server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = server)
                {
                    // Stop status thread
                    server = null;
                    status.Wait();
                    // Stop server and dispose
                    _server.Stop();
                }
            }
        }
Пример #12
0
        private async Task ConsoleSampleServer()
        {
            ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
            ApplicationInstance application = new ApplicationInstance();

            application.ApplicationName   = "UA Core Sample Server";
            application.ApplicationType   = ApplicationType.Server;
            application.ConfigSectionName = Utils.IsRunningOnMono() ? "Opc.Ua.MonoSampleServer" : "Opc.Ua.SampleServer";

            // load the application configuration.
            ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);

            // check the application certificate.
            bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);

            if (!haveAppCertificate)
            {
                throw new Exception("Application instance certificate invalid!");
            }

            if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
            {
                config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
            }

            // start the server.
            Server = new SampleServer();
            await application.Start(Server);

            if (ReverseConnectUrl != null)
            {
                Server.AddReverseConnection(ReverseConnectUrl);
            }

            var reverseConnections = Server.GetReverseConnections();

            if (reverseConnections?.Count > 0)
            {
                // print reverse connect info
                Console.WriteLine("Reverse Connect Clients:");
                foreach (var connection in reverseConnections)
                {
                    Console.WriteLine(connection.Key);
                }
            }

            // print endpoint info
            Console.WriteLine("Server Endpoints:");
            var endpoints = Server.GetEndpoints().Select(e => e.EndpointUrl).Distinct();

            foreach (var endpoint in endpoints)
            {
                Console.WriteLine(endpoint);
            }

            // start the status thread
            Status = Task.Run(new Action(StatusThread));

            // print notification on session events
            Server.CurrentInstance.SessionManager.SessionActivated += EventStatus;
            Server.CurrentInstance.SessionManager.SessionClosing   += EventStatus;
            Server.CurrentInstance.SessionManager.SessionCreated   += EventStatus;
        }