Exemple #1
0
        public static void SendError(string errmsg)
        {
            // string dummyError = "ERR0, Testing Error Message and Error Message Value!";

            SQLHelper getCityControl = new SQLHelper("SELECT ip FROM units WHERE unit_id='" + cityID + "'"); //get city IP

            getCityControl.Run_Cmd();
            getCityControl.SetIPs();

            List <string> city_ip_list = getCityControl.Get_List(); // shove city ip in a list (probably un-necessary)

            foreach (var ip in city_ip_list)
            {
                var client = new TcpClient(ip, portNum);               // connect to the city ip

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

                Krypto krypto = new Krypto();                          // Start Krypto

                string encErr = krypto.Encrypt(errmsg, AESKey);        // Encrypt the data

                byte[] outgoing_msg = Encoding.ASCII.GetBytes(encErr); // turn our encrypted message into bytes

                try
                {
                    ns.Write(outgoing_msg, 0, outgoing_msg.Length); // try to send it
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString()); // if any errors, print them and break
                    break;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Initializes the City TCP server
        /// </summary>
        /// <returns>
        /// none
        /// </returns>
        public static void CityServer()
        {
            bool done = false;

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

            listener.Start();

            // Echo we've started listening
            Console.WriteLine("City Control Listening on port " + portNum + "...");


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

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

                Byte[] data = new Byte[256];

                String responseString = string.Empty;

                // try to send our data
                try
                {
                    Int32 bytes = ns.Read(data, 0, data.Length);

                    Console.WriteLine("Receiving...");

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

                    if (!String.IsNullOrEmpty(responseString))
                    {
                        Krypto krypto    = new Krypto();                           // start krypto
                        string decString = krypto.Decrypt(responseString, AESKey); // Decrept the string
                        Console.WriteLine("Success! Received Error " + decString); // write the message we received

                        // split the individual errors by the ; delimiter
                        string[] errors = decString.Split(';', StringSplitOptions.RemoveEmptyEntries);

                        // for every error in errors
                        foreach (var error in errors)
                        {
                            // establish placeholders
                            var time = "";
                            var uid  = "";
                            var code = "";
                            var msg  = "";

                            // split into columns by the , delimiter
                            string[] columns = error.Split(",");

                            // for every column in columns
                            foreach (var column in columns)
                            {
                                if (!String.IsNullOrEmpty(column))
                                {
                                    if (column.Contains("TIME:"))
                                    {
                                        time = column.Substring(column.LastIndexOf(':') + 1);
                                    }

                                    if (column.Contains("UID:"))
                                    {
                                        uid = column.Substring(column.LastIndexOf(':') + 1);
                                    }

                                    if (column.Contains("CODE:"))
                                    {
                                        code = column.Substring(column.LastIndexOf(':') + 1);
                                    }

                                    if (column.Contains("MSG:"))
                                    {
                                        msg = column.Substring(column.LastIndexOf(':') + 1);
                                    }
                                }
                            }

                            // Insert the error into the database
                            SQLHelper insertcmd = new SQLHelper("INSERT INTO errors (timestamp, unit_id, code, message) VALUES(" + time + ",'" + uid + "'," + "'" + code + "'," + "'" + msg + "')");
                            insertcmd.Run_ErrCmd();

                            // Receipt
                            Console.WriteLine(time + ": ERROR " + code + " | Unit " + uid + " | " + msg);
                        }

                        // Close the network stream and client
                        ns.Close();
                        client.Close();
                    }

                    else
                    {
                        Console.WriteLine("Unable to receive data...");
                    }
                }

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