Beispiel #1
0
        protected LaundryRobot([NotNull] string name, [NotNull][ItemNotNull] IEnumerable <LaundryMachineVault> laundryMachines, [NotNull][ItemNotNull] IEnumerable <ILaundryRepository> laundryRepositories)
        {
            if (laundryMachines == null)
            {
                throw new ArgumentNullException(nameof(laundryMachines));
            }
            if (laundryRepositories == null)
            {
                throw new ArgumentNullException(nameof(laundryRepositories));
            }

            var tempRep = laundryRepositories.ToArray();
            var tempArr = laundryMachines.ToArray();

            if (tempArr.Any(itm => itm == null))
            {
                throw new ArgumentException(@"One or more laundry machines was null.", nameof(laundryMachines));
            }
            if (tempRep.Any(itm => itm == null))
            {
                throw new ArgumentException(@"One or more laundry repositories was null.", nameof(laundryRepositories));
            }
            Repositories        = tempRep.ToImmutableArray();
            Machines            = tempArr.ToImmutableArray();
            _robotName          = name ?? throw new ArgumentNullException(nameof(name));
            RobotId             = Guid.NewGuid();
            _concreteType       = new LocklessConcreteType(this);
            _eventRaisingThread = EventRaisingThread.CreateEventRaiser($"RobotEvent{name}");
            _robotThread        = new Thread(RobotThread)
            {
                Name = name, IsBackground = true, Priority = ThreadPriority.Lowest
            };
        }
 private LaundryMachineTaskExecutionContext()
 {
     _id = Interlocked.Increment(ref _sCount);
     _completionEventRaiser = EventRaisingThread.CreateEventRaiser($"LmTaskExCtxtEvntRsr{_id}");
     _thread = new Thread(ThreadLoop);
     _thread.IsBackground = true;
     _thread.Name         = $"LaundryMachineTaskThread{_id}";
     _thread.Priority     = ThreadPriority.Normal;
     if (!_threadStatus.TrySetInstantiated() || !_threadStatus.TrySetRequestedThreadStart())
     {
         throw new StateLogicErrorException("Unable to setup the status flags.");
     }
 }
Beispiel #3
0
        protected LaundryMachine(TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp)
        {
            LaundryStateMachine stateMachine = null;

            try
            {
                TimeToAddOneUnitDampness    = addOneUnitDamp;
                TimeToRemoveOneUnitDirt     = removeOneUnitDirt;
                TimeToRemoveOneUnitDampness = removeOneUnitDamp;
                MachineId    = Interlocked.Increment(ref s_idCounter);
                _eventRaiser = EventRaisingThread.CreateEventRaiser($"EventRaiserLm{MachineId}");
                stateMachine = new LaundryStateMachine(_delayAtBottom, _maxNoYieldBeforeWait, addOneUnitDamp,
                                                       removeOneUnitDirt, removeOneUnitDamp);
                _flagVault               = stateMachine.FlagVault;
                _stateVault              = stateMachine.StateVault;
                stateMachine.Disposed   += _stateMachine_Disposed;
                stateMachine.Terminated += _stateMachine_Terminated;
                stateMachine.TransitionPredicateTrue   += _stateMachine_TransitionPredicateTrue;
                stateMachine.StateChanged              += _stateMachine_StateChanged;
                stateMachine.UnexpectedExceptionThrown += _stateMachine_UnexpectedExceptionThrown;
                _stateCodeForDisplay = _stateVault.CopyCurrentValue(TimeSpan.FromSeconds(2));
                Debug.Assert(_stateCodeForDisplay == LaundryMachineStateCode.PoweredDown);
                stateMachine.StartStateMachine();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLineAsync(ex.ToString());
                try
                {
                    if (stateMachine != null)
                    {
                        _stateMachine.Disposed   -= _stateMachine_Disposed;
                        _stateMachine.Terminated -= _stateMachine_Terminated;
                        _stateMachine.TransitionPredicateTrue   -= _stateMachine_TransitionPredicateTrue;
                        _stateMachine.StateChanged              -= _stateMachine_StateChanged;
                        _stateMachine.UnexpectedExceptionThrown -= _stateMachine_UnexpectedExceptionThrown;
                        _stateMachine.Dispose();
                    }
                }
                catch (Exception ex2)
                {
                    Console.Error.WriteLineAsync(ex2.ToString());
                    Environment.Exit(-1);
                    throw;
                }
                throw;
            }
            _stateMachine = stateMachine;
            Debug.Assert(_stateMachine != null);
        }
 private LaundryRepository([NotNull] string description, bool forDirty)
 {
     Description     = description ?? throw new ArgumentNullException(nameof(description));
     _eventThread    = EventRaisingThread.CreateEventRaiser(description);
     ForDirtyLaundry = forDirty;
 }