Exemple #1
0
        public IConnection AcquireConnection(AccessMode mode)
        {
            _routingTableManager.EnsureRoutingTableForMode(mode);
            while (true)
            {
                Uri uri;

                switch (mode)
                {
                case AccessMode.Read:
                    uri = _loadBalancingStrategy.SelectReader(_routingTableManager.RoutingTable.Readers);
                    break;

                case AccessMode.Write:
                    uri = _loadBalancingStrategy.SelectWriter(_routingTableManager.RoutingTable.Writers);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown access mode {mode}");
                }

                if (uri == null)
                {
                    // no server known to routingTable
                    break;
                }
                IConnection conn = CreateClusterConnection(uri, mode);
                if (conn != null)
                {
                    return(conn);
                }
                //else  connection already removed by clusterConnection onError method
            }
            throw new SessionExpiredException($"Failed to connect to any {mode.ToString().ToLower()} server.");
        }
Exemple #2
0
        public async Task <IConnection> AcquireConnectionAsync(AccessMode mode, string database, Bookmark bookmark)
        {
            var routingTable = await _routingTableManager.EnsureRoutingTableForModeAsync(mode, database, bookmark)
                               .ConfigureAwait(false);

            while (true)
            {
                Uri uri;

                switch (mode)
                {
                case AccessMode.Read:
                    uri = _loadBalancingStrategy.SelectReader(routingTable.Readers, database);
                    break;

                case AccessMode.Write:
                    uri = _loadBalancingStrategy.SelectWriter(routingTable.Writers, database);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown access mode {mode}");
                }

                if (uri == null)
                {
                    // no server known to routingTable
                    break;
                }

                var conn =
                    await CreateClusterConnectionAsync(uri, mode, database, bookmark).ConfigureAwait(false);

                if (conn != null)
                {
                    return(conn);
                }

                //else  connection already removed by clusterConnection onError method
            }

            throw new SessionExpiredException($"Failed to connect to any {mode.ToString().ToLower()} server.");
        }
Exemple #3
0
        /// <summary>
        /// Configures the Access Filter in CWSRestart
        /// </summary>
        /// <param name="accessList">The accesslist entries</param>
        /// <param name="mode">The accessmode (whitelist/blacklist)</param>
        public void SetAccess(IList <string> accessList, AccessMode mode)
        {
            if (accessList != null)
            {
                if (accessList.Count == 0)
                {
                    sendCommand(Commands.Command.ACCESSLIST, "", Commands.Action.POST);
                }
                else if (tryConnect())
                {
                    StreamWriter writer = new StreamWriter(client, System.Text.Encoding.UTF8, 2048, true);
                    foreach (string s in accessList)
                    {
                        string message = String.Format("{0} {1} {2}", Commands.Action.POST.ToString(), Commands.Command.ACCESSLIST.ToString(), s);
                        writer.WriteLine(message);
                    }
                    writer.Close();
                }

                disconnectClient();
                sendCommand(Commands.Command.ACCESSMODE, mode.ToString(), Commands.Action.POST);
                disconnectClient();
            }
        }
Exemple #4
0
        public void SetPort(string port, AccessMode mode, Option <ushort> vlan)
        {
            Log.Info(Hostname, $"Setting port {port} to {mode.ToString().ToLower()} mode");
            if (mode == AccessMode.TRUNK)
            {
                if (vlan != null)
                {
                    throw new Exception("Can't set VLAN for a trunking port!");
                }
            }

            if (vlan != null)
            {
                Log.Debug(Hostname, $"Accessing VLAN {vlan.Get()} on port {port}");
            }
            else
            {
                vlan = Vlan.Get(1);
            }

            _switchPortInfos[port] = new SwitchPortInfo {
                Mode = mode, Vlan = vlan.Get()
            };
        }
Exemple #5
0
        /// <summary>
        /// Configures the Access Filter in CWSRestart
        /// </summary>
        /// <param name="accessList">The accesslist entries</param>
        /// <param name="mode">The accessmode (whitelist/blacklist)</param>
        public void SetAccess(IList<string> accessList, AccessMode mode)
        {
            if (accessList != null)
            {
                if (accessList.Count == 0)
                {
                    sendCommand(Commands.Command.ACCESSLIST, "", Commands.Action.POST);
                }
                else if (tryConnect())
                {
                    StreamWriter writer = new StreamWriter(client, System.Text.Encoding.UTF8, 2048, true);
                    foreach (string s in accessList)
                    {
                        string message = String.Format("{0} {1} {2}", Commands.Action.POST.ToString(), Commands.Command.ACCESSLIST.ToString(), s);
                        writer.WriteLine(message);
                    }
                    writer.Close();
                }

                disconnectClient();
                sendCommand(Commands.Command.ACCESSMODE, mode.ToString(), Commands.Action.POST);
                disconnectClient();
            }
        }