Example #1
0
        private ValueTask HandleEventAsync(ClientMessage eventMessage, object state)
        {
            var sstate = ToSafeState <SubscriptionState>(state);

            return(sstate.Mode switch
            {
                0 => ReplicatedMapAddEntryListenerCodec.HandleEventAsync(eventMessage, HandleEntryEventAsync, state, LoggerFactory),
                1 => ReplicatedMapAddEntryListenerToKeyCodec.HandleEventAsync(eventMessage, HandleEntryEventAsync, state, LoggerFactory),
                2 => ReplicatedMapAddEntryListenerWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEventAsync, state, LoggerFactory),
                3 => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.HandleEventAsync(eventMessage, HandleEntryEventAsync, state, LoggerFactory),
                _ => throw new NotSupportedException()
            });
        private async Task <Guid> SubscribeAsync(IPredicate predicate, bool hasPredicate, TKey key, bool hasKey, Action <ReplicatedDictionaryEventHandlers <TKey, TValue> > handle)
        {
            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 ReplicatedDictionaryEventHandlers <TKey, TValue>();

            handle(handlers);

            // 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 => ReplicatedMapAddEntryListenerCodec.EncodeRequest(Name, Cluster.IsSmartRouting),
                1 => ReplicatedMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(key), Cluster.IsSmartRouting),
                2 => ReplicatedMapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), Cluster.IsSmartRouting),
                3 => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(key), ToData(predicate), 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);
        }
Example #3
0
        public string AddEntryListener(IEntryListener <TKey, TValue> listener, IPredicate predicate, TKey key)
        {
            var keyData         = ToData(key);
            var predicateData   = ToData(predicate);
            var listenerAdapter =
                EntryListenerAdapter <TKey, TValue> .CreateAdapter(listener, GetContext().GetSerializationService());

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

            return(RegisterListener(request,
                                    message => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.DecodeResponse(message).response,
                                    id => ReplicatedMapRemoveEntryListenerCodec.EncodeRequest(GetName(), id), handler));
        }
Example #4
0
        private async Task <Guid> SubscribeAsync(Action <ReplicatedMapEventHandlers <TKey, TValue> > events, Maybe <TKey> key, IPredicate predicate, object state)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

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

            events(handlers);

            // 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 => ReplicatedMapAddEntryListenerCodec.EncodeRequest(Name, Cluster.IsSmartRouting),
                1 => ReplicatedMapAddEntryListenerToKeyCodec.EncodeRequest(Name, ToData(keyv), Cluster.IsSmartRouting),
                2 => ReplicatedMapAddEntryListenerWithPredicateCodec.EncodeRequest(Name, ToData(predicate), Cluster.IsSmartRouting),
                3 => ReplicatedMapAddEntryListenerToKeyWithPredicateCodec.EncodeRequest(Name, ToData(keyv), ToData(predicate), Cluster.IsSmartRouting),
                _ => throw new NotSupportedException()
            };

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

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

            return(subscription.Id);
        }