| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.Batch |
| | 5 | | { |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Threading; |
| | 9 | | using Models = Microsoft.Azure.Batch.Protocol.Models; |
| | 10 | |
|
| | 11 | | internal class AsyncListComputeNodesEnumerator : PagedEnumeratorBase<ComputeNode> |
| | 12 | | { |
| | 13 | | private readonly PoolOperations _parentPoolOps; |
| | 14 | | private readonly string _poolId; |
| | 15 | | private readonly BehaviorManager _behaviorMgr; |
| | 16 | | private readonly DetailLevel _detailLevel; |
| | 17 | |
|
| | 18 | | #region // constructors |
| | 19 | |
|
| 3 | 20 | | internal AsyncListComputeNodesEnumerator( |
| 3 | 21 | | PoolOperations parentPoolOps, |
| 3 | 22 | | string poolId, |
| 3 | 23 | | BehaviorManager behaviorMgr, |
| 3 | 24 | | DetailLevel detailLevel) |
| | 25 | | { |
| 3 | 26 | | _parentPoolOps = parentPoolOps; |
| 3 | 27 | | _poolId = poolId; |
| 3 | 28 | | _behaviorMgr = behaviorMgr; |
| 3 | 29 | | _detailLevel = detailLevel; |
| 3 | 30 | | } |
| | 31 | |
|
| | 32 | | #endregion // constructors |
| | 33 | |
|
| | 34 | | public override ComputeNode Current // for IPagedEnumerator<T> and IEnumerator<T> |
| | 35 | | { |
| | 36 | | get |
| | 37 | | { |
| | 38 | | // start with the current object off of base |
| 1 | 39 | | object curObj = base._currentBatch[base._currentIndex]; |
| | 40 | |
|
| | 41 | | // it must be a protocol object from previous call |
| 1 | 42 | | Models.ComputeNode protocolObj = curObj as Models.ComputeNode; |
| | 43 | |
|
| | 44 | | Debug.Assert(null != protocolObj); |
| | 45 | |
|
| | 46 | | // wrap protocol object |
| 1 | 47 | | ComputeNode wrapped = new ComputeNode(_parentPoolOps.ParentBatchClient, _poolId, protocolObj, _behaviorM |
| | 48 | |
|
| 1 | 49 | | return wrapped; |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// fetch another batch of objects from the server |
| | 55 | | /// </summary> |
| | 56 | | protected async override System.Threading.Tasks.Task GetNextBatchFromServerAsync(SkipTokenHandler skipHandler, C |
| | 57 | | { |
| | 58 | | do |
| | 59 | | { |
| | 60 | | // start the protocol layer call |
| 4 | 61 | | var asyncTask = _parentPoolOps.ParentBatchClient.ProtocolLayer.ListComputeNodes( |
| 4 | 62 | | _poolId, |
| 4 | 63 | | skipHandler.SkipToken, |
| 4 | 64 | | _behaviorMgr, |
| 4 | 65 | | _detailLevel, |
| 4 | 66 | | cancellationToken); |
| | 67 | |
|
| | 68 | | // extract the response |
| 4 | 69 | | var response = await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| | 70 | |
|
| | 71 | | // remember any skiptoken returned. This also sets the bool |
| 3 | 72 | | skipHandler.SkipToken = response.Body.NextPageLink; |
| | 73 | |
|
| | 74 | | // remember the protocol tasks returned |
| 3 | 75 | | base._currentBatch = null; |
| | 76 | |
|
| 3 | 77 | | if (null != response.Body.GetEnumerator()) |
| | 78 | | { |
| 3 | 79 | | base._currentBatch = response.Body.ToArray(); |
| | 80 | | } |
| | 81 | | } |
| | 82 | | // it is possible for there to be no results so we keep trying |
| 3 | 83 | | while (skipHandler.ThereIsMoreData && ((null == _currentBatch) || _currentBatch.Length <= 0)); |
| 2 | 84 | | } |
| | 85 | | } |
| | 86 | | } |