コード例 #1
0
        public int GetFreeServer(int arrival, int lastEndTime)
        {
            int retVal = -1;

            if (this.serverSelectionMode == ServerSelectionMode.highestPriority)
            {
                int ID = -1;
                for (int j = servers.Count - 1; j >= 0; j--)        //Start searching for a free server starting from least priority.
                {
                    if (servers[j].GetTimeServiceEnds() <= arrival) //Server is free.
                    {
                        ID = j;
                    }
                }
                if (ID == -1)//No free servers? Select the closest free one.
                {
                    int soonestTime = int.MaxValue;
                    for (int j = servers.Count - 1; j >= 0; j--)
                    {
                        if (servers[j].GetTimeServiceEnds() <= soonestTime)
                        {
                            ID          = j;
                            soonestTime = servers[j].GetTimeServiceEnds();
                        }
                    }
                }
                retVal = ID;
            }
            else if (serverSelectionMode == ServerSelectionMode.random)
            {
                List <int> serversID = new List <int>();
                for (int j = 0; j < servers.Count; j++)//A list of free servers.
                {
                    if (servers[j].GetTimeServiceEnds() <= arrival)
                    {
                        serversID.Add(j);
                    }
                }
                if (serversID.Count != 0)
                {
                    retVal = serversID[RandomValue.getRandom(0, serversID.Count - 1)];//Pick any random free server.
                }
                else
                {
                    //Pick the nearest free server if no one is free
                    serverSelectionMode = ServerSelectionMode.highestPriority;
                    retVal = GetFreeServer(arrival, lastEndTime);
                    serverSelectionMode = ServerSelectionMode.random;
                }
            }
            else if (serverSelectionMode == ServerSelectionMode.leastUtilization)
            {
                double maxIdle   = -1;
                int    maxIdleID = -1;
                for (int j = 0; j < servers.Count; j++)//Assign to server with maximum idle time.
                {
                    if (servers[j].GetIdle(lastEndTime) > maxIdle)
                    {
                        maxIdle   = servers[j].GetIdle(lastEndTime);
                        maxIdleID = j;
                    }
                }
                retVal = maxIdleID;
            }
            return(retVal);
        }
コード例 #2
0
 public int RandomProbability()
 {
     return(RandomValue.getRandom(0, 100));
 }