Exemple #1
0
        public Form1()
        {
            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                var exc = (Exception)args.ExceptionObject;
                var sb  = new StringBuilder();
                sb.AppendLine(exc.Message);
                sb.AppendLine(exc.StackTrace);

                File.WriteAllText("C:\\Temp\\WorkerManager.log", sb.ToString());
            };

            InitializeComponent();
            SetProcessPrio(ProcessPriorityClass.High);

            // ReSharper disable once AssignNullToNotNullAttribute
            this.settings = new Settings(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.json"));
            this.settings.Load();

            this.currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            this.thread1 = new Thread(ThreadStart1);
            this.thread1.Start(null);

            this.workersManager          = new WorkersManager(this.settings);
            this.workersManager.Added   += OnWorkerAdded;
            this.workersManager.Updated += OnWorkerUpdated;
            this.workersManager.Deleted += OnWorkerDeleted;
            this.workersManager.Load();

            this.endpoint = new WorkerManagerEndpoint(this.workersManager);

            var ip   = (string)this.settings["listen_ip"];
            var port = (long)this.settings["listen_port"];

            this.endpoint.Listen(ip, (int)port);

            var endpointUrl = $"http://localhost:{port}/worker";

            this.linkEndpoint.Text         = endpointUrl;
            this.linkEndpoint.LinkClicked += (sender, args) =>
            {
                Process.Start("explorer.exe", $"\"{endpointUrl}\"");
            };

            this.cbSpawner.Checked = (bool)this.settings["run_spawner"];

            // ReSharper disable once CoVariantArrayConversion
            this.comboBox1.Items.AddRange(this.settings["available_controllers"].Values <string>().ToArray());
            this.comboBox1.SelectedItem          = (string)this.settings["controller_host"];
            this.comboBox1.SelectedValueChanged += (sender, args) =>
            {
                this.settings["controller_host"] = this.comboBox1.SelectedItem.ToString();
                this.settings.Save();
                this.workersManager.RestartAll();
            };

            this.totalCpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            var controllerHostValue = (string)this.settings["controller_host"];

            if (!IPAddress.TryParse(controllerHostValue, out this.controllerHost))
            {
                var hostEntry = Dns.GetHostEntry(controllerHostValue);
                this.controllerHost = hostEntry.AddressList.FirstOrDefault();
                if (this.controllerHost == null)
                {
                    MessageBox.Show("Error", "Can't resolve DNS name: " + controllerHostValue, MessageBoxButtons.OK);
                    Application.Exit();
                }
            }


            this.heartbeatTimer = new System.Threading.Timer(SendHeartbeat, null, TimeSpan.FromMilliseconds(150), TimeSpan.FromSeconds(1));
            NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;

            this.ShowWindow();
        }
 public WorkerManagerEndpoint(IWorkersManager workersManager)
 {
     this.workersManager = workersManager;
 }
 public WorkesViewModels(IWorkersManager workersManager)
 {
     this._workersManager = workersManager;
 }