Пример #1
0
        public void MultiplyConditionFalse()
        {
            var mx1 = new List <List <int> >()
            {
                new List <int>()
                {
                    3, 2, 5
                },
                new List <int>()
                {
                    24, 1, 3
                }
            };

            var mx2 = new List <List <int> >()
            {
                new List <int>()
                {
                    1, 2, 3
                },
                new List <int>()
                {
                    4, 10, 2
                },
                new List <int>()
                {
                    2, 1, 4
                }
            };

            Assert.IsFalse(MatrixOperations.CheckMultiplyCondition(mx1, mx2));
        }
Пример #2
0
        public void StartListening()
        {
            var    ipPoint      = new IPEndPoint(IPAddress.Parse(ipAddress), port);
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket handler      = null;

            try
            {
                listenSocket.Bind(ipPoint);
                listenSocket.Listen(10);
                logger.Log(SocketServiceMessage.socketStartListening);

                while (true)
                {
                    handler = listenSocket.Accept();
                    var    builder = new StringBuilder();
                    int    bytes   = 0;
                    byte[] data    = new byte[256];

                    do
                    {
                        bytes = handler.Receive(data);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (handler.Available > 0);

                    string inputInfo = builder.ToString();
                    var    tpModel   = JsonSerializer.Deserialize <MxMultiplicationInputModel>(inputInfo);
                    logger.Log(inputInfo);
                    var mxResponse1 = dbSocket.GetData(tpModel.MX1Name);
                    var mxResponse2 = dbSocket.GetData(tpModel.MX2Name);
                    var result      = new OperationResult <List <List <int> > >();

                    if (mxResponse1.Status == OperationStatus.Ok && mxResponse2.Status == OperationStatus.Ok)
                    {
                        matrix1 = mxResponse1.Data;
                        matrix2 = mxResponse2.Data;

                        if (MatrixOperations.CheckMultiplyCondition(matrix1, matrix2))
                        {
                            List <List <int> > multiRes = MatrixOperations.MultiplyMatrices(matrix1, matrix2);
                            result.Data   = CutMatrixBuilder.Build(multiRes, tpModel.RowBegin, tpModel.RowEnd, tpModel.ColBegin, tpModel.ColEnd);
                            result.Status = OperationStatus.Ok;
                        }
                        else
                        {
                            result.Status  = OperationStatus.Error;
                            result.Message = Errors.MultiplyConditionFailed;
                        }
                    }
                    else
                    {
                        result.Status  = OperationStatus.Error;
                        result.Message = mxResponse1.Status == OperationStatus.Error ? mxResponse1.Message : mxResponse2.Message;
                    }


                    data = Encoding.Unicode.GetBytes(JsonSerializer.Serialize(result));
                    handler.Send(data);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                handler?.Shutdown(SocketShutdown.Both);
                listenSocket.Close();
                logger.Log(SocketServiceMessage.socketReloading);
                StartListening();
            }
        }