| | 1 | | /* |
| | 2 | | * Copyright 2018 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.Tasks; |
| | 22 | | using UtilPack; |
| | 23 | |
|
| | 24 | | using TDataProducerResult = System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<CBAM.NATS.NATSPublis |
| | 25 | |
|
| | 26 | | namespace CBAM.NATS |
| | 27 | | { |
| | 28 | | using TDataProducer = Func<TDataProducerResult>; |
| | 29 | | namespace Implementation |
| | 30 | | { |
| | 31 | | using TDataProducerFactory = Func<TDataProducer>; |
| | 32 | |
|
| | 33 | | // TODO Move to UtilPack |
| | 34 | | internal sealed class Reference<T> |
| | 35 | | { |
| | 36 | |
|
| 19 | 37 | | public T Value { get; set; } |
| | 38 | | } |
| | 39 | |
|
| | 40 | | internal abstract class NATSStatementInformationImpl : NATSStatementInformation |
| | 41 | | { |
| | 42 | | public NATSStatementInformationImpl( String subject ) |
| | 43 | | { |
| | 44 | | this.Subject = ArgumentValidator.ValidateNotEmpty( nameof( subject ), subject ); |
| | 45 | | if ( subject.ContainsNonASCIICharacters( IsInvalidASCIICharacter ) ) |
| | 46 | | { |
| | 47 | | throw new ArgumentException( "Invalid subject name: " + subject ); |
| | 48 | | } |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public String Subject { get; } |
| | 52 | |
|
| | 53 | | public static Boolean IsInvalidASCIICharacter( Byte ch ) |
| | 54 | | { |
| | 55 | | return ch <= 0x20; |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | internal sealed class NATSSubscribeStatementInformationImpl : NATSStatementInformationImpl, NATSSubscribeStatement |
| | 60 | | { |
| | 61 | | private readonly Reference<String> _queue; |
| | 62 | | private readonly Reference<Int64> _autoUnsubscribeAfter; |
| | 63 | | private readonly Reference<Func<NATSMessage, Boolean>> _dynamicUnsubscribe; |
| | 64 | |
|
| | 65 | | public NATSSubscribeStatementInformationImpl( |
| | 66 | | String subject, |
| | 67 | | Reference<String> queue, |
| | 68 | | Reference<Int64> autoUnsubscribeAfter, |
| | 69 | | Reference<Func<NATSMessage, Boolean>> dynamicUnsubscribe |
| | 70 | | ) : base( subject ) |
| | 71 | | { |
| | 72 | | this._queue = ArgumentValidator.ValidateNotNull( nameof( queue ), queue ); |
| | 73 | | this._autoUnsubscribeAfter = ArgumentValidator.ValidateNotNull( nameof( autoUnsubscribeAfter ), autoUnsubscr |
| | 74 | | this._dynamicUnsubscribe = ArgumentValidator.ValidateNotNull( nameof( dynamicUnsubscribe ), dynamicUnsubscri |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public String Queue => this._queue.Value; |
| | 78 | |
|
| | 79 | | public Int64 AutoUnsubscribeAfter => this._autoUnsubscribeAfter.Value; |
| | 80 | |
|
| | 81 | | public Func<NATSMessage, Boolean> DynamicUnsubscription => this._dynamicUnsubscribe.Value; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | internal abstract class NATSStatementImpl : NATSStatement |
| | 85 | | { |
| | 86 | | internal NATSStatementImpl( |
| | 87 | | NATSStatementInformationImpl information |
| | 88 | | ) |
| | 89 | | { |
| | 90 | | this.NATSStatementInformation = information; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public NATSStatementInformation NATSStatementInformation { get; } |
| | 94 | |
|
| | 95 | | public String Subject => this.NATSStatementInformation.Subject; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | internal sealed class NATSSubscribeStatementImpl : NATSStatementImpl, NATSSubscribeStatement |
| | 99 | | { |
| | 100 | | private readonly Reference<String> _queue; |
| | 101 | | private readonly Reference<Int64> _autoUnsubscribeAfter; |
| | 102 | | private readonly Reference<Func<NATSMessage, Boolean>> _dynamicUnsubscribe; |
| | 103 | |
|
| | 104 | | internal NATSSubscribeStatementImpl( |
| | 105 | | NATSSubscribeStatementInformationImpl information, |
| | 106 | | Reference<String> queue, |
| | 107 | | Reference<Int64> autoUnsubscribeAfter, |
| | 108 | | Reference<Func<NATSMessage, Boolean>> dynamicUnsubscribe |
| | 109 | | ) : base( information ) |
| | 110 | | { |
| | 111 | | this._queue = ArgumentValidator.ValidateNotNull( nameof( queue ), queue ); |
| | 112 | | this._autoUnsubscribeAfter = ArgumentValidator.ValidateNotNull( nameof( autoUnsubscribeAfter ), autoUnsubscr |
| | 113 | | this._dynamicUnsubscribe = ArgumentValidator.ValidateNotNull( nameof( dynamicUnsubscribe ), dynamicUnsubscri |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public String Queue |
| | 117 | | { |
| | 118 | | get => this._queue.Value; |
| | 119 | | set => this._queue.Value = value; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public Int64 AutoUnsubscribeAfter |
| | 123 | | { |
| | 124 | | get => this._autoUnsubscribeAfter.Value; |
| | 125 | | set => this._autoUnsubscribeAfter.Value = value; |
| | 126 | | } |
| | 127 | |
|
| | 128 | | public Func<NATSMessage, Boolean> DynamicUnsubscription |
| | 129 | | { |
| | 130 | | get => this._dynamicUnsubscribe.Value; |
| | 131 | | set => this._dynamicUnsubscribe.Value = value; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | String NATSSubscribeStatementInformation.Queue => this.Queue; |
| | 135 | |
|
| | 136 | | Func<NATSMessage, Boolean> NATSSubscribeStatementInformation.DynamicUnsubscription => this.DynamicUnsubscriptio |
| | 137 | | } |
| | 138 | |
|
| | 139 | | internal sealed class NATSPublishStatementImpl : NATSPublishStatement |
| | 140 | | { |
| | 141 | | private readonly Reference<TDataProducerFactory> _dataProducer; |
| | 142 | |
|
| | 143 | | internal NATSPublishStatementImpl( |
| | 144 | | NATSPublishStatementInformationImpl information, |
| | 145 | | Reference<TDataProducerFactory> dataProducer |
| | 146 | | ) |
| | 147 | | { |
| | 148 | | this.NATSStatementInformation = ArgumentValidator.ValidateNotNull( nameof( information ), information ); |
| | 149 | | this._dataProducer = ArgumentValidator.ValidateNotNull( nameof( dataProducer ), dataProducer ); |
| | 150 | | } |
| | 151 | |
|
| | 152 | | public TDataProducerFactory DataProducerFactory |
| | 153 | | { |
| | 154 | | get => this._dataProducer.Value; |
| | 155 | | set => this._dataProducer.Value = value; |
| | 156 | | } |
| | 157 | |
|
| | 158 | | public NATSPublishStatementInformation NATSStatementInformation { get; } |
| | 159 | |
|
| | 160 | | TDataProducerFactory NATSPublishStatementInformation.DataProducerFactory => this.DataProducerFactory; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | internal sealed class NATSPublishStatementInformationImpl : NATSPublishStatementInformation |
| | 164 | | { |
| | 165 | | private readonly Reference<TDataProducerFactory> _dataProducer; |
| | 166 | |
|
| | 167 | | public NATSPublishStatementInformationImpl( |
| | 168 | | Reference<TDataProducerFactory> dataProducer |
| | 169 | | ) |
| | 170 | | { |
| | 171 | | this._dataProducer = ArgumentValidator.ValidateNotNull( nameof( dataProducer ), dataProducer ); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | public TDataProducerFactory DataProducerFactory => this._dataProducer.Value; |
| | 175 | | } |
| | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| | 179 | | namespace UtilPack |
| | 180 | | { |
| | 181 | | public static partial class UtilPackExtensions |
| | 182 | | { |
| | 183 | | public static Boolean ContainsNonASCIICharacters( this String str, Func<Byte, Boolean> additionalCheck = null ) |
| | 184 | | { |
| | 185 | | var max = str.Length; |
| | 186 | | var retVal = false; |
| | 187 | | for ( var i = 0; i < str.Length && !retVal; ++i ) |
| | 188 | | { |
| | 189 | | var ch = str[i]; |
| | 190 | | if ( ch > Byte.MaxValue || ( additionalCheck?.Invoke( (Byte) ch ) ?? false ) ) |
| | 191 | | { |
| | 192 | | retVal = true; |
| | 193 | | } |
| | 194 | | } |
| | 195 | |
|
| | 196 | | return retVal; |
| | 197 | | } |
| | 198 | | } |
| | 199 | | } |