コード例 #1
0
        /**
         * <summary>
         * Creates a connected facade and returns it. Called either from constructor or inside
         * a write lock.</summary>
         *
         * <param name="srvs">List of server addresses that this method will try to connect to.</param>
         * <returns>Connected client facade.</returns>
         * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached.</exception>
         */
        private C connect(ICollection <IPEndPoint> srvs)
        {
            Dbg.Assert(guard.IsReaderLockHeld);

            if (srvs.Count == 0)
            {
                throw new GridClientServerUnreachableException("Failed to establish connection to the grid node (address " +
                                                               "list is empty).");
            }

            IOException cause = null;

            foreach (IPEndPoint srv in srvs)
            {
                try {
                    C cnn = connect(srv);

                    Dbg.WriteLine("Connection successfully opened: " + srv);

                    return(cnn);
                }
                catch (IOException e) {
                    Dbg.WriteLine("Unable to connect to grid node [srvAddr=" + srv + ", msg=" + e.Message + ']');

                    cause = e;
                }
            }

            A.NotNull(cause, "cause");

            throw new GridClientServerUnreachableException("Failed to connect to any of the servers in list: "
                                                           + String.Join <IPEndPoint>(",", srvs), cause);
        }
コード例 #2
0
        /**
         * <summary>
         * AND predicate. Passes if and only if both provided filters accept the node.
         * This filter uses short-term condition evaluation, i.e. second filter would not
         * be invoked if first filter returned <c>false</c>.</summary>
         *
         * <param name="first">First filter to check.</param>
         * <param name="second">Second filter to check.</param>
         * <returns>Conjunction predicate.</returns>
         */
        public static Predicate <T> And <T>(Predicate <T> first, Predicate <T> second)
        {
            A.NotNull(first, "first");
            A.NotNull(second, "second");

            return(delegate(T t) {
                return first(t) && second(t);
            });
        }
コード例 #3
0
        /**
         * <summary>
         * Applies filter and returns filtered collection of nodes.</summary>
         *
         * <param name="elements">Nodes to be filtered.</param>
         * <param name="filter">Filter to apply</param>
         * <returns>Filtered collection.</returns>
         */
        public static IList <T> ApplyFilter <T>(IEnumerable <T> elements, Predicate <T> filter) where T : class
        {
            A.NotNull(filter, "filter");

            IList <T> res = new List <T>();

            foreach (T e in elements)
            {
                if (filter(e))
                {
                    res.Add(e);
                }
            }

            return(res);
        }
コード例 #4
0
        /**
         * <summary>
         * Gets active communication facade.</summary>
         *
         * <param name="srvs">Remote nodes to which connection should be established.</param>
         * <returns>Communication facade.</returns>
         * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached after the exception.</exception>
         * <exception cref="GridClientClosedException">If client was closed manually.</exception>
         */
        public C connection(ICollection <IPEndPoint> srvs)
        {
            guard.AcquireReaderLock(Timeout.Infinite);

            try {
                checkClosed();

                if (srvs.Count == 0)
                {
                    throw new GridClientServerUnreachableException("Failed to establish connection to the grid" +
                                                                   " (address list is empty).");
                }

                C conn;

                /* Search for existent connection. */
                foreach (IPEndPoint endPoint in srvs)
                {
                    if (!conns.TryGetValue(endPoint, out conn))
                    {
                        continue;
                    }

                    A.NotNull(conn, "connection");

                    /* Ignore closed connections. */
                    if (conn.CloseIfIdle(DefaultIdleTimeout))
                    {
                        closeIdle(DefaultIdleTimeout);

                        continue;
                    }

                    return(conn);
                }

                /* Create new connection. */
                conn = connect(srvs);

                var cookie = guard.UpgradeToWriterLock(Timeout.Infinite);

                try {
                    var endPoint = conn.ServerAddress;

                    if (conns.ContainsKey(endPoint))
                    {
                        /* Destroy new connection. */
                        conn.Close(false);

                        /* Recover connection from the cache. */
                        conns.TryGetValue(endPoint, out conn);
                    }
                    else
                    {
                        /* Save connection in cache. */
                        conns.Add(conn.ServerAddress, conn);
                    }
                }
                finally {
                    guard.DowngradeFromWriterLock(ref cookie);
                }

                return(conn);
            }
            finally {
                guard.ReleaseReaderLock();
            }
        }