Example #1
0
        public void AddManyMessagesAndShowThem()
        {
            MessageContainer messageContainter = new MessageContainer();

            messageContainter.AddMessage("Hola");
            messageContainter.AddMessage("Pepe");
            messageContainter.AddMessage("Col");

            //Act
            var result = messageContainter.GetMessagesAsText();

            //Assert
            Assert.Equal("Hola (0 seconds ago) Pepe (0 seconds ago) Col (0 seconds ago)", String.Join(' ', result));
        }
Example #2
0
        public void AddAMessageAndReturnIt()
        {
            MessageContainer messageContainter = new MessageContainer();

            messageContainter.AddMessage("Hola");

            //Act
            var result = messageContainter.GetMessagesAsText();

            //Assert
            Assert.Equal("Hola (0 seconds ago)", result.First());
        }
        /// <summary>
        /// Transfers registered messages to the message container.
        /// </summary>
        /// <param name="messageContainer">The Message Containter.</param>
        public void MoveMessages(MessageContainer messageContainer)
        {
            lock (this)
            {
                for ( int i = 0; i < this._messages.Length; i++ )
                {
                    Message message = this._messages[i] as Message;

                    // 2.5.1 ignores messages that have been answered
                    if (message != null && ! message.HasBeenAsnwered)
                    {
                        message.SerializedContent.Position = 0;
                        messageContainer.AddMessage(message, false);
                        this._messages[i] = null;
                    }
                }
            }
        }
        /// <summary>
        /// Calls API to get reponse message for the user
        /// </summary>
        /// <param name="userRequestMessage">user request</param>
        public async void APICallAsync(string userRequestMessage)
        {
            HttpClient client = new HttpClient();
            //WebAPITest.Program.Main(new string[] { });
            string url = "https://localhost:44363/api/call/";

            url += userRequestMessage;
            HttpResponseMessage message = await client.GetAsync(url);

            if (!message.IsSuccessStatusCode)
            {
                System.Windows.MessageBox.Show($"Request failed with status code {message.StatusCode}");
            }
            else
            {
                MessageContainer.AddMessage((await message.Content.ReadAsStringAsync()).ToString(), false);
            }
        }
        private void SetupLogs()
        {
            foreach (EventLog eventLog in Logs)
            {
                eventLog.EnableRaisingEvents = false;
                eventLog.Dispose();
            }
            Logs.Clear();
            foreach (string logName in Configuration.WindowsEventLogsConfiguration.LogsToMonitor)
            {
                try
                {
                    if (EventLog.Exists(logName))
                    {
                        var eventLog = new EventLog(logName);
                        Logs.Add(eventLog);
                        // set event handler
                        eventLog.EntryWritten += (apps, arg) =>
                        {
                            if (LogLevel(arg.Entry.EntryType) >= Configuration.WindowsEventLogsConfiguration.MinimumLogLevel)
                            {
                                AnalogyGRPCLogMessage m = CreateGRPCMessageFromEvent(arg.Entry);
                                m.Module = logName;
                                MessageContainer.AddMessage(m);
                            }
                        };

                        eventLog.EnableRaisingEvents = true;
                    }
                }
                catch (Exception e)
                {
                    string m = "Error Opening log. Please make sure you are running as Administrator." + Environment.NewLine + "Error:" + e.Message;
                    AnalogyGRPCLogMessage err = new AnalogyGRPCLogMessage
                    {
                        Level = AnalogyGRPCLogLevel.Error, Text = m,
                        Date  = Timestamp.FromDateTime(DateTime.UtcNow),
                        Id    = Guid.NewGuid().ToString()
                    };
                    MessageContainer.AddMessage(err);
                }
            }
        }
 protected override Task ExecuteAsync(CancellationToken stoppingToken)
 {
     if (Configuration.WindowsEventLogsConfiguration.EnableMonitoring)
     {
         try
         {
             SetupLogs();
         }
         catch (Exception e)
         {
             string m = "Error Opening log. Please make sure you are running as Administrator." + Environment.NewLine + "Error:" + e.Message;
             AnalogyGRPCLogMessage err = new AnalogyGRPCLogMessage {
                 Level = AnalogyGRPCLogLevel.Error, Text = m, Date = Timestamp.FromDateTime(DateTime.UtcNow), Id = Guid.NewGuid().ToString()
             };
             MessageContainer.AddMessage(err);
         }
     }
     return(Task.CompletedTask);
 }
        private async Task HandleClientPublisingMessages(IAsyncStreamReader <AnalogyGRPCLogMessage> requestStream,
                                                         ServerCallContext serverCallContext, CancellationToken token)
        {
            try
            {
                await foreach (var message in requestStream.ReadAllAsync(token))
                {
                    try
                    {
                        if (message.Date == null)
                        {
                            message.Date = Timestamp.FromDateTime(DateTime.UtcNow);
                        }

                        if (string.IsNullOrEmpty(message.Id))
                        {
                            message.Id = Guid.NewGuid().ToString();
                        }

                        MessageContainer.AddMessage(message);
                    }
                    catch (Exception e)
                    {
                        Logger.LogError($"Error  receiving messages: {e}");
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Logger.LogError($"Consuming ended for Peer: {serverCallContext.Peer}");
            }
            catch (Exception e)
            {
                Logger.LogError($"Error: {e.Message}. Peer: {serverCallContext.Peer}");
            }
        }
 /// <summary>
 /// Add message by button clicked function
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void SendButton_Click(object sender, RoutedEventArgs e)
 {
     MessageContainer.AddMessage(InputField.Text, true);
     APICallAsync(InputField.Text);
     InputField.Text = string.Empty;
 }
 /// <summary>
 /// Add message function
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e">the message content</param>
 private void InputBox_TextSubmitted(object sender, string e)
 {
     MessageContainer.AddMessage(e.ToString(), true);
     APICallAsync(e);
 }