Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            this.InitializeComponent();

            if (!IsSupportedOsVersion(Environment.OSVersion))
            {
                MessageBox.Show(
                    Properties.Resources.SystemVersionNotSupportedMessage,
                    Properties.Resources.SystemVersionNotSupportedCaption,
                    MessageBoxButton.OK);
                Application.Current.Shutdown();
            }

            this.DataContext = this;

            this.errorBufferListener = Trace.Listeners["errorBufferListener"] as RecentEventBufferTraceListener;
            if (this.errorBufferListener != null)
            {
                this.errorBufferListener.RecentEventBufferChanged += this.ErrorBufferListenerOnChanged;
            }

            Uri originUri;

            try
            {
                originUri = new Uri(Properties.Settings.Default.OriginUri.Trim());
            }
            catch (UriFormatException)
            {
                Trace.TraceError("Invalid format for listening origin: \"{0}\"", Properties.Settings.Default.OriginUri);

                // If we can't listen on origin, we can't serve any data whatsover, so there is no sense in continuing
                // setup
                return;
            }

            this.webserver.SensorChooserMap.Add(DefaultSensorChooserName, this.sensorChooser);
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.FileRootDirectory))
            {
                this.webserver.FileServerRootDirectory = Properties.Settings.Default.FileRootDirectory;

                try
                {
                    this.RootDirectoryTextRun.Text = Path.GetFullPath(Properties.Settings.Default.FileRootDirectory);
                }
                catch (ArgumentException e)
                {
                    this.ShowNotServingFilesUi();
                    Trace.TraceError("Exception encountered while parsing root directory for serving files:\n{0}", e);
                }
            }
            else
            {
                this.ShowNotServingFilesUi();
            }

            this.webserver.OriginUri = originUri;
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.AccessControlAllowedOrigins))
            {
                foreach (var origin in Properties.Settings.Default.AccessControlAllowedOrigins.Split(AllowedOriginSeparator))
                {
                    try
                    {
                        originUri = new Uri(origin.Trim());
                    }
                    catch (UriFormatException)
                    {
                        Trace.TraceError("Invalid format for access control allowed origin: {0}", origin);
                        continue;
                    }

                    this.webserver.AccessControlAllowedOrigins.Add(originUri);
                }
            }

            //// TODO: Optionally add factories here for custom handlers:
            ////       this.webserver.SensorStreamHandlerFactories.Add(new MyCustomSensorStreamHandlerFactory());
            //// Your custom factory would implement ISensorStreamHandlerFactory, in which the
            //// CreateHandler method would return a class derived from SensorStreamHandlerBase
            //// which overrides one or more of its virtual methods.

            this.webserver.Started += (server, args) => this.IsStarted = true;
            this.webserver.Stopped += (server, args) => this.IsStarted = false;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            this.InitializeComponent();

            if (!IsSupportedOsVersion(Environment.OSVersion))
            {
                MessageBox.Show(
                    Properties.Resources.SystemVersionNotSupportedMessage,
                    Properties.Resources.SystemVersionNotSupportedCaption,
                    MessageBoxButton.OK);
                Application.Current.Shutdown();
            }

            this.DataContext = this;

            this.errorBufferListener = Trace.Listeners["errorBufferListener"] as RecentEventBufferTraceListener;
            if (this.errorBufferListener != null)
            {
                this.errorBufferListener.RecentEventBufferChanged += this.ErrorBufferListenerOnChanged;
            }

            Uri originUri;
            try
            {
                originUri = new Uri(Properties.Settings.Default.OriginUri.Trim());
            }
            catch (UriFormatException)
            {
                Trace.TraceError("Invalid format for listening origin: \"{0}\"", Properties.Settings.Default.OriginUri);

                // If we can't listen on origin, we can't serve any data whatsover, so there is no sense in continuing
                // setup
                return;
            }

            this.webserver.SensorChooserMap.Add(DefaultSensorChooserName, this.sensorChooser);
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.FileRootDirectory))
            {
                this.webserver.FileServerRootDirectory = Properties.Settings.Default.FileRootDirectory;

                try
                {
                    this.RootDirectoryTextRun.Text = Path.GetFullPath(Properties.Settings.Default.FileRootDirectory);
                }
                catch (ArgumentException e)
                {
                    this.ShowNotServingFilesUi();
                    Trace.TraceError("Exception encountered while parsing root directory for serving files:\n{0}", e);
                }
            }
            else
            {
                this.ShowNotServingFilesUi();
            }

            this.webserver.OriginUri = originUri;
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.AccessControlAllowedOrigins))
            {
                foreach (var origin in Properties.Settings.Default.AccessControlAllowedOrigins.Split(AllowedOriginSeparator))
                {
                    try
                    {
                        originUri = new Uri(origin.Trim());
                    }
                    catch (UriFormatException)
                    {
                        Trace.TraceError("Invalid format for access control allowed origin: {0}", origin);
                        continue;
                    }

                    this.webserver.AccessControlAllowedOrigins.Add(originUri);
                }
            }

            //// TODO: Optionally add factories here for custom handlers:
            ////       this.webserver.SensorStreamHandlerFactories.Add(new MyCustomSensorStreamHandlerFactory());
            //// Your custom factory would implement ISensorStreamHandlerFactory, in which the
            //// CreateHandler method would return a class derived from SensorStreamHandlerBase
            //// which overrides one or more of its virtual methods.

            this.webserver.Started += (server, args) => this.IsStarted = true;
            this.webserver.Stopped += (server, args) => this.IsStarted = false;
        }