コード例 #1
0
ファイル: Program.cs プロジェクト: kuzawskak/14-pl-05
        static void Main(string[] args)
        {
            try
            {
                NetworkClient nc = new NetworkClient("localhost", 22222);
                if (nc == null)
                {
                    Console.WriteLine("RemoteTester: NetworkClient object equals to null");
                    return;
                }

                byte[] data = new Register(NodeType.ComputationalNode, 1, new List<string>() { "test1", "test2" }).GetXmlData();

                byte[] bytes = nc.Work(data);

                if (bytes != null)
                {
                    XMLParser parser = new XMLParser(bytes.ToArray());
                    Console.WriteLine((parser.Message as RegisterResponse).Id);
                    Console.WriteLine((parser.Message as RegisterResponse).Timeout);
                }
                else Console.WriteLine("RemoteTester: bytes equals to null");
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
コード例 #2
0
ファイル: ClientTests.cs プロジェクト: kuzawskak/14-pl-05
        public void TestWork()
        {
            NetworkListener.ConnectionHandler ch = (d, cc) => { cc.Send(d); };
            byte[] data = new Register(NodeType.ComputationalNode, 1, new List<string>() { "test1", "test2" }).GetXmlData();
            byte[] bytes = null;
            NetworkClient nc = null;
            memcmp mcmp = (d1, d2) => { if (d1.Length != d2.Length) return false;
                                        for (int i = 0; i < d1.Length; ++i)
                                            if (d1[i] != d2[i])
                                                return false;
                                        return true;
                                        };

            NetworkListener nl = null;

            // *** Listener is started ***
            nl = new NetworkListener(22222, ch);
            Assert.IsNotNull(nl);

            Thread t = new Thread(nl.Start);
            t.Start();

            // test for valid port firstly
            nc = new NetworkClient("localhost", 22222);

            bytes = nc.Work(data);
            Assert.IsNotNull(bytes);
            Assert.IsTrue(mcmp(bytes, data));

            // test if data is null
            Assert.IsNull(nc.Work(null));

            // test for invalid address
            nc = new NetworkClient(null, 22222);
            Assert.IsNull(nc.Work(data));

            // test for invalid port number (negative)
            nc = new NetworkClient("localhost", -1);
            Assert.IsNull(nc.Work(data));

            // test for invalid port (not listening port)
            nc = new NetworkClient("localhost", 22221);
            Assert.IsNull(nc.Work(data));

            nl.Stop();
            t.Abort();
            t.Join();

            // *** listener is not started ***
            nl = new NetworkListener(22222, ch);
            Assert.IsNotNull(nl);

            // test for valid port firstly
            nc = new NetworkClient("localhost", 22222);

            bytes = nc.Work(data);
            Assert.IsNull(bytes);

            // test if data is null
            Assert.IsNull(nc.Work(null));

            // test for invalid address
            nc = new NetworkClient(null, 22222);
            Assert.IsNull(nc.Work(data));

            // test for invalid port number (negative)
            nc = new NetworkClient("localhost", -1);
            Assert.IsNull(nc.Work(data));

            // test for invalid port (not listening port)
            nc = new NetworkClient("localhost", 22221);
            Assert.IsNull(nc.Work(data));
        }
コード例 #3
0
ファイル: ServerTests.cs プロジェクト: kuzawskak/14-pl-05
        public void ServerRegisterMsgTest()
        {
            int port = 22222;
            Server srv = new Server(port, new TimeSpan(0, 0, 10));
            Task t = Task.Factory.StartNew(srv.Start);
            byte[] data = new Register(NodeType.ComputationalNode, 1, new List<string>() { "test1", "test2" }).GetXmlData();
            byte[] bytes = null;
            XMLParser parser = null;
            // *** Valid port and address ***
            NetworkClient nc = new NetworkClient("localhost", port);
            NetworkClient nc2 = new NetworkClient("localhost", port);

            Assert.IsNotNull(srv);
            Assert.IsNotNull(nc);
            Assert.IsNotNull(nc2);

            // first cli
            bytes = nc.Work(data);

            Assert.IsNotNull(bytes);

            parser = new XMLParser(bytes);

            Assert.IsNotNull(parser);
            Assert.AreEqual(parser.MessageType, MessageTypes.RegisterResponse);

            // first cli, trying to connect second time
            bytes = nc.Work(data);

            Assert.IsNotNull(bytes);

            parser = new XMLParser(bytes);

            Assert.IsNotNull(parser);
            Assert.AreEqual(parser.MessageType, MessageTypes.RegisterResponse);

            // second cli
            bytes = nc2.Work(data);

            Assert.IsNotNull(bytes);

            parser = new XMLParser(bytes);

            Assert.IsNotNull(parser);
            Assert.AreEqual(parser.MessageType, MessageTypes.RegisterResponse);

            srv.Stop();
            t.Wait();

            // *** Valid port and address (srv is stopped)
            bytes = nc.Work(data);

            Assert.IsNull(bytes);

            // *** invalid port number ***
            srv = new Server(-1, new TimeSpan(0, 0, 10));
            t = Task.Factory.StartNew(srv.Start);

            bytes = nc.Work(data);

            Assert.IsNull(bytes);

            srv.Stop();
            t.Wait();
        }
コード例 #4
0
ファイル: ListenerTests.cs プロジェクト: kuzawskak/14-pl-05
        public void TestStartStop()
        {
            NetworkListener.ConnectionHandler ch = (d, cc) => { cc.Send(d); };
            byte[] data = new Register(NodeType.ComputationalNode, 1, new List<string>() { "test1", "test2" }).GetXmlData();
            byte[] bytes = null;
            NetworkClient nc = null;
            NetworkListener nl = null;
            NetworkClient[] _nc = null;
            Thread[] _t = null;
            Thread t = null;
            bool?[] _state = null;
            const int port = 22222;
            memcmp mcmp = (d1, d2) => { if (d1.Length != d2.Length) return false;
                                        for (int i = 0; i < d1.Length; ++i)
                                            if (d1[i] != d2[i])
                                                return false;
                                        return true;
                                        };

            // *** Valid port: Listener is started (one client) ***
            nl = new NetworkListener(port, ch);
            Assert.IsNotNull(nl);
            t = new Thread(nl.Start);
            Assert.IsNotNull(t);
            t.Start();

            nc = new NetworkClient("localhost", port);

            bytes = nc.Work(data);
            Assert.IsNotNull(bytes);
            Assert.IsTrue(mcmp(bytes, data));

            // cleaning
            nl.Stop();
            Assert.IsFalse(nl.IsRunning());
            t.Abort();
            t.Join();

            // *** Invalid port: Listener is trying to start ***
            nl = new NetworkListener(-1, ch);
            Assert.IsNotNull(nl);

            nl.Start();
            Assert.IsFalse(nl.IsRunning());

            // *** Valid port: Listener is started (few clients) ***
            nl = new NetworkListener(port, ch);
            Assert.IsNotNull(nl);

            t = new Thread(nl.Start);
            Assert.IsNotNull(t);
            t.Start();

            // few clients connection simulation
            const int clients = 5;
            _nc = new NetworkClient[clients];
            _t = new Thread[clients];
            _state = new bool?[clients];

            Assert.IsNotNull(_nc);
            Assert.IsNotNull(_t);
            Assert.IsNotNull(_state);
            /*
            for (int j = 0; j < clients; ++j) {
                _nc[j] = new NetworkClient("localhost", port);
                _t[j] = new Thread(() => {
                                    byte[] _b = _nc[j].Work(data);
                                    if (_b == null) {
                                        _state[j] = false;
                                        return;
                                    }
                                    //Assert.IsTrue(mcmp(_b, data));
                                    _state[j] = mcmp(_b, data);
                                    });

                _t[j].Start();
            }
            */

            _nc[0] = new NetworkClient("localhost", port);
            _t[0] = new Thread(() =>
            {
                byte[] _b = _nc[0].Work(data);
                if (_b == null)
                {
                    _state[0] = false;
                    return;
                }
                //Assert.IsTrue(mcmp(_b, data));
                _state[0] = mcmp(_b, data);
            });

            _t[0].Start();

            _nc[1] = new NetworkClient("localhost", port);
            _t[1] = new Thread(() =>
            {
                byte[] _b = _nc[1].Work(data);
                if (_b == null)
                {
                    _state[1] = false;
                    return;
                }
                //Assert.IsTrue(mcmp(_b, data));
                _state[1] = mcmp(_b, data);
            });

            _t[1].Start();

            _nc[2] = new NetworkClient("localhost", port);
            _t[2] = new Thread(() =>
            {
                byte[] _b = _nc[2].Work(data);
                if (_b == null)
                {
                    _state[2] = false;
                    return;
                }
                //Assert.IsTrue(mcmp(_b, data));
                _state[2] = mcmp(_b, data);
            });

            _t[2].Start();

            _nc[3] = new NetworkClient("localhost", port);
            _t[3] = new Thread(() =>
            {
                byte[] _b = _nc[3].Work(data);
                if (_b == null)
                {
                    _state[3] = false;
                    return;
                }
                //Assert.IsTrue(mcmp(_b, data));
                _state[3] = mcmp(_b, data);
            });

            _t[3].Start();

            _nc[4] = new NetworkClient("localhost", port);
            _t[4] = new Thread(() =>
            {
                byte[] _b = _nc[4].Work(data);
                if (_b == null)
                {
                    _state[4] = false;
                    return;
                }
                //Assert.IsTrue(mcmp(_b, data));
                _state[4] = mcmp(_b, data);
            });

            _t[4].Start();

            // czekanie na wątki klientów

            for (int k = 0; k < clients; ++k) {
                _t[k].Join();
                Assert.IsTrue((bool)_state[k]);
            }

            // cleaning
            nl.Stop();
            t.Abort();
            t.Join();
        }