public void ExecuteCalcJob([NotNull] MessageFromServerToClient job, [NotNull] CalcExecutor calcExecutor, [NotNull] RequestSocket client)
        {
            var cdp = new CalcDirectoryPreparer(_mySettings, _logger, _threadId);

            cdp.Run();
            HouseCreationAndCalculationJob hcj = null;

            if (!string.IsNullOrWhiteSpace(job.HouseJobStr))
            {
                hcj = JsonConvert.DeserializeObject <HouseCreationAndCalculationJob>(job.HouseJobStr);
                if (hcj.CalcSpec == null)
                {
                    hcj.CalcSpec = JsonCalcSpecification.MakeDefaultsForProduction();
                }

                hcj.CalcSpec.OutputDirectory = "Results";
                string jsonFileName = Path.Combine(_mySettings.ClientSettings.LPGCalcDirectory, "calcjob.json");
                string correctedJob = JsonConvert.SerializeObject(hcj, Formatting.Indented);
                File.WriteAllText(jsonFileName, correctedJob);
                calcExecutor.Run(hcj);
            }
            else
            {
                _logger.Info("Client #" + _threadId + ": Got a task with an exe, not real, waiting 5s", _threadId);
                Thread.Sleep(5000);
            }

            ReportFinishedCalcJob(job, client, hcj);
        }
        private void ReportFinishedCalcJob([NotNull] MessageFromServerToClient job,
                                           [NotNull] RequestSocket client,
                                           [CanBeNull] HouseCreationAndCalculationJob hcj)
        {
            var msg = new MessageFromClientToServer(ClientRequestEnum.ReportFinish, _threadId.Name, "finished calculation", job.TaskGuid);

            if (hcj != null)
            {
                msg.Scenario   = hcj.Scenario;
                msg.Year       = hcj.Year;
                msg.Trafokreis = hcj.Trafokreis;
                msg.HouseName  = hcj.House?.Name;
            }

            string        resultDirectory = Path.Combine(_mySettings.ClientSettings.LPGCalcDirectory, "Results");
            DirectoryInfo di = new DirectoryInfo(resultDirectory);

            if (di.Exists)
            {
                var files         = di.GetFiles("*.*", SearchOption.AllDirectories);
                var filteredFiles = new List <FileInfo>();
                foreach (var file in files)
                {
                    if (file.Name.ToLower(CultureInfo.InvariantCulture).EndsWith(".dat"))
                    {
                        _logger.Error("Refusing dat file: " + file.FullName, _threadId);
                        continue;
                    }

                    if (file.Length > 100_000_000)
                    {
                        _logger.Error("Refusing too big file: " + file.FullName, _threadId);
                        continue;
                    }

                    filteredFiles.Add(file);
                }

                msg.ResultFiles = MsgFile.ReadMsgFiles(true, filteredFiles, di, _logger, _threadId);
                var reportAnswer = MakeRequest(client, msg);
                _logger.Info("Answer from the finish report:" + reportAnswer.ServerResponse, _threadId);
            }
            else
            {
                _logger.Info("No output directory created, reporting failure to the server", _threadId);
                var msg2 = new MessageFromClientToServer(ClientRequestEnum.ReportFailure,
                                                         _threadId.Name,
                                                         "result directory is missing",
                                                         job.TaskGuid);
                var reportAnswer = MakeRequest(client, msg2);
                _logger.Info("Answer from the finish report:" + reportAnswer.ServerResponse, _threadId);
            }
        }
        private bool AreAllFilesIdentical([NotNull] MessageFromServerToClient job)
        {
            if (job.LpgFiles.Count == 0)
            {
                throw new DistSimException("No lpg files were seen");
            }

            foreach (var file in job.LpgFiles)
            {
                string dstPath = Path.Combine(_mySettings.ClientSettings.LPGRawDirectory, file.FileName);
                var    dstFi   = new FileInfo(dstPath);
                if (!dstFi.Exists)
                {
                    return(false);
                }

                if (dstFi.Length != file.FileLength)
                {
                    return(false);
                }
            }

            return(true);
        }