Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015-2016 gRPC authors. |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | 054fae7 | 2016-03-23 07:54:50 -0700 | [diff] [blame] | 19 | using Grpc.Core.Internal; |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 20 | using Grpc.Core.Utils; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 21 | |
| 22 | namespace Grpc.Core |
| 23 | { |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 24 | /// <summary> |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 25 | /// Generic base class for client-side stubs. |
| 26 | /// </summary> |
| 27 | public abstract class ClientBase<T> : ClientBase |
Jan Tattermusch | b240358 | 2016-03-23 07:40:28 -0700 | [diff] [blame] | 28 | where T : ClientBase<T> |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 29 | { |
| 30 | /// <summary> |
Jan Tattermusch | b240358 | 2016-03-23 07:40:28 -0700 | [diff] [blame] | 31 | /// Initializes a new instance of <c>ClientBase</c> class that |
| 32 | /// throws <c>NotImplementedException</c> upon invocation of any RPC. |
| 33 | /// This constructor is only provided to allow creation of test doubles |
| 34 | /// for client classes (e.g. mocking requires a parameterless constructor). |
| 35 | /// </summary> |
| 36 | protected ClientBase() : base() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | /// <summary> |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 41 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 42 | /// </summary> |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 43 | /// <param name="configuration">The configuration.</param> |
| 44 | protected ClientBase(ClientBaseConfiguration configuration) : base(configuration) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 50 | /// </summary> |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 51 | /// <param name="channel">The channel to use for remote call invocation.</param> |
| 52 | public ClientBase(Channel channel) : base(channel) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 58 | /// </summary> |
| 59 | /// <param name="callInvoker">The <c>CallInvoker</c> for remote call invocation.</param> |
| 60 | public ClientBase(CallInvoker callInvoker) : base(callInvoker) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
Jan Tattermusch | c831a44 | 2016-03-22 17:23:55 -0700 | [diff] [blame] | 65 | /// Creates a new client that sets host field for calls explicitly. |
| 66 | /// gRPC supports multiple "hosts" being served by a single server. |
| 67 | /// By default (if a client was not created by calling this method), |
| 68 | /// host <c>null</c> with the meaning "use default host" is used. |
| 69 | /// </summary> |
| 70 | public T WithHost(string host) |
| 71 | { |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 72 | var newConfiguration = this.Configuration.WithHost(host); |
| 73 | return NewInstance(newConfiguration); |
Jan Tattermusch | c831a44 | 2016-03-22 17:23:55 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// <summary> |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 77 | /// Creates a new instance of client from given <c>ClientBaseConfiguration</c>. |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 78 | /// </summary> |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 79 | protected abstract T NewInstance(ClientBaseConfiguration configuration); |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// <summary> |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 83 | /// Base class for client-side stubs. |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 84 | /// </summary> |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 85 | public abstract class ClientBase |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 86 | { |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 87 | readonly ClientBaseConfiguration configuration; |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 88 | readonly CallInvoker callInvoker; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 89 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 90 | /// <summary> |
Jan Tattermusch | b240358 | 2016-03-23 07:40:28 -0700 | [diff] [blame] | 91 | /// Initializes a new instance of <c>ClientBase</c> class that |
| 92 | /// throws <c>NotImplementedException</c> upon invocation of any RPC. |
| 93 | /// This constructor is only provided to allow creation of test doubles |
| 94 | /// for client classes (e.g. mocking requires a parameterless constructor). |
| 95 | /// </summary> |
| 96 | protected ClientBase() : this(new UnimplementedCallInvoker()) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 101 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 102 | /// </summary> |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 103 | /// <param name="configuration">The configuration.</param> |
| 104 | protected ClientBase(ClientBaseConfiguration configuration) |
| 105 | { |
| 106 | this.configuration = GrpcPreconditions.CheckNotNull(configuration, "configuration"); |
| 107 | this.callInvoker = configuration.CreateDecoratedCallInvoker(); |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 112 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 113 | /// <param name="channel">The channel to use for remote call invocation.</param> |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 114 | public ClientBase(Channel channel) : this(new DefaultCallInvoker(channel)) |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 115 | { |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /// <summary> |
| 119 | /// Initializes a new instance of <c>ClientBase</c> class. |
| 120 | /// </summary> |
| 121 | /// <param name="callInvoker">The <c>CallInvoker</c> for remote call invocation.</param> |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 122 | public ClientBase(CallInvoker callInvoker) : this(new ClientBaseConfiguration(callInvoker, null)) |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 123 | { |
Jan Tattermusch | d9495ab | 2016-03-22 15:01:57 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /// <summary> |
| 127 | /// Gets the call invoker. |
| 128 | /// </summary> |
| 129 | protected CallInvoker CallInvoker |
| 130 | { |
| 131 | get { return this.callInvoker; } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 132 | } |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 133 | |
| 134 | /// <summary> |
| 135 | /// Gets the configuration. |
| 136 | /// </summary> |
| 137 | internal ClientBaseConfiguration Configuration |
| 138 | { |
| 139 | get { return this.configuration; } |
| 140 | } |
| 141 | |
| 142 | /// <summary> |
| 143 | /// Represents configuration of ClientBase. The class itself is visible to |
| 144 | /// subclasses, but contents are marked as internal to make the instances opaque. |
| 145 | /// The verbose name of this class was chosen to make name clash in generated code |
| 146 | /// less likely. |
| 147 | /// </summary> |
| 148 | protected internal class ClientBaseConfiguration |
| 149 | { |
| 150 | readonly CallInvoker undecoratedCallInvoker; |
| 151 | readonly string host; |
| 152 | |
| 153 | internal ClientBaseConfiguration(CallInvoker undecoratedCallInvoker, string host) |
| 154 | { |
| 155 | this.undecoratedCallInvoker = GrpcPreconditions.CheckNotNull(undecoratedCallInvoker); |
| 156 | this.host = host; |
| 157 | } |
| 158 | |
| 159 | internal CallInvoker CreateDecoratedCallInvoker() |
| 160 | { |
| 161 | return new InterceptingCallInvoker(undecoratedCallInvoker, hostInterceptor: (h) => host); |
| 162 | } |
| 163 | |
| 164 | internal ClientBaseConfiguration WithHost(string host) |
| 165 | { |
| 166 | GrpcPreconditions.CheckNotNull(host, "host"); |
| 167 | return new ClientBaseConfiguration(this.undecoratedCallInvoker, host); |
| 168 | } |
| 169 | } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 170 | } |
| 171 | } |