Пример #1
0
        private void RunClient(NetworkStream clientStream)
        {
            Channel channel;
            byte signal;
            Job currentJob = null;
            int pointNumber = 0;
            using (BinaryReader reader = new BinaryReader(clientStream))
            {
                using (BinaryWriter writer = new BinaryWriter(clientStream))
                {
                    channel = new Channel(reader, writer, true);

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

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

                                    currentJob = (Job)channel.ReadObject();
                                    pointNumber = channel.ReadData(typeof(int));
                                    _jobPointNumberDictionary.AddOrUpdate(currentJob.Number, new List<int> { pointNumber },
                                        (key, 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.Info($"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.ServerIP):
                                    {
                                        string ip = channel.ReadData(typeof(string));
                                        if (_server == null || _server.IpAddress.ToString() != ip)
                                        {
                                            _server = new HostInfo(ip, (int)Ports.ServerPort);
                                        }

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

                                default:
                                    _log.Error("Unknown signal received, stopping the application...");
                                    return;
                            }
                        }

                        catch (Exception ex)
                        {
                            _log.Error(ex.Message);
                            return;
                        }
                    }
                }
            }
        }
Пример #2
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;
                        }
                    }
                }
            }
        }