public string StartListening(IClientMessage request, DistributedEventHandler handler, DecodeStartListenerResponse responseDecoder, object key = null) { try { IFuture <IClientMessage> task; if (key == null) { task = _client.GetInvocationService() .InvokeListenerOnRandomTarget(request, handler, responseDecoder); } else { task = _client.GetInvocationService() .InvokeListenerOnKeyOwner(request, key, handler, responseDecoder); } var clientMessage = ThreadUtil.GetResult(task); var registrationId = responseDecoder(clientMessage); RegisterListener(registrationId, request.GetCorrelationId()); return(registrationId); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } }
public void DestroyConnection(ClientConnection connection, Exception cause) { var address = connection.GetAddress(); Logger.Finest("Destroying connection " + connection + " due to " + cause.Message); if (address != null) { ClientConnection conn; if (_activeConnections.TryRemove(address, out conn)) { connection.Close(); FireConnectionListenerEvent(f => f.ConnectionRemoved(connection)); } else { Logger.Warning("Could not find connection " + connection + " in list of connections, could not destroy connection. Current list of connections are " + string.Join(", ", _activeConnections.Keys)); connection.Close(); } } else { // connection has not authenticated yet ((ClientInvocationService)_client.GetInvocationService()).CleanUpConnectionResources(connection); } }
private void FetchMetadataInternal(IList <string> names, Action <MapFetchNearCacheInvalidationMetadataCodec.ResponseParameters> process) { var dataMembers = _client.GetClientClusterService().GetMemberList().Where(member => !member.IsLiteMember); foreach (var member in dataMembers) { var address = member.GetAddress(); var request = MapFetchNearCacheInvalidationMetadataCodec.EncodeRequest(names, address); try { var future = _client.GetInvocationService().InvokeOnTarget(request, address); var task = future.ToTask(); task.ContinueWith(t => { if (t.IsFaulted) { // ReSharper disable once PossibleNullReferenceException throw t.Exception.Flatten().InnerExceptions.First(); } var responseMessage = ThreadUtil.GetResult(t, AsyncResultWaitTimeoutMillis); var responseParameter = MapFetchNearCacheInvalidationMetadataCodec.DecodeResponse(responseMessage); process(responseParameter); }).IgnoreExceptions(); } catch (Exception e) { Logger.Warning(string.Format("Cant fetch invalidation meta-data from address:{0} [{1}]", address, e.Message)); } } }
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); }
private bool DeregisterListenerInternal(string userRegistrationId) { //This method should only be called from registrationExecutor Debug.Assert(Thread.CurrentThread.Name != null && Thread.CurrentThread.Name.Contains("eventRegistration")); ListenerRegistration listenerRegistration; if (!_registrations.TryGetValue(userRegistrationId, out listenerRegistration)) { return(false); } var successful = true; foreach (var connectionRegistration in listenerRegistration.ConnectionRegistrations.Values) { var connection = connectionRegistration.ClientConnection; try { var serverRegistrationId = connectionRegistration.ServerRegistrationId; var request = listenerRegistration.EncodeDeregisterRequest(serverRegistrationId); var future = ((ClientInvocationService)_client.GetInvocationService()).InvokeOnConnection(request, connection); ThreadUtil.GetResult(future); DistributedEventHandler removed; _eventHandlers.TryRemove(connectionRegistration.CorrelationId, out removed); EventRegistration reg; listenerRegistration.ConnectionRegistrations.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(userRegistrationId, out listenerRegistration); } return(successful); }
private ClientGetPartitionsCodec.ResponseParameters GetPartitionsFrom(ClientConnection connection) { var request = ClientGetPartitionsCodec.EncodeRequest(); var task = ((ClientInvocationService)_client.GetInvocationService()).InvokeOnConnection(request, connection); var result = ThreadUtil.GetResult(task, PartitionTimeout); return(ClientGetPartitionsCodec.DecodeResponse(result)); }
private void ClusterAuthenticator(ClientConnection connection) { var ss = _client.GetSerializationService(); var clusterService = (ClientClusterService)_client.GetClientClusterService(); var principal = clusterService.GetPrincipal(); var uuid = principal.GetUuid(); var ownerUuid = principal.GetOwnerUuid(); ClientMessage request; if (_credentials is UsernamePasswordCredentials) { var usernamePasswordCr = (UsernamePasswordCredentials)_credentials; request = ClientAuthenticationCodec.EncodeRequest(usernamePasswordCr.GetUsername(), usernamePasswordCr.GetPassword(), uuid, ownerUuid, false, ClientTypes.Csharp, _client.GetSerializationService().GetVersion()); } else { var data = ss.ToData(_credentials); request = ClientAuthenticationCustomCodec.EncodeRequest(data, uuid, ownerUuid, false, ClientTypes.Csharp, _client.GetSerializationService().GetVersion()); } connection.Init(); IClientMessage response; try { var future = ((ClientInvocationService)_client.GetInvocationService()).InvokeOnConnection(request, connection); response = ThreadUtil.GetResult(future); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } var rp = ClientAuthenticationCodec.DecodeResponse(response); var member = _client.GetClientClusterService().GetMember(rp.address); if (member == null) { throw new HazelcastException("Node with address '" + rp.address + "' was not found in the member list"); } connection.SetRemoteMember(member); }
public string AddDistributedObjectListener(IDistributedObjectListener listener) { var isSmart = _client.GetClientConfig().GetNetworkConfig().IsSmartRouting(); var request = ClientAddDistributedObjectListenerCodec.EncodeRequest(isSmart); var context = new ClientContext(_client.GetSerializationService(), _client.GetClientClusterService(), _client.GetClientPartitionService(), _client.GetInvocationService(), _client.GetClientExecutionService(), _client.GetListenerService(), this, _client.GetClientConfig()); //EventHandler<PortableDistributedObjectEvent> eventHandler = new _EventHandler_211(this, listener); DistributedEventHandler eventHandler = delegate(IClientMessage message) { ClientAddDistributedObjectListenerCodec.AbstractEventHandler.Handle(message, (name, serviceName, type) => { var ns = new ObjectNamespace(serviceName, name); ClientProxy proxy; _proxies.TryGetValue(ns, out proxy); if (proxy == null) { proxy = GetProxy(serviceName, name); } var _event = new DistributedObjectEvent(type, serviceName, proxy); if (DistributedObjectEvent.EventType.Created.Equals(type)) { listener.DistributedObjectCreated(_event); } else { if (DistributedObjectEvent.EventType.Destroyed.Equals(type)) { listener.DistributedObjectDestroyed(_event); } } }); }; //PortableDistributedObjectEvent return(context.GetListenerService().RegisterListener(request, m => ClientAddDistributedObjectListenerCodec.DecodeResponse(m).response, ClientRemoveDistributedObjectListenerCodec.EncodeRequest, eventHandler)); }
private void ManagerAuthenticator(ClientConnection connection) { Logger.Finest("Authenticating against the owner node"); var ss = _client.GetSerializationService(); string uuid = null; string ownerUuid = null; if (_principal != null) { uuid = _principal.GetUuid(); ownerUuid = _principal.GetOwnerUuid(); } ClientMessage request; if (_credentials is UsernamePasswordCredentials) { var usernamePasswordCr = (UsernamePasswordCredentials)_credentials; request = ClientAuthenticationCodec.EncodeRequest(usernamePasswordCr.GetUsername(), usernamePasswordCr.GetPassword(), uuid, ownerUuid, true, ClientTypes.Csharp, _client.GetSerializationService().GetVersion(), VersionUtil.GetDllVersion()); } else { var data = ss.ToData(_credentials); request = ClientAuthenticationCustomCodec.EncodeRequest(data, uuid, ownerUuid, false, ClientTypes.Csharp, _client.GetSerializationService().GetVersion(), VersionUtil.GetDllVersion()); } IClientMessage response; try { var invocationService = (ClientInvocationService)_client.GetInvocationService(); response = ThreadUtil.GetResult(invocationService.InvokeOnConnection(request, connection)); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } var result = ClientAuthenticationCodec.DecodeResponse(response); if (result.address == null) { throw new HazelcastException("Could not resolve address for owner node."); } var member = new Member(result.address, result.ownerUuid); _principal = new ClientPrincipal(result.uuid, result.ownerUuid); connection.Member = member; connection.SetOwner(); connection.ConnectedServerVersionStr = result.serverHazelcastVersion; }
public void DestroyConnection(ClientConnection connection) { var address = connection.GetAddress(); // ReSharper disable once InconsistentlySynchronizedField Logger.Finest("Destroying connection " + connection); if (address != null) { ClientConnection conn; if (_addresses.TryRemove(address, out conn)) { connection.Close(); FireConnectionListenerEvent(f => f.ConnectionRemoved(connection)); } } else { // connection has not authenticated yet ((ClientInvocationService)_client.GetInvocationService()).CleanUpConnectionResources(connection); } }
public string AddDistributedObjectListener(IDistributedObjectListener listener) { var isSmart = _client.GetClientConfig().GetNetworkConfig().IsSmartRouting(); var request = ClientAddDistributedObjectListenerCodec.EncodeRequest(isSmart); var context = new ClientContext(_client.GetSerializationService(), _client.GetClientClusterService(), _client.GetClientPartitionService(), _client.GetInvocationService(), _client.GetClientExecutionService(), _client.GetListenerService(), _client.GetNearCacheManager(), this, _client.GetClientConfig()); DistributedEventHandler eventHandler = delegate(IClientMessage message) { ClientAddDistributedObjectListenerCodec.EventHandler.HandleEvent(message, (name, serviceName, eventType) => { var _event = new LazyDistributedObjectEvent(eventType, serviceName, name, this); switch (eventType) { case DistributedObjectEvent.EventType.Created: listener.DistributedObjectCreated(_event); break; case DistributedObjectEvent.EventType.Destroyed: listener.DistributedObjectDestroyed(_event); break; default: Logger.Warning(string.Format("Undefined DistributedObjectListener event type received: {0} !!!", eventType)); break; } }); }; return(context.GetListenerService().RegisterListener(request, m => ClientAddDistributedObjectListenerCodec.DecodeResponse(m).response, ClientRemoveDistributedObjectListenerCodec.EncodeRequest, eventHandler)); }
private IClientMessage Invoke(IClientMessage request) { var rpc = _client.GetInvocationService(); try { var task = rpc.InvokeOnMember(request, _txOwner); return(ThreadUtil.GetResult(task)); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } }
internal void ListenMembershipEvents(Address ownerConnectionAddress) { if (Logger.IsFinestEnabled()) { Logger.Finest("Starting to listen for membership events from " + ownerConnectionAddress); } _initialListFetched = new ManualResetEventSlim(); try { var clientMessage = ClientAddMembershipListenerCodec.EncodeRequest(false); DistributedEventHandler handler = m => ClientAddMembershipListenerCodec.EventHandler .HandleEvent(m, HandleMember, HandleMemberCollection, HandleMemberAttributeChange); try { var connection = _connectionManager.GetConnection(ownerConnectionAddress); if (connection == null) { throw new InvalidOperationException( "Can not load initial members list because owner connection is null. Address " + ownerConnectionAddress); } var invocationService = (ClientInvocationService)_client.GetInvocationService(); var future = invocationService.InvokeListenerOnConnection(clientMessage, handler, connection); var response = ThreadUtil.GetResult(future); //registration id is ignored as this listener will never be removed var registirationId = ClientAddMembershipListenerCodec.DecodeResponse(response).response; WaitInitialMemberListFetched(); } catch (Exception e) { throw ExceptionUtil.Rethrow(e); } } catch (Exception e) { if (_client.GetLifecycleService().IsRunning()) { if (Logger.IsFinestEnabled()) { Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress, e); } else { Logger.Warning("Error while registering to cluster events! -> " + ownerConnectionAddress + ", Error: " + e); } } } }