private static Guid ReadSubscribeResponse(ClientMessage responseMessage, object state)
        {
            var sstate = ToSafeState <MapSubscriptionState>(state);

            return(sstate.Mode switch
            {
                0 => MapAddEntryListenerCodec.DecodeResponse(responseMessage).Response,
                1 => MapAddEntryListenerToKeyCodec.DecodeResponse(responseMessage).Response,
                2 => MapAddEntryListenerWithPredicateCodec.DecodeResponse(responseMessage).Response,
                3 => MapAddEntryListenerToKeyWithPredicateCodec.DecodeResponse(responseMessage).Response,
                _ => throw new NotSupportedException()
            });
        private async Task <Guid> SubscribeAsync(bool includeValues, IPredicate predicate, bool hasPredicate, TKey key, bool hasKey, Action <DictionaryEventHandlers <TKey, TValue> > handle, CancellationToken cancellationToken)
        {
            if (hasKey && key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (hasPredicate && predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            var handlers = new DictionaryEventHandlers <TKey, TValue>();

            handle(handlers);

            var flags = HDictionaryEventTypes.Nothing;

            foreach (var handler in handlers)
            {
                flags |= handler.EventType;
            }

            // 0: no entryKey, no predicate
            // 1: entryKey, no predicate
            // 2: no entryKey, predicate
            // 3: entryKey, predicate
            var mode = (hasKey ? 1 : 0) + (hasPredicate ? 2 : 0);

            var subscribeRequest = mode switch
            {
                0 => MapAddEntryListenerCodec.EncodeRequest(Name, includeValues, (int)flags, Cluster.IsSmartRouting),
                1 => MapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(key), includeValues, (int)flags, Cluster.IsSmartRouting),
                2 => MapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                3 => MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(key), ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

            var subscription = new ClusterSubscription(
                subscribeRequest,
                ReadSubscribeResponse,
                CreateUnsubscribeRequest,
                ReadUnsubscribeResponse,
                HandleEventAsync,
                new MapSubscriptionState(mode, Name, handlers));

            await Cluster.Events.InstallSubscriptionAsync(subscription, cancellationToken).CAF();

            return(subscription.Id);
        }
示例#3
0
        private ValueTask HandleEventAsync(ClientMessage eventMessage, object state)
        {
            var sstate = ToSafeState <MapSubscriptionState>(state);

            return(sstate.Mode switch
            {
                0 => MapAddEntryListenerCodec.HandleEventAsync(eventMessage, HandleEntryEvent, state, LoggerFactory),
                1 => MapAddEntryListenerToKeyCodec.HandleEventAsync(eventMessage, HandleEntryEvent, state, LoggerFactory),
                2 => MapAddEntryListenerWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEvent, state, LoggerFactory),
                3 => MapAddEntryListenerToKeyWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEvent, state, LoggerFactory),
                _ => throw new NotSupportedException()
            });
        public string AddEntryListener(IEntryListener <K, V> listener, bool includeValue)
        {
            var listenerFlags = GetListenerFlags(listener);
            var request       = MapAddEntryListenerCodec.EncodeRequest(GetName(), includeValue, listenerFlags, false);
            DistributedEventHandler handler =
                eventData => MapAddEntryListenerCodec.AbstractEventHandler.Handle(eventData,
                                                                                  (key, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(key, value, oldValue, mergingValue, type, uuid, entries, includeValue, listener);
            });

            return(Listen(request, message => MapAddEntryListenerCodec.DecodeResponse(message).response, handler));
        }
示例#5
0
        public Guid AddEntryListener(MapListener listener, bool includeValue)
        {
            var listenerAdapter = EntryListenerAdapter <TKey, TValue> .CreateAdapter(listener, Client.SerializationService);

            var listenerFlags = (int)listenerAdapter.ListenerFlags;
            var request       = MapAddEntryListenerCodec.EncodeRequest(Name, includeValue, listenerFlags, IsSmart());
            DistributedEventHandler handler = eventData => MapAddEntryListenerCodec.EventHandler.HandleEvent(eventData,
                                                                                                             (key, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(key, value, oldValue, mergingValue, type, uuid, entries, listenerAdapter);
            });

            return(RegisterListener(request, message => MapAddEntryListenerCodec.DecodeResponse(message).Response,
                                    id => MapRemoveEntryListenerCodec.EncodeRequest(Name, id), handler));
        }
示例#6
0
        private async Task <Guid> SubscribeAsync(Action <MapEventHandlers <TKey, TValue> > events, Maybe <TKey> key, IPredicate predicate, bool includeValues, object state, CancellationToken cancellationToken)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            var handlers = new MapEventHandlers <TKey, TValue>();

            events(handlers);

            var flags = default(MapEventTypes);

            foreach (var handler in handlers)
            {
                flags |= handler.EventType;
            }

            // 0: no entryKey, no predicate
            // 1: entryKey, no predicate
            // 2: no entryKey, predicate
            // 3: entryKey, predicate
            var mode = key.Match(1, 0) + (predicate != null ? 2 : 0);
            var keyv = key.ValueOrDefault();

            var subscribeRequest = mode switch
            {
                0 => MapAddEntryListenerCodec.EncodeRequest(Name, includeValues, (int)flags, Cluster.IsSmartRouting),
                1 => MapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(keyv), includeValues, (int)flags, Cluster.IsSmartRouting),
                2 => MapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                3 => MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(keyv), ToData(predicate), includeValues, (int)flags, Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

            var subscription = new ClusterSubscription(
                subscribeRequest,
                ReadSubscribeResponse,
                CreateUnsubscribeRequest,
                ReadUnsubscribeResponse,
                HandleEventAsync,
                new MapSubscriptionState(mode, Name, handlers, state));

            await Cluster.Events.AddSubscriptionAsync(subscription, cancellationToken).CfAwait();

            return(subscription.Id);
        }
        public string AddEntryListener(IEntryListener <K, V> listener, IPredicate <K, V> predicate, K _key,
                                       bool includeValue)
        {
            var keyData       = ToData(_key);
            var predicateData = ToData(predicate);
            var flags         = GetListenerFlags(listener);
            var request       = MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(GetName(), keyData, predicateData,
                                                                                         includeValue, flags, false);
            DistributedEventHandler handler =
                eventData => MapAddEntryListenerToKeyWithPredicateCodec.AbstractEventHandler.Handle(eventData,
                                                                                                    (key, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(key, value, oldValue, mergingValue, type, uuid, entries, includeValue, listener);
            });

            return(Listen(request, message => MapAddEntryListenerCodec.DecodeResponse(message).response, keyData,
                          handler));
        }
        private ValueTask HandleEventAsync(ClientMessage eventMessage, object state)
        {
            var sstate = ToSafeState <MapSubscriptionState>(state);

            async ValueTask HandleEntryEvent(IData keyData, IData valueData, IData oldValueData, IData mergingValueData, int eventTypeData, Guid memberId, int numberOfAffectedEntries)
            {
                var eventType = (HDictionaryEventTypes)eventTypeData;

                if (eventType == HDictionaryEventTypes.Nothing)
                {
                    return;
                }

                var member       = Cluster.Members.GetMember(memberId);
                var key          = LazyArg <TKey>(keyData);
                var value        = LazyArg <TValue>(valueData);
                var oldValue     = LazyArg <TValue>(oldValueData);
                var mergingValue = LazyArg <TValue>(mergingValueData);

                // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
                foreach (var handler in sstate.Handlers)
                {
                    if (handler.EventType.HasAll(eventType))
                    {
                        var task = handler switch
                        {
                            IDictionaryEntryEventHandler <TKey, TValue, IHDictionary <TKey, TValue> > entryHandler => entryHandler.HandleAsync(this, member, key, value, oldValue, mergingValue, eventType, numberOfAffectedEntries),
                            IDictionaryEventHandler <TKey, TValue, IHDictionary <TKey, TValue> > mapHandler => mapHandler.HandleAsync(this, member, numberOfAffectedEntries),
                            _ => throw new NotSupportedException()
                        };
                        await task.CAF();
                    }
                }
            }

            return(sstate.Mode switch
            {
                0 => MapAddEntryListenerCodec.HandleEventAsync(eventMessage, HandleEntryEvent, LoggerFactory),
                1 => MapAddEntryListenerToKeyCodec.HandleEventAsync(eventMessage, HandleEntryEvent, LoggerFactory),
                2 => MapAddEntryListenerWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEvent, LoggerFactory),
                3 => MapAddEntryListenerToKeyWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEvent, LoggerFactory),
                _ => throw new NotSupportedException()
            });
        }
        public string AddEntryListener(IEntryListener <TKey, TValue> listener, IPredicate predicate,
                                       TKey key,
                                       bool includeValue)
        {
            var keyData       = ToData(key);
            var predicateData = ToData(predicate);
            var flags         = GetListenerFlags(listener);
            var request       = MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(GetName(), keyData, predicateData,
                                                                                         includeValue, flags, IsSmart());
            DistributedEventHandler handler =
                eventData => MapAddEntryListenerToKeyWithPredicateCodec.AbstractEventHandler.Handle(eventData,
                                                                                                    (k, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(k, value, oldValue, mergingValue, type, uuid, entries, includeValue, listener);
            });

            return(RegisterListener(request, message => MapAddEntryListenerCodec.DecodeResponse(message).response,
                                    id => MapRemoveEntryListenerCodec.EncodeRequest(GetName(), id), handler));
        }
示例#10
0
        public string AddEntryListener(MapListener listener, IPredicate predicate, TKey key, bool includeValue)
        {
            var keyData         = ToData(key);
            var predicateData   = ToData(predicate);
            var listenerAdapter =
                EntryListenerAdapter <TKey, TValue> .CreateAdapter(listener, GetContext().GetSerializationService());

            var listenerFlags = (int)listenerAdapter.ListenerFlags;
            var request       = MapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(GetName(), keyData, predicateData,
                                                                                         includeValue, listenerFlags, IsSmart());
            DistributedEventHandler handler = eventData =>
                                              MapAddEntryListenerToKeyWithPredicateCodec.EventHandler.HandleEvent(eventData,
                                                                                                                  (k, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(k, value, oldValue, mergingValue, type, uuid, entries, listenerAdapter);
            });

            return(RegisterListener(request, message => MapAddEntryListenerCodec.DecodeResponse(message).response,
                                    id => MapRemoveEntryListenerCodec.EncodeRequest(GetName(), id), handler));
        }