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