コード例 #1
0
        /// <summary>
        /// Creates a TCP timeserver that listens for incoming connections and then responds with the current levels
        /// </summary>
        /// <returns>
        /// void
        /// </returns>
        public static void TcpUnitServer()
        {
            bool done = false;

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

            // Echo we're beginning the listener
            Console.WriteLine("Unit server now listening for TCP requests on port " + portNum);

            listener.Start();

            while (!done)
            {
                TcpClient client = listener.AcceptTcpClient();                           // accept the connection

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

                byte[] byte_levels = Encoding.ASCII.GetBytes(RetrieveData.ReadLevels()); // Retrieve and convert

                // try to send our data
                try
                {
                    ns.Write(byte_levels, 0, byte_levels.Length);
                    ns.Close();
                    client.Close();

                    // Show how many bytes we sent on each connection
                    Console.WriteLine("Connection Established... Sent " + byte_levels.Length.ToString() + " Bytes");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
コード例 #2
0
        public static void TcpTownClient()
        {
            // Placeholders
            String received = "";

            // Fetch ips in a list
            Console.WriteLine("Fetching IPs");
            SQLHelper getips = new SQLHelper("SELECT ip FROM units");

            getips.Run_Cmd();
            getips.SetIPs();

            // Make a new list for the ips and ids
            List <string> iplist = getips.Get_List();
            //List<string> idlist = getids.Get_List();

            // Make a string for errors
            String errors = "";

            Console.WriteLine("Beginning Requests...");

            // Now go through down each ip and id in the lists and request the data
            foreach (var ipaddress in iplist)
            {
                // Grab the associated unit address from the database
                SQLHelper getid = new SQLHelper("SELECT unit_id FROM units WHERE ip='" + ipaddress + "' LIMIT 1");
                //getid.Run_Reader();
                string unit_id = getid.RunAndReturnOne();

                // if the id is not our city identifier, proceed with asking for levels
                if (!unit_id.ToString().Contains(cityID))
                {
                    // Check if we're the unit before we try opening a network string
                    string checkipcmd = "hostname -I";
                    var    result     = checkipcmd.ExecBash();
                    result = result.TrimEnd('\r', '\n');
                    result = result.TrimEnd();

                    // if we aren't the unit, proceed
                    if (ipaddress.ToString() != result)
                    {
                        // Create a new TCP Client with the address and default port number
                        var client = new TcpClient(ipaddress, portNum);

                        // Establish a network Stream
                        NetworkStream ns = client.GetStream();

                        // Setup a byte array
                        byte[] bytes = new byte[1024];

                        // Read the bytes from the network stream into the array
                        int bytesRead = ns.Read(bytes, 0, bytes.Length);

                        // Format to string
                        received = Encoding.ASCII.GetString(bytes, 0, bytesRead);
                    }

                    // if we ARE the unit, read the files locally
                    else
                    {
                        received = RetrieveData.ReadLevels();
                    }

                    // Turn the input levels to an array
                    string[] levels = received.Split(',');

                    // Turn each level into a double
                    double wat = Convert.ToDouble(levels[0]);
                    double sew = Convert.ToDouble(levels[1]);
                    double pow = Convert.ToDouble(levels[2]);

                    // Generate a timestamp
                    Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

                    // Insert values with associated unit id into Database
                    SQLHelper insertcmd = new SQLHelper("INSERT INTO status VALUES(" + unixTimestamp + "," + "'" + unit_id + "'," + wat + "," + sew + "," + pow + ")");
                    insertcmd.Run_Cmd();

                    // Check for errors
                    errors = RetrieveData.EvaluateLevels(unixTimestamp, unit_id, wat, sew, pow);

                    // If we have errors, pause briefly and then send them!
                    if (!String.IsNullOrEmpty(errors))
                    {
                        Console.WriteLine("Errors detected, sending to city control...");
                        System.Threading.Thread.Sleep(5000);
                        try
                        {
                            SendError(errors);
                        }

                        catch (Exception e)
                        {
                            Console.WriteLine("Error reporting failed! Please ensure city control is powered on and running");
                        }
                    }

                    // Report levels to the console output
                    Console.WriteLine("Unit " + unit_id + " reports " + wat + " water, " + sew + " sewage, " + pow + " power");
                }

                // if we are dealing with a city, we need to send the acknowledgement message - sp spin up a server
                else
                {
                    // echo we're ignoring
                    Console.WriteLine("Skipping " + unit_id);
                }
            }
        }