Esempio n. 1
0
        private async Task <Guid> SubscribeAsync(Action <MultiMapEventHandlers <TKey, TValue> > events, Maybe <TKey> key, bool includeValues, object state)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

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

            events(handlers);

            // 0: no entryKey
            // 1: entryKey
            var mode = key.Match(1, 0);
            var keyv = key.ValueOrDefault();

            var subscribeRequest = mode switch
            {
                0 => MultiMapAddEntryListenerCodec.EncodeRequest(Name, includeValues, Cluster.IsSmartRouting),
                1 => MultiMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(keyv), includeValues, Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

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

            await Cluster.Events.InstallSubscriptionAsync(subscription).CfAwait();

            return(subscription.Id);
        }
        public virtual string AddEntryListener(IEntryListener <K, V> listener, bool includeValue)
        {
            var request = MultiMapAddEntryListenerCodec.EncodeRequest(GetName(), includeValue, false);

            DistributedEventHandler handler =
                eventData => MultiMapAddEntryListenerCodec.AbstractEventHandler.Handle(eventData,
                                                                                       (key, value, oldValue, mergingValue, type, uuid, entries) =>
                                                                                       OnEntryEvent(key, value, oldValue, mergingValue, (EntryEventType)type, uuid, entries, includeValue, listener)
                                                                                       );

            return(Listen(request, message => MultiMapAddEntryListenerCodec.DecodeResponse(message).response, handler));
        }
Esempio n. 3
0
        public virtual string AddEntryListener(IEntryListener <TKey, TValue> listener, bool includeValue)
        {
            var listenerAdapter =
                EntryListenerAdapter <TKey, TValue> .CreateAdapter(listener, GetContext().GetSerializationService());

            var request = MultiMapAddEntryListenerCodec.EncodeRequest(GetName(), includeValue, IsSmart());

            DistributedEventHandler handler =
                eventData => MultiMapAddEntryListenerCodec.EventHandler.HandleEvent(eventData,
                                                                                    (key, value, oldValue, mergingValue, type, uuid, entries) =>
            {
                OnEntryEvent(key, value, oldValue, mergingValue, type, uuid, entries, listenerAdapter);
            });

            return(RegisterListener(request, message => MultiMapAddEntryListenerCodec.DecodeResponse(message).response,
                                    id => MultiMapRemoveEntryListenerCodec.EncodeRequest(GetName(), id), handler));
        }
        private async Task <Guid> SubscribeAsync(bool includeValues, TKey key, bool hasKey, Action <MultiDictionaryEventHandlers <TKey, TValue> > handle)
        {
            if (hasKey && key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

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

            handle(handlers);

            // 0: no entryKey
            // 1: entryKey
            var mode = hasKey ? 1 : 0;

            var subscribeRequest = mode switch
            {
                0 => MultiMapAddEntryListenerCodec.EncodeRequest(Name, includeValues, Cluster.IsSmartRouting),
                1 => MultiMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(key), includeValues, Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

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

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

            return(subscription.Id);
        }