Ejemplo n.º 1
0
    // monitors run, since it will only accept a token
    public override void Run()
    {
        // connect with neighboring nodes
        this.Connect();

        // Accept incoming connection and wait for
        // right neighbor to be ready.

        // create the first token
        Frame sendFrame = new Token(0, 0, 0);
        byte[] bFrame = sendFrame.ToBinary();
        Frame recieveFrame;
        Frame newToken;
        Frame frame;

        // send it!
        sendStream.Write(bFrame, 0, bFrame.Length);
        recieveFrame = Frame.MakeFrame(Receive());
        Debug.Assert(recieveFrame.DA == this.nodeNum);
        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

        try
        {
            while (true)
            {
                // listen state
                // recieve frame, check if token

                frame = Frame.MakeFrame(Receive());

                if (frame is Token)
                {
                    // if it was sent by the monitor, everyone is done sending
                    if (frame.SA == this.nodeNum)
                    {
                        // send a frame with the finished bit enabled
                        sendFrame = new Frame(2, 255, 0, null);
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }
                    // if the token wasn't sent by the monitor, must send ack
                    else
                    {
                        sendFrame = new Frame(0, frame.SA, this.nodeNum, null);
                        sendFrame.FS = 2;
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }

                    // otherwise send a new token if the SA is not the monitor
                    newToken = new Token(0, this.nodeNum, this.nodeNum);

                    bFrame = newToken.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                    frame = Frame.MakeFrame(Receive());

                    if (frame.SA == this.nodeNum)
                    {
                        // send a frame with the kill bit enabled
                        sendFrame = new Frame(2, lastNode, 0, null);
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }
                    else
                    {
                        Debug.Assert(frame.DA == this.nodeNum);
                        Debug.Assert(frame.FS == 2 || frame.FS == 3);
                    }
                }
                // otherwise, check if the bridge is telling it to shutdown
                else if (frame.AC == 1)
                {
                    Console.WriteLine("Exterminate list");
                    // send a frame with the kill bit enabled
                    sendFrame = new Frame(1, lastNode, 0, null);
                    bFrame = sendFrame.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                    break;
                }
                else
                {
                    bFrame = frame.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                }
            }
        }
        finally
        {
            // Close all open resources (i.e. sockets!)
            Console.WriteLine("Monitor node EX-TERM-INATED!");
            this.recieveStream.Close();
            this.recieveClient.Close();
        }
    }
Ejemplo n.º 2
0
    protected void Transmit(Frame token)
    {
        // Send until THT reached
        // first send the token ack
        Frame sendFrame;
        Frame recieveFrame;
        byte[] bFrame;
        String[] splitInput;
        int dataSize;
        this.THT = 1040;
        Frame newToken;

        // send ack for token
        sendFrame = new Frame(0, token.SA, this.nodeNum, null);
        sendFrame.FS = 2;
        bFrame = sendFrame.ToBinary();
        sendStream.Write(bFrame, 0, bFrame.Length);

        // read from file, and build frame from it
        while (true)
        {
            // tokenize the input line
            splitInput = this.inputFile[index].Split(new char[] { ',' });
            dataSize = Convert.ToByte(splitInput[1]); // the size of data is always in the second array element
            if (dataSize > 254)
            {
                Console.WriteLine("LOL BAD STUFF");
            }

            if (this.THT < dataSize)
            {
                // not enough THT to send the frame, send a token
                newToken = new Token(0, 0, this.nodeNum);
                bFrame = newToken.ToBinary();

                sendStream.Write(bFrame, 0, bFrame.Length);
                recieveFrame = Frame.MakeFrame(Receive());
                Debug.Assert(recieveFrame.DA == this.nodeNum);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                return;
            }

            // otherwise, build and send the frame
            sendFrame = new Frame(0, Convert.ToByte(splitInput[0]), this.nodeNum, new System.Text.UTF8Encoding().GetBytes(splitInput[2]));
            bFrame = sendFrame.ToBinary();
            sendStream.Write(bFrame, 0, bFrame.Length);
            THT -= sendFrame.size;

            // recieve ack
            while (true)
            {
                recieveFrame = Frame.MakeFrame(Receive());
                Debug.Assert(recieveFrame.SA == this.nodeNum);
                Debug.Assert(recieveFrame.DA == sendFrame.DA);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                // frame wasn't accepted, resend
                if (recieveFrame.FS == 3)
                {
                    // is there enough THT to resend?
                    if (this.THT > sendFrame.size)
                    {
                        // send it!
                        sendStream.Write(bFrame, 0, bFrame.Length);
                        THT -= sendFrame.size;
                        continue;
                    }
                    else
                    {
                        // send the token along
                        newToken = new Token(0, 0, this.nodeNum);
                        bFrame = newToken.ToBinary();

                        sendStream.Write(bFrame, 0, bFrame.Length);
                        recieveFrame = Frame.MakeFrame(Receive());
                        Debug.Assert(recieveFrame.DA == this.nodeNum);
                        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                        return;
                    }
                }
                else
                {
                    // frame was successfully recieved, move onto the next one
                    this.index++;
                    if (this.index >= this.inputFile.Length)
                    {
                        // nothing more to send
                        newToken = new Token(0, 0, this.nodeNum);
                        bFrame = newToken.ToBinary();

                        sendStream.Write(bFrame, 0, bFrame.Length);
                        recieveFrame = Frame.MakeFrame(Receive());
                        Debug.Assert(recieveFrame.DA == this.nodeNum);
                        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                        return;
                    }
                    break;
                }
            }
        }
    }
Ejemplo n.º 3
0
    protected void Transmit(Frame token)
    {
        // Send until THT reached
        // first send the token ack
        Frame sendFrame;
        Frame recieveFrame;
        byte[] bFrame;
        int dataSize;
        this.THT = 1040;
        Frame newToken;
        Queue<Frame> sendQueue;
        Object locker;

        // send ack for token
        sendFrame = new Frame(0, token.SA, this.nodeNum, null);
        sendFrame.FS = 2;
        bFrame = sendFrame.ToBinary();
        sendStream.Write(bFrame, 0, bFrame.Length);

        // setup which buffer to send from
        if (bridgeNumber == 1)
        {
            lock (buf1Locker)
            {
                sendQueue = buffer1;
                locker = buf1Locker;
            }
        }
        else
        {
            lock (buf2Locker)
            {
                sendQueue = buffer2;
                locker = buf1Locker;
            }
        }

        // read from buffer and send it
        while (true)
        {
            // get the next frame to send and its size
            lock (locker)
            {
                sendFrame = sendQueue.Peek();
            }
            dataSize = sendFrame.size;

            if (this.THT < dataSize)
            {
                // not enough THT to send the frame, send a token
                newToken = new Token(0, 0, this.nodeNum);
                bFrame = newToken.ToBinary();

                sendStream.Write(bFrame, 0, bFrame.Length);
                recieveFrame = Frame.MakeFrame(Receive());
                //Debug.Assert(recieveFrame.DA == this.nodeNum);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                return;
            }

            // otherwise, send the frame
            bFrame = sendFrame.ToBinary();
            sendStream.Write(bFrame, 0, bFrame.Length);
            THT -= sendFrame.size;

            // recieve ack
            while (true)
            {
                recieveFrame = Frame.MakeFrame(Receive());
                //Debug.Assert(recieveFrame.SA == this.nodeNum);
                //Debug.Assert(recieveFrame.DA == sendFrame.DA);
                //Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                // if it needs to be setup into the routing table
                if (recieveFrame.AC == 4)
                {
                    // if the frame recieved doesn't have the FS changed, nothing processed it
                    if (recieveFrame.FS == 0)
                    {
                        lock (locker)
                        {
                            sendQueue.Dequeue();
                            if (sendQueue.Count == 0)
                            {
                                // nothing more to send
                                newToken = new Token(0, 0, this.nodeNum);
                                bFrame = newToken.ToBinary();

                                sendStream.Write(bFrame, 0, bFrame.Length);
                                recieveFrame = Frame.MakeFrame(Receive());
                                //Debug.Assert(recieveFrame.DA == this.nodeNum);
                                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                                return;
                            }
                        }
                        break;
                    }
                    // otherwise, add it to the routing table
                    else
                    {
                        lock (lookupTableLocker)
                        {
                            if (!lookupTable.Contains(sendFrame.DA))
                            {
                                lookupTable.Add(sendFrame.DA, bridgeNumber);
                                lock (logLocker)
                                {
                                    logFile.WriteLine(DateTime.Now.ToString() + " " + sendFrame.DA + " added to the routting table");
                                }
                            }
                        }

                        // make sure that if it needs to be resent, it doesn't get added again
                        sendFrame.AC = 0;
                        break;
                    }
                }

                // frame wasn't accepted, resend
                else if (recieveFrame.FS == 3)
                {
                    // is there enough THT to resend?
                    if (this.THT > sendFrame.size)
                    {
                        lock (logLocker)
                        {
                            //logFile.WriteLine(DateTime.Now.ToString() + " resending frame to " + sendFrame.DA + " from " + sendFrame.SA);
                        }
                        // send it!
                        sendStream.Write(bFrame, 0, bFrame.Length);
                        THT -= sendFrame.size;
                        continue;
                    }
                    else
                    {
                        // send the token along
                        newToken = new Token(0, 0, this.nodeNum);
                        bFrame = newToken.ToBinary();

                        sendStream.Write(bFrame, 0, bFrame.Length);
                        recieveFrame = Frame.MakeFrame(Receive());
                        //Debug.Assert(recieveFrame.DA == this.nodeNum);
                        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                        return;
                    }
                }
                else
                {
                    // frame was successfully recieved, move onto the next one
                    lock (locker)
                    {
                        sendQueue.Dequeue();
                        if (sendQueue.Count == 0)
                        {
                            // nothing more to send
                            newToken = new Token(0, 0, this.nodeNum);
                            bFrame = newToken.ToBinary();

                            sendStream.Write(bFrame, 0, bFrame.Length);
                            recieveFrame = Frame.MakeFrame(Receive());
                            //Debug.Assert(recieveFrame.DA == this.nodeNum);
                            Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                            return;
                        }
                    }
                    break;
                }
            }
        }
    }