Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.Write("\n  Demonstrating Project #4 Channel Prototype");
            Console.Write("\n ============================================\n");

            ChannelDemo <Client> demo1 = new ChannelDemo <Client>();

            string sndrEndPoint1 = Comm <Client> .makeEndPoint("http://localhost", 8080);

            string rcvrEndPoint1 = Comm <Server> .makeEndPoint("http://localhost", 8080);

            demo1.comm.rcvr.CreateRecvChannel(rcvrEndPoint1);
            Thread rcvThread1 = demo1.comm.rcvr.start(demo1.rcvThreadProc);

            Console.Write("\n  rcvr thread id = {0}", rcvThread1.ManagedThreadId);
            Console.WriteLine();

            ChannelDemo <Server> demo2 = new ChannelDemo <Server>();

            string sndrEndPoint2 = Comm <Client> .makeEndPoint("http://localhost", 8081);

            string rcvrEndPoint2 = Comm <Server> .makeEndPoint("http://localhost", 8081);

            demo2.comm.rcvr.CreateRecvChannel(rcvrEndPoint2);
            Thread rcvThread2 = demo2.comm.rcvr.start(demo2.rcvThreadProc);

            Console.Write("\n  rcvr thread id = {0}", rcvThread2.ManagedThreadId);
            Console.WriteLine();

            // make a TestRequest message and send five times to two different endpoints

            Message msg = null;
            string  rcvrEndPoint;

            for (int i = 0; i < 5; ++i)
            {
                msg      = new Message(demo1.makeTestRequest());
                msg.type = "TestRequest";
                msg.from = sndrEndPoint1;
                if (i < 3)
                {
                    msg.to = rcvrEndPoint = rcvrEndPoint1;
                }
                else
                {
                    msg.to = rcvrEndPoint = rcvrEndPoint2;
                }
                msg.author = "Fawcett";
                msg.time   = DateTime.Now;

                demo1.comm.sndr.PostMessage(msg);

                Console.Write("\n  {0}\n  posting message with body:\n{1}", msg.from, msg.body.shift());
                Thread.Sleep(20);
            }
            msg      = new Message();
            msg.from = sndrEndPoint1;
            msg.to   = rcvrEndPoint2;
            msg.body = "quit";

            demo1.comm.sndr.PostMessage(msg);
            rcvThread2.Join();
            Console.Write("\n  rcvThread1.state = {0}", rcvThread1.ThreadState.ToString());
            Console.Write("\n  rcvThread2.state = {0}", rcvThread2.ThreadState.ToString());
            msg      = new Message();
            msg.from = sndrEndPoint2;
            msg.to   = rcvrEndPoint1;
            msg.body = "quit";

            demo2.comm.sndr.PostMessage(msg);
            Thread.Sleep(500);
            Console.Write("\n  rcvThread1.state = {0}", rcvThread1.ThreadState.ToString());

            rcvThread1.Join();
            Console.Write("\n  passed 2nd Join");

            Console.Write("\n\n");
        }