Example #1
0
        public void TestTokenMessage()
        {
            TokenMessage msg = new TokenMessage();
            msg.EncodeHiddenComm("A", "B");
            TokenMessage msgTwo = new TokenMessage();

            msgTwo.Decode(msg.ToSend);
            CompareTokenMessages(msg, msgTwo);

            msg.EncodeMessage("C", "D", "hello!! :D |");
            msgTwo.Decode(msg.ToSend);
            CompareTokenMessages(msg, msgTwo);
        }
Example #2
0
        private void CompareTokenMessages(TokenMessage msg, TokenMessage msgTwo)
        {
            Assert.AreEqual(msg.Destination, msgTwo.Destination);
            Assert.AreEqual(msg.Sender, msgTwo.Sender);
            Assert.AreEqual(msg.Token, msgTwo.Token);
            Assert.AreEqual(msg.Info, msgTwo.Info);

            int len = 0;
            if (msg.ToSend.Length >= msgTwo.ToSend.Length)
            {
                len = msg.ToSend.Length;
            }
            else
            {
                len = msgTwo.ToSend.Length;
            }

            for (int i = 0; i < len; i++)
            {
                Assert.AreEqual(msg.ToSend[i], msgTwo.ToSend[i]);
            }
        }
Example #3
0
        /// <summary>
        /// The instance so that we can send a message or token.
        /// </summary>
        private void sender()
        {
            int numConnected = 0;
            while (numConnected != this.nodeCount)
            {
                lock (this.nodes)
                {
                    numConnected = this.nodes.Count;
                }
                Thread.Sleep(100);
            }

            TokenMessage msg = new TokenMessage();
            msg.EncodeHiddenComm(GlobalConst.serverName, GlobalConst.serverName);

            lock (this.nodes)
            {
                NetworkStream stream = this.nodes.First().GetStream();
            }
            //Need to check the index
            curIndex = 0;
            this.tokenCount++;
            lock (stopwatch)
            {
                stopwatch.Start();
            }

            //Send first token
            //Set a stopwatch and then use that stopwatch here.
            //Every time ANY tcpclient receives something, reset stopwatch.
            //If the stopwatch runs out, resend token ad infinitum until SOMETHING tells it to stop
            //That something is up to you.
            while (!interrupted)
            {
                TcpClient client = null;
                lock(this.nodes)
                {
                    client = this.nodes[curIndex];
                }
                actuallySend(client, msg);
                while (stopwatch.Elapsed.TotalSeconds < 5)
                {
                    Thread.Sleep(2500);
                    if(!stopwatch.IsRunning)
                    {
                        stopwatch.Start();
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Handles the client comm. STarts a new thread for each node.
        /// </summary>
        /// <param name="clientObject">The client object.</param>
        private void HandleClientComm(object clientObject)
        {
            try
            {
                TcpClient client = (TcpClient)clientObject;
                NetworkStream clientStream = client.GetStream();
                TokenMessage msg = new TokenMessage();
                byte[] message;
                int bytesRead;

                while (true)
                {
                    bytesRead = 0;
                    message = GlobalConst.getSpaceByteArray(GlobalConst.BUFSIZE);

                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, GlobalConst.BUFSIZE);
                        lock (stopwatch)
                        {
                            stopwatch.Reset();
                        }
                        //Console.WriteLine("Got a message on tokenHub");
                        //RESET STOPWATCH

                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        break;
                    }

                    bool good = msg.Decode(message);
                    if (msg.Token)
                    {
                        //SEND TOKEN TO NEXT PERSON
                        this.tokenCount++;
                        lock(this.nodes)
                        {
                            if (this.tokenCount >= this.nodes.Count)
                            {
                                break;
                            }
                        }
                    }
                    else if (!good)
                    {
                        continue;
                    }
                    else
                    {
                        this.tokenCount = 0;
                    }

                    int index = nodes.IndexOf(client) + 1;
                    int max = nodes.Count;
                    if (index >= max)
                    {
                        index = 0;
                    }

                    curIndex = index;

                    TcpClient clientNew = null;

                    lock(this.nodes)
                    {
                        clientNew = this.nodes[curIndex];
                    }

                    actuallySend(clientNew, msg);

                    //message has successfully been received
                    //Send it to the next node. get the index of the current tcpclient then call send.
                }
            }
            catch (ThreadInterruptedException)
            {
                TcpClient c = (TcpClient)clientObject;
                c.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #5
0
 /// <summary>
 /// Actuallies the SwitchSend.
 /// </summary>
 /// <param name="client">The client.</param>
 /// <param name="msg">The MSG.</param>
 private void actuallySend(TcpClient client, TokenMessage msg)
 {
     stopwatch.Reset();
     client.GetStream().Write(msg.ToSend, 0, msg.ToSend.Length);
     client.GetStream().Flush();
 }
        /// <summary>
        /// Tokens the sending.
        /// </summary>
        private void TokenSending()
        {
            int toSendCount = 0;
            char[] sep = { ',' };

            NetworkStream stream = this.client.GetStream();

            lock (ToSend)
            {
                toSendCount = ToSend.Count;
            }

            try
            {
                while (toSendCount > 0)
                {
                    while (!this.hasToken)
                    {
                        try
                        {
                            Thread.Sleep(Timeout.Infinite);
                        }
                        catch(ThreadAbortException)
                        {
                            throw;
                        }
                        catch
                        {
                            ;
                        }

                        if (!this.hasToken)
                        {
                            Console.WriteLine("Sending for node {0} received an interrupt when it wasn't supposed to.", parentNode.Name);
                        }
                    }

                    String msgToSend = toSend.Peek();
                    String[] info = msgToSend.Split(sep, 2);
                    TokenMessage msg = new TokenMessage();
                    msg.EncodeMessage(this.parentNode.Name, info[0], info[1]);

                    if (this.parentNode.GetRandomNum() < GlobalConst.errorPerc)
                    {
                        msg.ToSend[4] = 0;
                    }

                    UseDelegate(stream, msg);

                    //ASSUMING NO ERRORS. Need to update once errors have been put in.
                    try
                    {
                        Thread.Sleep(GlobalConst.TokenTimer * 1000);
                        continue;
                    }
                    catch(ThreadAbortException)
                    {
                        throw;
                    }
                    catch
                    {
                        ;
                    }

                    lock (ToSend)
                    {
                        toSendCount = this.toSend.Count;
                    }
                }

                this.doneSending = true;
            }
            catch (ThreadAbortException)
            {

            }
        }
        /// <summary>
        /// Tokens the listening.
        /// </summary>
        private void TokenListening()
        {
            int bytesRead = 0;
            int countTimesSent = 0;
            byte[] message;
            NetworkStream stream = this.client.GetStream();
            TokenMessage token = new TokenMessage(); //Whenever we receive something, we ALWAYS send something back.

            while (true)
            {
                bytesRead = 0;
                message = GlobalConst.getSpaceByteArray(GlobalConst.BUFSIZE);

                try
                {
                    bytesRead = stream.Read(message, 0, GlobalConst.BUFSIZE);
                }
                catch
                {
                    //socket error. WTF.
                    break;
                }

                if(bytesRead == 0)
                {
                    break;
                }

                TokenMessage msg = new TokenMessage();
                bool msgOK = msg.Decode(message);

                if(msg.Token)
                {

                    //Console.WriteLine("Got token on Node " + this.parentNode.Name);
                    this.hasToken = true;

                    if (this.senderThread.ThreadState.Equals(ThreadState.Stopped))
                    {
                        UseDelegate(stream, msg);
                    }
                    else
                    {
                        while (this.SenderThread.ThreadState.Equals(ThreadState.WaitSleepJoin) == false)
                        {
                            Thread.Sleep(50);
                        }

                        this.SenderThread.Interrupt();
                    }

                }
                else if (!msgOK)
                {

                    Console.WriteLine("Got a bad message on Node " + this.parentNode.Name);
                }
                else
                {

                    //Console.WriteLine("Got message on Node {0} ", this.parentNode.Name);
                    if (msg.Sender.Equals(this.parentNode.Name))
                    {
                        if (msg.FS == '1' || countTimesSent > 3)
                        {
                            this.toSend.Dequeue();
                            token.EncodeHiddenComm(GlobalConst.serverName, GlobalConst.serverName);
                            this.hasToken = false;
                            while (!this.senderThread.ThreadState.Equals(ThreadState.WaitSleepJoin))
                            {
                                Thread.Sleep(50);
                            }
                            this.SenderThread.Interrupt();
                        }
                        else
                        {
                            countTimesSent++;
                            token = msg;
                        }
                    }
                    else if (msg.Destination.Equals(this.parentNode.Name))
                    {
                        this.parentNode.writeOutMessage(msg);
                        token = msg;
                        token.FS = '1';
                    }
                    else
                    {
                        token = msg;
                    }

                    UseDelegate(stream, token);
                }
            }

            this.doneReceiving = true;
            if(!this.doneSending)
            {
                SenderThread.Abort();
            }

            while (!this.parentThread.ThreadState.Equals(ThreadState.WaitSleepJoin))
            {
                Thread.Sleep(100);
            }

            this.parentThread.Interrupt();
        }