예제 #1
0
        protected void AcceptRequest(Request request, IPort port)
        {
            _Logger.WriteToLog("-> ATS process");
            _Logger.WriteToLog(ObjectToLogString.ToLogString(request));

            port.IncomingRequest(request);
        }
예제 #2
0
        protected void ProcessResponse(object sender, Response response)
        {
            var senderPort = _RoutingPorts[response.IncomingRequest.SourceNumber];
            var targetPort = _RoutingPorts[response.IncomingRequest.TargetNumber];

            var callInfo = _ConnectingCalls
                           .FirstOrDefault(x => x.Source == response.IncomingRequest.SourceNumber &&
                                           x.Target == response.IncomingRequest.TargetNumber);

            _Logger.WriteToLog("-> ATS process");
            _Logger.WriteToLog(ObjectToLogString.ToLogString(response));

            if (callInfo != null)
            {
                _ConnectingCalls.Remove(callInfo);

                if (response.State == ResponseState.Answer)
                {
                    callInfo.StartedAt = LocalDateTime.Now;
                    _ActiveCalls.Add(callInfo);
                }
                else
                {
                    TerminateCall(_ConnectingCalls, response, targetPort);
                }
            }
        }
예제 #3
0
        protected void SendResponse(Request request, ResponseState state)
        {
            if (OnResponse != null)
            {
                var respond = new Response(state, request);

                _Logger.WriteToLog("-> Response on incoming call");
                _Logger.WriteToLog(ObjectToLogString.ToLogString(respond));

                OnResponse(this, respond);
            }
        }
예제 #4
0
        protected void RejectRequest(Request request, ResponseState responseState, IPort port)
        {
            var response = new Response(responseState, request);

            _Logger.WriteToLog("-> ATS reject");
            _Logger.WriteToLog(ObjectToLogString.ToLogString(response));

            CallInfo call = new CallInfo(request.SourceNumber, request.TargetNumber);

            call.StartedAt = LocalDateTime.Now;

            port.CallWasCompleted(response);
            OnTerminateCall(this, call);
        }
예제 #5
0
        public void SendRequest(PhoneNumber targetNumber)
        {
            if (OnRequest != null)
            {
                if (_ActiveCall != null)
                {
                    this.TerminateConnection();
                }
                _ActiveCall = new Request(_PhoneNumber, targetNumber);

                _Logger.WriteToLog("-> Send request");
                _Logger.WriteToLog(ObjectToLogString.ToLogString(_ActiveCall));

                OnRequest(this, _ActiveCall);
            }
        }
        protected void AddCall(object sender, CallInfo callInfo)
        {
            var currentTariffPlan =
                this._CurrentContracts
                .Where(x => x.Number == callInfo.Source)
                .OrderBy(x => x.Date)
                .Last()
                .TariffPlan;

            callInfo.Price = currentTariffPlan.CalculatePriceOfCall(callInfo);

            _Logger.WriteToLog("-> Billing system get new call");
            _Logger.WriteToLog(ObjectToLogString.ToLogString(callInfo));

            _NotPayCalls.Add(callInfo);
        }
예제 #7
0
        protected void ProcessEndOfCall(object sender, Response response)
        {
            var senderPort = _RoutingPorts[response.IncomingRequest.SourceNumber];
            var targetPort = _RoutingPorts[response.IncomingRequest.TargetNumber];

            var callInfo = _ActiveCalls.FirstOrDefault(x => x.Source == response.IncomingRequest.SourceNumber &&
                                                       x.Target == response.IncomingRequest.TargetNumber);

            if (callInfo != null)
            {
                callInfo.Duration = LocalTimeSpan.Duration(callInfo.StartedAt, LocalDateTime.Now);

                _Logger.WriteToLog("-> ATS process end on");
                _Logger.WriteToLog(ObjectToLogString.ToLogString(response.IncomingRequest));

                TerminateCall(_ActiveCalls, response, (sender as IPort));
            }
        }
        public ITerminal ConcludeContract(ITariffPlan tariffPlan)
        {
            if (_TariffPlans.Select(x => tariffPlan.GetType()).Contains(tariffPlan.GetType()))
            {
                var phoneNumber = new PhoneNumber(_NumberCode + _Number.ToString());
                _Number++;

                var dateTime    = LocalDateTime.Now;
                var newContract = new Contract(phoneNumber, tariffPlan.GetNewInstance(), dateTime);
                _CurrentContracts.Add(newContract);
                _LastPayment.Add(phoneNumber, dateTime);

                var newTerminal = new Terminal(phoneNumber, _Logger);

                _Logger.WriteToLog("-> Billing system conclude contract");
                _Logger.WriteToLog(ObjectToLogString.ToLogString(newContract));

                OnContract(this, newTerminal);
                return(newTerminal);
            }
            return(null);
        }
예제 #9
0
        static void Main(string[] args)
        {
            var logger      = new ConsoleLogger();
            var tariffPlans = new List <ITariffPlan> {
                new PaidTariffPlan()
            };
            var billingSystem = new BillingSystem("+37529", 100100100, logger, tariffPlans);
            var atsStation    = new ATS(new List <IPort>()
            {
                new Port(logger), new Port(logger), new Port(logger), new Port(logger)
            }, logger);

            billingSystem.ConnectToATS(atsStation);
            atsStation.ConnectToBillingSystem(billingSystem);

            LocalDateTime.OnDayChanged += billingSystem.DayChanged;

            var User1 = billingSystem.ConcludeContract(new PaidTariffPlan());

            User1.Plug();
            var User2 = billingSystem.ConcludeContract(new PaidTariffPlan());

            User2.Plug();
            var User3 = billingSystem.ConcludeContract(new PaidTariffPlan());

            User3.Plug();
            var User4 = billingSystem.ConcludeContract(new PaidTariffPlan());

            User4.Plug();

            logger.WriteToLog("\n---Connection when two ports are free---");
            User1.SendRequest(User2.Number);
            User2.Answer();
            User2.TerminateConnection();

            logger.WriteToLog("\n---Try to call with unplug terminal");
            User3.UnPlug();
            User3.SendRequest(User2.Number);

            logger.WriteToLog("\n---Try to call to busy terminal");
            User3.Plug();
            User3.SendRequest(User2.Number);
            logger.WriteToLog("---\n");
            User4.SendRequest(User3.Number);
            logger.WriteToLog("---\n");
            User3.Drop();

            logger.WriteToLog("\n---Try to call to unplug terminal");
            User2.UnPlug();
            User1.SendRequest(User2.Number);

            User1.SendRequest(User3.Number);
            User3.Answer();
            User3.TerminateConnection();

            User1.SendRequest(User2.Number);
            User2.Answer();
            User2.TerminateConnection();

            User1.SendRequest(User4.Number);
            User4.Answer();
            User4.TerminateConnection();

            logger.WriteToLog("\n---Get debt of user with number " + User1.Number);
            var debt = billingSystem.GetDebtOnCurrentMoment(User1.Number);

            logger.WriteToLog(debt.ToString());

            logger.WriteToLog("\n---Try to call without money");
            User2.Plug();

            var dateOfContract = billingSystem.GetContracts.SingleOrDefault(x => x.Number == User1.Number);

            while (true)
            {
                var time = LocalDateTime.Now;
                if (time.Day >= dateOfContract.Date.Day && time.Month == dateOfContract.Date.AddMonths(2).Month)
                {
                    break;
                }
            }
            User1.SendRequest(User2.Number);
            logger.WriteToLog("\n---Pay and try to call again");
            billingSystem.Pay(User1.Number);
            User1.SendRequest(User2.Number);
            User2.Answer();
            User2.TerminateConnection();



            logger.WriteToLog("\n---Get outgoing call from  " + User1.Number.GetValue);
            var info = billingSystem.GetInfoAboutCalls((x) => { return(x.Source == User1.Number); });

            foreach (var call in info)
            {
                logger.WriteToLog(ObjectToLogString.ToLogString(call));
            }
            Console.ReadLine();
        }