"Fossies" - the Fresh Open Source Software Archive

Member "muscle/test/readmessage.cpp" (8 Jun 2019, 4359 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "readmessage.cpp": 7.21_vs_7.30.

    1 /* This file is Copyright 2000-2013 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */  
    2 
    3 #include <stdio.h>
    4 
    5 #include "dataio/FileDataIO.h"
    6 #include "system/SetupSystem.h"
    7 #include "message/Message.h"
    8 #include "util/ByteBuffer.h"
    9 #include "util/Hashtable.h"
   10 #include "zlib/ZLibUtilityFunctions.h"
   11 
   12 using namespace muscle;
   13 
   14 static String GetBytesSizeString(uint64 val)
   15 {
   16    const double b = 1000.0;  // note that we defined 1KB=1000 bytes, not 1024 bytes!
   17         if (val < b)     return String("%1 bytes").Arg(val);
   18    else if (val < b*b)   return String("%1kB").Arg(((double)val)/b,       "%.0f");
   19    else if (val < b*b*b) return String("%1MB").Arg(((double)val)/(b*b),   "%.01");
   20    else                  return String("%1GB").Arg(((double)val)/(b*b*b), "%.02");
   21 }
   22 
   23 static void GenerateMessageSizeReportAux(const String & curPath, Message & msg, Hashtable<String, uint32> & results)
   24 {
   25    for (MessageFieldNameIterator fnIter(msg, B_MESSAGE_TYPE); fnIter.HasData(); fnIter++)
   26    {
   27       const String & fn = fnIter.GetFieldName();
   28       MessageRef subMsg;
   29       for (uint32 i=0; msg.FindMessage(fn, i, subMsg) == B_NO_ERROR; i++)
   30       {
   31          String subPath = curPath + "/" + fn;
   32          if (i > 0) subPath += String(":%1").Arg(i+1);
   33          GenerateMessageSizeReportAux(subPath, *subMsg(), results);
   34       }
   35       (void) msg.RemoveName(fn);  // so that child-fields won't be counted in our own payload-size below
   36    }
   37    (void) results.Put(curPath, msg.FlattenedSize());
   38 }
   39 
   40 static void PrintMessageReport(Message & msg, bool isSizeReport)
   41 {
   42    if (isSizeReport)
   43    {
   44       Hashtable<String, uint32> results;
   45       GenerateMessageSizeReportAux(GetEmptyString(), msg, results);
   46       results.SortByValue();
   47       for (HashtableIterator<String, uint32> iter(results); iter.HasData(); iter++) printf("%s:  %s\n", GetBytesSizeString(iter.GetValue())(), iter.GetKey()());
   48    }
   49    else msg.PrintToStream();
   50 }
   51 
   52 // A simple utility to read in a flattened Message file from disk, and print it out.
   53 int main(int argc, char ** argv)
   54 {
   55    int retVal = 0;
   56 
   57    CompleteSetupSystem css;
   58 
   59    const char * fileName   = (argc > 1) ? argv[1] : "test.msg";
   60    const bool isSizeReport = (((argc > 2)&&(strstr(argv[2], "sizes") != NULL))||(String(argv[0]).EndsWith("sizes")));
   61    FILE * fpIn = muscleFopen(fileName, "rb");
   62    if (fpIn)
   63    {
   64       FileDataIO fdio(fpIn);
   65 
   66       const uint64 fileSize = fdio.GetLength();
   67       ByteBufferRef buf = GetByteBufferFromPool(fileSize);
   68       if (buf() == NULL)
   69       {
   70          WARN_OUT_OF_MEMORY;
   71          return 10;
   72       }
   73 
   74       const uint32 numBytesRead = fdio.ReadFully(buf()->GetBuffer(), buf()->GetNumBytes());
   75       if (numBytesRead == fileSize) LogTime(MUSCLE_LOG_INFO, "Read " INT32_FORMAT_SPEC " bytes from [%s]\n", numBytesRead, fileName);
   76       else
   77       {
   78          LogTime(MUSCLE_LOG_CRITICALERROR, "Short read error (" UINT32_FORMAT_SPEC "/" UINT64_FORMAT_SPEC " bytes read)\n", numBytesRead, fileSize);
   79          return 10;
   80       }
   81 
   82       ByteBufferRef infBuf = InflateByteBuffer(buf);
   83       if (infBuf())
   84       {
   85          LogTime(MUSCLE_LOG_INFO, "Zlib-inflated file data from " INT32_FORMAT_SPEC " to " UINT32_FORMAT_SPEC " bytes.\n", numBytesRead, infBuf()->GetNumBytes());
   86          buf = infBuf;
   87       }
   88 
   89       Message msg;
   90       if (msg.Unflatten(infBuf()->GetBuffer(), infBuf()->GetNumBytes()) == B_NO_ERROR)
   91       {
   92          MessageRef infMsg = InflateMessage(MessageRef(&msg, false));
   93          if ((infMsg())&&(infMsg() != &msg))
   94          {
   95             LogTime(MUSCLE_LOG_INFO, "Zlib-inflated Message from " UINT32_FORMAT_SPEC " bytes to " UINT32_FORMAT_SPEC " bytes\n", msg.FlattenedSize(), infMsg()->FlattenedSize());
   96             LogTime(MUSCLE_LOG_INFO, "Message is:\n");
   97             PrintMessageReport(*infMsg(), isSizeReport);
   98             infMsg()->PrintToStream();
   99          }
  100          else
  101          {
  102             LogTime(MUSCLE_LOG_INFO, "Message is:\n");
  103             PrintMessageReport(msg, isSizeReport);
  104          }
  105       }
  106       else 
  107       {
  108          LogTime(MUSCLE_LOG_CRITICALERROR, "Error unflattening message! (" INT32_FORMAT_SPEC " bytes read)\n", numBytesRead);
  109          retVal = 10;
  110       }
  111    }
  112    else 
  113    {
  114       LogTime(MUSCLE_LOG_CRITICALERROR, "Could not read input flattened-message file [%s]\n", fileName);
  115       retVal = 10;
  116    }
  117 
  118    return retVal;
  119 }