private void ExecuteMethod()
        {
            object[] result = null;

            try
            {
                var parameters = calatog.GetParametersValues();
                result = ReceiveMessages.ReceiveMessage(calatog.Description, parameters, (int)SystemAramis.CurrentUserId);
            }
            catch (Exception exp)
            {
                string.Format("Исключение при выполнении метода: {0}", exp.Message);
                return;
            }

            var message = new StringBuilder();
            int index   = 0;

            foreach (var returnValue in result)
            {
                message.AppendWithSeparatorFormat(string.Format("[{0}] = {1}\tType = {2}", index, returnValue.ToString().PadRight(25),
                                                                (returnValue ?? new object()).GetType()));
                index++;
            }

            message.ToString().AlertBox();
        }
 public void PushReceiveMessage(byte[] receiveBytes)
 {
     if (receiveBytes == null)
     {
         return;
     }
     ReceiveMessages.Enqueue(receiveBytes);
 }
Exemple #3
0
        protected override void OnStart(string[] args)
        {
            file.Write($"Service is started at {DateTime.Now}");


            // Receive messages and process them
            ReceiveMessages obj = new ReceiveMessages();
        }
        public static async Task Main(string[] args)
        {
            //
            //	get configuration information
            //
            MessageQueueAppConfig messageQueueAppConfig = new MessageQueueAppConfig();
            ConnectionStrings     connectionStrings     = new ConnectionStrings();

            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            string jsonFile    = $"appsettings.{environment}.json";

            var configBuilder = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile(jsonFile, optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = configBuilder.Build();

            configuration.GetSection("MessageQueueAppConfig").Bind(messageQueueAppConfig);
            configuration.GetSection("ConnectionStrings").Bind(connectionStrings);
            //
            //	set up sending queue
            //
            IMessageQueueConnection sendingQueueConnection = new MessageQueueConnection(messageQueueAppConfig);

            sendingQueueConnection.CreateConnection();

            List <IMessageQueueConfiguration> messageQueueConfigurations = new List <IMessageQueueConfiguration>();
            //
            //	Inventory Received Transactions
            //
            IMessageQueueConfiguration inventoryReceivedConfiguration = new MessageQueueConfiguration(MessageQueueExchanges.InventoryReceived, messageQueueAppConfig, sendingQueueConnection);

            inventoryReceivedConfiguration.AddQueue(MessageQueueEndpoints.SalesOrderQueue);
            inventoryReceivedConfiguration.AddQueue(MessageQueueEndpoints.PurchaseOrderQueue);
            inventoryReceivedConfiguration.AddQueue(MessageQueueEndpoints.LoggingQueue);

            inventoryReceivedConfiguration.InitializeOutboundMessageQueueing();
            messageQueueConfigurations.Add(inventoryReceivedConfiguration);
            //
            //	Product Creation and Updates
            //
            IMessageQueueConfiguration productUpdatedConfiguration = new MessageQueueConfiguration(MessageQueueExchanges.ProductUpdated, messageQueueAppConfig, sendingQueueConnection);

            productUpdatedConfiguration.AddQueue(MessageQueueEndpoints.SalesOrderQueue);
            productUpdatedConfiguration.AddQueue(MessageQueueEndpoints.PurchaseOrderQueue);
            productUpdatedConfiguration.AddQueue(MessageQueueEndpoints.LoggingQueue);

            productUpdatedConfiguration.InitializeOutboundMessageQueueing();
            messageQueueConfigurations.Add(productUpdatedConfiguration);
            //
            //	Inventory Shipped Transactions
            //
            IMessageQueueConfiguration inventoryShippedConfiguration = new MessageQueueConfiguration(MessageQueueExchanges.InventoryShipped, messageQueueAppConfig, sendingQueueConnection);

            inventoryShippedConfiguration.AddQueue(MessageQueueEndpoints.SalesOrderQueue);
            inventoryShippedConfiguration.AddQueue(MessageQueueEndpoints.LoggingQueue);

            inventoryShippedConfiguration.InitializeOutboundMessageQueueing();
            messageQueueConfigurations.Add(inventoryShippedConfiguration);

            //
            //	initialize Sending Messages
            //
            IInventoryManagementDataService inventoryManagementDataService = new InventoryManagementDataService();
            IMessageQueueProcessing         messageProcessing = new MessageProcessing(inventoryManagementDataService);

            IHostedService sendInventoryManagementMessages = new SendMessages(sendingQueueConnection, messageProcessing,
                                                                              messageQueueAppConfig, connectionStrings, messageQueueConfigurations, MessageQueueEndpoints.InventoryQueue);
            //
            //	set up receiving queue
            //
            IMessageQueueConnection receivingConnection = new MessageQueueConnection(messageQueueAppConfig);

            receivingConnection.CreateConnection();

            List <IMessageQueueConfiguration> inboundMessageQueueConfigurations = new List <IMessageQueueConfiguration>();
            IMessageQueueConfiguration        inboundConfiguration = new MessageQueueConfiguration(messageQueueAppConfig, receivingConnection);

            inboundMessageQueueConfigurations.Add(inboundConfiguration);

            inboundConfiguration.InitializeInboundMessageQueueing(MessageQueueEndpoints.InventoryQueue);
            inboundConfiguration.InitializeLoggingExchange(MessageQueueExchanges.Logging, MessageQueueEndpoints.LoggingQueue);
            IInventoryManagementDataService inboundInventoryManagementDataService = new InventoryManagementDataService();
            IMessageQueueProcessing         inboundMessageProcessing = new MessageProcessing(inboundInventoryManagementDataService);

            IHostedService receiveInventoryManagementMessages = new ReceiveMessages(receivingConnection, inboundMessageProcessing, messageQueueAppConfig, connectionStrings, inboundMessageQueueConfigurations);
            //
            //	Set Up Message Processing
            //
            IInventoryManagementDataService inventoryManagementProcessingDataService = new InventoryManagementDataService();
            IMessageQueueProcessing         messageProcessor = new MessageProcessing(inventoryManagementProcessingDataService);
            ProcessMessages processMessages = new ProcessMessages(messageProcessor, messageQueueAppConfig, connectionStrings);

            var builder = new HostBuilder().ConfigureAppConfiguration((hostingContext, config) => {})
                          .ConfigureServices((hostContext, services) =>
            {
                services.AddTransient <IHostedService>(provider => processMessages);
            })
                          .ConfigureServices((hostContext, services) =>
            {
                services.AddTransient <IHostedService>(provider => sendInventoryManagementMessages);
            })
                          .ConfigureServices((hostContext, services) =>
            {
                services.AddTransient <IHostedService>(provider => receiveInventoryManagementMessages);
            })
                          .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
            });

            await builder.RunConsoleAsync();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            //string remoteHostIP = "192.168.101.210";
            //string remoteHostIP = "127.0.0.1";                                  //For testing change later for testing with other servers
            MessageCount = 100;
            LocalDelayRequest_Milliseconds = 10;
            string remoteHostIP = "10.170.4.84";                                  //For testing change later for testing with other servers
            string servername = "James Brooks";
            int remoteHostPort = 2605;
            //string file = "scenario_1_output_log_james_brooks_";
            string file = "Lab3.ScenarioC.BrooksJamesE_";

            string localIP = "127.0.0.1";
            string localPort = "";

            int i = 0;
            int error = 0;

            //Initialize outgoing messages to template values and add in known info
            for (i = 0; i < MessageCount; i++)
            {
                txmessage txmsg = new txmessage();

                //Initialize with predefined template values
                mc.makeOutgoingMessageTemplate(ref txmsg.msg);

                //Unique request id for each message
                //Request ID incremented in Fill_Message_RequestID
                pc.Fill_Message_RequestID(ref txmsg.msg, ref RequestID);

                pc.Fill_Message_SeverDelayTime_Milliseconds(ref txmsg.msg, RemoteDelayRequest_Milliseconds);
                pc.Fill_Message_Client_IP_Address(ref txmsg.msg, pc.GetLocalIP());
                pc.Fill_Message_RemoteHost_IP_Address(ref txmsg.msg, remoteHostIP);
                outgoingMessages.Add(txmsg);
            }

            //Null incoming message array bytes
            for (i = 0; i < MessageCount; i++)
            {
                rcmessage rmsg = new rcmessage();
                mc.nullIcomingMessage(ref rmsg.msg);
                incomingMessages.Add(rmsg);
            }

            //Open Socket Session

            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(remoteHostIP), remoteHostPort);
            socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(ipe);

            if (socket != null)
            {
                Console.WriteLine("Connected to " + remoteHostIP + " at " + remoteHostPort.ToString());
            }
            else
            {
                Console.WriteLine("Connection failed to " + remoteHostIP + " at " + remoteHostPort.ToString());
            }

            //Get socket statistics

            localIP = IPAddress.Parse(((IPEndPoint)socket.LocalEndPoint).Address.ToString()).ToString();
            localPort = (((IPEndPoint)socket.LocalEndPoint).Port.ToString()).ToString();

            //Fill in Session info to outgoing messages

            for (i = 0; i < MessageCount; i++)
            {
                pc.Fill_Message_Client_IP_Address(ref outgoingMessages[i].msg, localIP);
                pc.Fill_Message_ClientServicePort(ref outgoingMessages[i].msg, localPort);
                pc.Fill_Message_ClientSocket(ref outgoingMessages[i].msg, localPort);
            }

            //Transmit/Receive Threads

            SendMessages oSendMessages = new SendMessages();
            ReceiveMessages oReceiveMessages = new ReceiveMessages();

            SendStartTime = DateTime.Now;
            Thread oThread1 = new Thread(new ThreadStart(oSendMessages.sndmessages));
            ReceiveStartTime = DateTime.Now;
            Thread oThread2 = new Thread(new ThreadStart(oReceiveMessages.RcvMessages));

            // Start the threads
            oThread1.Start();
            while (!oThread1.IsAlive) ;
            oThread2.Start();
            while (!oThread2.IsAlive) ;

            oThread1.Join();
            oThread2.Join();

            // Do half session shutdowns

            // Close the transmitter
            socket.Shutdown(SocketShutdown.Send);

            // Close the listener
            socket.Shutdown(SocketShutdown.Receive);

            // Close the session

            socket.Close();

            // Put messages into ascii strings
            for (i = 0; i < incomingMessages.Count; i++)
            {
                //Put entire message to ascii
                incomingMessages[i].msgAscii = enc.GetString(incomingMessages[i].msg);

                //Insert appropriate response type
                if (incomingMessages[i].msgAscii.Contains("Delayed"))
                {
                    StringBuilder s = new StringBuilder(incomingMessages[i].msgAscii);
                    s[144] = '3';
                    incomingMessages[i].msgAscii = s.ToString();
                    incomingMessages[i].msg[144] = (byte) '3';
                }
                else
                {
                    StringBuilder s = new StringBuilder(incomingMessages[i].msgAscii);
                    s[144] = '1';
                    incomingMessages[i].msgAscii = s.ToString();
                    incomingMessages[i].msg[144] = (byte)'1';
                }
                string tempstr1 = incomingMessages[i].msgAscii.Substring(2, incomingMessages[i].msgAscii.Length - 2);

                //Make length into int
                byte[] tempbytes = new byte[4];
                tempbytes[2] = 0;
                tempbytes[3] = 0;
                tempbytes[1] = incomingMessages[i].msg[0];
                tempbytes[0] = incomingMessages[i].msg[1];
                int k = BitConverter.ToInt32(tempbytes, 0);

                //Put the components in seperated fields
                string[] messageParts = tempstr1.Split(incomingMessages[i].seperator);
                if (messageParts.Count() == 14)
                {
                    incomingMessages[i].msgLength = k;
                    incomingMessages[i].msgType = messageParts[0];
                    incomingMessages[i].msgTimeStamp = messageParts[1];
                    incomingMessages[i].msgRequestID = messageParts[2];
                    incomingMessages[i].msgStudentName = messageParts[3];
                    incomingMessages[i].msgStudentID = messageParts[4];
                    incomingMessages[i].msgServerDelay = messageParts[5];
                    incomingMessages[i].msgClientIPAddress = messageParts[6];
                    incomingMessages[i].msgClientServicePort = messageParts[7];
                    incomingMessages[i].msgClientSocketNumber = messageParts[8];
                    incomingMessages[i].msgHostIPAddress = messageParts[9];
                    incomingMessages[i].msgHostServicePort = messageParts[10];
                    incomingMessages[i].msgResponseID = messageParts[11];
                    incomingMessages[i].msgScenarioNumber = messageParts[12];
                }
                else
                {
                    error = 1;
                    incomingMessages[i].msgScenarioNumber = "4";
                    incomingMessages[i].msgTimeStamp = "9999999999";
                }
            }

            //Sort incoming messages by time stamp

            incomingMessages = incomingMessages.OrderBy(x => x.msgTimeStamp).ToList();

            //Create the last line of the log

            string LastLine = "Requests transmitted = \t" + MessageCount.ToString().PadLeft(5, '0') + "\r\n";
            LastLine = LastLine + "Responses received = \t" + MessageCount.ToString().PadLeft(5, '0') + "\r\n";

            TimeSpan et = SendStopTime - SendStartTime;
            LastLine = LastLine + "Req. run duration = \t" + et.TotalMilliseconds.ToString().PadLeft(9, '0') + "\r\n";

            et = ReceiveStopTime - ReceiveStartTime;
            LastLine = LastLine + "Rsp. Run duration = \t" + et.TotalMilliseconds.ToString().PadLeft(9, '0') + "\r\n";

            et = ReceiveStopTime - SendStartTime;
            LastLine = LastLine + "Trans. Duration = \t" + et.TotalMilliseconds.ToString().PadLeft(9, '0') + "\r\n";

            et = SendStopTime - SendStartTime;
            int durtime = (int)et.TotalMilliseconds / MessageCount;
            LastLine = LastLine + "Actual req. pace = \t" + durtime.ToString().PadLeft(5, '0') + "\r\n";

            et = ReceiveStopTime - ReceiveStartTime;
            durtime = (int)et.TotalMilliseconds / MessageCount;
            LastLine = LastLine + "Actual rsp. Pace = \t" + durtime.ToString().PadLeft(5, '0') + "\r\n";

            LastLine = LastLine + "Configured pace = \t" + LocalDelayRequest_Milliseconds.ToString().PadLeft(5, '0') + "\r\n";

            et = ReceiveStopTime - SendStartTime;
            durtime = (int)et.TotalMilliseconds / MessageCount;
            LastLine = LastLine + "Transaction avg. = \t" + durtime.ToString().PadLeft(5, '0') + "\r\n";

            LastLine = LastLine + "Your name: \t" + servername + "\r\n";
            LastLine = LastLine + "Name of student whose client was used: \t" + "James Brooks\r\n";

            //Create a log file

            file = file + DateTime.Now.ToString("yyyy.MM.dd_HH.mm.ss") + ".txt";
            StreamWriter writer = new StreamWriter(file);

            for (i = 0; i < incomingMessages.Count; i++)
            {
                writer.WriteLine(outgoingMessages[i].msgAscii);
                writer.WriteLine(incomingMessages[i].msgAscii);
            }
            writer.WriteLine(LastLine);
            writer.Close();
        }
        public byte[] GetReceiveMessage()
        {
            var bytes = ReceiveMessages.Dequeue();

            return(bytes);
        }