Ejemplo n.º 1
0
        private void DeletePoint(int jobNum, int pointNum)
        {
            _jobPointNumberDictionary.AddOrUpdate(jobNum, new List <int>(), (key, oldvalue) =>
            {
                lock (oldvalue) oldvalue.Remove(pointNum);
                return(oldvalue);
            });

            if (_server != null)
            {
                if (_server.IsConnected || _server.Connect())
                {
                    _server.Writer.Write((byte)Constants.PointDeleted);
                    _server.Writer.Write(jobNum);
                    _server.Writer.Write(pointNum);
                }
            }
        }
Ejemplo n.º 2
0
 public void AddDaemon(string ip)
 {
     if (string.IsNullOrEmpty(ip))
     {
         return;
     }
     if (HostList.All(h => h.IpAddress.ToString() != ip))
     {
         var newHost = new HostInfo(ip, (int)Ports.DaemonPort);
         if (newHost.Connect())
         {
             lock (_hostSyncRoot)
             {
                 HostList = HostList.Concat(new[] { newHost }).ToArray();
             }
         }
     }
 }
Ejemplo n.º 3
0
        private static void TryConnectToHostServer()
        {
            if (!string.IsNullOrWhiteSpace(_hostServerIp))
            {
                int serverPort = (int)Ports.ServerPort;

                _log.Information($"Connecting to Host Server, IP: {_hostServerIp}, port: {serverPort}");

                _server = new HostInfo(_hostServerIp, serverPort);
                if (_server.Connect())
                {
                    _server.SendLocalIp();
                }
            }
            else
            {
                _log.Warning($"Environment Variable {EnvironmentVariables.HostServerAddress} is not set");
            }
        }
Ejemplo n.º 4
0
        private void RunClient(NetworkStream clientStream)
        {
            using (BinaryReader reader = new BinaryReader(clientStream))
            {
                using (BinaryWriter writer = new BinaryWriter(clientStream))
                {
                    Channel channel     = new Channel(reader, writer);
                    Job     currentJob  = null;
                    int     pointNumber = 0;

                    while (true)
                    {
                        try
                        {
                            byte signal = channel.ReadByte();

                            switch (signal)
                            {
                            case ((byte)Constants.RecieveTask):

                                currentJob  = (Job)channel.ReadObject();
                                pointNumber = channel.ReadInt();
                                _jobPointNumberDictionary.AddOrUpdate(currentJob.Number, new List <int> {
                                    pointNumber
                                },
                                                                      (key, oldvalue) =>
                                {
                                    lock (oldvalue) oldvalue.Add(pointNumber);
                                    return(oldvalue);
                                });
                                _cancellationDictionary.AddOrUpdate(currentJob.Number, new CancellationTokenSource(),
                                                                    (key, oldValue) => oldValue);
                                continue;

                            case ((byte)Constants.ExecuteClass):

                                if (currentJob != null)
                                {
                                    var cancellationTokenSource = _cancellationDictionary[currentJob.Number];
                                    if (!_cancellationDictionary[currentJob.Number].IsCancellationRequested)
                                    {
                                        var executor = new ModuleExecutor(channel, currentJob, pointNumber);

                                        try
                                        {
                                            executor.Run(cancellationTokenSource.Token);
                                        }
                                        catch (OperationCanceledException)
                                        {
                                            _log.Information($"Point N {currentJob.Number}:{pointNumber} was cancelled");
                                        }

                                        DeletePoint(currentJob.Number, pointNumber);

                                        if (_jobPointNumberDictionary[currentJob.Number].Count == 0)
                                        {
                                            lock (_locker)
                                            {
                                                if (File.Exists(currentJob.FileName))
                                                {
                                                    File.Delete(currentJob.FileName);
                                                }
                                            }
                                        }
                                    }
                                }

                                return;

                            case ((byte)Constants.LoadFile):
                            {
                                LoadFile(channel, currentJob);
                                continue;
                            }

                            case ((byte)Constants.ProcessorsCountRequest):
                            {
                                channel.WriteData(Environment.ProcessorCount);
                                continue;
                            }

                            case ((byte)Constants.LinpackRequest):
                            {
                                var linpack = new Linpack();
                                linpack.RunBenchmark();
                                channel.WriteData(linpack.MFlops);
                                continue;
                            }

                            case ((byte)Constants.IpAddress):
                            {
                                string ip = channel.ReadString();
                                if (_server == null || !_server.IsConnected)
                                {
                                    _server = new HostInfo(ip, (int)Ports.ServerPort);
                                    if (!_server.Connect())
                                    {
                                        TryConnectToHostServer();
                                    }
                                    else
                                    {
                                        _hostServerIp = ip;
                                    }
                                }

                                continue;
                            }

                            case ((byte)Constants.CancelJob):
                            {
                                int jobNumber = channel.ReadInt();
                                CancellationTokenSource tokenSource;
                                if (_cancellationDictionary.TryGetValue(jobNumber, out tokenSource))
                                {
                                    tokenSource.Cancel();
                                    _log.Information($"Cancelling job N {jobNumber}");
                                }
                                else
                                {
                                    _log.Information($"Job N {jobNumber} does not exist or does not have cancellation token");
                                }
                                continue;
                            }

                            default:
                                _log.Warning($"Unknown signal received: {signal}");
                                return;
                            }
                        }

                        catch (Exception ex)
                        {
                            Log.Error(ex, ex.Message);
                            return;
                        }
                    }
                }
            }
        }