| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | #nullable enable |
| | | 4 | | |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace Ice.Internal; |
| | | 8 | | |
| | | 9 | | internal sealed class ServantManager : Object |
| | | 10 | | { |
| | | 11 | | private readonly Instance _instance; |
| | | 12 | | private readonly string _adapterName; |
| | 1 | 13 | | private readonly Dictionary<Identity, Dictionary<string, Object>> _servantMapMap = []; |
| | 1 | 14 | | private readonly Dictionary<string, Object> _defaultServantMap = []; |
| | 1 | 15 | | private readonly Dictionary<string, ServantLocator> _locatorMap = []; |
| | | 16 | | private bool _isDestroyed; |
| | 1 | 17 | | private readonly object _mutex = new(); |
| | | 18 | | |
| | | 19 | | public async ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) |
| | | 20 | | { |
| | 1 | 21 | | Current current = request.current; |
| | 1 | 22 | | Object? servant = findServant(current.id, current.facet); |
| | | 23 | | |
| | 1 | 24 | | if (servant is not null) |
| | | 25 | | { |
| | | 26 | | // the simple, common path |
| | 1 | 27 | | return await servant.dispatchAsync(request).ConfigureAwait(false); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // Else, check servant locators |
| | 1 | 31 | | ServantLocator? locator = findServantLocator(current.id.category); |
| | 1 | 32 | | if (locator is null && current.id.category.Length > 0) |
| | | 33 | | { |
| | 1 | 34 | | locator = findServantLocator(""); |
| | | 35 | | } |
| | | 36 | | |
| | 1 | 37 | | if (locator is not null) |
| | | 38 | | { |
| | | 39 | | object? cookie; |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | 1 | 43 | | servant = locator.locate(current, out cookie); |
| | 1 | 44 | | } |
| | 1 | 45 | | catch |
| | | 46 | | { |
| | | 47 | | // Skip the encapsulation. This allows the next batch requests in the same InputStream to proceed. |
| | 1 | 48 | | request.inputStream.skipEncapsulation(); |
| | 1 | 49 | | throw; |
| | | 50 | | } |
| | | 51 | | |
| | 1 | 52 | | if (servant is not null) |
| | | 53 | | { |
| | | 54 | | try |
| | | 55 | | { |
| | 1 | 56 | | return await servant.dispatchAsync(request).ConfigureAwait(false); |
| | | 57 | | } |
| | | 58 | | finally |
| | | 59 | | { |
| | 1 | 60 | | locator.finished(current, servant, cookie); |
| | | 61 | | } |
| | | 62 | | } |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | Debug.Assert(servant is null); |
| | | 66 | | |
| | | 67 | | // Skip the encapsulation. This allows the next batch requests in the same InputStream to proceed. |
| | 1 | 68 | | request.inputStream.skipEncapsulation(); |
| | 1 | 69 | | if (hasServant(current.id)) |
| | | 70 | | { |
| | 1 | 71 | | throw new FacetNotExistException(); |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 1 | 75 | | throw new ObjectNotExistException(); |
| | | 76 | | } |
| | 1 | 77 | | } |
| | | 78 | | |
| | | 79 | | internal void addServant(Object servant, Identity ident, string facet) |
| | | 80 | | { |
| | 1 | 81 | | lock (_mutex) |
| | | 82 | | { |
| | 1 | 83 | | facet ??= ""; |
| | | 84 | | |
| | 1 | 85 | | if (_servantMapMap.TryGetValue(ident, out Dictionary<string, Object>? m)) |
| | | 86 | | { |
| | 1 | 87 | | if (m.ContainsKey(facet)) |
| | | 88 | | { |
| | 1 | 89 | | string id = Ice.Util.identityToString(ident, _instance.toStringMode()); |
| | 1 | 90 | | if (facet.Length > 0) |
| | | 91 | | { |
| | 1 | 92 | | id += " -f " + UtilInternal.StringUtil.escapeString(facet, "", _instance.toStringMode()); |
| | | 93 | | } |
| | 1 | 94 | | throw new AlreadyRegisteredException("servant", id); |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | else |
| | | 98 | | { |
| | 1 | 99 | | m = new(); |
| | 1 | 100 | | _servantMapMap[ident] = m; |
| | | 101 | | } |
| | | 102 | | |
| | 1 | 103 | | m[facet] = servant; |
| | 1 | 104 | | } |
| | 1 | 105 | | } |
| | | 106 | | |
| | | 107 | | internal void addDefaultServant(Object servant, string category) |
| | | 108 | | { |
| | 1 | 109 | | lock (_mutex) |
| | | 110 | | { |
| | 1 | 111 | | if (_defaultServantMap.TryGetValue(category, out Object? obj)) |
| | | 112 | | { |
| | 0 | 113 | | throw new AlreadyRegisteredException("default servant", category); |
| | | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | _defaultServantMap[category] = servant; |
| | 1 | 117 | | } |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | internal Object removeServant(Identity ident, string facet) |
| | | 121 | | { |
| | 1 | 122 | | lock (_mutex) |
| | | 123 | | { |
| | 1 | 124 | | facet ??= ""; |
| | | 125 | | |
| | 1 | 126 | | _servantMapMap.TryGetValue(ident, out Dictionary<string, Object>? m); |
| | 1 | 127 | | Object? obj = null; |
| | 1 | 128 | | if (m is null || !m.TryGetValue(facet, out Object? value)) |
| | | 129 | | { |
| | 1 | 130 | | string id = Ice.Util.identityToString(ident, _instance.toStringMode()); |
| | 1 | 131 | | if (facet.Length > 0) |
| | | 132 | | { |
| | 1 | 133 | | id += " -f " + UtilInternal.StringUtil.escapeString(facet, "", _instance.toStringMode()); |
| | | 134 | | } |
| | 1 | 135 | | throw new NotRegisteredException("servant", id); |
| | | 136 | | } |
| | 1 | 137 | | obj = value; |
| | 1 | 138 | | m.Remove(facet); |
| | | 139 | | |
| | 1 | 140 | | if (m.Count == 0) |
| | | 141 | | { |
| | 1 | 142 | | _servantMapMap.Remove(ident); |
| | | 143 | | } |
| | 1 | 144 | | return obj; |
| | | 145 | | } |
| | 1 | 146 | | } |
| | | 147 | | |
| | | 148 | | internal Object removeDefaultServant(string category) |
| | | 149 | | { |
| | 1 | 150 | | lock (_mutex) |
| | | 151 | | { |
| | 1 | 152 | | if (!_defaultServantMap.TryGetValue(category, out Object? obj)) |
| | | 153 | | { |
| | 0 | 154 | | throw new NotRegisteredException("default servant", category); |
| | | 155 | | } |
| | | 156 | | |
| | 1 | 157 | | _defaultServantMap.Remove(category); |
| | 1 | 158 | | return obj; |
| | | 159 | | } |
| | 1 | 160 | | } |
| | | 161 | | |
| | | 162 | | internal Dictionary<string, Object> removeAllFacets(Identity ident) |
| | | 163 | | { |
| | 1 | 164 | | lock (_mutex) |
| | | 165 | | { |
| | 1 | 166 | | if (!_servantMapMap.TryGetValue(ident, out Dictionary<string, Object>? m)) |
| | | 167 | | { |
| | 1 | 168 | | throw new NotRegisteredException( |
| | 1 | 169 | | "servant", |
| | 1 | 170 | | Ice.Util.identityToString(ident, _instance.toStringMode())); |
| | | 171 | | } |
| | 1 | 172 | | _servantMapMap.Remove(ident); |
| | | 173 | | |
| | 1 | 174 | | return m; |
| | | 175 | | } |
| | 1 | 176 | | } |
| | | 177 | | |
| | | 178 | | internal Object? findServant(Identity ident, string facet) |
| | | 179 | | { |
| | 1 | 180 | | lock (_mutex) |
| | | 181 | | { |
| | 1 | 182 | | facet ??= ""; |
| | | 183 | | |
| | 1 | 184 | | Object? obj = null; |
| | 1 | 185 | | if (_servantMapMap.TryGetValue(ident, out Dictionary<string, Object>? m)) |
| | | 186 | | { |
| | 1 | 187 | | m.TryGetValue(facet, out obj); |
| | | 188 | | } |
| | | 189 | | else |
| | | 190 | | { |
| | 1 | 191 | | _defaultServantMap.TryGetValue(ident.category, out obj); |
| | 1 | 192 | | if (obj is null) |
| | | 193 | | { |
| | 1 | 194 | | _defaultServantMap.TryGetValue("", out obj); |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | 1 | 198 | | return obj; |
| | | 199 | | } |
| | 1 | 200 | | } |
| | | 201 | | |
| | | 202 | | internal Object? findDefaultServant(string category) |
| | | 203 | | { |
| | 1 | 204 | | lock (_mutex) |
| | | 205 | | { |
| | 1 | 206 | | return _defaultServantMap.TryGetValue(category, out Object? obj) ? obj : null; |
| | | 207 | | } |
| | 1 | 208 | | } |
| | | 209 | | |
| | | 210 | | internal Dictionary<string, Object> findAllFacets(Identity ident) |
| | | 211 | | { |
| | 1 | 212 | | lock (_mutex) |
| | | 213 | | { |
| | 1 | 214 | | Dictionary<string, Object>? m = _servantMapMap[ident]; |
| | 1 | 215 | | if (m is not null) |
| | | 216 | | { |
| | 1 | 217 | | return new Dictionary<string, Object>(m); |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | return new Dictionary<string, Object>(); |
| | | 221 | | } |
| | 1 | 222 | | } |
| | | 223 | | |
| | | 224 | | internal bool hasServant(Identity ident) |
| | | 225 | | { |
| | 1 | 226 | | lock (_mutex) |
| | | 227 | | { |
| | 1 | 228 | | return _servantMapMap.TryGetValue(ident, out Dictionary<string, Object>? m) ? m.Count != 0 : false; |
| | | 229 | | } |
| | 1 | 230 | | } |
| | | 231 | | |
| | | 232 | | internal void addServantLocator(ServantLocator locator, string category) |
| | | 233 | | { |
| | 1 | 234 | | lock (_mutex) |
| | | 235 | | { |
| | 1 | 236 | | if (_locatorMap.TryGetValue(category, out ServantLocator? l)) |
| | | 237 | | { |
| | 1 | 238 | | throw new AlreadyRegisteredException( |
| | 1 | 239 | | "servant locator", |
| | 1 | 240 | | UtilInternal.StringUtil.escapeString(category, "", _instance.toStringMode())); |
| | | 241 | | } |
| | | 242 | | |
| | 1 | 243 | | _locatorMap[category] = locator; |
| | 1 | 244 | | } |
| | 1 | 245 | | } |
| | | 246 | | |
| | | 247 | | internal ServantLocator removeServantLocator(string category) |
| | | 248 | | { |
| | 1 | 249 | | lock (_mutex) |
| | | 250 | | { |
| | 1 | 251 | | if (!_locatorMap.TryGetValue(category, out ServantLocator? l)) |
| | | 252 | | { |
| | 0 | 253 | | throw new NotRegisteredException( |
| | 0 | 254 | | "servant locator", |
| | 0 | 255 | | UtilInternal.StringUtil.escapeString(category, "", _instance.toStringMode())); |
| | | 256 | | } |
| | 1 | 257 | | _locatorMap.Remove(category); |
| | 1 | 258 | | return l; |
| | | 259 | | } |
| | 1 | 260 | | } |
| | | 261 | | |
| | | 262 | | internal ServantLocator? findServantLocator(string category) |
| | | 263 | | { |
| | 1 | 264 | | lock (_mutex) |
| | | 265 | | { |
| | 1 | 266 | | return _locatorMap.TryGetValue(category, out ServantLocator? result) ? result : null; |
| | | 267 | | } |
| | 1 | 268 | | } |
| | | 269 | | |
| | | 270 | | // |
| | | 271 | | // Only for use by ObjectAdapter. |
| | | 272 | | // |
| | 1 | 273 | | internal ServantManager(Instance instance, string adapterName) |
| | | 274 | | { |
| | 1 | 275 | | _instance = instance; |
| | 1 | 276 | | _adapterName = adapterName; |
| | 1 | 277 | | } |
| | | 278 | | |
| | | 279 | | // |
| | | 280 | | // Only for use by ObjectAdapter. |
| | | 281 | | // |
| | | 282 | | internal void destroy() |
| | | 283 | | { |
| | | 284 | | Dictionary<string, ServantLocator> locatorMap; |
| | 1 | 285 | | lock (_mutex) |
| | | 286 | | { |
| | 1 | 287 | | if (_isDestroyed) |
| | | 288 | | { |
| | 0 | 289 | | return; |
| | | 290 | | } |
| | | 291 | | |
| | 1 | 292 | | _isDestroyed = true; |
| | 1 | 293 | | _servantMapMap.Clear(); |
| | 1 | 294 | | _defaultServantMap.Clear(); |
| | | 295 | | |
| | 1 | 296 | | locatorMap = new Dictionary<string, ServantLocator>(_locatorMap); |
| | 1 | 297 | | _locatorMap.Clear(); |
| | 1 | 298 | | } |
| | | 299 | | |
| | 1 | 300 | | foreach (KeyValuePair<string, ServantLocator> p in locatorMap) |
| | | 301 | | { |
| | 1 | 302 | | ServantLocator locator = p.Value; |
| | | 303 | | try |
| | | 304 | | { |
| | 1 | 305 | | locator.deactivate(p.Key); |
| | 1 | 306 | | } |
| | 0 | 307 | | catch (System.Exception ex) |
| | | 308 | | { |
| | 0 | 309 | | string s = "exception during locator deactivation:\n" + "object adapter: `" |
| | 0 | 310 | | + _adapterName + "'\n" + "locator category: `" + p.Key + "'\n" + ex; |
| | 0 | 311 | | _instance.initializationData().logger!.error(s); |
| | 0 | 312 | | } |
| | | 313 | | } |
| | 1 | 314 | | } |
| | | 315 | | } |