示例#1
0
        public void Run()
        {
            BackendSettings backendSettings = new BackendSettings();

            backendSettings.LoadFromFile("settings.xml");
            while (true)
            {
                TcpClient currentConnection = null;
                foreach (string connectionString in backendSettings.reporters)
                {
                    Console.WriteLine("Selecting reporter " + connectionString);
                    foreach (IPEndPoint endpoint in FindEndpoint(connectionString))
                    {
                        Console.WriteLine("Connecting to " + endpoint);
                        try
                        {
                            TcpClient    newClient = new TcpClient(endpoint.AddressFamily);
                            IAsyncResult ar        = newClient.BeginConnect(endpoint.Address, endpoint.Port, null, null);
                            if (ar.AsyncWaitHandle.WaitOne(5000))
                            {
                                if (newClient.Connected)
                                {
                                    newClient.EndConnect(ar);
                                    currentConnection = newClient;
                                    currentEndpoint   = connectionString;
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("Connection failed to " + endpoint.Address + " port " + endpoint.Port);
                                    newClient.Close();
                                }
                            }
                            else
                            {
                                Console.WriteLine("Connection failed to " + endpoint.Address + " port " + endpoint.Port);
                                newClient.Close();
                            }
                        }
                        catch
                        {
                            Console.WriteLine("Connection failed to " + endpoint.Address + " port " + endpoint.Port);
                        }
                    }
                    if (currentConnection != null)
                    {
                        break;
                    }
                }
                if (currentConnection == null)
                {
                    Console.WriteLine("Failed to connect to all endpoints. Waiting 60 seconds.");
                }
                else
                {
                    Console.WriteLine("Connected!");
                    DatabaseConnection databaseConnection = new DatabaseConnection(backendSettings);
                    databaseDriver = new DatabaseDriver(databaseConnection);
                    databaseClient = new DatabaseClient(currentConnection, databaseDriver);
                    databaseClient.Run();
                    Console.WriteLine("Disconnected! Reconnecting in 60 seconds...");
                    databaseClient  = null;
                    databaseDriver  = null;
                    currentEndpoint = null;
                }
                Thread.Sleep(60000);
            }
        }
 public DatabaseConnection(BackendSettings settings)
 {
     this.settings = settings;
 }