Exemplo n.º 1
0
static void CheckFile(const String & path, Queue<String> & codes)
{
   FileDataIO dio(muscleFopen(path(), "r"));
   if (dio.GetFile())
   {
      String fileName = path.Substring(GetFilePathSeparator());

      PlainTextMessageIOGateway gw;
      gw.SetDataIO(DataIORef(&dio, false));

      // Read in the file
      QueueGatewayMessageReceiver q;
      while(gw.DoInput(q) > 0) {/* empty */}

      // Now parse the lines, and for any that have LogTime() in them, print out the line and the corresponding code key
      uint32 lineNumber = 1;
      MessageRef msg;
      while(q.RemoveHead(msg) == B_NO_ERROR)
      {
         const String * line;
         for (uint32 i=0; msg()->FindString(PR_NAME_TEXT_LINE, i, &line) == B_NO_ERROR; i++)
         {
            int32 ltIdx = line->IndexOf("LogTime(");
            if (ltIdx >= 0)
            {
               int32 commentIdx = line->IndexOf("//");   // don't include LogTime() calls that are commented out
               if ((commentIdx < 0)||(commentIdx > ltIdx)) 
               {
                  char buf[128]; 
                  muscleSprintf(buf, "[%s] %s:" UINT32_FORMAT_SPEC ": ", SourceCodeLocationKeyToString(GenerateSourceCodeLocationKey(fileName(), lineNumber))(), path(), lineNumber);
                  codes.AddTail(line->Prepend(buf));
               }
            }
            lineNumber++;
         }
      }
   }
}
Exemplo n.º 2
0
// This program is equivalent to the portableplaintext client, except
// that we communicate with a child process instead of a socket.
int main(int argc, char ** argv)
{
   CompleteSetupSystem css;

   if (argc < 3) PrintUsageAndExit();

   const uint32 numProcesses = atol(argv[1]);
   if (numProcesses == 0) PrintUsageAndExit();

   const char * cmd = argv[2];

   Hashtable<String,String> testEnvVars;
   (void) testEnvVars.Put("Peanut Butter", "Jelly");
   (void) testEnvVars.Put("Jelly", "Peanut Butter");
   (void) testEnvVars.Put("Oranges", "Grapes");

   Queue<DataIORef> refs;
   for (uint32 i=0; i<numProcesses; i++)
   {
      ChildProcessDataIO * dio = new ChildProcessDataIO(false);
      refs.AddTail(DataIORef(dio));
      printf("About To Launch child process #" UINT32_FORMAT_SPEC ":  [%s]\n", i+1, cmd); fflush(stdout);
      ConstSocketRef s = (dio->LaunchChildProcess(argc-2, ((const char **) argv)+2, ChildProcessLaunchFlags(MUSCLE_DEFAULT_CHILD_PROCESS_LAUNCH_FLAGS), NULL, &testEnvVars) == B_NO_ERROR) ? dio->GetReadSelectSocket() : ConstSocketRef();
      printf("Finished Launching child process #" UINT32_FORMAT_SPEC ":  [%s]\n", i+1, cmd); fflush(stdout);
      if (s() == NULL)
      {
         LogTime(MUSCLE_LOG_CRITICALERROR, "Error launching child process #" UINT32_FORMAT_SPEC " [%s]!\n", i+1, cmd);
         return 10;
      }
   }

   StdinDataIO stdinIO(false);
   PlainTextMessageIOGateway stdinGateway;
   QueueGatewayMessageReceiver stdinInputQueue;
   stdinGateway.SetDataIO(DataIORef(&stdinIO, false)); 

   SocketMultiplexer multiplexer;

   for (uint32 i=0; i<refs.GetNumItems(); i++)
   {
      printf("------------ CHILD PROCESS #" UINT32_FORMAT_SPEC " ------------------\n", i+1);
      PlainTextMessageIOGateway ioGateway;
      ioGateway.SetDataIO(refs[i]);
      ConstSocketRef readSock = refs[i]()->GetReadSelectSocket();
      QueueGatewayMessageReceiver ioInputQueue;
      while(1)
      {
         int readFD = readSock.GetFileDescriptor();
         multiplexer.RegisterSocketForReadReady(readFD);

         const int writeFD = ioGateway.HasBytesToOutput() ? refs[i]()->GetWriteSelectSocket().GetFileDescriptor() : -1;
         if (writeFD >= 0) multiplexer.RegisterSocketForWriteReady(writeFD);

         const int stdinFD = stdinIO.GetReadSelectSocket().GetFileDescriptor();
         multiplexer.RegisterSocketForReadReady(stdinFD);

         if (multiplexer.WaitForEvents() < 0) printf("testchildprocess: WaitForEvents() failed!\n");

         // First, deliver any lines of text from stdin to the child process
         if ((multiplexer.IsSocketReadyForRead(stdinFD))&&(stdinGateway.DoInput(ioGateway) < 0))
         {
            printf("Error reading from stdin, aborting!\n");
            break;
         }

         const bool reading    = multiplexer.IsSocketReadyForRead(readFD);
         const bool writing    = ((writeFD >= 0)&&(multiplexer.IsSocketReadyForWrite(writeFD)));
         const bool writeError = ((writing)&&(ioGateway.DoOutput() < 0));
         const bool readError  = ((reading)&&(ioGateway.DoInput(ioInputQueue) < 0));
         if ((readError)||(writeError))
         {
            printf("Connection closed, exiting.\n");
            break;
         }

         MessageRef incoming;
         while(ioInputQueue.RemoveHead(incoming) == B_NO_ERROR)
         {
            printf("Heard message from server:-----------------------------------\n");
            const char * inStr;
            for (int i=0; (incoming()->FindString(PR_NAME_TEXT_LINE, i, &inStr) == B_NO_ERROR); i++) printf("Line %i: [%s]\n", i, inStr);
           
            printf("-------------------------------------------------------------\n");
         }

         if ((reading == false)&&(writing == false)) break;

         multiplexer.RegisterSocketForReadReady(readFD);
         if (ioGateway.HasBytesToOutput()) multiplexer.RegisterSocketForWriteReady(writeFD);
      }

      if (ioGateway.HasBytesToOutput())
      {
         printf("Waiting for all pending messages to be sent...\n");
         while((ioGateway.HasBytesToOutput())&&(ioGateway.DoOutput() >= 0)) {printf ("."); fflush(stdout);}
      }
   }
   printf("\n\nBye!\n");
   return 0;
}
Exemplo n.º 3
0
// This program tests the functionality of the MessageIOGateway by writing a Message
// out to a file, then reading it back in.
int main(int argc, char ** argv) 
{
   CompleteSetupSystem css;
   QueueGatewayMessageReceiver inQueue;
   if (argc == 1)
   {
      FILE * f = muscleFopen("test.dat", "wb");
      if (f)
      {
         printf("Outputting test messages to test.dat...\n");
         MessageIOGateway g;
         g.SetDataIO(GetFileRef(f));
         for (int i=0; i<100; i++)
         {
            MessageRef m = GetMessageFromPool(MAKETYPE("TeSt"));
            TEST(m()->AddString("Jo", "Mama"));
            TEST(m()->AddInt32("Age", 90+i));
            TEST(m()->AddBool("Ugly", (i%2)!=0));
            TEST(g.AddOutgoingMessage(m));
         }
         while(g.HasBytesToOutput()) TESTSIZE(g.DoOutput());
         //g.GetDataIO()()->FlushOutput();
         printf("Done Writing!\n");
      }
      else printf("Error, could not open test file!\n");

      // Now try to read it back in
      FILE * in = muscleFopen("test.dat", "rb");
      if (in)
      {
         printf("Reading test messages from test.dat...\n");
         MessageIOGateway g;
         g.SetDataIO(GetFileRef(in));
         while(g.DoInput(inQueue) >= 0) 
         {
            MessageRef msgRef;
            while(inQueue.RemoveHead(msgRef) == B_NO_ERROR) msgRef()->PrintToStream();
         }

         printf("Done Reading!\n");
      }
      else printf("Error, could not re-open test file!\n");
   }
   else if (argc > 1)
   {
      for (int i=1; i<argc; i++)
      {
         FILE * in = muscleFopen(argv[i], "rb");
         if (in)
         {
            printf("Reading message file %s...\n", argv[i]);
            MessageIOGateway g;
            g.SetDataIO(GetFileRef(in));
            int32 readBytes;
            while((readBytes = g.DoInput(inQueue)) >= 0) 
            {
               printf("Read " UINT32_FORMAT_SPEC " bytes...\n", readBytes);
               MessageRef msgRef;
               while(inQueue.RemoveHead(msgRef) == B_NO_ERROR) msgRef()->PrintToStream();
            }
            printf("Done Reading file [%s]!\n", argv[i]);
         }
         else printf("Error, could not open file [%s]!\n", argv[i]);
      }
   }
   return 0;
}