Example #1
0
        public void ManageReport(BusReportMsg busReportMsg)
        {
            UserInfoSet userInfoSet = new UserInfoSet();

            //only registered user will be handled
            foreach (ulong userPhoneAddr in busReportMsg.cellPhoneAddrSet)
            {
                //he or she is a registered user
                if (AccountManager.ContainsUser(userPhoneAddr))
                {
                    lock (AccountManager.UserBase[userPhoneAddr])
                    {
                        TripRecord aTripRecord = new TripRecord();
                        aTripRecord.busLine  = busReportMsg.busLine;
                        aTripRecord.isBoard  = busReportMsg.isBoard;
                        aTripRecord.stopName = busReportMsg.stopName;
                        aTripRecord.time     = busReportMsg.time;
                        TripManager.AddTripRecord(userPhoneAddr, aTripRecord);
                        UserInfo aUserInfo;
                        AccountManager.QueryUser(userPhoneAddr, out aUserInfo);
                        userInfoSet.Add(aUserInfo);
                    }
                    Console.WriteLine("Server has received MSG. BUSLINE={0}, userId={1}\n", busReportMsg.busLine, userPhoneAddr);
                }
            }

            //Charge users
            AccountManager.ChargeUsers(busReportMsg, userInfoSet);
        }
Example #2
0
 public static void ChargeUsers(BusReportMsg busReportMsg, UserInfoSet userInfoSet)
 {
     if (busReportMsg.isBoard)
     {
         foreach (UserInfo aUserInfo in userInfoSet)
         {
             TimeSpan ts = DateTime.Now.Subtract(aUserInfo.ticketBeginTime);
             //if the user hold a valid ticket(within 2 hours but for simulation just one minute)
             if (ts.TotalMinutes <= ServerConfig.GetT_VAL() && ts.TotalMinutes >= 0 && aUserInfo.hasTicket)
             {
                 //if the user own a e-ticket, won't charge him/she
                 UserNotifycation.SendNotify(aUserInfo, "You have boarded on bus " + busReportMsg.busLine + " at " + busReportMsg.stopName + ". You are not charged as you are holding a valid ticket.");
                 return;
             }
             else
             {
                 //pay by credit card
                 if (aUserInfo.isPayByCreditCard)
                 {
                     UserInfo newUserInfo = aUserInfo;
                     newUserInfo.ticketBeginTime = DateTime.Now;
                     newUserInfo.hasTicket       = true;
                     ModifyUser(newUserInfo);
                     UserNotifycation.SendNotify(newUserInfo,
                                                 "You have boarded on bus " + busReportMsg.busLine + " at " + busReportMsg.stopName + ". You have been charged by your credit card and you will own an e-ticket for " + ServerConfig.GetT_VAL() + " hours.");
                 }
                 //pay by account balance
                 else
                 {
                     UserInfo newUserInfo = aUserInfo;
                     newUserInfo.hasTicket = false;
                     if ((newUserInfo.balance -= (int)ServerConfig.GetT_COST()) <= 0)
                     {
                         ModifyUser(newUserInfo);
                         //balance is less than 0, the user should be notified
                         UserNotifycation.SendNotify(newUserInfo, "You have boarded on bus " + busReportMsg.busLine + " at " + busReportMsg.stopName + ". You have been charged " + ServerConfig.GetT_COST() + " EUR and please add value!");
                     }
                     else
                     {
                         ModifyUser(newUserInfo);
                         UserNotifycation.SendNotify(newUserInfo, "You have boarded on bus " + busReportMsg.busLine + " at " + busReportMsg.stopName + ". You have been charged " + ServerConfig.GetT_COST() + " EUR.");
                     }
                 }
             }
         }
     }
 }
Example #3
0
        public void SendMsg(BusReportMsg busReportMsg)
        {
            BinaryFormatter fmt = new BinaryFormatter();

            //do
            {
                try
                {
                    //TODO:这个在将来要移除
                    //Thread.Sleep(2000);

                    TcpClient client = new TcpClient();
                    client.Connect("127.0.0.1", 6666);
                    NetworkStream stream      = client.GetStream();
                    RequestInfo   requestInfo = new RequestInfo();
                    requestInfo.servicetype = ServiceType.SERVICE_BUSREPORT;
                    requestInfo.Content     = busReportMsg;

                    //byte[] bytes = Encoding.Unicode.GetBytes(busReportMsg.ToString());
                    lock (stream)
                    {
                        //stream.Write(bytes, 0, bytes.Length);
                        fmt.Serialize(stream, requestInfo);
                    }
                    Console.WriteLine("bus NO.{0} has sent MSG to server", busReportMsg.busLine);
                    stream.Close();
                    client.Close();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                    //break;
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                    //break;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: {0}", e);
                    //break;
                }
            } //while (!isStopSend);
        }
Example #4
0
        public static void ServiceThread(object tcpClient)
        {
            NetworkStream stream = null;

            try
            {
                BinaryFormatter fmt         = new BinaryFormatter();
                RequestInfo     requestInfo = new RequestInfo();
                stream = ((TcpClient)tcpClient).GetStream();
                //lock (stream)
                {
                    //get the RequestInfo instance by Deserializing
                    requestInfo = (RequestInfo)fmt.Deserialize(stream);
                }
                switch (requestInfo.servicetype)
                {
                case ServiceType.SERVICE_BUSREPORT:
                    BusReportMsg busReportMsg = (BusReportMsg)requestInfo.Content;
                    //transfer BusReportManager to process
                    BusReportManager busReportManger = new BusReportManager();
                    busReportManger.ManageReport(busReportMsg);
                    break;

                case ServiceType.SERVICE_ROADCOND:
                    System.Windows.MessageBox.Show("SERVICE_ROADCOND");
                    break;

                default:
                    break;
                }
            }
            catch
            {
            }
            finally
            {
                ((TcpClient)tcpClient).Close();
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
Example #5
0
        //Thread: Update Passenger and BusLine info
        private void AtBusStopActivity()
        {
            RoadStop currentStop = StopBase.StopTable[Stops[localid % Stops.Count]];

            lock (currentStop)
            {
                //Passengers alight---------------------------------
                //Bus Report
                BusReportMsg alightMessage = new BusReportMsg();
                alightMessage.busLine  = this.BusLineName;
                alightMessage.isBoard  = false;
                alightMessage.stopName = currentStop.StopName;
                alightMessage.time     = System.DateTime.Now;
                alightMessage.busId    = (uint)BusLineNo;
                HashSet <ulong> alightAddresses = new HashSet <ulong>();

                //Terminal Stop
                if (localid % Stops.Count == Stops.Count - 1)
                {
                    foreach (Passenger p in this.OnBoard)
                    {
                        currentStop.Passengers.Add(p);
                    }
                    foreach (Passenger p in this.OnBoard)
                    {
                        alightAddresses.Add(p.CellPhoneAddress);
                    }
                    this.OnBoard.Clear();
                }
                //Other Stops
                else
                {
                    List <Passenger> removeList = new List <Passenger>();
                    foreach (Passenger p in this.OnBoard)
                    {
                        if (!p.NextAction().Equals(this.BusLineName))
                        {
                            removeList.Add(p);
                            currentStop.Passengers.Add(p);
                        }
                    }
                    foreach (Passenger p in removeList)
                    {
                        alightAddresses.Add(p.CellPhoneAddress);
                        //Remove on board
                        this.OnBoard.Remove(p);
                    }
                }
                alightMessage.cellPhoneAddrSet = alightAddresses;
                if (alightMessage.cellPhoneAddrSet.Count != 0)
                {
                    netComm.SendMsg(alightMessage);
                }

                //Pick up passengers---------------------------------
                List <Passenger> removeList2  = new List <Passenger>();
                BusReportMsg     boardMessage = new BusReportMsg();
                boardMessage.busLine  = this.BusLineName;
                boardMessage.isBoard  = true;
                boardMessage.stopName = currentStop.StopName;
                boardMessage.time     = System.DateTime.Now;
                boardMessage.busId    = (uint)BusLineNo;
                HashSet <ulong> boardAddresses = new HashSet <ulong>();

                foreach (Passenger p in currentStop.Passengers)
                {
                    if (p.NextAction().Equals(this.BusLineName))
                    {
                        this.OnBoard.Add(p);
                        boardAddresses.Add(p.CellPhoneAddress);
                        removeList2.Add(p);
                    }
                }
                foreach (Passenger p in removeList2)
                {
                    currentStop.Passengers.Remove(p);
                }

                //Bus Report
                boardMessage.cellPhoneAddrSet = boardAddresses;
                if (boardMessage.cellPhoneAddrSet.Count != 0)
                {
                    netComm.SendMsg(boardMessage);
                }

                OnBoardDesc = GetOnBoardDesc();
            }

            //Spend some time at the bus stop
            Thread.Sleep(rn.Next(1000, 6000));

            mre.WaitOne();
            Parent.Dispatcher.Invoke(new UpdateBusInfo(UpdateBusInfoDelegate));
        }