private void RegisterListenerOnConnection(ListenerRegistrationKey registrationKey, ClientConnection connection) { //This method should only be called from registrationExecutor Debug.Assert(!Thread.CurrentThread.Name.Contains("eventRegistration")); ConcurrentDictionary <ClientConnection, EventRegistration> registrationMap; if (_registrations.TryGetValue(registrationKey, out registrationMap) && registrationMap.ContainsKey(connection)) { return; } var future = ((ClientInvocationService)_client.GetInvocationService()) .InvokeListenerOnConnection(registrationKey.RegistrationRequest, registrationKey.EventHandler, connection); IClientMessage clientMessage; try { clientMessage = ThreadUtil.GetResult(future); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } var serverRegistrationId = registrationKey.ResponseDecoder(clientMessage); var correlationId = registrationKey.RegistrationRequest.GetCorrelationId(); var registration = new EventRegistration(serverRegistrationId, correlationId, connection); Debug.Assert(registrationMap != null, "registrationMap should be created!"); registrationMap[connection] = registration; }
private bool DeregisterListenerInternal(string userRegistrationId, EncodeDeregisterListenerRequest encodeDeregisterListenerRequest) { Debug.Assert(!Thread.CurrentThread.Name.Contains("eventRegistration")); var key = new ListenerRegistrationKey(userRegistrationId); ConcurrentDictionary <ClientConnection, EventRegistration> registrationMap; if (!_registrations.TryGetValue(key, out registrationMap)) { return(false); } var successful = true; foreach (var registration in registrationMap.Values) { var connection = registration.ClientConnection; try { var serverRegistrationId = registration.ServerRegistrationId; var request = encodeDeregisterListenerRequest(serverRegistrationId); var future = ((ClientInvocationService)_client.GetInvocationService()) .InvokeOnConnection(request, connection); ThreadUtil.GetResult(future); DistributedEventHandler removed; _eventHandlers.TryRemove(registration.CorrelationId, out removed); EventRegistration reg; registrationMap.TryRemove(connection, out reg); } catch (Exception e) { if (connection.Live) { successful = false; Logger.Warning( string.Format("Deregistration of listener with ID {0} has failed to address {1}", userRegistrationId, connection.GetLocalSocketAddress()), e); } } } if (successful) { _registrations.TryRemove(key, out registrationMap); } return(successful); }
public string RegisterListener(IClientMessage registrationMessage, DecodeRegistrationResponse responseDecoder, EncodeDeregisterListenerRequest encodeDeregisterListenerRequest, DistributedEventHandler eventHandler) { Debug.Assert(!Thread.CurrentThread.Name.Contains("eventRegistration")); TrySyncConnectToAllConnections(); var registrationTask = new Task <string>(() => { var userRegistrationId = Guid.NewGuid().ToString(); var registrationKey = new ListenerRegistrationKey(userRegistrationId, registrationMessage, responseDecoder, eventHandler); _registrations.TryAdd(registrationKey, new ConcurrentDictionary <ClientConnection, EventRegistration>()); var connections = _connectionManager.ActiveConnections; foreach (var connection in connections) { try { RegisterListenerOnConnection(registrationKey, connection); } catch (Exception e) { if (connection.Live) { DeregisterListenerInternal(userRegistrationId, encodeDeregisterListenerRequest); throw new HazelcastException("Listener cannot be added ", e); } } } return(userRegistrationId); }); try { registrationTask.Start(_registrationScheduler); return(registrationTask.Result); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } }
private void RegisterListenerFromInternal(ListenerRegistrationKey registrationKey, ClientConnection connection) { try { RegisterListenerOnConnection(registrationKey, connection); } catch (IOException) { ICollection <ListenerRegistrationKey> failedRegsToConnection; if (!_failedRegistrations.TryGetValue(connection, out failedRegsToConnection)) { failedRegsToConnection = new HashSet <ListenerRegistrationKey>(); _failedRegistrations[connection] = failedRegsToConnection; } failedRegsToConnection.Add(registrationKey); } catch (Exception e) { Logger.Warning(string.Format("Listener {0} can not be added to a new connection: {1}, reason: {2}", registrationKey, connection, e.Message)); } }
protected bool Equals(ListenerRegistrationKey other) { return(string.Equals(UserRegistrationId, other.UserRegistrationId)); }