Exemple #1
0
        public ReservationMessage SendReservation(ReservationMessage reservationMessage, int timeout)
        {
            Console.WriteLine("[Calling] SendReservation");

            lock (User.userLoger)
            {
                User.userLoger.IncrementNumberOfExchangedMessages("SendReservation");
            }
            if (User.Calendar == null) {
                Console.WriteLine("Calendar is null...initializing it");
                CreateCalendar();
            }

            Console.WriteLine("Timeout to send message: " + timeout);
            if (User.connected == false) {
                Console.WriteLine("I am not connected. Do not answer call");
                Thread.Sleep(2 * timeout);
                return new ReservationMessage();
            }

            List<ReservationSlot> slotList = reservationMessage.reservation.getSlotList();
            bool isDisseminating = IsDisseminating("SEND_RESERVATION");

                if (isDisseminating)
                {
                    Dictionary<int, String> sResponse = ChangeCalendarSlotStatus(slotList, false);

                    ReservationMessage a = new ReservationMessage();
                    a.reservation = reservationMessage.reservation;
                    a.messageCounter *= 2;
                    a.senderName = User.Name;
                    a.AddSlotResponse(User.Name, sResponse);
                    return a;
                }

            List<UserView> unknownUsers = GetUnkownUsersFromReservationUserList(reservationMessage.reservation.getUserList());

            Console.WriteLine("Printing unknown users...");
            foreach (UserView u in unknownUsers) {
                Console.WriteLine("User: "******"Print received slots");
            foreach (ReservationSlot s in slotList) {
                Console.WriteLine(s.ToString());

                if (!ExistsReservationInTheList(reservationMessage.reservation, s.GetNumber())) {
                    User.ReservationPerSlot[s.GetNumber()].addReservation(reservationMessage.reservation);
                    Console.WriteLine("Added Reservation: " + User.ReservationPerSlot[s.GetNumber()].GetReservations()[0].ToString());
                }
                else Console.WriteLine("Reservation was already in the slot list. Do not add it");
            }
            Dictionary<int, String> slotResponse = ChangeCalendarSlotStatus(slotList, false);

            ReservationMessage answer = new ReservationMessage();
            answer.reservation = reservationMessage.reservation;
            answer.messageCounter *= 2;
            answer.senderName = User.Name;
            answer.AddSlotResponse(User.Name, slotResponse);

            int lenght = 2;
            if (Thread.CurrentThread.Name == null) {
                Console.WriteLine("Thread Name is null. Original remote invocation thread");
            }
            else {
                String tName = Thread.CurrentThread.Name;
                Console.WriteLine("Thread Name: " + tName);
                char[] separator = { ' ' };
                String[] words = tName.Split(separator);
                lenght = words.Length;
            }

            //Calls the gossip dissemination protocol to send the message to other nodes
            //Before sending the message we need to remove the user that sent the message
            int userListSize = reservationMessage.reservation.getUserList().Count;
            double userListDouble = (double)userListSize;
            double messageCounterDouble = (double)reservationMessage.messageCounter;
            if ((messageCounterDouble <= (userListDouble / 2)) && (userListSize != 1) && messageCounterDouble > 0) {

                if (Thread.CurrentThread.Name == null || lenght == 2) {
                    Console.WriteLine("Disseminate!");
                    reservationMessage.reservation = DeleteSenderFromUserList(reservationMessage);
                    reservationMessage.messageCounter *= 2;
                    PrintReservation(reservationMessage.reservation);
                    DisseminateInformation(reservationMessage, MessageTypeToDisseminate.SEND_RESERVATION, true);

                    //add received answers to the answer we will send
                    foreach (KeyValuePair<int, ReservationMessage> responses in threadResponses) {
                        Console.WriteLine("Thread " + responses.Key + " responses");
                        Console.WriteLine(responses.Value.PrintSlotResponses());

                        foreach (KeyValuePair<String, Dictionary<int, String>> rpsIndex in responses.Value.GetAllResponses()) {
                            answer.AddSlotResponse(rpsIndex.Key, rpsIndex.Value);
                        }
                    }
                }
                else Console.WriteLine("Thread has no permission to disseminate!");
            }

            Console.WriteLine("Answer to send");
            Console.WriteLine(answer.ToString());
            return answer;
        }