Example #1
0
        //---------------------------------------------------------------------
        /// <summary>
        /// Register a msg Code with a generic action.
        /// Link/Attach a msg code to an action.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="messengerName"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public bool Register <T>(string msgCode, object recipient, Action <T> actionT)
        {
            _lastErrCode = MessengerErrCode.None;

            // the syntax code must be ok
            if (!IsMsgCodeSyntaxOk(msgCode))
            {
                _lastErrCode = MessengerErrCode.MsgCodeSyntaxWrong;
                return(false);
            }

            if (recipient == null)
            {
                _lastErrCode = MessengerErrCode.RecipientObjIsNull;
                return(false);
            }

            if (actionT == null)
            {
                _lastErrCode = MessengerErrCode.ActionTIsNull;
                return(false);
            }

            // if the code is not already used, create an entry in the dict
            MsgCodeListAction msgCodeListAction;

            if (_dictMsgCodeListAction.ContainsKey(msgCode))
            {
                // msg code already exists, get it
                msgCodeListAction = _dictMsgCodeListAction[msgCode];
            }
            else
            {
                // msg code not exists so create it
                msgCodeListAction = new MsgCodeListAction(msgCode);
                _dictMsgCodeListAction.TryAdd(msgCode, msgCodeListAction);
            }

            // check that the object is already registered with the code msg
            RecipientObjAction regInstAction = msgCodeListAction.FindByRecipientObj(recipient);

            if (regInstAction != null)
            {
                _lastErrCode = MessengerErrCode.RecipientAlreadyRegisteredOnCodeMsg;
                return(false);
            }

            // add the registered inst-Action to the msgCode
            msgCodeListAction.AddRecipientObjAction(recipient, actionT);

            // check the registering
            msgCodeListAction.FindByRecipientObj(recipient);
            if (regInstAction != null)
            {
                _lastErrCode = MessengerErrCode.RegisterMsgCodeRecipientActionFailed;
                return(false);
            }
            return(true);
        }
Example #2
0
        //=====================================================================
        #region Private Methods.

        //---------------------------------------------------------------------
        private bool RemoveMsgCodeListAction(MsgCodeListAction msgCodeListAction)
        {
            MsgCodeListAction outObj;

            if (msgCodeListAction == null)
            {
                return(false);
            }

            // check that the key is present
            if (!_dictMsgCodeListAction.ContainsKey(msgCodeListAction.MsgCode))
            {
                return(false);
            }

            _dictMsgCodeListAction.TryRemove(msgCodeListAction.MsgCode, out outObj);
            return(true);
        }
Example #3
0
        //---------------------------------------------------------------------
        /// <summary>
        /// Notify, send a message.
        /// Execute all actions susbscribed under the msgCode.
        ///
        /// Return the number of actions notifications executed, or -1 if an error occurs.
        /// If no subscriber is found return 0.
        /// </summary>
        /// <param name="messengerName"></param>
        /// <param name="obj"></param>
        public int Notify <T>(object whoNotify, string msgCode, T obj)
        {
            _lastErrCode = MessengerErrCode.None;

            // check params
            if (whoNotify == null)
            {
                _lastErrCode = MessengerErrCode.ObjNotifyIsNull;
                return(-1);
            }

            // the syntax code must be ok
            if (!IsMsgCodeSyntaxOk(msgCode))
            {
                _lastErrCode = MessengerErrCode.MsgCodeSyntaxWrong;
                return(-1);
            }

            // find the CodeListAction
            if (!_dictMsgCodeListAction.ContainsKey(msgCode))
            {
                _lastErrCode = MessengerErrCode.MsgCodeNotFound;
                // not an error
                return(0);
            }

            // msg code already exists, get it
            MsgCodeListAction msgCodeListAction = _dictMsgCodeListAction[msgCode];

            // execute all actions presents
            int notifCount = 0;
            List <RecipientObjAction> listRemoveRecipientAction = new List <RecipientObjAction>();

            foreach (RecipientObjAction recipientAction in msgCodeListAction.GetListRecipientObjAction())
            {
                // check that the object already exists (with the weak reference)
                if (recipientAction.Recipient != null)
                {
                    // the type of the notify parameter should match the type of the param of the registered recipient
                    if (recipientAction.ActionType == typeof(T))
                    {
                        // ok, execute the action
                        ((Action <T>)recipientAction.ActionT)(obj);
                        notifCount++;
                    }
                }
                else
                {
                    // the recipient is now null, unregister it
                    listRemoveRecipientAction.Add(recipientAction);
                }
            }

            //now remove all unregistered recipient
            while (listRemoveRecipientAction.Count > 0)
            {
                msgCodeListAction.RemoveRecipientObjAction(listRemoveRecipientAction[0]);
                listRemoveRecipientAction.RemoveAt(0);
            }

            // no recipient-action attached to the msgCode, remove the msgCode
            if (msgCodeListAction.Count == 0)
            {
                RemoveMsgCodeListAction(msgCodeListAction);
            }

            return(notifCount);
        }