Пример #1
0
        //public string Property(string name, string defaultValue)
        //{
        //    name = (name ?? string.Empty).ToLower();
        //    if ("host.address" == name)
        //    {
        //        return HostAddress ?? defaultValue;
        //    }
        //    else if ("host.name" == name)
        //    {
        //        return HostName ?? defaultValue;
        //    }
        //    else
        //    {
        //        return defaultValue;
        //    }
        //}

        void IProvider.Initialize()
        {
            try {
                NetworkInterfaceManager.Refresh();
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Пример #2
0
        public void ChannelManagementTask(object o)
        {
            int count = 0;

            while (true)
            {
                // For every 5 minutes
                try
                {
                    _messageIdFactory.SaveMark(true);
                    if (_mActive)
                    {
                        // 1. If TCP connection is down, try to reconnect.
                        if (_mActiveChannel == null || !_mActiveChannel.Connected)
                        {
                            _mClientConfig.Refresh();
                            serversFromRemoteConfig = _mClientConfig.Servers;
                            if (null != _mActiveChannel)
                            {
                                Logger.Warn("The current TCP connection to " + _mActiveChannel + " is no longer connected. Will try to re-connect to server.");
                            }

                            var channel = GetActiveConnection(_mClientConfig.Servers, serversFromRemoteConfig);
                            if (channel != null && channel != _mActiveChannel)
                            {
                                var lastChannel = _mActiveChannel;
                                _mActiveChannel = channel;
                                if (lastChannel != null)
                                {
                                    lastChannel.Close();
                                }
                                NetworkInterfaceManager.Refresh();
                            }
                        }
                        // 2.If TCP connection is up, rebalance by connecting to a better CAT server, if necessary.
                        else
                        {
                            // Rebalance step (1): for every one hour, refresh router config from CAT server.
                            if (count % (CatConstants.REFRESH_ROUTER_CONFIG_INTERVAL / CatConstants.TCP_RECONNECT_INTERVAL) == 0)
                            {
                                Logger.Info("Refreshing router config from CAT server");
                                _mClientConfig.Refresh();
                            }
                            // Rebalance step (2): for every 10 min, forcefully re-establish TCP connection, according to latest router config
                            if (count % (CatConstants.TCP_REBALANCE_INTERVAL / CatConstants.TCP_RECONNECT_INTERVAL) == 0)
                            {
                                serversFromRemoteConfig = _mClientConfig.Servers;
                                var channel = GetActiveConnection(_mClientConfig.Servers, serversFromRemoteConfig);
                                if (channel != null && channel != _mActiveChannel)
                                {
                                    var lastChannel = _mActiveChannel;
                                    _mActiveChannel = channel;
                                    if (lastChannel != null)
                                    {
                                        if (lastChannel.Client != null && channel.Client != null)
                                        {
                                            Logger.Info("Rebalancing by re-establising TCP connection. Old connection to: "
                                                        + lastChannel.Client.RemoteEndPoint + " New connection to: " + channel.Client.RemoteEndPoint);
                                        }
                                        lastChannel.Close();
                                    }
                                    NetworkInterfaceManager.Refresh();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex) { Cat.lastException = ex; }
                finally { count++; }
                Thread.Sleep(CatConstants.TCP_RECONNECT_INTERVAL);
            }
        }