Exemplo n.º 1
0
        public void AddSubscriber(IPubSubSubscriberInterface _Subscriber, Actions.EAction _Action)
        {
            lock (Observers)
            {
                if (Observers.ContainsKey(_Action))
                {
                    var RelevantList = Observers[_Action];

                    foreach (var Weak in RelevantList)
                    {
                        if (Weak.TryGetTarget(out IPubSubSubscriberInterface Strong) && Strong == _Subscriber)
                        {
                            return;
                        }
                    }
                    RelevantList.Add(new WeakReference <IPubSubSubscriberInterface>(_Subscriber));
                }
                else
                {
                    Observers[_Action] = new List <WeakReference <IPubSubSubscriberInterface> >()
                    {
                        new WeakReference <IPubSubSubscriberInterface>(_Subscriber)
                    };
                }
            }
        }
Exemplo n.º 2
0
        public bool DeserializeReceivedMessage(string _SerializedMessage, out Actions.EAction _Action, out string _SerializedAction, Action <string> _ErrorMessageAction = null)
        {
            JObject Parsed = null;

            try
            {
                Parsed            = JObject.Parse(_SerializedMessage);
                _Action           = StringActionMap[(string)Parsed[ACTION_KEY_NAME]];
                _SerializedAction = (string)Parsed[SERIALIZED_ACTION_KEY_NAME];
            }
            catch (Exception e)
            {
                //Check if message is originated from storage actions
                if (Parsed != null)
                {
                    if (Action_StorageFileUploaded.IsMatch(Parsed))
                    {
                        _Action           = Actions.EAction.ACTION_STORAGE_FILE_UPLOADED;
                        _SerializedAction = _SerializedMessage;
                        return(true);
                    }
                    else if (Action_StorageFileDeleted.IsMatch(Parsed))
                    {
                        _Action           = Actions.EAction.ACTION_STORAGE_FILE_DELETED;
                        _SerializedAction = _SerializedMessage;
                        return(true);
                    }
                    else if (Action_StorageFileUploaded_CloudEventSchemaV1_0.IsMatch(Parsed))
                    {
                        _Action           = Actions.EAction.ACTION_STORAGE_FILE_UPLOADED_CLOUDEVENT;
                        _SerializedAction = _SerializedMessage;
                        return(true);
                    }
                    else if (Action_StorageFileDeleted_CloudEventSchemaV1_0.IsMatch(Parsed))
                    {
                        _Action           = Actions.EAction.ACTION_STORAGE_FILE_DELETED_CLOUDEVENT;
                        _SerializedAction = _SerializedMessage;
                        return(true);
                    }
                }

                _ErrorMessageAction?.Invoke("Manager_PubSubService->DeserializeReceivedMessage: Deserialization error: " + e.Message + ", trace: " + e.StackTrace + ", serialized message: " + _SerializedMessage);
                _Action           = Actions.EAction.NONE;
                _SerializedAction = null;
                return(false);
            }
            return(true);
        }
Exemplo n.º 3
0
        //Publish to services
        public bool PublishAction(Actions.EAction _Action, string _SerializedAction, Action <string> _ErrorMessageAction = null)
        {
            if (PubSubService == null)
            {
                return(false);
            }

            var ActionName = ActionStringMap[_Action];

            return(PubSubService.CustomPublish(ActionName,
                                               new JObject()
            {
                [ACTION_KEY_NAME] = ActionName,
                [SERIALIZED_ACTION_KEY_NAME] = _SerializedAction
            }.ToString(),
                                               _ErrorMessageAction));
        }
Exemplo n.º 4
0
 public bool RemoveSubscriber(IPubSubSubscriberInterface _Subscriber, Actions.EAction _Action)
 {
     lock (Observers)
     {
         if (Observers.ContainsKey(_Action))
         {
             var Counter = 0;
             foreach (var Weak in Observers[_Action])
             {
                 if (Weak.TryGetTarget(out IPubSubSubscriberInterface Strong) && Strong == _Subscriber)
                 {
                     Observers[_Action].RemoveAt(Counter);
                     return(true);
                 }
                 Counter++;
             }
         }
         return(false);
     }
 }
Exemplo n.º 5
0
        public void PublishSerializedAction(string _SerializedAction, Actions.EAction _ActionServiceIdentifier, Action <string> _ErrorMessageAction = null)
        {
            bool bSuccess     = false;
            int  TrialCounter = 0;

            do
            {
                bSuccess = Manager_PubSubService.Get().PublishAction(
                    _ActionServiceIdentifier,
                    _SerializedAction,
                    _ErrorMessageAction);

                if (!bSuccess)
                {
                    Thread.Sleep(500);
                }
            }while (!bSuccess && TrialCounter++ < MAX_PUBLISH_FAILURE_TRIAL_COUNTS);

            if (!bSuccess)
            {
                _ErrorMessageAction?.Invoke("PublishSerializedAction: Manager_PubSubService.Get().PublishAction has failed " + MAX_PUBLISH_FAILURE_TRIAL_COUNTS + " times. Operation timed out.");
            }
        }
Exemplo n.º 6
0
 public void SetServiceIdentifier(string _ServiceName, Actions.EAction _ActionServiceIdentifier)
 {
     ServiceName             = _ServiceName;
     ActionServiceIdentifier = _ActionServiceIdentifier;
 }