//@Override
        public int createNewAppointment(String dateTimeString, int secDurationInt, String header, String comment)     //call by this client but throw XML-RPC
        {
            //call by the client of the current machine to make a new appointment
            //it must return unique sequence number of the new appointment or -1 if it fail to add

            //because in C# XmlRpcServer Library stoping the servicing has an error we refuse the incoming requests if the server
            //is not in its online mode.
            if (!ServerStatus.getServerStatus())
            {
                return(-1);
            }

            Date    dateTime;
            Integer secDuration;

            try
            {
                dateTime    = new Date(dateTimeString);
                secDuration = new Integer(secDurationInt);
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
                return(-1);
            }

            //throwing an exception has sence or meaning here
            //because this function will call locally
            if (secDuration.intValue() < 1)
            {
                throw new System.ArgumentOutOfRangeException("The seconds of duration[secDuration] must be greater than 0.");
            }

            //Create a new seqNumber and add the appointment to the array list!
            try
            {
                SequentialNumber sqn         = new SequentialNumber();
                Appointment      appointment = new Appointment(sqn, dateTime, secDuration.intValue(), header, comment);
                this.appointmentsList.Add(appointment);
                Console.WriteLine("\n_______________________________");
                Console.WriteLine("You have made a new appointment on this host successfully.\n" + appointment.ToString());
                this.setLastModified();
                this.updateLocalDatabase();
                return(sqn.getSequentialNumber());
            }
            catch (System.IndexOutOfRangeException e)         //for creating sequential number
            {
                Console.WriteLine(e.Message);
                return(-1);
            }
        }
        //@Override
        public bool addNewAppointment(int seqNumberInt, String dateTimeString, int secDurationInt, String header, String comment)
        {
            //call by other clients to send a new appointment

            //because in C# XmlRpcServer Library stoping the servicing has an error we refuse the incoming requests if the server
            //is not in its online mode.
            if (!ServerStatus.getServerStatus())
            {
                return(false);
            }

            Integer seqNumber;
            Date    dateTime;
            Integer secDuration;

            try
            {
                seqNumber   = new Integer(seqNumberInt);
                dateTime    = new Date(dateTimeString);
                secDuration = new Integer(secDurationInt);
            }
            catch (System.Exception)
            {
                //Console.WriteLine("\nOne request for adding a new appointment to the current system has received but in converting the data in has crashed by the following exception : ");
                //Console.WriteLine("Exception Message : \n" + e.Message);
                //Console.WriteLine("Received Data : " + "\nSeqential Number : " + seqNumberInt + "\nDate : " + dateTimeString + "\nDuration " + secDurationInt + "\nHeader" + header + "\nComment" + comment);
                return(false);
            }

            if (seqNumber.intValue() < 1)
            {
                //because it will called by remote host throwing exception is meaning less.
                //throw new System.ArgumentOutOfRangeException("The sequential number[seqNumber] must be greater than 0.");
                //Console.WriteLine("\nOne request for adding a new appointment to the current system has received but the sequential number was invalid.");
                //Console.WriteLine("Received Data : " + "\nSeqential Number : " + seqNumberInt + "\nDate : " + dateTimeString + "\nDuration " + secDurationInt + "\nHeader" + header + "\nComment" + comment);
                return(false);
            }
            else if (secDuration.intValue() < 1)
            {
                //throw new System.ArgumentOutOfRangeException("The seconds of duration[secDuration] must be greater than 0.");
                //Console.WriteLine("\nOne request for adding a new appointment to the current system has received but the duration number was invalid.");
                //Console.WriteLine("Received Data : " + "\nSeqential Number : " + seqNumberInt + "\nDate : " + dateTimeString + "\nDuration " + secDurationInt + "\nHeader" + header + "\nComment" + comment);
                return(false);
            }

            //Create requested sequence number or a new sequence number and add the appointment to the array list.
            try
            {
                //We make the sequential number and then check for its existance
                //if the sequential number is exist then we make a new one and it will continue till get a unique sequential number
                //another strategy for here can be to return false if we have the current sequential number in this host
                //but we think it is better to not lose any apointment even with registering it with a wrong sequential number
                //but based on what said in the assignment sheet
                //we must not consider the conflict of the concurrency of the creation of appointments
                SequentialNumber sqn = new SequentialNumber(seqNumber.intValue());

                bool flag;
                do
                {
                    flag = false;
                    foreach (Appointment tempAppointment in this.appointmentsList)                   //search to sure about non existence of the new sequence number in appointment list
                    {
                        if (tempAppointment.getSequentialNumber() == sqn.getSequentialNumber())
                        {
                            sqn  = new SequentialNumber();
                            flag = true;
                            break;
                        }
                    }
                }while(flag);

                if (sqn.getSequentialNumber() != seqNumber.intValue())
                {
                    Console.WriteLine("Remote addAppointment: The sequance number [" + seqNumber.intValue() + "] that has been sent by a remote host, is exist. So a new sequance number has assigned : " + sqn.getSequentialNumber());
                }
                Appointment appointment = new Appointment(sqn, dateTime, secDuration.intValue(), header, comment);
                this.appointmentsList.Add(appointment);

                if (this.appointmentsList.Exists(temp => temp == appointment)) //if the add was successful?
                {
                    //This will show when a far host add an appointment to the list
                    //Console.WriteLine("");
                    //Console.WriteLine("_______________________________");
                    //Console.WriteLine("One appointment has been added.");
                    //Console.WriteLine("-------------------------------");
                    //Console.WriteLine(appointment.ToString());
                    this.setLastModified();
                    this.updateLocalDatabase();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (System.IndexOutOfRangeException) //for creating sequential number
            {
                //Console.WriteLine(e.Message);
                return(false);
            }
        }
Beispiel #3
0
        //Other functions that are helpful for output!

        public int getSequentialNumber()
        {
            return(seqNumber.getSequentialNumber());        //Do not return any reference to the SequentialNumber to prevent any unwanted changes! Just the integer number is enough.
        }