public virtual void Flush() { // snapshot the queue size so we don't run the risk of a infinite loop int count = EventQueue.Count; Console.WriteLine("Flushing {0} events", count); while (count > 0) { var message = new Msg(); int items = Math.Min(count, BufferSize); for (int i = 0; i < items; i++) { Event ev; if (!EventQueue.TryDequeue(out ev)) { break; } message.Events.Add(ev); } if (message.Events.Count > 0) { SendMessage(message); } count -= message.Events.Count; } }
public IEnumerable<ServiceState> Query(string query) { var q = new Query {String = query}; var msg = new Msg {Query = q}; Serializer.Serialize(Tcp, msg); var response = Serializer.Deserialize<Msg>(Tcp); if (response.Ok) { return response.States; } throw new Exception(response.Error); }
public IEnumerable<Proto.State> Query(string query) { var q = new Proto.Query {@string = query}; var msg = new Proto.Msg {query = q}; Serializer.Serialize(Stream, msg); var response = Serializer.Deserialize<Proto.Msg>(Stream); if (response.ok) { return response.states; } throw new Exception(response.error); }
/// /// <summary>Queries Riemann</summary> /// <param name='query'>Query to send Riemann for process</param> /// <returns>List of States that answer the query.</returns> /// public IEnumerable <Proto.State> Query(string query) { var q = new Proto.Query { @string = query }; var msg = new Proto.Msg { query = q }; Serializer.SerializeWithLengthPrefix(_tcpStream, msg, PrefixStyle.Fixed32BigEndian); var response = Serializer.DeserializeWithLengthPrefix <Msg>(_tcpStream, PrefixStyle.Fixed32BigEndian); if (response.ok) { return(response.states); } throw new Exception(response.error); }
private void ClientConnected(IAsyncResult ar) { if (_tcpListener == null) { Console.WriteLine("server shutdown, not handling client request"); _isStopped.Set(); return; } Console.WriteLine("MockServer handling client connection"); var server = (TcpListener) ar.AsyncState; using (_tcpClient = server.EndAcceptTcpClient(ar)) using (var stream = _tcpClient.GetStream()) { try { while (true) { var msg = Serializer.DeserializeWithLengthPrefix<Msg>(stream, PrefixStyle.Fixed32BigEndian); if (msg != null) { Console.WriteLine("MockServer read client message: {0}", msg); TcpMessageQueue.Enqueue(msg); var reply = new Msg {ok = true}; Console.WriteLine("MockServer writing client reply"); Serializer.SerializeWithLengthPrefix(stream, reply, PrefixStyle.Fixed32BigEndian); Console.WriteLine("MockServer client reply written"); } } } catch (Exception ex) { Console.WriteLine("MockServer caught exception in client handler: {0}", ex.Message); } } _isStopped.Set(); }
private static byte[] MessageBytes(Msg message) { using (var memoryStream = new MemoryStream()) { Serializer.Serialize(memoryStream, message); return memoryStream.ToArray(); } }
/// /// <summary>Send many events to Riemann at once.</summary> /// <param name="events"> /// Enumerable of the events to process. /// Enumerable will be enumerated after being passed in. /// </param> /// public void SendEvents(IEnumerable<Event> events) { var tags = new List<string>(); lock (_tagLock) { if (_tag != null) { tags = _tag.Tags.ToList(); } } var protoEvents = events.Select( e => { var evnt = new Proto.Event { host = _name, service = e.Service, state = e.State, description = e.Description, metric_f = e.Metric, ttl = e.TTL }; evnt.tags.AddRange(e.Tags); return evnt; }).ToList(); var message = new Proto.Msg(); foreach (var protoEvent in protoEvents) { if(protoEvent.tags.Count == 0) protoEvent.tags.AddRange(tags); message.events.Add(protoEvent); } var array = MessageBytes(message); try { Datagram.Send(array); } catch (SocketException se) { if (se.SocketErrorCode == SocketErrorMessageTooLong) { var x = BitConverter.GetBytes(array.Length); Array.Reverse(x); Stream.Write(x, 0, 4); Stream.Write(array, 0, array.Length); Stream.Flush(); var response = Serializer.Deserialize<Proto.Msg>(Stream); if (!response.ok) { throw new Exception(response.error); } } else { throw; } } }
public virtual void SendMessage(Msg message) { lock (_clientLock) { Client.SendMessage(message); } }
public void SendEvent(Event ev) { var message = new Msg(); message.Events.Add(ev); SendMessage(message); }
public void SendMessage(Msg message) { var memoryStream = new MemoryStream(); Serializer.Serialize(memoryStream, message); var array = memoryStream.ToArray(); try { Udp.Send(array); } catch (SocketException se) { if (se.ErrorCode == SocketExceptionErrorCodeMessageTooLong) { var x = BitConverter.GetBytes(array.Length); if (BitConverter.IsLittleEndian) Array.Reverse(x); Tcp.Write(x, 0, 4); Tcp.Write(array, 0, array.Length); Tcp.Flush(); var response = Serializer.Deserialize<Msg>(Tcp); if (!response.Ok) { throw new Exception(response.Error); } } else { throw; } } }
public void SendEvents(IEnumerable<Event> events) { var message = new Msg(); message.Events.AddRange(events); SendMessage(message); }