]> granicus.if.org Git - icinga2/commitdiff
stream/livestatus: refactor ReadLine with context saving
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 3 Jul 2013 14:16:38 +0000 (16:16 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 3 Jul 2013 14:16:38 +0000 (16:16 +0200)
components/livestatus/component.cpp
lib/base/stream.cpp
lib/base/stream.h

index fe30e165ddca88e4f839b39d8437e167b3e7182c..7ef3138516e74ad5828bcf5dcb8eff48dab1855d 100644 (file)
@@ -84,13 +84,11 @@ void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
 
        for (;;) {
                String line;
-               bool read_line = false;
+               ReadLineContext context;
 
                std::vector<String> lines;
 
-               while (stream->ReadLine(&line)) {
-                       read_line = true;
-
+               while (stream->ReadLine(&line, context)) {
                        if (line.GetLength() > 0)
                                lines.push_back(line);
                        else
index 1405194c40e5765390b9b783c34d0fbe6254e9a1..0ef95715c57cb0cedcd955ce4c8044b211a6a174 100644 (file)
 
 using namespace icinga;
 
-bool Stream::ReadLine(String *line, size_t maxLength)
+bool Stream::ReadLine(String *line, ReadLineContext& context, size_t maxLength)
 {
-       BOOST_THROW_EXCEPTION(std::runtime_error("Not implemented."));
-       /*
-       char *buffer = new char[maxLength];
+       if (context.Eof)
+               return false;
 
-       size_t rc = Peek(buffer, maxLength);
+       for (;;) {
+               if (context.MustRead) {
+                       context.Buffer = (char *)realloc(context.Buffer, context.Size + maxLength);
 
-       if (rc == 0)
-               return false;
+                       if (!context.Buffer)
+                               throw std::bad_alloc();
 
-       for (size_t i = 0; i < rc; i++) {
-               if (buffer[i] == '\n') {
-                       *line = String(buffer, &(buffer[i]));
-                       boost::algorithm::trim_right(*line);
+                       size_t rc = Read(context.Buffer + context.Size, maxLength);
 
-                       Read(NULL, i + 1);
+                       if (rc == 0) {
+                               *line = String(context.Buffer, &(context.Buffer[context.Size]));
+                               boost::algorithm::trim_right(*line);
 
-                       delete buffer;
+                               context.Eof = true;
 
-                       return true;
+                               return true;
+                       }
+
+                       context.Size += rc;
                }
-       }
 
-       if (IsReadEOF()) {
-               *line = String(buffer, buffer + rc);
-               boost::algorithm::trim_right(*line);
+               int count = 0;
+               size_t first_newline;
 
-               Read(NULL, rc);
+               for (size_t i = 0; i < context.Size; i++) {
+                       if (context.Buffer[i] == '\n') {
+                               count++;
 
-               delete buffer;
+                               if (count == 1)
+                                       first_newline = i;
+                       }
+               }
 
-               return true;
-       }
+               context.MustRead = (count <= 1);
+
+               if (count > 0) {
+                       *line = String(context.Buffer, &(context.Buffer[first_newline]));
+                       boost::algorithm::trim_right(*line);
 
-       delete buffer;*/
+                       memmove(context.Buffer, context.Buffer + first_newline + 1, context.Size - first_newline - 1);
+                       context.Size -= first_newline + 1;
 
-       return false;
+                       return true;
+               }
+       }
 }
index 510cbb0cd99c583c45e3c478e55be601882e26d1..b51eb59ad0729731ba7d3207e74e2f3923c40d64 100644 (file)
 namespace icinga
 {
 
+struct ReadLineContext
+{
+       ReadLineContext(void) : Buffer(NULL), Size(0), Eof(false), MustRead(true)
+       { }
+
+       char *Buffer;
+       size_t Size;
+       bool Eof;
+       bool MustRead;
+};
+
 /**
  * A stream.
  *
@@ -64,7 +75,7 @@ public:
         */
        virtual void Close(void) = 0;
 
-       bool ReadLine(String *line, size_t maxLength = 4096);
+       bool ReadLine(String *line, ReadLineContext& context, size_t maxLength = 4096);
 };
 
 }