/// <summary>
        /// push the letter into this postoffice for transfering to remote computer
        /// </summary>
        /// <param name="letter"></param>
        /// <param name="senderInfo"></param>
        /// <param name="recipientInfo"></param>
        internal EMLetter Push(EMLetter letter, MailRouteInfo recipientInfo)
        {
            if (IsActivated)  // postoffice is activated
            {
                List <string> offlineEntities;
                lock (this.registeredReceiverEntityNames)
                {
                    offlineEntities = recipientInfo.EntityNames.Except(registeredReceiverEntityNames).ToList();
                }

                if (letter.HasFlag(StatusCode.Post))                             // Post
                {
                    if (offlineEntities.Count < recipientInfo.EntityNames.Count) // if any entity is online
                    {
                        this.Dispatch(letter, recipientInfo);
                    }

                    return(null);
                }
                else  // Get
                {
                    if (offlineEntities.Count > 0)
                    {
                        throw new Exception($"faild to send letter '{letter.Title}', entity(ies) '{string.Join(", ", offlineEntities)}@{recipientInfo.UserName}' is/are offline.");
                    }

                    return(this.Dispatch(letter, recipientInfo));
                }
            }
            else  // postoffice is not activated
            {
                if (letter.HasFlag(StatusCode.Post))
                {
                    // pass
                    return(null);
                }
                else  // Get
                {
                    throw new Exception($"unable to '{letter.GetLetterType()}' letter '{letter.Title}', target user '{recipientInfo.UserName}' is offline.");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// push the letter into corresponding postoffice
        /// </summary>
        /// <param name="letter"></param>
        /// <returns>error message, null if there is no error</returns>
        internal EMLetter Deliver(EMLetter letter)
        {
            // routing
            rwlsDictName2User.EnterReadLock();
            var allRecipientInfos = this.router.RouteRecipient(letter, this.dictName2User.Values);

            rwlsDictName2User.ExitReadLock();
            allRecipientInfos = MailRouteInfo.Format(allRecipientInfos);
            var notExistsUserRouteInfos = allRecipientInfos.Where(info => !this.Contains(info.UserName)).ToList();

            if (letter.HasFlag(StatusCode.Get))
            {  // check recipient
                if (allRecipientInfos.Count > 1)
                {
                    throw new Exception($"letter of type '{nameof(StatusCode.Get)}' should not have multiple recipients.");
                }
            }

            if (notExistsUserRouteInfos.Count > 0)
            {
                throw new Exception($"user '{string.Join("; ", notExistsUserRouteInfos.Select(info => info.UserName).ToArray())}' not exists,faild to send letter");  // operation failed
            }

            foreach (var rInfo in allRecipientInfos)
            {
                var recipientOpr = this.GetUser(rInfo.UserName);
                var result       = recipientOpr.PostOffice.Push(letter, rInfo);

                if (letter.HasFlag(StatusCode.Get))
                {
                    return(result);  // Get
                }
            }

            return(null);  // Post, SafePost
        }
        /// <summary>
        /// an interface provided to 'ClientPostOffice'
        /// </summary>
        /// <param name="letter"></param>
        /// <returns></returns>
        internal EMLetter Receive(EMLetter letter)
        {
            if (letter.HasFlag(StatusCode.Post))
            {
                ThreadPool.QueueUserWorkItem(o =>  // async mode
                {
                    var result = this.Pickup(letter);

                    if (result != null)
                    {
                        this.Reply(letter, result.Title, result.Content, result.Timeout);  // async reply
                    }
                });

                return(null);
            }
            else  // Get
            {
                return(this.Pickup(letter));
            }
        }
예제 #4
0
        /// <summary>
        /// an interface which is used to send letter to the reomote postoffices
        /// </summary>
        /// <param name="letter"></param>
        internal EMLetter Transfer(EMLetter letter)
        {
            var routeInfos = MailRouteInfo.Parse(letter.Recipient);

            if (routeInfos == null)
            {
                throw new Exception($"cannot deliver letter '{letter.Title}', the '{nameof(letter.Recipient)}' of which is not in a valid format.");
            }

            var           teleRouteInfos = new List <MailRouteInfo>(routeInfos.Count);
            MailRouteInfo localRouteInfo = null;

            foreach (var info in routeInfos)  // find letters sent to local host
            {
                if (info.UserName == "localhost")
                {
                    localRouteInfo = info;  // only one
                }
                else
                {
                    teleRouteInfos.Add(info);
                }
            }

            if (teleRouteInfos.Count != routeInfos.Count)                   // the route information has been changed
            {
                letter.Recipient = MailRouteInfo.ToLiteral(teleRouteInfos); // new tele-recipient info
            }

            letter.UpdateDDL(this.Now);

            // tele transmission
            if (letter.Recipient != "")
            {
                // send to tele-entity
                if (letter.HasFlag(Messages.StatusCode.Get))
                {
                    return(this.transceiver.Get(letter));  // Get
                }
                else
                {
                    this.transceiver.Post(letter);  // Post
                }
            }

            // local transmission
            if (localRouteInfo != null)
            {
                var copy = new EMLetter(letter);
                copy.Recipient = localRouteInfo.ToLiteral();

                if (letter.HasFlag(Messages.StatusCode.Get))
                {
                    return(this.LocalGet(copy));  // Get
                }
                else
                {
                    this.LocalGet(copy);  // Post
                }
            }

            return(null);
        }