Esempio n. 1
0
 /// <summary>
 /// Rejestracja komponentu u CS
 /// </summary>
 /// <returns></returns>
 public bool Register()
 {
     try
         {
             Register register_message = new Register(type, computational_power, problem_names);
             byte[] register_response = client.Work(register_message.GetXmlData());
             XMLParser parser = new XMLParser(register_response);
             if (parser.MessageType == MessageTypes.RegisterResponse)
             {
                 RegisterResponse register_response_msg = parser.Message as RegisterResponse;
                 id = register_response_msg.Id;
                 timeout = register_response_msg.Timeout;
                 timeout_in_ms = (timeout.Hour * 3600 + timeout.Minute * 60 + timeout.Second) * 1000 + timeout.Millisecond;
                 Console.WriteLine("Received register values: id = {0}, timeout = {1} ms", id, timeout_in_ms);
             }
             else
             {
                 Console.WriteLine("SolverNode: registration failed");
                 return false;
             }
             return true;
         }
         catch {
             Console.WriteLine("Register failure");
             return false;
         }
 }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        public void RegisterConstructorTest()
        {
            //Arrange
            string[] problemNames = new string[] { "abc", "def", "ghi" };

            //Act
            Register register = new Register(NodeType.ComputationalNode, 2, problemNames);

            //Assert
            Assert.AreEqual(register.SolvableProblems.Count, problemNames.Length);
            Assert.IsTrue(register.SolvableProblems.Contains(problemNames[0]));
        }
Esempio n. 4
0
        public void RegisterToXmlTest()
        {
            //Arrange
            Register r = new Register(NodeType.ComputationalNode, 2, new List<string>() { "abc", "def"});

            //Act
            byte[] data = r.GetXmlData();

            //Assert
            Assert.IsNotNull(data);

            //Debug.WriteLine(StringToBytesConverter.GetString(data));
            Debug.WriteLine(System.Text.Encoding.UTF8.GetString(data));
        }
Esempio n. 5
0
        public void RegisterParseTest()
        {
            //Arrange
            Register register = new Register(NodeType.TaskManager, 120, new List<string>() { "abc", "def" });
            byte[] data = register.GetXmlData();

            //Act
            XMLParser parser = new XMLParser(data);

            //Assert
            Assert.IsNotNull(parser);
            Assert.AreEqual(MessageTypes.Register, parser.MessageType);
            Register result = (Register)parser.Message;
            Assert.AreEqual(result.ParallelThreads, register.ParallelThreads);
            Assert.AreEqual(result.Type, register.Type);
        }
Esempio n. 6
0
        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));
        }
Esempio n. 7
0
        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();
        }
Esempio n. 8
0
        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();
        }