Exemplo n.º 1
0
        public DatabaseClient GetClient()
        {
            lock (this)
            {
                if(mClients.Length > 2)
                for (uint i = 0; i < mClients.Length; i++)
                {
                    if (mClientAvailable[i] == true)
                    {
                        mClientAvailable[i] = false;
                        mClientStarvationCounter = 0;

                        if (mClients[i].State == ConnectionState.Closed)
                        {
                            try
                            {
                                ConnectionState StateConn = mClients[i].Connect();
                                if (StateConn == ConnectionState.Connecting)
                                {
                                    Log.WriteLine(LogLevel.Debug,"Opening connection for database client #" + mClients[i].mHandle);
                                }
                                else if (StateConn == ConnectionState.Open)
                                {
                                    mClients[i].Destroy();
                                    mClients[i] = new DatabaseClient(i, this);
                                    mClients[i].Connect();
                                }
                                else if (StateConn == ConnectionState.Closed)
                                {
                                   //TODO Caching
                                    Console.WriteLine("Caching client Message");
                                    return mClients[i];
                                }
                                else if(StateConn == ConnectionState.Broken)
                                {
                                    mClients[i].Destroy();
                                    mClients[i] = new DatabaseClient(i, this);
                                    mClients[i].Connect();
                                }
                            }
                            catch(Exception ex)
                            {

                                Log.WriteLine(LogLevel.Exception,"{0}", ex.Message);
                            }
                        }

                        if (mClients[i].State == ConnectionState.Open)
                        {
                            mClients[i].UpdateLastActivity();
                            if (!mClients[i].IsBussy)
                            {
                                mClients[i].IsBussy = true;
                                return mClients[i];
                            }
                        }
                    }
                }

                mClientStarvationCounter++;

                if (mClientStarvationCounter >= ((mClients.Length + 1) / 2))
                {
                    mClientStarvationCounter = 0;
                    SetClientAmount((uint)(mClients.Length + 1 * 1.3f));

                    return GetClient();
                }

                DatabaseClient pAnonymous = new DatabaseClient(0, this);
                ConnectionState StateConns = pAnonymous.Connect();
                if (StateConns == ConnectionState.Connecting)
                {
                    pAnonymous.IsBussy = true;
                    Log.WriteLine(LogLevel.Debug, "Opening connection for database clientanon");
                }
                else if (StateConns == ConnectionState.Open)
                {
                    pAnonymous.Destroy();
                    pAnonymous = new DatabaseClient(0, this);
                    pAnonymous.Connect();
                }
                else if (StateConns == ConnectionState.Closed)
                {
                    //TODO Caching
                    Console.WriteLine("Caching client Message");
                    return pAnonymous;
                }
                else if (StateConns == ConnectionState.Broken)
                {
                    pAnonymous.Destroy();
                    pAnonymous = new DatabaseClient(0, this);
                    pAnonymous.Connect();
                }
            //                pAnonymous.Connect();

                 Log.WriteLine(LogLevel.Debug,"Handed out anonymous client.");
                return pAnonymous;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the amount of clients that will be available to requesting methods. If the new amount is lower than the current amount, the 'excluded' connections are destroyed. If the new connection amount is higher than the current amount, new clients are prepared. Already existing clients and their state will be maintained.
        /// </summary>
        /// <param name="Amount">The new amount of clients.</param>
        internal void SetClientAmount(uint Amount)
        {
            lock (this)
            {
                if (mClients.Length == Amount)
                    return;

                if (Amount < mClients.Length) // Client amount shrinks, dispose clients that will die
                {
                    for (uint i = Amount; i < mClients.Length; i++)
                    {
                        mClients[i].Destroy();
                        mClients[i] = null;
                    }
                }

                DatabaseClient[] pClients = new DatabaseClient[Amount];
                bool[] pClientAvailable = new bool[Amount];
                for (uint i = 0; i < Amount; i++)
                {
                    if (i < mClients.Length) // Keep the existing client and it's available state
                    {
                        pClients[i] = mClients[i];
                        pClientAvailable[i] = mClientAvailable[i];
                    }
                    else // We are in need of more clients, so make another one
                    {
                        pClients[i] = new DatabaseClient((i + 1), this);
                        pClientAvailable[i] = true; // Elegant?
                    }
                }

                // Update the instance fields
                mClients = pClients;
                mClientAvailable = pClientAvailable;
            }
        }