Example #1
0
        public Worker(string name,
                      IpAddress ipAddress,
                      int port,
                      IReplyServer electionReplyServer,
                      string primaryDirectory,
                      IDirectoryPreparerAsync directoryPreparer,
                      IElectionRequester electionRequester,
                      IMasterFileReader masterFileReader,
                      IMasterFileWriter masterFileWriter,
                      IUnitOfWorkFileReader unitOfWorkFileReader,
                      IUnitOfWorkFileWriter unitOfWorkFileWriter,
                      IWorkerLogger logger         = null,
                      IWorkProcessor workProcessor = null
                      )
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name", string.Format(Messages.StringNullEmptyOrWhiteSpace, "name"));
            }
            Connection = new WorkerConnection
            {
                Name      = name,
                IpAddress = ipAddress ?? throw new ArgumentNullException("ipAddress", string.Format(Messages.ObjectNull, "ipAddress")),
                                  Port = (port > 1024 && port < 65536) ? port : throw new ArgumentNullException("port", Messages.PortNoInRange)
            };
            _DirectoryPreparer    = directoryPreparer ?? throw new ArgumentNullException("directoryPreparer", string.Format(Messages.ObjectNull, "directoryPreparer"));;
            _PrimaryDirectory     = primaryDirectory;
            _ReplyServer          = electionReplyServer ?? throw new ArgumentNullException("electionReplyServer", string.Format(Messages.ObjectNull, "electionReplyServer"));
            _ElectionRequester    = electionRequester ?? throw new ArgumentNullException("electionRequester", string.Format(Messages.ObjectNull, "electionRequester"));
            _MasterFileReader     = masterFileReader ?? throw new ArgumentNullException("masterFileReader", string.Format(Messages.ObjectNull, "masterFileReader"));
            _MasterFileWriter     = masterFileWriter ?? throw new ArgumentNullException("masterFileWriter", string.Format(Messages.ObjectNull, "masterFileWriter"));
            _UnitOfWorkFileReader = unitOfWorkFileReader ?? throw new ArgumentNullException("unitOfWorkFileReader", string.Format(Messages.ObjectNull, "unitOfWorkFileReader"));
            _UnitOfWorkFileWriter = unitOfWorkFileWriter ?? throw new ArgumentNullException("unitOfWorkFileWriter", string.Format(Messages.ObjectNull, "unitOfWorkFileWriter"));

            _Logger = logger;
            if (_Logger == null)
            {
                _Logger = new MultiLogger(name, new ConsoleLogger(),
                                          new Log4NetLogger(LogManager.GetLogger(Assembly.GetExecutingAssembly(), $"{Connection.Name}.log")));
            }

            _WorkProcessor             = workProcessor ?? new WorkProcessor(_Logger);
            _WorkProcessor.OnComplete += OnWorkComplete;
        }
        public async Task <UnitOfWork> ProcessWorkAsync(IUnitOfWorkFileReader reader, UnitOfWork unitOfWork)
        {
            _Logger?.Debug("Started processing: " + unitOfWork?.Id);
            if (IsProcessing)
            {
                throw new Exception("Already processing a unit of work.");
            }
            IsProcessing = true;
            var text = await reader.ReadAllTextAsync(unitOfWork.AssociateFile);

            var strings = Regex.Split(text, "\\s+", RegexOptions.Multiline);
            var result  = Levenshtein.WagnerFischer(strings[0], strings[1]);

            unitOfWork.Result = result[result.GetLength(0) - 1, result.GetLength(1) - 1];
            OnComplete?.Invoke(this, new WorkCompleteEventArgs {
                UnitOfWork = unitOfWork
            });
            IsProcessing = false;
            _Logger?.Debug("Finished processing: " + unitOfWork?.Id);
            return(unitOfWork);
        }