Exemplo n.º 1
0
        private static void InteractiveConsole()
        {
            bool running = true;

            while (running)
            {
                try
                {
                    string line    = Console.ReadLine().ToLower();
                    bool   handled = false;
                    if (line == "q")
                    {
                        handled = true;
                        running = false;
                        mainThread.Abort();
                    }
                    if (line == "p")
                    {
                        handled = true;
                        DatabaseDriver thisDriver = databaseDriver;
                        if (thisDriver != null)
                        {
                            thisDriver.PrintServers();
                        }
                    }
                    if (line == "r")
                    {
                        handled = true;
                        DatabaseClient thisClient = databaseClient;
                        if (thisClient != null)
                        {
                            thisClient.Disconnect();
                        }
                    }
                    if (line == "d")
                    {
                        handled = true;
                        string displayEndpoint = currentEndpoint;
                        if (displayEndpoint != null)
                        {
                            Console.WriteLine("Currently connected to " + displayEndpoint);
                        }
                    }
                    if (!handled)
                    {
                        Console.WriteLine("Commands: q for quit, p for print server/relay tree, r for reconnect to the network, d to display the current remote endpoint");
                    }
                }
                catch
                {
                    NoConsole();
                }
            }
        }
        public DatabaseClient(TcpClient connectedConnection, DatabaseDriver databaseDriver)
        {
            connected           = true;
            this.databaseDriver = databaseDriver;
            NetworkHandler <object> networkHandler = new NetworkHandler <object>();

            networkHandler.SetHeartbeatCallback(HandleSendHeartbeat, 5000, 20000);
            networkHandler.SetDisconnectCallback(HandleDisconnect);
            networkHandler.SetMessageCallback((int)MessageType.HEARTBEAT, HandleHeartbeat);
            networkHandler.SetMessageCallback((int)MessageType.CONNECT, HandleConnect);
            networkHandler.SetMessageCallback((int)MessageType.REPORT, HandleReport);
            networkHandler.SetMessageCallback((int)MessageType.DISCONNECT, HandleDisconnect);
            networkClient = new NetworkClient <object>(networkHandler, connectedConnection, false);
            if (networkClient == null)
            {
                //Shutup compiler.
            }
        }
Exemplo n.º 3
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);
            }
        }