示例#1
0
        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();
        }
示例#2
0
        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.");
        }
示例#3
0
 public ILease WaitForLease(Specification spec)
 {
     return Lease(spec).Result;
 }
示例#4
0
 public bool CanLease(Specification spec)
 {
     if (_activeResources.Any(resource => resource.CanLease(this, spec)))
         return true;
     return _blueprints.Any(blueprint => blueprint.CanAllocate(this, spec));
 }
示例#5
0
 public async Task<ILease> Lease(IAllocator allocator, Specification spec)
 {
     LeasedOut = true;
     _folderLease = new FolderLease(this, Directory);
     return _folderLease;
 }
示例#6
0
 public bool CanLease(IAllocator allocator, Specification spec)
 {
     return spec.ResourceType == "Folder" && !LeasedOut;
 }
示例#7
0
 public async Task<IResource> Allocate(IAllocator allocator, Specification spec)
 {
     var directory = new DirectoryInfo("C:\\Workspace");
     return new FolderResource(directory.CreateSubdirectory("FolderAllocation_" + RandomString()));
 }
示例#8
0
 public bool CanAllocate(IAllocator allocator, Specification spec)
 {
     return spec.ResourceType == "Folder" &&
         Directory.Exists("C:\\Workspace");
 }