protected internal void LockByBox(BoxModule box) { lock(locks) { if(!locks.Contains(box)) { locks.Add(box); //Debug.WriteLine("Vstupuji " + this.UserName); Stack<IBoxModule> stack = new Stack<IBoxModule>(); foreach(IBoxModule otherBox in this.ConnectionsFrom()) { stack.Push(otherBox); lockUnlockRecursive(otherBox, box, stack, true); stack.Pop(); } } } }
protected internal void UnlockByBox(BoxModule box) { lock(locks) { if(locks.Contains(box)) { locks.Remove(box); //Debug.WriteLine("Opoustim " + this.UserName); Stack<IBoxModule> stack = new Stack<IBoxModule>(); foreach(IBoxModule otherBox in this.ConnectionsFrom()) { stack.Push(otherBox); lockUnlockRecursive(otherBox, box, stack, false); stack.Pop(); } } } }
private void lockUnlockRecursive(IBoxModule box, BoxModule lockBox, Stack<IBoxModule> stack, bool locking) { BoxModule boxModule = box as BoxModule; if(boxModule!=null) { if(locking) boxModule.LockByBox(lockBox); else boxModule.UnlockByBox(lockBox); } else { foreach(IBoxModule newBox in box.ConnectionsFrom()) { if(!stack.Contains(newBox)) { stack.Push(newBox); lockUnlockRecursive(newBox, lockBox, stack, locking); stack.Pop(); } } } }