Beispiel #1
0
        /**
         * Methode networkWrite (ecrire_vers_reseau) qui lit le fichier s_lec.txt
         * jusqu'a une ligne qui contiens la String N_DISCONNECT ou jusqu'a ce que
         * la fin du fichier soit atteint.
         *
         * Si la ligne lu contiens N_CONNECT, N_DATA ou N_DISCONNECT un pacquet (Npdu)
         * est créer et ajouté a la file transport2Network
         */
        public void networkWrite()
        {
            string lineRead;
            Npdu   networkNNpdu;

            string[] settings;
            bool     valid;

            end = false;

            while (!end && !disconnect)
            {
                if ((lineRead = reader.ReadLine()) != null)
                {
                    networkNNpdu = new Npdu();
                    try
                    {
                        valid    = false;
                        settings = lineRead.Split(' ');
                        Form1._UI.write2S_lec(lineRead);

                        if (settings[0] == "N_CONNECT")
                        {
                            networkNNpdu.type       = "N_CONNECT.req";
                            networkNNpdu.destAddr   = settings[1];
                            networkNNpdu.sourceAddr = settings[2];
                            networkNNpdu.routeAddr  = "";
                            valid = true;
                        }
                        else if (settings[0] == "N_DATA")
                        {
                            networkNNpdu.type = "N_DATA.req";
                            for (int i = 1; i < settings.Length; i++)
                            {
                                networkNNpdu.data += settings[i] + " ";
                            }
                            valid = true;
                        }
                        else if (settings[0] == "N_DISCONNECT")
                        {
                            networkNNpdu.type      = "N_DISCONNECT.req";
                            networkNNpdu.routeAddr = settings[1];
                            valid      = true;
                            disconnect = true;
                        }
                        if (valid)
                        {
                            transport2Network.Enqueue(networkNNpdu);
                        }
                    }
                    catch (ThreadAbortException)
                    {
                    }
                }
                else
                {
                    end = true;
                }
            }
        }
Beispiel #2
0
        private static Npdu decapRelease(PACKET currentPacket)
        {
            Npdu currentNpdu = new Npdu();

            currentNpdu.type       = "N_DISCONNECT.ind";
            currentNpdu.sourceAddr = currentPacket.sourceAddr.ToString();
            currentNpdu.destAddr   = currentPacket.destAddr.ToString();
            currentNpdu.connection = currentPacket.connectionNumber.ToString();

            BitArray target = new BitArray(new byte[] { currentPacket.target });

            if (target[0])
            {
                currentNpdu.target = "Closed by Client";
            }
            else if (target[1])
            {
                currentNpdu.target = "Closed by Provider";
            }
            else
            {
                currentNpdu.target = "Unknown";
            }

            return(currentNpdu);
        }
Beispiel #3
0
        private static Npdu decapData(PACKET currentPacket)
        {
            Npdu currentNpdu = new Npdu();

            currentNpdu.type       = "N_DATA.ind";
            currentNpdu.connection = currentPacket.connectionNumber.ToString();

            BitArray type = new BitArray(new byte[] { currentPacket.packetType });
            BitArray prps = new BitArray(3);

            prps.Set(0, type.Get(5));
            prps.Set(1, type.Get(6));
            prps.Set(2, type.Get(7));
            int[] num = new int[1];
            prps.CopyTo(num, 0);
            currentNpdu.pr = num[0];
            prps.Set(0, type.Get(1));
            prps.Set(1, type.Get(2));
            prps.Set(2, type.Get(3));
            prps.CopyTo(num, 0);
            currentNpdu.ps = num[0];

            if (currentPacket.dataArray != null)
            {
                currentNpdu.data = GetString(currentPacket.dataArray);
            }

            currentNpdu.flag = type[4];

            return(currentNpdu);
        }
Beispiel #4
0
        private static Npdu decapAcknowledge(PACKET currentPacket)
        {
            Npdu     currentNpdu = new Npdu();
            BitArray type        = new BitArray(new byte[] { currentPacket.target });

            if (type[0])
            {
                if (type[3])
                {
                    currentNpdu.type = "NACK";
                }
                else
                {
                    currentNpdu.type = "ACK";
                }
                BitArray pr = new BitArray(3);
                pr.Set(0, type.Get(5));
                pr.Set(1, type.Get(6));
                pr.Set(2, type.Get(7));
                int[] next = new int[1];
                pr.CopyTo(next, 0);
                currentNpdu.pr         = next[0];
                currentNpdu.connection = currentPacket.connectionNumber.ToString();
            }
            else
            {
                currentNpdu.type = "WrongPacketFormat";
            }


            return(currentNpdu);
        }
Beispiel #5
0
        private static Npdu decapRequest(PACKET currentPacket)
        {
            Npdu currentNpdu = new Npdu();

            currentNpdu.type       = "N_CONNECT.ind";
            currentNpdu.sourceAddr = currentPacket.sourceAddr.ToString();
            currentNpdu.destAddr   = currentPacket.destAddr.ToString();
            currentNpdu.connection = currentPacket.connectionNumber.ToString();

            return(currentNpdu);
        }
Beispiel #6
0
        public static Npdu decapPacket(PACKET currentPacket)
        {
            Npdu _4Transport = new Npdu();

            BitArray type = new BitArray(new byte[] { currentPacket.packetType });
            BitArray connectionInit, connectionEstablished, releasing;

            bool[] boolArray = new bool[8] {
                true, true, false, true, false, false, false, false
            };
            connectionInit = new BitArray(boolArray);
            boolArray      = new bool[8] {
                true, true, true, true, false, false, false, false
            };
            connectionEstablished = new BitArray(boolArray);
            boolArray             = new bool[8] {
                true, true, false, false, true, false, false, false
            };
            releasing = new BitArray(boolArray);


            if (currentPacket.packetType == 255)
            {
                return(decapAcknowledge(currentPacket));
            }
            else if (isEqualBitArrays(type, releasing))
            {
                return(decapRelease(currentPacket));
            }
            else if (isEqualBitArrays(type, connectionEstablished))
            {
                return(decapConnectionEstablished(currentPacket));
            }
            else if (isEqualBitArrays(type, connectionInit))
            {
                return(decapRequest(currentPacket));
            }
            else if (!type[0])
            {
                return(decapData(currentPacket));
            }


            _4Transport.type = "WrongPacketFormat";
            return(_4Transport);
        }
Beispiel #7
0
        public void transportWrite()
        {
            while (true)
            {
                try
                {
                    if (!disconnected)
                    {
                        if (packetProcessing2Network.Count > 0)
                        {
                            if (packetProcessing2Network.Peek().GetType() == typeof(byte[]))
                            {
                                string msg            = "";
                                byte[] received       = (byte[])packetProcessing2Network.Dequeue();
                                PACKET receivedPacket = Packet.decapBytes(received);
                                Npdu   _4Transport    = Packet.decapPacket(receivedPacket);

                                switch (_4Transport.type)
                                {
                                case "WrongPacketFormat":
                                    msg = "Wrong Packet Format";
                                    break;

                                case "release":
                                    msg      = "released packet: " + receivedPacket.packetType.ToString();
                                    rejected = true;
                                    break;

                                case "N_DISCONNECT.ind":
                                    msg = "N_DISCONNECT " + _4Transport.target;
                                    network2Transport.Enqueue(_4Transport);
                                    accepted     = true;
                                    disconnected = true;
                                    break;

                                case "N_CONNECT.ind":
                                    msg = "N_CONNECT  dest Address :" + _4Transport.destAddr + " source Address: " + _4Transport.sourceAddr;
                                    network2Transport.Enqueue(_4Transport);
                                    accepted = true;
                                    break;

                                case "N_DATA.ind":
                                    msg           = "N_DATA  transferring network data";
                                    receivedData += _4Transport.data;
                                    if (!_4Transport.flag)
                                    {
                                        _4Transport.data = receivedData;
                                        network2Transport.Enqueue(_4Transport);
                                    }
                                    break;

                                default:
                                    break;
                                }


                                write2Transport.WriteLine(msg);
                                Form1._UI.write2L_ecr(msg);
                            }
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                }
            }
        }
Beispiel #8
0
        public void transportRead()
        {
            while (true)
            {
                try
                {
                    if (!disconnected)
                    {
                        if (transport2Network.Count > 0)
                        {
                            if (transport2Network.Peek().GetType() == typeof(Npdu))
                            {
                                Npdu   transportNpdu = (Npdu)transport2Network.Dequeue();
                                Npdu   npdu2Transport;
                                PACKET packet4Processing;
                                string msg;

                                if (transportNpdu.type != "N_CONNECT.req" && !connected)
                                {
                                }
                                else
                                {
                                    if (transportNpdu.type == "N_CONNECT.req")

                                    {
                                        int intSource = int.Parse(transportNpdu.sourceAddr);
                                        int intDest   = int.Parse(transportNpdu.destAddr);
                                        sourceAddr[0] = (byte)intSource;
                                        destAddr[0]   = (byte)intDest;

                                        if (intSource > 249 || intDest > 249)
                                        {
                                            transportNpdu.routeAddr = " Error, not found !";
                                        }
                                        else
                                        {
                                            if (intSource >= 0 && intSource <= 99)
                                            {
                                                if (intDest >= 0 && intDest <= 99)
                                                {
                                                    transportNpdu.routeAddr = "" + intDest;
                                                }
                                                else if (intDest >= 100 && intDest <= 199)
                                                {
                                                    transportNpdu.routeAddr = "" + 255;
                                                }
                                                else if (intDest >= 200 && intDest <= 249)
                                                {
                                                    transportNpdu.routeAddr = "" + 254;
                                                }
                                            }
                                            else if (intSource >= 100 && intSource <= 199)
                                            {
                                                if (intDest >= 0 && intDest <= 99)
                                                {
                                                    transportNpdu.routeAddr = "" + 250;
                                                }
                                                else if (intDest >= 100 && intDest <= 199)
                                                {
                                                    transportNpdu.routeAddr = "" + intDest;
                                                }
                                                else if (intDest >= 200 && intDest <= 249)
                                                {
                                                    transportNpdu.routeAddr = "" + 253;
                                                }
                                            }
                                            else if (intSource >= 200 && intSource <= 249)
                                            {
                                                if (intDest >= 0 && intDest <= 99)
                                                {
                                                    transportNpdu.routeAddr = "" + 251;
                                                }
                                                else if (intDest >= 100 && intDest <= 199)
                                                {
                                                    transportNpdu.routeAddr = "" + 252;
                                                }
                                                else if (intDest >= 200 && intDest <= 249)
                                                {
                                                    transportNpdu.routeAddr = "" + intDest;
                                                }
                                            }
                                        }

                                        msg = "N_CONNECT " + transportNpdu.destAddr + " " + transportNpdu.sourceAddr + "  route:" + transportNpdu.routeAddr;
                                        writeFromTransport.WriteLine(msg);
                                        Form1._UI.write2L_lec(msg);

                                        if (sourceAddr[0] % 27 == 0 || int.Parse(transportNpdu.sourceAddr) > 249 || int.Parse(transportNpdu.destAddr) > 249)
                                        {
                                            disconnected = true;
                                            connected    = false;

                                            npdu2Transport            = new Npdu();
                                            npdu2Transport.type       = "N_DISCONNECT.ind";
                                            npdu2Transport.target     = "00000010";
                                            npdu2Transport.connection = "255";
                                            network2Transport.Enqueue(npdu2Transport);
                                        }
                                        else
                                        {
                                            connected = true;

                                            npdu2Transport            = new Npdu();
                                            npdu2Transport.type       = "N_CONNECT.conf";
                                            npdu2Transport.sourceAddr = sourceAddr[0].ToString();
                                            npdu2Transport.destAddr   = destAddr[0].ToString();
                                            npdu2Transport.connection = outputNo[0].ToString();
                                            network2Transport.Enqueue(npdu2Transport);

                                            packet4Processing = Packet.encapsulateRequest(outputNo[0], sourceAddr[0], destAddr[0]);
                                            byte[] sending = Packet.encapsulateBytes(packet4Processing, "request");

                                            sentCount = 0;
                                            rejected  = false;
                                            expired   = true;
                                            accepted  = false;
                                            timer.Start();
                                            while (!accepted && sentCount < 2)
                                            {
                                                if (expired || rejected)
                                                {
                                                    network2PacketProcessing.Enqueue(sending);
                                                    expired  = false;
                                                    rejected = false;
                                                    sentCount++;
                                                }
                                            }
                                            timer.Stop();
                                        }
                                    }
                                    else if (transportNpdu.type == "N_DATA.req")
                                    {
                                        msg = "N_DATA " + transportNpdu.data;
                                        writeFromTransport.WriteLine(msg);
                                        Form1._UI.write2L_lec(msg);

                                        PACKET[] packets4Processing = Packet.encapsulateFullData(transportNpdu.data, outputNo[0], pr);
                                        foreach (PACKET packet in packets4Processing)
                                        {
                                            byte[] sending = Packet.encapsulateDataBytes(packet);

                                            sentCount = 0;
                                            rejected  = false;
                                            expired   = true;
                                            accepted  = false;
                                            timer.Start();
                                            while (!accepted && sentCount < 2)
                                            {
                                                if (expired || rejected)
                                                {
                                                    network2PacketProcessing.Enqueue(sending);
                                                    expired  = false;
                                                    rejected = false;
                                                    sentCount++;
                                                }
                                            }
                                            timer.Stop();
                                        }
                                    }
                                    else if (transportNpdu.type == "N_DISCONNECT.req")
                                    {
                                        msg = "N_DISCONNECT " + transportNpdu.routeAddr;
                                        writeFromTransport.WriteLine(msg);
                                        Form1._UI.write2L_lec(msg);

                                        npdu2Transport      = new Npdu();
                                        npdu2Transport.type = "N_DISCONNECT.ind";

                                        packet4Processing = Packet.encapsulateRelease(outputNo[0], sourceAddr[0], destAddr[0], true);
                                        string packetType = "release";
                                        byte[] sending    = Packet.encapsulateBytes(packet4Processing, packetType);

                                        sentCount = 0;
                                        rejected  = false;
                                        expired   = true;
                                        accepted  = false;
                                        timer.Start();
                                        while (!accepted && sentCount < 2)
                                        {
                                            if (expired || rejected)
                                            {
                                                network2PacketProcessing.Enqueue(sending);
                                                expired  = false;
                                                rejected = false;
                                                sentCount++;
                                            }
                                        }
                                        timer.Stop();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (ThreadAbortException) { }
            }
        }
Beispiel #9
0
        /**
         * Methode networkRead (lire_de_reseau) qui verifie si la file network2Transport
         * contiens un Npdu a lire, si tel est le cas le type du Npdu est verifié et un
         * traitement est effectué selon le type trouvé.
         */
        public void networkRead()
        {
            while (!end)
            {
                try
                {
                    if (network2Transport.Count > 0)
                    {
                        if (network2Transport.Peek().GetType() == typeof(Npdu))
                        {
                            Npdu Npdu4Network = (Npdu)network2Transport.Dequeue();

                            if (Npdu4Network.type == "N_CONNECT.ind")
                            {
                                if (connected.Contains(Npdu4Network.connection))
                                {
                                    msg = "connection: " + Npdu4Network.connection + " dest Address: " + Npdu4Network.destAddr + "  source Address: " + Npdu4Network.sourceAddr;
                                    writer.WriteLine(msg);
                                    Form1._UI.write2S_ecr(msg);
                                }
                            }
                            else if (Npdu4Network.type == "N_CONNECT.conf")
                            {
                                msg = "connection: " + Npdu4Network.connection + " Connection established ";
                                writer.WriteLine(msg);
                                Form1._UI.write2S_ecr(msg);
                                connected.Add(Npdu4Network.connection);
                            }
                            else if (Npdu4Network.type == "N_DATA.ind")
                            {
                                if (connected.Contains(Npdu4Network.connection))
                                {
                                    msg = Npdu4Network.data;
                                    writer.WriteLine(msg);
                                    Form1._UI.write2S_ecr(msg);
                                }
                            }
                            else if (Npdu4Network.type == "N_DISCONNECT.ind")
                            {
                                if (connected.Contains(Npdu4Network.connection))
                                {
                                    connected.Remove(Npdu4Network.connection);
                                    msg = "connection: " + Npdu4Network.connection + " " + Npdu4Network.routeAddr + " disconnected " + Npdu4Network.target;
                                    writer.WriteLine(msg);
                                    Form1._UI.write2S_ecr(msg);
                                    Form1._UI.closeThreads();
                                }
                                else if (Npdu4Network.connection.Equals("255"))
                                {
                                    Form1._UI.write2S_ecr("connection: declined by Network! ");
                                    Form1._UI.closeThreads();
                                }
                            }
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                }
            }
        }