| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using System; |
| | 19 | | using System.Collections.Generic; |
| | 20 | | using System.Text; |
| | 21 | | using System.Threading; |
| | 22 | | using System.Threading.Tasks; |
| | 23 | | using UtilPack; |
| | 24 | |
|
| | 25 | | namespace CBAM.HTTP.Implementation |
| | 26 | | { |
| | 27 | | internal sealed class Reference<T> |
| | 28 | | { |
| | 29 | | public T Value { get; set; } |
| | 30 | | } |
| | 31 | |
|
| | 32 | | internal sealed class HTTPStatementInformationImpl<TRequestMetaData> : HTTPStatementInformation<TRequestMetaData> |
| | 33 | | { |
| | 34 | | private readonly Reference<Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInfo<TRequestMetaData>>>> |
| | 35 | | private readonly HTTPRequestInfo<TRequestMetaData> _initialRequest; |
| | 36 | |
|
| 2 | 37 | | public HTTPStatementInformationImpl( |
| 2 | 38 | | HTTPRequestInfo<TRequestMetaData> initialRequest, |
| 2 | 39 | | Reference<Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInfo<TRequestMetaData>>>> generator |
| 2 | 40 | | ) |
| | 41 | | { |
| 2 | 42 | | this._initialRequest = initialRequest; |
| 2 | 43 | | ArgumentValidator.ValidateNotNull( nameof( initialRequest.Request ), initialRequest.Request ); |
| 2 | 44 | | this._nextRequestGenerator = ArgumentValidator.ValidateNotNull( nameof( generator ), generator ); |
| 2 | 45 | | } |
| | 46 | |
|
| 2 | 47 | | public TRequestMetaData InitialRequestMetaData => this._initialRequest.RequestMetaData; |
| | 48 | |
|
| 2 | 49 | | public HTTPRequest InitialRequest => this._initialRequest.Request; |
| | 50 | |
|
| | 51 | | //public HTTPRequestInfo<TRequestMetaData> InitialRequest { get; } |
| | 52 | |
|
| 2 | 53 | | public Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInfo<TRequestMetaData>>> NextRequestGenerator |
| | 54 | | } |
| | 55 | |
|
| | 56 | | internal sealed class HTTPStatementImpl<TRequestMetaData> : HTTPStatement<TRequestMetaData> |
| | 57 | | { |
| | 58 | | //private const Int32 INITIAL = 0; |
| | 59 | | //private const Int32 RETURNING = 1; |
| | 60 | | //private const Int32 DONE = 2; |
| | 61 | |
|
| | 62 | | private readonly Reference<Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInfo<TRequestMetaData>>>> |
| | 63 | |
|
| | 64 | | public HTTPStatementImpl( |
| | 65 | | HTTPRequestInfo<TRequestMetaData> initialRequest |
| | 66 | | ) |
| | 67 | | { |
| | 68 | | //var state = INITIAL; |
| | 69 | | this.InitialRequest = initialRequest; |
| | 70 | | this.Information = new HTTPStatementInformationImpl<TRequestMetaData>( |
| | 71 | | initialRequest, |
| | 72 | | this._nextRequestGenerator = new Reference<Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInf |
| | 73 | | ); |
| | 74 | |
|
| | 75 | | // () => |
| | 76 | | //{ |
| | 77 | | // var generator = this.MessageGenerator; |
| | 78 | |
|
| | 79 | | // HTTPRequest retVal; |
| | 80 | | // if ( generator != null ) |
| | 81 | | // { |
| | 82 | | // retVal = generator(); |
| | 83 | | // } |
| | 84 | | // else if ( Interlocked.CompareExchange( ref state, RETURNING, INITIAL ) == INITIAL ) |
| | 85 | | // { |
| | 86 | | // try |
| | 87 | | // { |
| | 88 | | // retVal = this.StaticMessage; |
| | 89 | | // } |
| | 90 | | // finally |
| | 91 | | // { |
| | 92 | | // Interlocked.Exchange( ref state, DONE ); |
| | 93 | | // } |
| | 94 | | // } |
| | 95 | | // else |
| | 96 | | // { |
| | 97 | | // Interlocked.CompareExchange( ref state, DONE, INITIAL ); |
| | 98 | | // retVal = null; |
| | 99 | | // } |
| | 100 | |
|
| | 101 | | // return retVal; |
| | 102 | | //} ); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | public HTTPRequestInfo<TRequestMetaData> InitialRequest { get; } |
| | 106 | |
|
| | 107 | | public TRequestMetaData InitialRequestMetaData => this.InitialRequest.RequestMetaData; |
| | 108 | |
|
| | 109 | | public Func<HTTPResponseInfo<TRequestMetaData>, ValueTask<HTTPRequestInfo<TRequestMetaData>>> NextRequestGenerator |
| | 110 | | { |
| | 111 | | get => this._nextRequestGenerator.Value; |
| | 112 | | set => this._nextRequestGenerator.Value = value; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | //public HTTPRequest StaticMessage { get; set; } |
| | 116 | | //public Func<HTTPRequest> MessageGenerator { get; set; } |
| | 117 | |
|
| | 118 | | public HTTPStatementInformation<TRequestMetaData> Information { get; } |
| | 119 | |
|
| | 120 | | //Func<HTTPRequest> HTTPStatementInformation.MessageGenerator => this.MessageGenerator; |
| | 121 | | } |
| | 122 | | } |