static void Main(string[] args) { // force reference for reflection. var folderBlueprint = new FolderBlueprint(); var spec = new Specification("Folder", new object()); var allocator = new DefaultAllocator(); if (!allocator.CanLease(spec)) { Console.WriteLine("Can't provide leased folder."); } var folderLease = allocator.Lease(spec).Result as FolderLease; Debug.Assert(folderLease != null, "folderLease != null"); Console.WriteLine("Got folder lease at " + folderLease.Directory.FullName); folderLease.Release(); }
public async Task<ILease> Lease(Specification spec) { // Check to see if any resources can give us a lease. foreach (var resource in _activeResources.Where(resource => resource.CanLease(this, spec))) return await resource.Lease(this, spec); // Nope, see if any blueprints can allocate a resource that // will give us a lease. foreach (var blueprint in _blueprints.Where(blueprint => blueprint.CanAllocate(this, spec))) { var resource = await blueprint.Allocate(this, spec); if (resource.CanLease(this, spec)) return await resource.Lease(this, spec); resource.Deallocate(); throw new Exception("Blueprint stated that it could allocate a resource matching the spec, but the new resource could not lease."); } // Otherwise throw an exception. throw new Exception("Lease can't be provided."); }
public ILease WaitForLease(Specification spec) { return Lease(spec).Result; }
public bool CanLease(Specification spec) { if (_activeResources.Any(resource => resource.CanLease(this, spec))) return true; return _blueprints.Any(blueprint => blueprint.CanAllocate(this, spec)); }
public async Task<ILease> Lease(IAllocator allocator, Specification spec) { LeasedOut = true; _folderLease = new FolderLease(this, Directory); return _folderLease; }
public bool CanLease(IAllocator allocator, Specification spec) { return spec.ResourceType == "Folder" && !LeasedOut; }
public async Task<IResource> Allocate(IAllocator allocator, Specification spec) { var directory = new DirectoryInfo("C:\\Workspace"); return new FolderResource(directory.CreateSubdirectory("FolderAllocation_" + RandomString())); }
public bool CanAllocate(IAllocator allocator, Specification spec) { return spec.ResourceType == "Folder" && Directory.Exists("C:\\Workspace"); }