Ejemplo n.º 1
0
        /// <summary>
        /// Sendings this instance.
        /// </summary>
        private void SwitchSending()
        {
            //try
            //{
            while (!this.Client.Connected)
            {
                Thread.SpinWait(100);
            }

            NetworkStream stream = this.Client.GetStream();
            char[] splitOpt = { ',' };
            String[] dstMsg;
            WriteStream caller = new WriteStream(ParentNode.WriteToNetworkStream);
            bool resend = true;

            while (this.toSend.Count > 0)
            {

                lock (this.toSend)
                {
                    dstMsg = this.toSend.Dequeue().Split(splitOpt, 2);
                }

                SwitchMessage msg = new SwitchMessage();
                msg.EncodeMessage(dstMsg[0], this.ParentNode.Name, dstMsg[1]);
                //Console.WriteLine(" : " + msg.Info + " : Node " + this.name + " is sending to Node " + dstMsg[0] + " this message " );
                while (resend)
                {
                    lock (stream)
                    {
                        if (stream.CanWrite)
                        {
                            caller.BeginInvoke(stream, msg, null, null);
                        }
                        else
                        {
                            Console.WriteLine("Node " + this.ParentNode.Name + " cannot write via switch.");
                        }
                    }
                    try
                    {
                        //Sleep until an ack is recieved.
                        Thread.Sleep(10000);

                    }
                    catch (ThreadInterruptedException)
                    {
                        resend = false;
                    }

                    if(this.doneSending == true)
                    {
                        resend = false;
                    }
                }
                resend = true;

                if (this.doneSending == true)
                {
                    break;
                }
            }

            //Console.WriteLine("Sender thread for node " + this.name + " exiting.");

            SwitchMessage done = new SwitchMessage();
            done.EncodeHiddenComm(GlobalConst.serverName, GlobalConst.serverName);
            this.doneSending = true;

            while (stream.CanWrite)
            {
                try
                {
                    stream.Write(done.ToSend, 0, done.ToSend.Length);
                    Thread.Sleep(1000);
                }
                catch (System.IO.IOException)
                {
                    break;
                }
                catch (ThreadInterruptedException)
                {
                    ;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Listens for this instance.
        /// </summary>
        private void SwitchListening()
        {
            try
            {
                NetworkStream clientStream = this.Client.GetStream();

                byte[] message;
                int bytesRead;
                WriteStream caller = new WriteStream(this.parentNode.WriteToNetworkStream);

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

                    lock (this.toSend)
                    {
                        if (this.toSend.Count < 1)
                        {
                            clientStream.ReadTimeout = 30000;
                        }
                    }

                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, GlobalConst.BUFSIZE);
                    }
                    catch
                    {
                        //a socket error has occured
                        Console.WriteLine("Socket error on node " + this.parentNode.Name);
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        Console.WriteLine("Node " + this.parentNode.Name + " has disconnected from the switch.");
                        break;
                    }

                    SwitchMessage msg = new SwitchMessage();
                    bool msgOk = msg.Decode(message);
                    Console.WriteLine("Got message on switch node " + this.parentNode.Name);

                    if(!msgOk)
                    {

                    }
                    else if (msg.Ack)
                    {
                        if (SenderThread.ThreadState.Equals(ThreadState.WaitSleepJoin))
                        {
                            //Notify the sender thread that the receiving node did get it.
                            SenderThread.Interrupt();
                        }
                        else
                        {
                            //Console.WriteLine("Node {0} sent to node {1} (really {2}) an ack when it wasn't supposed to!", msg.Sender, msg.Destination, this.name);
                        }
                    }
                    else
                    {
                        //message has successfully been received
                        parentNode.writeOutMessage(msg);
                        SwitchMessage ackMsg = new SwitchMessage();
                        ackMsg.EncodeHiddenComm(msg.Sender, msg.Destination);

                        lock (clientStream)
                        {
                            if (clientStream.CanWrite)
                            {
                                caller.BeginInvoke(clientStream, ackMsg, null, null);
                            }
                            else
                            {
                                Console.WriteLine("Node " + this.ParentNode.Name + " can't send an ack.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

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

            this.doneReceiving = true;

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