Example #1
0
        public void SendingNodeIsUpMessage(Socket socketout, string addressIP, short WhichRouter)
        {
            IPEndPoint ippoint = socketout.RemoteEndPoint as IPEndPoint;

            Console.WriteLine(ippoint.Address.ToString());

            try
            {
                lock (_syncRoot)
                {
                    // wiadomośc przesyłana do komunikacji z NMSem
                    string     message    = "Network node is up";
                    short      length     = (Int16)message.Length;
                    NMSPackage nmspackage = new NMSPackage(addressIP, WhichRouter, message, length);



                    byte[] bytemessage = nmspackage.toBytes();

                    // Send the data through the socket.
                    socketout.Send(bytemessage);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Lack connection with NMS");
            }
        }
Example #2
0
        public void sendingMessageCommunication(string address, string communication, Socket socket)
        {
            try
            {
                short      length     = (Int16)communication.Length;
                NMSPackage nmspackage = new NMSPackage(address, communication, length);

                byte[] bytemessage = nmspackage.toBytes();

                // Send the data through the socket.
                socket.Send(bytemessage);
            }
            catch (Exception)
            {
                Console.WriteLine("Lack of connection with NMS");
            }
        }
Example #3
0
        public void SendingKeepAliveMessage(string address, Socket socketout)
        {
            Task.Run(async() =>
            {
                try
                {
                    while (true)
                    {
                        if (!socketout.Connected)
                        {
                            Console.WriteLine("Lack connection NMS");
                            break;
                        }
                        string keep_alive_message = "Keep Alive";
                        short length          = (Int16)keep_alive_message.Length;
                        NMSPackage nmspackage = new NMSPackage(address, keep_alive_message, length);

                        byte[] bytemessage = nmspackage.toBytes();

                        // Send the data through the socket.
                        socketout.Send(bytemessage);

                        var delay = await Task.Run(async() =>
                        {
                            Stopwatch sw = Stopwatch.StartNew();
                            await Task.Delay(2000);
                            sw.Stop();
                            return(sw.ElapsedMilliseconds);
                        });
                    }
                }
                catch (Exception err)
                {
                    Console.WriteLine("Lack of connection with NMS");
                }
            });
        }
Example #4
0
        public void CreateConnect(string addressConnectIP, string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            Socket socket        = null;
            Socket listenerAgent = null;
            Socket agentSending  = null;


            try
            {
                byte[] bytes = new Byte[128];



                string numberOfRouter = key.Substring(0, 1);

                //Sklejenie czesci wspolnej klucza dla socketu OUT oraz indeksu
                string settingsString = numberOfRouter + "Agent";

                IPAddress ipAddress =
                    ipAddress = IPAddress.Parse(OperationConfiguration.getSetting(settingsString, mySettings));
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

                // Create a TCP/IP socket.
                listenerAgent = new Socket(ipAddress.AddressFamily,
                                           SocketType.Stream, ProtocolType.Tcp);

                if (!listenerAgent.IsBound)
                {
                    //zabindowanie na sokecie punktu koncowego
                    listenerAgent.Bind(localEndPoint);
                    listenerAgent.Listen(100);
                }
                int milliseconds = 100;
                //Nasluchujemy bez przerwy
                while (Last)
                {
                    if (Listening)
                    {
                        //Dodanie socketu do listy socketow OUT
                        agentSending = sS.ConnectToEndPoint(addressConnectIP);


                        Thread.Sleep(milliseconds);

                        SendingNodeIsUpMessage(agentSending, OperationConfiguration.getSetting(settingsString, mySettings), Int16.Parse(numberOfRouter));



                        //oczekiwanie na polaczenie
                        socket = listenerAgent.Accept();

                        SendingKeepAliveMessage(OperationConfiguration.getSetting(settingsString, mySettings), agentSending);

                        Listening = false;

                        /* LingerOption myOpts = new LingerOption(true, 1);
                         * socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, myOpts);
                         * socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, false);
                         * socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);*/

                        Console.WriteLine("Connect on port  " + NetworkNode.takingAddresListenerSocket(socket));
                        byte[] msg;


                        if (socket.Connected)
                        {
                            //Oczekiwanie w petli na przyjscie danych
                            while (true)
                            {
                                string from = string.Empty;
                                //Odebranie tablicy bajtow na obslugiwanym w watku sluchaczu

                                msg = sl.ProcessRecivedBytes3(socket);
                                NMSPackage package       = new NMSPackage();
                                string     usableMessage = NMSPackage.extractUsableMessage(msg, NMSPackage.extractUsableInfoLength(msg));
                                Console.WriteLine(usableMessage);
                                fillingTable(usableMessage, agentSending, settingsString);
                                // Package.extractHowManyPackages(msg);
                                // listByte.Add(msg);

                                //Wykonuje jezeli nadal zestawione jest polaczenie

                                if (msg == null)
                                {
                                    //Rozpoczynamy proces nowego polaczenia

                                    break;
                                }
                            }
                        }
                    }
                    ConsoleKeyInfo cki;
                    while (true)
                    {
                        cki = Console.ReadKey();
                        if (cki.Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                    }
                }
            }
            catch (SocketException se)
            {
                Console.WriteLine($"Socket Exception: {se}");
            }
            finally
            {
                // StopListening();
            }
            if (socket == null)
            {
            }
        }
Example #5
0
        /// <summary>
        /// funkcja obsługująca przychodzącą wiadomość
        /// </summary>
        /// <param name="listener"></param>
        private void ReceivedMessage(Socket listener)
        {
            string usableMessage = "";

            while (true)
            {
                // wyczyszczenie paczki przed jej wysłaniem oraz przed ponownym wysyłaniem
                usableMessage = string.Empty;

                //przypisywanie do paczki wiadomości przesłanej przez klienta, w tym przypadku przez agenta
                byte[] nmspackage = new byte[64];

                // tylko jeden wątek moze wykonywac ten kod w danym czasie
                //  lock (_syncRoot)
                //   {
                nmspackage = ls.ProcessRecivedByteMessage(listener);
                //   }
                // wykonuje się tylko wtedy jeśli agent nie  jest podłączony
                if (!listener.Connected)
                {
                    // pobranie indeksu socketu z listy soecketów
                    int index = listening_socket.IndexOf(listener);
                    UpdateListBoxReceived("Network Node" + getIPAddressRemote(sends_socket.ElementAt(index)) + " is disconnected");
                    // rozłączanie obu socketów
                    listener.Disconnect(true);
                    //send.Disconnect(true);

                    //usuwanie socketów, adresów z list by przy ponownym połączeniu dodawać je na ponownie
                    listening_socket.RemoveAt(index);
                    sends_socket.RemoveAt(index);
                    routers_IP.RemoveAt(index);

                    // wyświetlanie pobranego adresu IP z list podłączonych agentów
                    _Application.comboBoxRouters.Invoke(new Action(delegate()
                    {
                        comboBoxRouters.Items.RemoveAt(index);
                    }));
                    break;
                }
                // wykonuje się tylko wtedy jeśli agent jest podłączony
                else
                {
                    string sourceip;
                    //tylko jesli paczka nie jest nullem
                    if (nmspackage != null)
                    {
                        usableMessage = NMSPackage.extractUsableMessage(nmspackage, NMSPackage.extractUsableInfoLength(nmspackage));
                        //tylko w przypadku pierwszej wiadomości od danego agenta
                        if (usableMessage == "Network node is up")
                        {
                            sourceip = NMSPackage.exctractSourceIP(nmspackage).ToString();
                            //jesli lista z adresami IP routerów nie zawiera danego IP to je dodaje a następnie wyśwuietlam komunikat
                            if (!routers_IP.Contains(sourceip))
                            {
                                routers_IP.Add(sourceip);
                                UpdateListBoxReceived("Network Node: " + sourceip + " is up");
                                UpdateListBoxReceived(listening_socket.Count.ToString());
                            }
                            //tworze połączenie z socketem routera, który wysłał do mnie wiadomość
                            send = sS.ConnectToEndPoint(NMSPackage.exctractSourceIP(nmspackage).ToString());
                            // a następnie dodaje ten socket do listy socketów, by potem móc z nich korzystać
                            sends_socket.Add(send);

                            short numberOfRouter = NMSPackage.extractNumberOfRouterNumber(nmspackage);

                            List <string> configurationRouter = ReadingFromFile(paths.ElementAt(numberOfRouter - 1));
                            foreach (var line in configurationRouter)
                            {
                                short      length              = (Int16)line.Length;
                                NMSPackage tablePackage        = new NMSPackage(interfaces.ElementAt(numberOfRouter - 1), line, length);
                                byte[]     tablePackageInBytes = tablePackage.toBytes();
                                send.Send(tablePackageInBytes);
                                Task.Delay(10);
                            }



                            /* z kazdą wiadomością "Network node is up" dodaje IP routera do checkboca w celu mozliwości wybrania
                             * docelwoego punktu komunikacji */
                            _Application.comboBoxRouters.Invoke(new Action(delegate()
                            {
                                _Application.comboBoxRouters.Items.Add(sourceip);
                            }));
                        }
                        //jesli wiadmośc keep alive
                        else if (usableMessage == "Keep Alive")
                        {
                            // UpdateListBoxReceived(usableMessage);
                        }
                        else if (usableMessage == "ERROR")
                        {
                            UpdateListBoxReceived("Cannot send EON Table to the node agent");
                        }
                        else if (usableMessage.StartsWith("TOPOLOGY"))
                        {
                            UpdateListBoxReceived(usableMessage);
                        }
                    }
                    //jesli paczka jest nullem
                    else
                    {
                        int index = listening_socket.IndexOf(listener);
                        // stwierdzam, że agent nie odpowiada, a potem go rozłączam
                        UpdateListBoxReceived("Network Node" + getIPAddressRemote(sends_socket.ElementAt(index)) + " is not responding");
                        UpdateListBoxReceived("Network Node" + getIPAddressRemote(sends_socket.ElementAt(index)) + " is disconnected");

                        listening_socket.RemoveAt(index);
                        sends_socket.RemoveAt(index);
                        routers_IP.RemoveAt(index);
                        _Application.comboBoxRouters.Invoke(new Action(delegate()
                        {
                            comboBoxRouters.Items.RemoveAt(index);
                        }));
                        //odłączanie
                        listener.Disconnect(true);
                        //send.Disconnect(true);
                        //usuwanie socketów, adresów z list by przy ponownym połączeniu dodawać je na ponownie

                        break;
                    }
                }
            }
        }
Example #6
0
        // tworzenie wiadomości w zalezności od zaznaczonego typu wiadomości w comboBocActions
        /// <summary>
        /// tworzenie wiadomości w zalezności od zaznaczonego typu wiadomości w comboBocActions
        /// </summary>
        /// <returns> byte[] </returns>
        private byte[] returnBytes()
        {
            byte[] table_in_bytes = new byte[128];
            try
            {
                if (comboBoxTables.GetItemText(comboBoxTables.SelectedItem) == "Commutation Table")
                {
                    if (comboBoxActions.GetItemText(comboBoxActions.SelectedItem) == "DELETE")
                    {
                        table_in_bytes = null;
                        string builder = string.Empty;;
                        string command = string.Empty;
                        string freq_in = string.Empty;
                        string port_in = string.Empty;
                        command = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        freq_in = (textBoxFrequencyIN.Text).ToString();
                        port_in = (textBox_Port_IN.Text).ToString();
                        builder = command + "#" + "3" + "#" + freq_in + "#" + port_in;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        if (freq_in == "" || port_in == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    else if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "ADD")
                    {
                        table_in_bytes = null;
                        string builder  = string.Empty;
                        string command  = string.Empty;
                        string freq_in  = string.Empty;
                        string port_in  = string.Empty;
                        string freq_out = string.Empty;
                        string port_out = string.Empty;
                        command  = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        freq_in  = (textBoxFrequencyIN.Text).ToString();
                        port_in  = (textBox_Port_IN.Text).ToString();
                        freq_out = (textBoxFrequencyOUT.Text).ToString();
                        port_out = (textBoxPort_OUT.Text).ToString();
                        builder  = command + "#" + "3" + "#" + freq_in + "#" + port_in + "#" + freq_out + "#" + port_out;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);

                        if (freq_in == "" || port_in == "" || freq_out == "" || port_out == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    else if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "TOPOLOGY")
                    {
                        string builder = string.Empty;
                        string command = string.Empty;
                        command = "TOPOLOGY";
                        builder = command + "#" + "3";
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        table_in_bytes = commutation_table.toBytes();
                    }
                }
                if (this.comboBoxTables.GetItemText(this.comboBoxTables.SelectedItem) == "Border Node Commutation Table")
                {
                    if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "ADD")
                    {
                        table_in_bytes = null;
                        string builder        = string.Empty;
                        string port_in        = string.Empty;
                        string port_out       = string.Empty;
                        string Hops           = string.Empty;
                        string command        = string.Empty;
                        string band_out       = string.Empty;
                        string destination_IP = string.Empty;
                        string Modulation     = string.Empty;
                        string BitRate        = string.Empty;
                        string IP_IN          = string.Empty;
                        string Frequency_out  = string.Empty;
                        command        = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        IP_IN          = (textBox_IP_IN.Text).ToString();
                        port_in        = (textBox_Port_IN.Text).ToString();
                        band_out       = (textBoxBand_OUT.Text).ToString();
                        Frequency_out  = (textBoxFrequencyOUT.Text).ToString();
                        Modulation     = (textBoxModulation.Text).ToString();
                        BitRate        = (textBoxBitrate.Text).ToString();
                        destination_IP = (textBoxDestination_IP.Text).ToString();
                        port_out       = (textBoxPort_OUT.Text).ToString();
                        Hops           = (textBoxHops.Text).ToString();

                        builder = command + "#" + "1" + "#" + IP_IN + "#" + port_in + "#" + band_out + "#" + Frequency_out + "#" +
                                  Modulation + "#" + BitRate + "#" + destination_IP + "#" + port_out + "#" + Hops;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        if (IP_IN == "" || port_in == "" || band_out == "" || port_out == "" || Frequency_out == "" || Modulation == "" || BitRate == "" || destination_IP == "" || Hops == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "DELETE")
                    {
                        table_in_bytes = null;
                        string builder        = string.Empty;
                        string command        = string.Empty;
                        string IP_IN          = string.Empty;
                        string port_in        = string.Empty;
                        string destination_IP = string.Empty;
                        command        = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        IP_IN          = (textBox_IP_IN.Text).ToString();
                        port_in        = (textBox_Port_IN.Text).ToString();
                        destination_IP = (textBoxDestination_IP.Text).ToString();

                        builder = command + "#" + "1" + "#" + IP_IN + "#" + port_in + "#" + destination_IP;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        if (IP_IN == "" || port_in == "" || destination_IP == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    else if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "TOPOLOGY")
                    {
                        table_in_bytes = null;
                        string builder = string.Empty;
                        string command = string.Empty;
                        command = "TOPOLOGY";
                        builder = command + "#" + "1";
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        table_in_bytes = commutation_table.toBytes();
                    }
                }
                if (this.comboBoxTables.GetItemText(this.comboBoxTables.SelectedItem) == "EON Table")
                {
                    if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "ADD")
                    {
                        table_in_bytes = null;
                        string builder       = string.Empty;
                        string command       = string.Empty;
                        string Band_out      = string.Empty;
                        string Band_in       = string.Empty;
                        string frequency_in  = string.Empty;
                        string Frequency_out = string.Empty;
                        command       = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        Band_out      = (textBoxBand_OUT.Text).ToString();
                        Frequency_out = (textBoxFrequencyOUT.Text).ToString();
                        Band_in       = (textBoxBand_IN.Text).ToString();
                        frequency_in  = (textBoxFrequencyIN.Text).ToString();

                        builder = command + "#" + "2" + "#" + frequency_in + "#" + Band_in + "#" + Frequency_out + "#" + Band_out;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        if (Band_out == "" || Frequency_out == "" || Band_in == "" || frequency_in == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    else if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "DELETE")
                    {
                        table_in_bytes = null;
                        string builder       = string.Empty;
                        string command       = string.Empty;
                        string Band_out      = string.Empty;
                        string Band_in       = string.Empty;
                        string frequency_in  = string.Empty;
                        string Frequency_out = string.Empty;
                        command       = this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem);
                        Band_out      = (textBoxBand_OUT.Text).ToString();
                        Frequency_out = (textBoxFrequencyOUT.Text).ToString();
                        Band_in       = (textBoxBand_IN.Text).ToString();
                        frequency_in  = (textBoxFrequencyIN.Text).ToString();

                        builder = command + "#" + "2" + "#" + frequency_in + "#" + Band_in + "#" + Frequency_out + "#" + Band_out;
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        if (Band_out == "" || Frequency_out == "" || Band_in == "" || frequency_in == "")
                        {
                            table_in_bytes = null;
                        }
                        else
                        {
                            table_in_bytes = commutation_table.toBytes();
                        }
                    }
                    else if (this.comboBoxActions.GetItemText(this.comboBoxActions.SelectedItem) == "TOPOLOGY")
                    {
                        table_in_bytes = null;
                        string builder = string.Empty;
                        string command = string.Empty;
                        command = "TOPOLOGY";
                        builder = command + "#" + "2";
                        short      length            = (Int16)builder.Length;
                        NMSPackage commutation_table = new NMSPackage(interfaces.ElementAt(1 - 1), builder, length);
                        table_in_bytes = commutation_table.toBytes();
                    }
                }
            }
            catch (Exception e)
            {
                listBoxReceived.Items.Add("Error occured with getting data for the agent");
                table_in_bytes = null;
            }
            return(table_in_bytes);
        }