Ejemplo n.º 1
0
        public void Subscribe(IProxy proxy, EventInfo @event, Delegate @delegate)
        {
            var eventDesc = new EventDescriptor
            {
                ServiceId = ((ServiceProxyContext)proxy.Context).Service.Id,
                EventId   = _eventIdProvider.GetId(@event)
            };

            if (@delegate.Target is IProxy subscriberProxy)
            {
                var subscriberProxyContext = (ServiceProxyContext)(subscriberProxy.Context ?? ServiceProxyBuildingContext.CurrentServiceProxyContext);
                var subscriberServiceId    = subscriberProxyContext.Service.Id;
                var subscriberMethodId     = _routineMethodIdProvider.GetId(@delegate.GetMethodInfo());

                var subscriberDesc = new EventSubscriberDescriptor
                {
                    ServiceId = subscriberServiceId,
                    MethodId  = subscriberMethodId
                };

                _eventSubscriber.Subscribe(eventDesc, subscriberDesc);
            }
            else
            {
                throw new NotSupportedException("At this moment event subscribers must be routines of services.");
            }
        }
Ejemplo n.º 2
0
 public void Subscribe(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber)
 {
     if (!_subscriberMap.TryGetValue(eventDesc, out var subscribers))
     {
         _subscriberMap.Add(eventDesc, subscribers = new HashSet <EventSubscriberDescriptor>());
     }
     subscribers.Add(subscriber);
 }
Ejemplo n.º 3
0
        public async void Subscribe(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber)
        {
            var fabricConnectorToSubscriber = _fabricConnectorSelector.Select(subscriber.ServiceId);
            var fabricConnectorToPublisher = _fabricConnectorSelector.Select(eventDesc.ServiceId);

            if (fabricConnectorToSubscriber.GetType() != fabricConnectorToPublisher.GetType())
                throw new NotSupportedException("Multi-type fabric is not supported for events, because it's an infrastructure configuration concern.");

            await fabricConnectorToSubscriber.SubscribeForEventAsync(eventDesc, subscriber, fabricConnectorToPublisher);
            await fabricConnectorToPublisher.OnEventSubscriberAddedAsync(eventDesc, subscriber, fabricConnectorToSubscriber);
        }
Ejemplo n.º 4
0
 public void AddEventListener(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber)
 {
     lock (_eventListeners)
     {
         if (!_eventListeners.TryGetValue(eventDesc, out var listeners))
         {
             _eventListeners[eventDesc] = listeners = new List <EventSubscriberDescriptor>();
         }
         listeners.Add(subscriber);
     }
 }
Ejemplo n.º 5
0
        public Task OnEventSubscriberAddedAsync(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber, IFabricConnector subsriberFabricConnector)
        {
            var configuration             = (FileBasedFabricConnectorConfiguration)((IFabricConnectorWithConfiguration)subsriberFabricConnector).Configuration;
            var subscriberEventsDirectory = configuration.EventsDirectory;

            var observers = ReadEventObservers();

            if (observers.Add(subscriberEventsDirectory))
            {
                WriteEventObservers(observers);
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 6
0
        internal void ScheduleRoutineFromEvent(EventSubscriberDescriptor eventSubscriberDescriptor, RoutineEventData raisedEventData)
        {
            var intentId = _idGenerator.NewId();

            var pregeneratedRoutineId = intentId.ToString();

            var routineDescriptor = new RoutineDescriptor
            {
                IntentId  = intentId,
                MethodId  = eventSubscriberDescriptor.MethodId,
                RoutineId = pregeneratedRoutineId
            };

            var eventData = new RoutineEventData
            {
                ServiceId = eventSubscriberDescriptor.ServiceId,
                Routine   = routineDescriptor,
                Caller    = new CallerDescriptor
                {
                    ServiceId = raisedEventData.ServiceId
                },
                Parameters = raisedEventData.Parameters
            };

            var eventEnvelope = new RoutineEventEnvelope
            {
                CloudEventsVersion = CloudEventsEnvelope.Version,
                EventType          = DasyncCloudEventsTypes.InvokeRoutine.Name,
                EventTypeVersion   = DasyncCloudEventsTypes.InvokeRoutine.Version,
                Source             = "/" + (raisedEventData.ServiceId?.ServiceName ?? ""),
                EventID            = intentId.ToString(),
                EventTime          = DateTimeOffset.Now,
                ContentType        = "application/json",
                Data = CloudEventsSerialization.Serialize(eventData)
            };

            var fileName = intentId.ToString() + ".json";
            var filePath = Path.Combine(_transitionsDirectory, fileName);
            var content  = CloudEventsSerialization.Serialize(eventEnvelope);

            File.WriteAllText(filePath, content, Encoding.UTF8);
        }
Ejemplo n.º 7
0
        public void Subscribe(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber)
        {
            var publisherServiceDefinition = GetServiceDefinition(eventDesc.ServiceId);

            lock (_eventHandlers)
            {
                if (!_eventHandlers.TryGetValue(eventDesc, out var handlers))
                {
                    _eventHandlers[eventDesc] = handlers = new List <EventSubscriberDescriptor>();
                }
                handlers.Add(subscriber);
            }

            if (publisherServiceDefinition.Type == ServiceType.Local)
            {
                OnSubscriberAdded(eventDesc, subscriber.ServiceId);
            }
            if (publisherServiceDefinition.Type == ServiceType.External)
            {
                SubscribePeriodicallyInBackground(eventDesc, subscriber.ServiceId, publisherServiceDefinition);
            }
        }
Ejemplo n.º 8
0
 public Task SubscribeForEventAsync(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber, IFabricConnector publisherFabricConnector)
 {
     _addEventListener(eventDesc, subscriber);
     return(Task.FromResult(0));
 }
Ejemplo n.º 9
0
 public Task OnEventSubscriberAddedAsync(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber, IFabricConnector subsriberFabricConnector)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 10
0
 public Task SubscribeForEventAsync(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber, IFabricConnector publisherFabricConnector)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 11
0
 public Task OnEventSubscriberAddedAsync(EventDescriptor eventDesc, EventSubscriberDescriptor subscriber, IFabricConnector subsriberFabricConnector)
 {
     return(Task.FromResult(0));
 }