Exemple #1
0
        public IndexedHashtable disconnect()
        {
            int lth = cxnTable.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                queries[i] = new QueryThread(cxnTable.GetValue(i), "disconnect", new Object[0]);
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(lth);

            for (int i = 0; i < lth; i++)
            {
                string key = (string)cxnTable.GetKey(i);
                threads[i].Join();
                if (queries[i].isExceptionResult())
                {
                    result.Add(key, queries[i].Result);
                }
                else
                {
                    result.Add(key, "OK");
                }
            }
            cxnTable.Clear();
            return(result);
        }
        public IndexedHashtable connect()
        {
            int lth = cxnTbl.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                queries[i] = new QueryThread(cxnTbl.GetValue(i), "connect", new Object[0]);
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(lth);

            for (int i = 0; i < lth; i++)
            {
                string key = (string)cxnTbl.GetKey(i);
                threads[i].Join();

                //Need to report result whether it's a connection or an exception.
                if (queries[i].isExceptionResult())
                {
                    result.Add(key, queries[i].Result);
                    Connection cxn = (Connection)cxnTbl.GetValue(i);
                    cxn.ErrorMessage = ((Exception)queries[i].Result).Message;
                    cxn.IsConnected  = false;
                }
                else
                {
                    result.Add(key, ((Connection)cxnTbl.GetValue(key)).DataSource);
                }
            }
            return(result);
        }
Exemple #3
0
        private void QueryThreadChanged(object sender, EventArgs e)
        {
            QueryThread messenger        = (QueryThread)sender;
            QueryThreadPoolEventArgs arg = (QueryThreadPoolEventArgs)e;
            string siteId = messenger.Connection.DataSource.SiteId.Id;

            if (arg.ConnectionEventType == QueryThreadPoolEventArgs.ConnectionChangeEventType.ConnectionAvailable)
            {
                messenger.CompleteTimestamp      = DateTime.Now;
                messenger.Connection.IsAvailable = true;
                QueryThread qt;
                if ((qt = _queue.dequeue(siteId)) != null) // if there are queued items for this cxn site
                {
                    qt.setConnection(messenger.Connection);
                    qt.DequeueTimestamp = DateTime.Now;
                    qt.Changed         += new EventHandler(QueryThreadChanged);
                    qt.execute();
                }
            }
            else if (arg.ConnectionEventType == QueryThreadPoolEventArgs.ConnectionChangeEventType.Disconnected)
            {
                _connections[siteId].Remove(messenger.Connection);
                _currentActiveConnections--;
                // re-allocate connection? some out of process algorithm to re-use?
            }
        }
Exemple #4
0
        public static IndexedHashtable execute2(IndexedHashtable cxnTable, string daoName, string methodName, Object[] args)
        {
            if (cxnTable == null || cxnTable.Count == 0)
            {
                throw new Exception("No connections!");
            }
            int lth = cxnTable.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                if (cxnTable.GetValue(i).GetType().IsAssignableFrom(typeof(Exception)))
                {
                    continue;
                }
                Connection cxn = (Connection)cxnTable.GetValue(i);
                if (!cxn.IsConnected)
                {
                    continue;
                }
                Object dao = ((Connection)cxnTable.GetValue(i)).getDao(daoName);
                if (dao == null)
                {
                    continue;
                }
                queries[i] = new QueryThread(dao, methodName, args);
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(cxnTable.Count);

            for (int i = 0; i < threads.Length; i++)
            {
                if (cxnTable.GetValue(i).GetType().IsAssignableFrom(typeof(Exception)))
                {
                    result.Add((string)cxnTable.GetKey(i), (Exception)cxnTable.GetValue(i));
                    continue;
                }
                Connection cxn = (Connection)cxnTable.GetValue(i);
                if (!cxn.IsConnected)
                {
                    result.Add((string)cxnTable.GetKey(i), new Exception("Source is not connected"));
                    continue;
                }
                Object dao = ((Connection)cxnTable.GetValue(i)).getDao(daoName);
                if (dao == null)
                {
                    result.Add((string)cxnTable.GetKey(i), new Exception("Invalid dao: " + daoName));
                    continue;
                }
                threads[i].Join();
                result.Add((String)cxnTable.GetKey(i), queries[i].Result);
            }
            return(result);
        }
Exemple #5
0
        /// <summary>
        /// This function is the heart of the connection pool dictionary. It connects to a site
        /// and attaches the event handlers by executing a standard gettimestamp request
        /// so the connection can immediately begin processing queued query threads
        /// </summary>
        /// <param name="site">Site to which to connect</param>
        /// <param name="addToCxnCollection">Should the site be added to the pools connection collection?</param>
        /// <returns></returns>
        AbstractConnection connectWithReturn(Site site, bool addToCxnCollection)
        {
            //Site[] sites = _poolSites.ToArray();

            Site               currentSite = site;
            DataSource         src         = currentSite.Sources[0];
            AbstractDaoFactory factory     = AbstractDaoFactory.getDaoFactory(AbstractDaoFactory.getConstant(src.Protocol));
            AbstractConnection c           = factory.getConnection(src);

            //c.ConnectionId = new Guid();
            c.Account = _account;
            c.Account = new VistaAccount(c);
            c.Account.AuthenticationMethod = _account.AuthenticationMethod;
            c.Account.Permissions          = new Dictionary <string, AbstractPermission>();
            AbstractPermission cprs = new MenuOption("OR CPRS GUI CHART");

            cprs.IsPrimary = true;
            c.Account.Permissions.Add(cprs.Name, cprs);
            // this needs to be up here so we can reference it if an error occurs
            QueryThread  qt = new QueryThread("", new VistaToolsDao(null), "getTimestamp", new object[] { });
            EventHandler eh = new EventHandler(QueryThreadChanged);

            try
            {
                User user = (User)c.authorizedConnect(_credentials, cprs, src);
                c.IsAvailable = true;
                // initiliaze this connection's eventhandler by making a standard timestamp request
                qt.setConnection(c);
                qt.Changed += eh;
                _currentActiveConnections++;
                qt.execute();

                if (!(qt.Result is string))
                {
                    throw new ConnectionException("Connection does not appear to be active");
                }

                if (addToCxnCollection)
                {
                    addConnection(currentSite.Id, c);
                }
                return(c);
            }
            catch (Exception)
            {
                qt.Changed   -= eh;
                c.IsAvailable = false;
                _currentActiveConnections--;
                try
                {
                    c.disconnect();
                }
                catch (Exception) { }
            }
            return(null);
        }
Exemple #6
0
        public Dictionary <string, object> debug(String daoName, String methodName, Object[] args)
        {
            int lth = cxnTable.Count;

            QueryThread[] queries = new QueryThread[lth];
            Dictionary <string, object> result = new Dictionary <string, object>(cxnTable.Count);

            for (int i = 0; i < lth; i++)
            {
                Object dao = ((Connection)cxnTable.GetValue(i)).getDao(daoName);
                queries[i] = new QueryThread(cxnTable.GetValue(i), methodName, args);
                queries[i].execute();
                result.Add((String)cxnTable.GetKey(i), queries[i].Result);
            }
            return(result);
        }
Exemple #7
0
        private void QueueChanged(object sender, EventArgs e)
        {
            QueryThreadPoolEventArgs arg = (QueryThreadPoolEventArgs)e;

            if (arg.QueueEventType == QueryThreadPoolEventArgs.QueueChangeEventType.QueryAdded)
            {
                // if connection available then execute
                AbstractConnection cxn = getAvailableConnection(arg.SiteId);
                if (cxn != null)
                {
                    QueryThread qt = _queue.dequeue(arg.SiteId);
                    qt.DequeueTimestamp = DateTime.Now;
                    qt.setConnection(cxn);
                    qt.Changed += new EventHandler(QueryThreadChanged);
                    qt.execute();
                }
            }
        }
Exemple #8
0
        public void queue(QueryThread qt, string siteId)
        {
            lock (_connectionQueues)
            {
                if (!_connectionQueues.ContainsKey(siteId))
                {
                    _connectionQueues.Add(siteId, new Queue <QueryThread>());
                }
                _connectionQueues[siteId].Enqueue(qt);
            }
            QueryThreadPoolEventArgs e = new QueryThreadPoolEventArgs()
            {
                QueueEventType = QueryThreadPoolEventArgs.QueueChangeEventType.QueryAdded,
                SiteId         = siteId
            };

            QueueChanged(e);
        }
Exemple #9
0
            public void QueryMethod(string daoName, string methodName, object[] args)
            {
                ArrayList queries = new ArrayList(myCset.Count);
                ArrayList threads = new ArrayList(myCset.Count);

                foreach (KeyValuePair <string, AbstractConnection> kvp in myCset.Connections)
                {
                    AbstractConnection c = kvp.Value;
                    if (skipThisConnection(c))
                    {
                        continue;
                    }
                    object      dao = getDao(daoName, c);
                    QueryThread q   = new QueryThread(c.DataSource.SiteId.Id, dao, methodName, args);
                    Thread      t   = new Thread(new ThreadStart(q.execute));
                    queries.Add(q);
                    threads.Add(t);
                    t.Start();
                }
                for (int i = 0; i < threads.Count; i++)
                {
                    ((Thread)threads[i]).Join();
                }
                myCset.Results    = new IndexedHashtable();
                myCset.Exceptions = null;
                for (int i = 0; i < queries.Count; i++)
                {
                    QueryThread t = (QueryThread)queries[i];
                    if (t.isExceptionResult())
                    {
                        handleException(t);
                        if (myCset.Exceptions == null)
                        {
                            myCset.Exceptions = new IndexedHashtable();
                        }
                        myCset.Exceptions.Add(t.Id, t.Result);
                    }
                    else
                    {
                        handleNonException(t);
                        myCset.Results.Add(t.Id, t.Result);
                    }
                }
            }
Exemple #10
0
        public IndexedHashtable connect()
        {
            int lth = cxnTable.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                queries[i] = new QueryThread(cxnTable.GetValue(i), "connect", new Object[0]);
                //queries[i].execute();
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(lth);

            for (int i = 0; i < lth; i++)
            {
                string key = (string)cxnTable.GetKey(i);
                threads[i].Join();

                //Need to report result whether it's a connection or an exception.
                if (queries[i].isExceptionResult())
                {
                    result.Add(key, queries[i].Result);
                }
                else
                {
                    result.Add(key, ((Connection)cxnTable.GetValue(key)).DataSource);
                }
            }

            //Now for all the results that failed to connect, we remove them from the connection table.
            for (int i = 0; i < lth; i++)
            {
                if (queries[i].isExceptionResult())
                {
                    string key = (string)cxnTable.GetKey(i);
                    cxnTable.Remove(key);
                    cxnTable.Add(key, queries[i].Result);
                }
            }
            return(result);
        }
        public IndexedHashtable disconnectRemoteSites()
        {
            if (cxnTbl.Count < 2)
            {
                throw new Exception("No remote connections");
            }

            // Only disconnect from the ones that are connected
            IndexedHashtable myCxns = getConnections();

            int lth = myCxns.Count - 1;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int threadIdx = 0, cxnIdx = 1; threadIdx < lth; threadIdx++, cxnIdx++)
            {
                queries[threadIdx] = new QueryThread(myCxns.GetValue(cxnIdx), "disconnect", new Object[0]);
                threads[threadIdx] = new Thread(new ThreadStart(queries[threadIdx].execute));
                threads[threadIdx].Start();
            }
            IndexedHashtable result = new IndexedHashtable(lth);

            for (int threadIdx = 0, cxnIdx = 1; threadIdx < lth; threadIdx++, cxnIdx++)
            {
                string key = (string)myCxns.GetKey(cxnIdx);
                threads[threadIdx].Join();
                if (queries[threadIdx].isExceptionResult())
                {
                    result.Add(key, queries[threadIdx].Result);
                }
                else
                {
                    result.Add(key, "OK");
                }
            }

            // Now remove all but the logon connection from the table
            Connection loginCxn = LoginConnection;

            cxnTbl = new IndexedHashtable();
            cxnTbl.Add(loginCxn.SiteId.Key, loginCxn);
            return(result);
        }
        public IndexedHashtable disconnect()
        {
            if (cxnTbl.Count == 0)
            {
                throw new Exception("No connections");
            }

            // Only disconnect from the ones that are connected
            IndexedHashtable myCxns = getConnections();

            int lth = myCxns.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                queries[i] = new QueryThread(myCxns.GetValue(i), "disconnect", new Object[0]);
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(lth);

            for (int i = 0; i < lth; i++)
            {
                string key = (string)myCxns.GetKey(i);
                threads[i].Join();
                if (queries[i].isExceptionResult())
                {
                    result.Add(key, queries[i].Result);
                }
                else
                {
                    result.Add(key, "OK");
                }
            }

            // We only disconnected from connected connections, but we want to clear
            // the entire table
            cxnTbl = new IndexedHashtable();
            return(result);
        }
Exemple #13
0
        public IndexedHashtable execute(String daoName, String methodName, Object[] args)
        {
            int lth = cxnTable.Count;

            QueryThread[] queries = new QueryThread[lth];
            Thread[]      threads = new Thread[lth];
            for (int i = 0; i < lth; i++)
            {
                if (cxnTable.GetValue(i).GetType().IsAssignableFrom(typeof(Exception)))
                {
                    continue;
                }
                Object dao = ((Connection)cxnTable.GetValue(i)).getDao(daoName);
                queries[i] = new QueryThread(dao, methodName, args);
                threads[i] = new Thread(new ThreadStart(queries[i].execute));
                threads[i].Start();
            }
            IndexedHashtable result = new IndexedHashtable(cxnTable.Count);

            for (int i = 0; i < threads.Length; i++)
            {
                if (cxnTable.GetValue(i).GetType().IsAssignableFrom(typeof(Exception)))
                {
                    result.Add((string)cxnTable.GetKey(i), (Exception)cxnTable.GetValue(i));
                    continue;
                }
                try
                {
                    threads[i].Join();
                    result.Add((String)cxnTable.GetKey(i), queries[i].Result);
                }
                catch (Exception)
                {
                    //throw new MdoException(e);
                }
            }
            return(result);
        }
Exemple #14
0
 public void queue(QueryThread qt, string siteId)
 {
     _queue.queue(qt, siteId);
 }
Exemple #15
0
 public override void handleNonException(QueryThread t)
 {
 }
Exemple #16
0
 public override void handleNonException(QueryThread t)
 {
     ConnectionSet.Remove(t.Id);
 }
Exemple #17
0
 public abstract void handleNonException(QueryThread t);