Beispiel #1
0
        // Initial mode for city - sends identifier, then awaits for acknowledgement to close listening server
        // returns true if successful, false if unsuccessful.
        public static void CitySetup()
        {
            bool done   = false;
            bool status = false; // we set to true if connection was successful!

            // Listen for connections on our defined port
            var listener = new TcpListener(IPAddress.Any, portNum);

            // Echo we're beginning the listener
            Console.WriteLine("City server now awaiting TCP connection to Town Control on port " + portNum);

            listener.Start();

            TcpClient client = listener.AcceptTcpClient(); // accept the connection

            NetworkStream ns = client.GetStream();         // establish a network stream

            while (!done)
            {
                byte[] outgoing_msg = Encoding.ASCII.GetBytes(cityID); // Retrieve and convert

                // try to send our data
                try
                {
                    // Show we received a connection request
                    Console.WriteLine("Incoming Connection....");

                    ns.Write(outgoing_msg, 0, outgoing_msg.Length);
                    //ns.Close();
                    //client.Close();

                    // Show we sent the data
                    Console.WriteLine("Sent city...");


                    Byte[] data = new Byte[256];

                    String responseString = string.Empty;

                    Int32 bytes = ns.Read(data, 0, data.Length);

                    Console.WriteLine("Looking for response...");

                    responseString = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

                    if (!String.IsNullOrEmpty(responseString))
                    {
                        Console.WriteLine("Success! Received MSG " + responseString);
                        status = true;
                        ContextSwitch.FinishSetup();
                        break;
                    }
                    // START THE CLIENT
                    // IF CLIENT RETURNS TRUE SET DONE = TRUE
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    break;
                }
            }

            // Stop the listener on success
            ns.Close();
            listener.Stop();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            #region Command Arguments
            // Check for user input
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "--set-unit")
                {
                    ContextSwitch.SetAsUnit();
                }

                if (args[i] == "--set-town")
                {
                    ContextSwitch.SetAsTown();
                }

                if (args[i] == "--set-city")
                {
                    ContextSwitch.SetAsCity();
                }

                if (args[i] == "--get-mode")
                {
                    Console.WriteLine(ContextSwitch.GetMode());
                }

                if (args[i] == "--tcpscan")
                {
                    NetworkRW.TCPScan();
                }

                if (args[i] == "--gendb")
                {
                    SQLHelper.CreateDB();
                }

                if (args[i] == "--refresh-city")
                {
                    NetworkRW.FindCity();
                }

                if (args[i] == "--send-errors")
                {
                    NetworkRW.SendError();
                }
            }
            #endregion

            #region DeviceDefaults
            // If mode is not already set, then set it by default to unit mode
            if (!File.Exists("mode"))
            {
                ContextSwitch.SetAsDefault();
            }

            // Create our dummy files
            if (!File.Exists("water") || !File.Exists("sewage") || !File.Exists("power"))
            {
                RetrieveData.CreateFiles();
            }

            // Create a setupstat file
            if (!File.Exists("setupstat"))
            {
                ContextSwitch.CreateSetup();
            }

            #endregion

            #region ModeDependants
            if (ContextSwitch.GetMode() == 0) // if unit
            {
                // We await a connection from the Town Control
                // by using the TCP Unit Server in Network Read/Write
                Console.WriteLine("Unit Mode Detected!");
                ContextSwitch.FinishSetup(); // Setup can be set to finished
                NetworkRW.TcpUnitServer();
            }

            if (ContextSwitch.GetMode() == 1) // if town
            {
                Console.WriteLine("Town Mode Detected!");

                string stat = ContextSwitch.GetSetup();

                // if the database doesn't exist, generate it and populate it
                if (stat == "pending")
                {
                    Console.WriteLine("Generating Database...");
                    SQLHelper.CreateDB();        // create the database
                    Console.WriteLine("Scanning Network...");
                    NetworkRW.TCPScan();         // populate the database
                    Console.WriteLine("Detecting City...");
                    NetworkRW.FindCity();        // find the city
                    ContextSwitch.FinishSetup(); // finish the setup
                }

                // Check setup again
                stat = ContextSwitch.GetSetup();

                if (stat == "done")
                {
                    // Initiate the TCP town client
                    Console.WriteLine("Starting town client...");
                    NetworkRW.TcpTownClient();

                    // When finished, echo the errors to city
                    Console.WriteLine("Sending any errors to city control...");
                    System.Threading.Thread.Sleep(5000); // pause briefly before continuing
                    NetworkRW.SendError();
                    Console.WriteLine("Successfully sent errors");
                }
            }

            if (ContextSwitch.GetMode() == 2) // if city
            {
                Console.WriteLine("City Mode Detected!");

                string stat = ContextSwitch.GetSetup();

                // if the database doesn't exist yet generate it, but do not populate it.
                if (stat == "pending")
                {
                    Console.WriteLine("Initializating setup:");
                    Console.WriteLine("Generating Database...");
                    SQLHelper.CreateDB();  // create the database
                    NetworkRW.CitySetup(); // run the city setup
                }

                //check setup again
                stat = ContextSwitch.GetSetup();

                if (stat == "done")
                {
                    // just run the city server
                    NetworkRW.CityServer();
                }
            }

            #endregion
        }