/// <summary> /// Implements command reader function for Ipc Command Reader. /// When Ipc data is received on the IpcReader, this function is invoked and the command carried out. /// </summary> /// <param name="evt"></param> /// <returns></returns> public bool CmdReaderImpl(IpcController.IpcEvent evt) { try { switch (evt.EventType) { case "StatusReport": /* Transmit status reports*/ logger.Info("Transmitting status reports"); /*NiawaNetworkAdapter*/ ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); //NNA.UdpReceiver _udpReceiver.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); //NNA.UdpTransmitter _udpTransmitter.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); /*TcpSessionManager*/ _tcpSessionManager.SessionReceiverListThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); _tcpSessionManager.HandshakeReceiverThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); _tcpSessionManager.HandshakeReceiver.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); //sessions foreach (KeyValuePair<string, TcpSession> sessionKvp in _tcpSessionManager.TcpSessions) { //Session sessionKvp.Value.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); //Session.TcpReceiver sessionKvp.Value.Receiver.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); //Session.TcpTransmitter sessionKvp.Value.Transmitter.ThreadStatus.RaiseStatusReport(evt.ApplicationName + "-" + evt.ApplicationInstance); } break; default: break; } } catch (Exception ex) { logger.Error("[" + _description + "-M] Error while processing Command Reader Ipc Command: " + ex.Message + "]", ex); _evtRaiser.RaiseEvent("Error", "Error while processing Command Reader Ipc Command [" + ex.Message + "]", null, _threadStatus.NodeID, _threadStatus.ParentNodeID); } return true; }
/// <summary> /// Writes a message to the Web API Writer. /// </summary> /// <param name="message"></param> private async Task WriteMessage(IpcController.IpcEvent message) { try { //write message using (var client = new HttpClient()) { // Establish connection: client.BaseAddress = new Uri(_webApiUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue ("application/json")); // HTTP POST var newMessage = new NiawaWebMessage() { Sender = Environment.MachineName, Message = message.ToJson() }; HttpResponseMessage response = await client.PostAsJsonAsync("api/message", newMessage); if (response.IsSuccessStatusCode) { // Get the URI of the created resource. Uri messageUrl = response.Headers.Location; } else { logger.Error("[" + _description + "-T] Failed to write message to HttpClient. HttpResponseMessage: " + response.ToString()); } } } catch (Exception ex) { _threadStatus.ErrorCount += 1; _threadStatus.MessageErrorCount += 1; logger.Error("[" + _description + "-T] Error writing mesage to web client: " + ex.Message, ex); } //original code /* // Establish connection: client.BaseAddress = new Uri("http://localhost:3465/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue ("application/json")); // Get message: HttpResponseMessage response = await client.GetAsync("api/message/1"); if (response.IsSuccessStatusCode) { NiawaWebMessage message = await response.Content.ReadAsAsync<NiawaWebMessage>(); Console.WriteLine("{0}\t{1}\t{2}", message.Id, message.Sender, message.Message); } // HTTP POST var newMessage = new NiawaWebMessage() { Sender = Environment.MachineName, Message = "Test message " + DateTime.Now.ToString() }; response = await client.PostAsJsonAsync("api/message", newMessage); if (response.IsSuccessStatusCode) { // Get the URI of the created resource. Uri messageUrl = response.Headers.Location; } // Get message just posted: HttpResponseMessage response2 = await client.GetAsync("api/message/2"); if (response2.IsSuccessStatusCode) { NiawaWebMessage message2 = await response.Content.ReadAsAsync<NiawaWebMessage>(); Console.WriteLine("{0}\t{1}\t{2}", message2.Id, message2.Sender, message2.Message); } */ }