| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace IceLocatorDiscovery; |
| | | 7 | | |
| | | 8 | | public sealed class PluginFactory : Ice.PluginFactory |
| | | 9 | | { |
| | | 10 | | public string pluginName => "IceLocatorDiscovery"; |
| | | 11 | | |
| | | 12 | | public Ice.Plugin create(Ice.Communicator communicator, string name, string[] args) |
| | | 13 | | { |
| | | 14 | | if (name != pluginName) |
| | | 15 | | { |
| | | 16 | | throw new Ice.PluginInitializationException( |
| | | 17 | | $"The Locator Discovery plug-in must be named '{pluginName}'."); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | return new PluginI(communicator); |
| | | 21 | | } |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | internal class Request : TaskCompletionSource<Ice.Object_Ice_invokeResult> |
| | | 25 | | { |
| | | 26 | | public Request( |
| | | 27 | | LocatorI locator, |
| | | 28 | | string operation, |
| | | 29 | | Ice.OperationMode mode, |
| | | 30 | | byte[] inParams, |
| | | 31 | | Dictionary<string, string> context) |
| | | 32 | | : base(TaskCreationOptions.RunContinuationsAsynchronously) |
| | | 33 | | { |
| | | 34 | | _locator = locator; |
| | | 35 | | _operation = operation; |
| | | 36 | | _mode = mode; |
| | | 37 | | _inParams = inParams; |
| | | 38 | | _context = context; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public void |
| | | 42 | | invoke(Ice.LocatorPrx l) |
| | | 43 | | { |
| | | 44 | | if (_locatorPrx == null || !_locatorPrx.Equals(l)) |
| | | 45 | | { |
| | | 46 | | _locatorPrx = l; |
| | | 47 | | _ = performInvokeAsync(l); |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | | 51 | | Debug.Assert(_exception != null); |
| | | 52 | | throw _exception; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | async Task performInvokeAsync(Ice.LocatorPrx locator) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | | 59 | | Ice.Object_Ice_invokeResult result = |
| | | 60 | | await locator.ice_invokeAsync(_operation, _mode, _inParams, _context).ConfigureAwait(false); |
| | | 61 | | SetResult(result); |
| | | 62 | | } |
| | | 63 | | catch (Ice.RequestFailedException exc) |
| | | 64 | | { |
| | | 65 | | SetException(exc); |
| | | 66 | | } |
| | | 67 | | catch (Ice.UnknownException exc) |
| | | 68 | | { |
| | | 69 | | SetException(exc); |
| | | 70 | | } |
| | | 71 | | catch (Ice.NoEndpointException) |
| | | 72 | | { |
| | | 73 | | SetException(new Ice.ObjectNotExistException()); |
| | | 74 | | } |
| | | 75 | | catch (Ice.ObjectAdapterDeactivatedException) |
| | | 76 | | { |
| | | 77 | | SetException(new Ice.ObjectNotExistException()); |
| | | 78 | | } |
| | | 79 | | catch (Ice.ObjectAdapterDestroyedException) |
| | | 80 | | { |
| | | 81 | | SetException(new Ice.ObjectNotExistException()); |
| | | 82 | | } |
| | | 83 | | catch (Ice.CommunicatorDestroyedException) |
| | | 84 | | { |
| | | 85 | | SetException(new Ice.ObjectNotExistException()); |
| | | 86 | | } |
| | | 87 | | catch (Exception exc) |
| | | 88 | | { |
| | | 89 | | _exception = exc; |
| | | 90 | | _locator.invoke(_locatorPrx, this); // Retry with new locator proxy |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private readonly LocatorI _locator; |
| | | 96 | | private readonly string _operation; |
| | | 97 | | private readonly Ice.OperationMode _mode; |
| | | 98 | | private readonly Dictionary<string, string> _context; |
| | | 99 | | private readonly byte[] _inParams; |
| | | 100 | | |
| | | 101 | | private Ice.LocatorPrx _locatorPrx; |
| | | 102 | | private Exception _exception; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | internal class VoidLocatorI : Ice.LocatorDisp_ |
| | | 106 | | { |
| | | 107 | | public override Ice.ObjectPrx findObjectById(Ice.Identity id, Ice.Current current) => null; |
| | | 108 | | |
| | | 109 | | public override Ice.ObjectPrx findAdapterById(string id, Ice.Current current) => null; |
| | | 110 | | |
| | | 111 | | public override Ice.LocatorRegistryPrx getRegistry(Ice.Current current) => null; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal class LocatorI : Ice.BlobjectAsync, Ice.Internal.TimerTask |
| | | 115 | | { |
| | | 116 | | public |
| | | 117 | | LocatorI(LookupPrx lookup, Ice.Properties properties, string instanceName, Ice.LocatorPrx voidLocator) |
| | | 118 | | { |
| | | 119 | | _lookup = lookup; |
| | | 120 | | _timeout = properties.getIcePropertyAsInt("IceLocatorDiscovery.Timeout"); |
| | | 121 | | if (_timeout < 0) |
| | | 122 | | { |
| | | 123 | | _timeout = 300; |
| | | 124 | | } |
| | | 125 | | _retryCount = properties.getIcePropertyAsInt("IceLocatorDiscovery.RetryCount"); |
| | | 126 | | if (_retryCount < 0) |
| | | 127 | | { |
| | | 128 | | _retryCount = 0; |
| | | 129 | | } |
| | | 130 | | _retryDelay = properties.getIcePropertyAsInt("IceLocatorDiscovery.RetryDelay"); |
| | | 131 | | if (_retryDelay < 0) |
| | | 132 | | { |
| | | 133 | | _retryDelay = 0; |
| | | 134 | | } |
| | | 135 | | _timer = Ice.Internal.Util.getInstance(lookup.ice_getCommunicator()).timer(); |
| | | 136 | | _traceLevel = properties.getIcePropertyAsInt("IceLocatorDiscovery.Trace.Lookup"); |
| | | 137 | | _instanceName = instanceName; |
| | | 138 | | _warned = false; |
| | | 139 | | _locator = lookup.ice_getCommunicator().getDefaultLocator(); |
| | | 140 | | _voidLocator = voidLocator; |
| | | 141 | | _pending = false; |
| | | 142 | | _pendingRetryCount = 0; |
| | | 143 | | _failureCount = 0; |
| | | 144 | | _warnOnce = true; |
| | | 145 | | |
| | | 146 | | // |
| | | 147 | | // Create one lookup proxy per endpoint from the given proxy. We want to send a multicast |
| | | 148 | | // datagram on each endpoint. |
| | | 149 | | // |
| | | 150 | | var single = new Ice.Endpoint[1]; |
| | | 151 | | foreach (Ice.Endpoint endpoint in lookup.ice_getEndpoints()) |
| | | 152 | | { |
| | | 153 | | single[0] = endpoint; |
| | | 154 | | _lookups[(LookupPrx)lookup.ice_endpoints(single)] = null; |
| | | 155 | | } |
| | | 156 | | Debug.Assert(_lookups.Count > 0); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | public void |
| | | 160 | | setLookupReply(LookupReplyPrx lookupReply) |
| | | 161 | | { |
| | | 162 | | // |
| | | 163 | | // Use a lookup reply proxy whose address matches the interface used to send multicast datagrams. |
| | | 164 | | // |
| | | 165 | | var single = new Ice.Endpoint[1]; |
| | | 166 | | foreach (LookupPrx key in new List<LookupPrx>(_lookups.Keys)) |
| | | 167 | | { |
| | | 168 | | var info = (Ice.UDPEndpointInfo)key.ice_getEndpoints()[0].getInfo(); |
| | | 169 | | if (info.mcastInterface.Length > 0) |
| | | 170 | | { |
| | | 171 | | foreach (Ice.Endpoint q in lookupReply.ice_getEndpoints()) |
| | | 172 | | { |
| | | 173 | | Ice.EndpointInfo r = q.getInfo(); |
| | | 174 | | if (r is Ice.IPEndpointInfo && |
| | | 175 | | ((Ice.IPEndpointInfo)r).host.Equals(info.mcastInterface, StringComparison.Ordinal)) |
| | | 176 | | { |
| | | 177 | | single[0] = q; |
| | | 178 | | _lookups[key] = (LookupReplyPrx)lookupReply.ice_endpoints(single); |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | if (_lookups[key] == null) |
| | | 184 | | { |
| | | 185 | | // Fallback: just use the given lookup reply proxy if no matching endpoint found. |
| | | 186 | | _lookups[key] = lookupReply; |
| | | 187 | | } |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | public override Task<Ice.Object_Ice_invokeResult> |
| | | 192 | | ice_invokeAsync(byte[] inParams, Ice.Current current) |
| | | 193 | | { |
| | | 194 | | lock (_mutex) |
| | | 195 | | { |
| | | 196 | | var request = new Request(this, current.operation, current.mode, inParams, current.ctx); |
| | | 197 | | invoke(null, request); |
| | | 198 | | return request.Task; |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public void |
| | | 203 | | foundLocator(Ice.LocatorPrx locator) |
| | | 204 | | { |
| | | 205 | | lock (_mutex) |
| | | 206 | | { |
| | | 207 | | if (locator == null) |
| | | 208 | | { |
| | | 209 | | if (_traceLevel > 2) |
| | | 210 | | { |
| | | 211 | | _lookup.ice_getCommunicator().getLogger().trace( |
| | | 212 | | "Lookup", |
| | | 213 | | "ignoring locator reply: (null locator)"); |
| | | 214 | | } |
| | | 215 | | return; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | if (_instanceName.Length > 0 && |
| | | 219 | | !locator.ice_getIdentity().category.Equals(_instanceName, StringComparison.Ordinal)) |
| | | 220 | | { |
| | | 221 | | if (_traceLevel > 2) |
| | | 222 | | { |
| | | 223 | | var s = new StringBuilder("ignoring locator reply: instance name doesn't match\n"); |
| | | 224 | | s.Append("expected = ").Append(_instanceName); |
| | | 225 | | s.Append("received = ").Append(locator.ice_getIdentity().category); |
| | | 226 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 227 | | } |
| | | 228 | | return; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | // |
| | | 232 | | // If we already have a locator assigned, ensure the given locator |
| | | 233 | | // has the same identity, otherwise ignore it. |
| | | 234 | | // |
| | | 235 | | if (_pendingRequests.Count > 0 && |
| | | 236 | | _locator != null && |
| | | 237 | | !locator.ice_getIdentity().category.Equals( |
| | | 238 | | _locator.ice_getIdentity().category, |
| | | 239 | | StringComparison.Ordinal)) |
| | | 240 | | { |
| | | 241 | | if (!_warned) |
| | | 242 | | { |
| | | 243 | | _warned = true; // Only warn once |
| | | 244 | | |
| | | 245 | | locator.ice_getCommunicator().getLogger().warning( |
| | | 246 | | "received Ice locator with different instance name:\n" + |
| | | 247 | | "using = `" + _locator.ice_getIdentity().category + "'\n" + |
| | | 248 | | "received = `" + locator.ice_getIdentity().category + "'\n" + |
| | | 249 | | "This is typically the case if multiple Ice locators with different " + |
| | | 250 | | "instance names are deployed and the property `IceLocatorDiscovery.InstanceName'" + |
| | | 251 | | "is not set."); |
| | | 252 | | } |
| | | 253 | | return; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | if (_pending) // No need to continue, we found a locator |
| | | 257 | | { |
| | | 258 | | _timer.cancel(this); |
| | | 259 | | _pendingRetryCount = 0; |
| | | 260 | | _pending = false; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | if (_traceLevel > 0) |
| | | 264 | | { |
| | | 265 | | var s = new StringBuilder("locator lookup succeeded:\nlocator = "); |
| | | 266 | | s.Append(locator); |
| | | 267 | | if (_instanceName.Length > 0) |
| | | 268 | | { |
| | | 269 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 270 | | } |
| | | 271 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | Ice.LocatorPrx l = null; |
| | | 275 | | if (_pendingRequests.Count == 0) |
| | | 276 | | { |
| | | 277 | | _locators.TryGetValue(locator.ice_getIdentity().category, out _locator); |
| | | 278 | | } |
| | | 279 | | else |
| | | 280 | | { |
| | | 281 | | l = _locator; |
| | | 282 | | } |
| | | 283 | | if (l != null) |
| | | 284 | | { |
| | | 285 | | // |
| | | 286 | | // We found another locator replica, append its endpoints to the |
| | | 287 | | // current locator proxy endpoints. |
| | | 288 | | // |
| | | 289 | | var newEndpoints = new List<Ice.Endpoint>(l.ice_getEndpoints()); |
| | | 290 | | foreach (Ice.Endpoint p in locator.ice_getEndpoints()) |
| | | 291 | | { |
| | | 292 | | // |
| | | 293 | | // Only add endpoints if not already in the locator proxy endpoints |
| | | 294 | | // |
| | | 295 | | bool found = false; |
| | | 296 | | foreach (Ice.Endpoint q in newEndpoints) |
| | | 297 | | { |
| | | 298 | | if (p.Equals(q)) |
| | | 299 | | { |
| | | 300 | | found = true; |
| | | 301 | | break; |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | if (!found) |
| | | 305 | | { |
| | | 306 | | newEndpoints.Add(p); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | l = (Ice.LocatorPrx)l.ice_endpoints(newEndpoints.ToArray()); |
| | | 310 | | } |
| | | 311 | | else |
| | | 312 | | { |
| | | 313 | | l = locator; |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | if (_pendingRequests.Count == 0) |
| | | 317 | | { |
| | | 318 | | _locators[locator.ice_getIdentity().category] = l; |
| | | 319 | | } |
| | | 320 | | else |
| | | 321 | | { |
| | | 322 | | _locator = l; |
| | | 323 | | if (_instanceName.Length == 0) |
| | | 324 | | { |
| | | 325 | | _instanceName = _locator.ice_getIdentity().category; // Stick to the first locator |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | // |
| | | 329 | | // Send pending requests if any. |
| | | 330 | | // |
| | | 331 | | foreach (Request req in _pendingRequests) |
| | | 332 | | { |
| | | 333 | | req.invoke(_locator); |
| | | 334 | | } |
| | | 335 | | _pendingRequests.Clear(); |
| | | 336 | | } |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | public void |
| | | 341 | | invoke(Ice.LocatorPrx locator, Request request) |
| | | 342 | | { |
| | | 343 | | lock (_mutex) |
| | | 344 | | { |
| | | 345 | | if (request != null && _locator != null && _locator != locator) |
| | | 346 | | { |
| | | 347 | | request.invoke(_locator); |
| | | 348 | | } |
| | | 349 | | else if (request != null && Ice.Internal.Time.currentMonotonicTimeMillis() < _nextRetry) |
| | | 350 | | { |
| | | 351 | | request.invoke(_voidLocator); // Don't retry to find a locator before the retry delay expires |
| | | 352 | | } |
| | | 353 | | else |
| | | 354 | | { |
| | | 355 | | _locator = null; |
| | | 356 | | |
| | | 357 | | if (request != null) |
| | | 358 | | { |
| | | 359 | | _pendingRequests.Add(request); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | if (!_pending) // No request in progress |
| | | 363 | | { |
| | | 364 | | _pending = true; |
| | | 365 | | _pendingRetryCount = _retryCount; |
| | | 366 | | _failureCount = 0; |
| | | 367 | | try |
| | | 368 | | { |
| | | 369 | | if (_traceLevel > 1) |
| | | 370 | | { |
| | | 371 | | var s = new StringBuilder("looking up locator:\nlookup = "); |
| | | 372 | | s.Append(_lookup); |
| | | 373 | | if (_instanceName.Length > 0) |
| | | 374 | | { |
| | | 375 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 376 | | } |
| | | 377 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | foreach (KeyValuePair<LookupPrx, LookupReplyPrx> l in _lookups) |
| | | 381 | | { |
| | | 382 | | _ = performFindLocatorAsync(l.Key, l.Value); |
| | | 383 | | } |
| | | 384 | | _timer.schedule(this, _timeout); |
| | | 385 | | } |
| | | 386 | | catch (Ice.LocalException ex) |
| | | 387 | | { |
| | | 388 | | if (_traceLevel > 0) |
| | | 389 | | { |
| | | 390 | | var s = new StringBuilder("locator lookup failed:\nlookup = "); |
| | | 391 | | s.Append(_lookup); |
| | | 392 | | if (_instanceName.Length > 0) |
| | | 393 | | { |
| | | 394 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 395 | | } |
| | | 396 | | s.Append('\n').Append(ex); |
| | | 397 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | foreach (Request req in _pendingRequests) |
| | | 401 | | { |
| | | 402 | | req.invoke(_voidLocator); |
| | | 403 | | } |
| | | 404 | | _pendingRequests.Clear(); |
| | | 405 | | _pendingRetryCount = 0; |
| | | 406 | | _pending = false; |
| | | 407 | | } |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | async Task performFindLocatorAsync(LookupPrx lookupPrx, LookupReplyPrx lookupReplyPrx) |
| | | 413 | | { |
| | | 414 | | // Exit the mutex lock before proceeding. |
| | | 415 | | await Task.Yield(); |
| | | 416 | | |
| | | 417 | | // Send multicast request. |
| | | 418 | | try |
| | | 419 | | { |
| | | 420 | | await lookupPrx.findLocatorAsync(_instanceName, lookupReplyPrx).ConfigureAwait(false); |
| | | 421 | | } |
| | | 422 | | catch (System.Exception ex) |
| | | 423 | | { |
| | | 424 | | exception(ex); |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | private void exception(Exception ex) |
| | | 430 | | { |
| | | 431 | | lock (_mutex) |
| | | 432 | | { |
| | | 433 | | if (++_failureCount == _lookups.Count && _pending) |
| | | 434 | | { |
| | | 435 | | // |
| | | 436 | | // All the lookup calls failed, cancel the timer and propagate the error to the requests. |
| | | 437 | | // |
| | | 438 | | _timer.cancel(this); |
| | | 439 | | _pendingRetryCount = 0; |
| | | 440 | | _pending = false; |
| | | 441 | | |
| | | 442 | | if (_warnOnce) |
| | | 443 | | { |
| | | 444 | | var builder = new StringBuilder(); |
| | | 445 | | builder.Append("failed to lookup locator with lookup proxy `"); |
| | | 446 | | builder.Append(_lookup); |
| | | 447 | | builder.Append("':\n"); |
| | | 448 | | builder.Append(ex); |
| | | 449 | | _lookup.ice_getCommunicator().getLogger().warning(builder.ToString()); |
| | | 450 | | _warnOnce = false; |
| | | 451 | | } |
| | | 452 | | |
| | | 453 | | if (_traceLevel > 0) |
| | | 454 | | { |
| | | 455 | | var s = new StringBuilder("locator lookup failed:\nlookup = "); |
| | | 456 | | s.Append(_lookup); |
| | | 457 | | if (_instanceName.Length > 0) |
| | | 458 | | { |
| | | 459 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 460 | | } |
| | | 461 | | s.Append('\n').Append(ex); |
| | | 462 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | if (_pendingRequests.Count > 0) |
| | | 466 | | { |
| | | 467 | | foreach (Request req in _pendingRequests) |
| | | 468 | | { |
| | | 469 | | req.invoke(_voidLocator); |
| | | 470 | | } |
| | | 471 | | _pendingRequests.Clear(); |
| | | 472 | | } |
| | | 473 | | } |
| | | 474 | | } |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | public void runTimerTask() |
| | | 478 | | { |
| | | 479 | | lock (_mutex) |
| | | 480 | | { |
| | | 481 | | if (!_pending) |
| | | 482 | | { |
| | | 483 | | Debug.Assert(_pendingRequests.Count == 0); |
| | | 484 | | return; // Request failed |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | if (_pendingRetryCount > 0) |
| | | 488 | | { |
| | | 489 | | --_pendingRetryCount; |
| | | 490 | | try |
| | | 491 | | { |
| | | 492 | | if (_traceLevel > 1) |
| | | 493 | | { |
| | | 494 | | var s = new StringBuilder("retrying locator lookup:\nlookup = "); |
| | | 495 | | s.Append(_lookup); |
| | | 496 | | s.Append("\nretry count = ").Append(_retryCount); |
| | | 497 | | if (_instanceName.Length > 0) |
| | | 498 | | { |
| | | 499 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 500 | | } |
| | | 501 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | foreach (KeyValuePair<LookupPrx, LookupReplyPrx> l in _lookups) |
| | | 505 | | { |
| | | 506 | | l.Key.findLocatorAsync(_instanceName, l.Value).ContinueWith( |
| | | 507 | | t => |
| | | 508 | | { |
| | | 509 | | try |
| | | 510 | | { |
| | | 511 | | t.Wait(); |
| | | 512 | | } |
| | | 513 | | catch (AggregateException ex) |
| | | 514 | | { |
| | | 515 | | exception(ex.InnerException); |
| | | 516 | | } |
| | | 517 | | }, |
| | | 518 | | l.Key.ice_scheduler()); // Send multicast request. |
| | | 519 | | } |
| | | 520 | | _timer.schedule(this, _timeout); |
| | | 521 | | return; |
| | | 522 | | } |
| | | 523 | | catch (Ice.LocalException) |
| | | 524 | | { |
| | | 525 | | } |
| | | 526 | | _pendingRetryCount = 0; |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | Debug.Assert(_pendingRetryCount == 0); |
| | | 530 | | _pending = false; |
| | | 531 | | |
| | | 532 | | if (_traceLevel > 0) |
| | | 533 | | { |
| | | 534 | | var s = new StringBuilder("locator lookup timed out:\nlookup = "); |
| | | 535 | | s.Append(_lookup); |
| | | 536 | | if (_instanceName.Length > 0) |
| | | 537 | | { |
| | | 538 | | s.Append("\ninstance name = ").Append(_instanceName); |
| | | 539 | | } |
| | | 540 | | _lookup.ice_getCommunicator().getLogger().trace("Lookup", s.ToString()); |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | if (_pendingRequests.Count > 0) |
| | | 544 | | { |
| | | 545 | | foreach (Request req in _pendingRequests) |
| | | 546 | | { |
| | | 547 | | req.invoke(_voidLocator); |
| | | 548 | | } |
| | | 549 | | _pendingRequests.Clear(); |
| | | 550 | | } |
| | | 551 | | _nextRetry = Ice.Internal.Time.currentMonotonicTimeMillis() + _retryDelay; |
| | | 552 | | } |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | private readonly LookupPrx _lookup; |
| | | 556 | | private readonly Dictionary<LookupPrx, LookupReplyPrx> _lookups = new Dictionary<LookupPrx, LookupReplyPrx>(); |
| | | 557 | | private readonly int _timeout; |
| | | 558 | | private readonly Ice.Internal.Timer _timer; |
| | | 559 | | private readonly int _traceLevel; |
| | | 560 | | private readonly int _retryCount; |
| | | 561 | | private readonly int _retryDelay; |
| | | 562 | | |
| | | 563 | | private string _instanceName; |
| | | 564 | | private bool _warned; |
| | | 565 | | private Ice.LocatorPrx _locator; |
| | | 566 | | private readonly Ice.LocatorPrx _voidLocator; |
| | | 567 | | private readonly Dictionary<string, Ice.LocatorPrx> _locators = new Dictionary<string, Ice.LocatorPrx>(); |
| | | 568 | | |
| | | 569 | | private bool _pending; |
| | | 570 | | private int _pendingRetryCount; |
| | | 571 | | private int _failureCount; |
| | | 572 | | private bool _warnOnce = true; |
| | | 573 | | private readonly List<Request> _pendingRequests = new List<Request>(); |
| | | 574 | | private long _nextRetry; |
| | | 575 | | private readonly object _mutex = new(); |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | internal class LookupReplyI : LookupReplyDisp_ |
| | | 579 | | { |
| | 1 | 580 | | public LookupReplyI(LocatorI locator) => _locator = locator; |
| | | 581 | | |
| | | 582 | | public override void |
| | 1 | 583 | | foundLocator(Ice.LocatorPrx locator, Ice.Current current) => _locator.foundLocator(locator); |
| | | 584 | | |
| | | 585 | | private readonly LocatorI _locator; |
| | | 586 | | } |
| | | 587 | | |
| | | 588 | | internal class PluginI : Ice.Plugin |
| | | 589 | | { |
| | | 590 | | public |
| | | 591 | | PluginI(Ice.Communicator communicator) => _communicator = communicator; |
| | | 592 | | |
| | | 593 | | public void |
| | | 594 | | initialize() |
| | | 595 | | { |
| | | 596 | | Ice.Properties properties = _communicator.getProperties(); |
| | | 597 | | |
| | | 598 | | bool ipv4 = properties.getIcePropertyAsInt("Ice.IPv4") > 0; |
| | | 599 | | bool preferIPv6 = properties.getIcePropertyAsInt("Ice.PreferIPv6Address") > 0; |
| | | 600 | | string address = properties.getIceProperty("IceLocatorDiscovery.Address"); |
| | | 601 | | if (address.Length == 0) |
| | | 602 | | { |
| | | 603 | | address = ipv4 && !preferIPv6 ? "239.255.0.1" : "ff15::1"; |
| | | 604 | | } |
| | | 605 | | int port = properties.getIcePropertyAsInt("IceLocatorDiscovery.Port"); |
| | | 606 | | string intf = properties.getIceProperty("IceLocatorDiscovery.Interface"); |
| | | 607 | | |
| | | 608 | | string lookupEndpoints = properties.getIceProperty("IceLocatorDiscovery.Lookup"); |
| | | 609 | | if (lookupEndpoints.Length == 0) |
| | | 610 | | { |
| | | 611 | | int protocol = ipv4 && !preferIPv6 ? Ice.Internal.Network.EnableIPv4 : Ice.Internal.Network.EnableIPv6; |
| | | 612 | | List<string> interfaces = Ice.Internal.Network.getInterfacesForMulticast(intf, protocol); |
| | | 613 | | foreach (string p in interfaces) |
| | | 614 | | { |
| | | 615 | | if (p != interfaces[0]) |
| | | 616 | | { |
| | | 617 | | lookupEndpoints += ":"; |
| | | 618 | | } |
| | | 619 | | lookupEndpoints += "udp -h \"" + address + "\" -p " + port + " --interface \"" + p + "\""; |
| | | 620 | | } |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | if (properties.getIceProperty("IceLocatorDiscovery.Reply.Endpoints").Length == 0) |
| | | 624 | | { |
| | | 625 | | properties.setProperty( |
| | | 626 | | "IceLocatorDiscovery.Reply.Endpoints", |
| | | 627 | | "udp -h " + (intf.Length == 0 ? "*" : "\"" + intf + "\"")); |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | if (properties.getIceProperty("IceLocatorDiscovery.Locator.Endpoints").Length == 0) |
| | | 631 | | { |
| | | 632 | | properties.setProperty("IceLocatorDiscovery.Locator.AdapterId", Guid.NewGuid().ToString()); |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | _replyAdapter = _communicator.createObjectAdapter("IceLocatorDiscovery.Reply"); |
| | | 636 | | _locatorAdapter = _communicator.createObjectAdapter("IceLocatorDiscovery.Locator"); |
| | | 637 | | |
| | | 638 | | // We don't want those adapters to be registered with the locator so clear their locator. |
| | | 639 | | _replyAdapter.setLocator(null); |
| | | 640 | | _locatorAdapter.setLocator(null); |
| | | 641 | | |
| | | 642 | | Ice.ObjectPrx lookupPrx = Ice.ObjectPrxHelper.createProxy( |
| | | 643 | | _communicator, |
| | | 644 | | "IceLocatorDiscovery/Lookup -d:" + lookupEndpoints); |
| | | 645 | | |
| | | 646 | | lookupPrx = lookupPrx.ice_router(null); |
| | | 647 | | |
| | | 648 | | Ice.LocatorPrx voidLo = Ice.LocatorPrxHelper.uncheckedCast(_locatorAdapter.addWithUUID(new VoidLocatorI())); |
| | | 649 | | |
| | | 650 | | string instanceName = properties.getIceProperty("IceLocatorDiscovery.InstanceName"); |
| | | 651 | | var id = new Ice.Identity("Locator", instanceName.Length > 0 ? instanceName : Guid.NewGuid().ToString()); |
| | | 652 | | |
| | | 653 | | _defaultLocator = _communicator.getDefaultLocator(); |
| | | 654 | | _locator = new LocatorI(LookupPrxHelper.uncheckedCast(lookupPrx), properties, instanceName, voidLo); |
| | | 655 | | _locatorPrx = Ice.LocatorPrxHelper.uncheckedCast(_locatorAdapter.add(_locator, id)); |
| | | 656 | | _communicator.setDefaultLocator(_locatorPrx); |
| | | 657 | | |
| | | 658 | | Ice.ObjectPrx lookupReply = _replyAdapter.addWithUUID(new LookupReplyI(_locator)).ice_datagram(); |
| | | 659 | | _locator.setLookupReply(LookupReplyPrxHelper.uncheckedCast(lookupReply)); |
| | | 660 | | |
| | | 661 | | _replyAdapter.activate(); |
| | | 662 | | _locatorAdapter.activate(); |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | public void |
| | | 666 | | destroy() |
| | | 667 | | { |
| | | 668 | | _replyAdapter?.destroy(); |
| | | 669 | | _locatorAdapter?.destroy(); |
| | | 670 | | if (_communicator.getDefaultLocator().Equals(_locatorPrx)) |
| | | 671 | | { |
| | | 672 | | // Restore original default locator proxy, if the user didn't change it in the meantime |
| | | 673 | | _communicator.setDefaultLocator(_defaultLocator); |
| | | 674 | | } |
| | | 675 | | } |
| | | 676 | | |
| | | 677 | | private readonly Ice.Communicator _communicator; |
| | | 678 | | private Ice.ObjectAdapter _locatorAdapter; |
| | | 679 | | private Ice.ObjectAdapter _replyAdapter; |
| | | 680 | | private LocatorI _locator; |
| | | 681 | | private Ice.LocatorPrx _locatorPrx; |
| | | 682 | | private Ice.LocatorPrx _defaultLocator; |
| | | 683 | | } |