Exemplo n.º 1
0
static status_t ExpandFilePathWildCardsAux(const String & curDir, const String & path, Queue<String> & outputPaths, bool isSimpleFormat)
{
   const char * fs = GetFilePathSeparator();
   const int32 sepIdx = path.IndexOf(fs);
   const String firstClause  = (sepIdx >= 0) ? path.Substring(0, sepIdx) : path;
   const String restOfString = (sepIdx >= 0) ? path.Substring(sepIdx+1) : GetEmptyString();

   StringMatcher sm(firstClause, isSimpleFormat);
   Directory dir(curDir());
   if (dir.IsValid())
   {
      if (CanWildcardStringMatchMultipleValues(firstClause))
      {
         while(1)
         {
            const char * fn = dir.GetCurrentFileName();
            if (fn)
            {
               if ((strcmp(fn, ".") != 0)&&(strcmp(fn, "..") != 0)&&((fn[0] != '.')||(firstClause.StartsWith(".")))&&(sm.Match(fn)))
               {
                  const String childPath = String(dir.GetPath())+fn;
                  if (restOfString.HasChars())
                  {
                     if (ExpandFilePathWildCardsAux(childPath, restOfString, outputPaths, isSimpleFormat) != B_NO_ERROR) return B_ERROR;
                  }
                  else if (outputPaths.AddTail(childPath) != B_NO_ERROR) return B_ERROR;
               } 
               dir++;
            }
            else break;
         }
      }
      else
      {
         const String childPath = String(dir.GetPath())+firstClause;
         if (FilePathInfo(childPath()).Exists())
         {
            if (restOfString.HasChars())
            {
               if (ExpandFilePathWildCardsAux(childPath, restOfString, outputPaths, isSimpleFormat) != B_NO_ERROR) return B_ERROR;
            }
            else if (outputPaths.AddTail(childPath) != B_NO_ERROR) return B_ERROR;
         }
      }
   }
   return B_NO_ERROR;
}
Exemplo n.º 2
0
// This program exercises the Ref class.
int main(void) 
{
   CompleteSetupSystem setupSystem;

   printf("sizeof(TestItemRef)=%i\n", (int)sizeof(TestItemRef));

   {
      printf("Checking queue...\n");
      Queue<TestItemRef> q;
      printf("Adding refs...\n");
      for (int i=0; i<10; i++) 
      {
         char temp[50]; muscleSprintf(temp, "%i", i);
         TestItemRef tr(new TestItem(temp));
         ConstTestItemRef ctr(tr);
         ConstTestItemRef t2(ctr);
         q.AddTail(tr);
      }
      printf("Removing refs...\n");
      while(q.HasItems()) q.RemoveHead();
      printf("Done with queue test!\n");
   }

   {
      printf("Checking hashtable\n");
      Hashtable<String, TestItemRef> ht;
      printf("Adding refs...\n");
      for (int i=0; i<10; i++) 
      {
         char temp[50]; muscleSprintf(temp, "%i", i);
         ht.Put(String(temp), TestItemRef(new TestItem(temp)));
      }
      printf("Removing refs...\n");
      for (int i=0; i<10; i++) 
      {
         char temp[50]; muscleSprintf(temp, "%i", i);
         ht.Remove(String(temp));
      }
      printf("Done with hash table test!\n");
   }
    
   printf("Beginning multithreaded object usage test...\n");
   {
      const uint32 NUM_THREADS = 50;
      TestThread threads[NUM_THREADS]; 
      for (uint32 i=0; i<NUM_THREADS; i++) threads[i].StartInternalThread();
      Snooze64(SecondsToMicros(10));
      for (uint32 i=0; i<NUM_THREADS; i++) threads[i].ShutdownInternalThread();
      printf("Multithreaded object usage test complete.\n");
   }

   printf("testrefcount complete, bye!\n");
   return 0;
}
Exemplo n.º 3
0
MuscledWindow :: MuscledWindow(const char * argv0) : _cpdio(false), _notifier(NULL)
{
   resize(800, 400);
   setWindowTitle("MUSCLEd Server Process");

   QBoxLayout * bl = new QBoxLayout(QBoxLayout::TopToBottom, this);
   bl->setMargin(0);
   bl->setSpacing(0);

   _muscledStdoutText = new QPlainTextEdit;
   bl->addWidget(_muscledStdoutText); 

   Queue<String> argv;
   argv.AddTail(argv0);
   argv.AddTail("muscled");   
   argv.AddTail("displaylevel=trace");
   argv.AddTail("catchsignals");
   if (_cpdio.LaunchChildProcess(argv) == B_NO_ERROR) 
   {
      _gateway.SetDataIO(DataIORef(&_cpdio, false));
      _notifier = new QSocketNotifier(_cpdio.GetReadSelectSocket().GetFileDescriptor(), QSocketNotifier::Read, this);
      connect(_notifier, SIGNAL(activated(int)), this, SLOT(TextAvailableFromChildProcess()));
   }
Exemplo n.º 4
0
static status_t ReadIncomingData(const String & desc, DataIO & readIO, const SocketMultiplexer & multiplexer, Queue<ByteBufferRef> & outQ)
{
   if (multiplexer.IsSocketReadyForRead(readIO.GetReadSelectSocket().GetFileDescriptor()))
   {
      uint8 buf[4096];
      int32 ret = readIO.Read(buf, sizeof(buf));
      if (ret > 0) 
      {
         LogTime(MUSCLE_LOG_TRACE, "Read " INT32_FORMAT_SPEC " bytes from %s:\n", ret, desc());
         LogHexBytes(MUSCLE_LOG_TRACE, buf, ret);
     
         ByteBufferRef toNetworkBuf = GetByteBufferFromPool(ret, buf);
         if (toNetworkBuf()) (void) outQ.AddTail(toNetworkBuf); 
      }
      else if (ret < 0) {LogTime(MUSCLE_LOG_ERROR, "Error, readIO.Read() returned %i\n", ret); return B_ERROR;}
   }
   return B_NO_ERROR;
}
Exemplo n.º 5
0
   virtual void InternalThreadEntry()
   {
      char buf[128]; muscleSprintf(buf, "TestThread-%p", this);
      const String prefix = buf;
      Queue<TestItemRef> q;
      bool keepGoing = 1;
      uint32 counter = 0;
      while(keepGoing)
      {
         uint32 x = rand() % 10000;
         while(q.GetNumItems() < x) 
         {
            TestItemRef tRef(_pool.ObtainObject());
            if (tRef())
            {
               char buf[128]; muscleSprintf(buf, "-" UINT32_FORMAT_SPEC, ++counter);
               tRef()->SetName(prefix+buf);
               q.AddTail(tRef);
            }
            else WARN_OUT_OF_MEMORY; 
         }
         while(q.GetNumItems() > x) q.RemoveTail();

         // Check to make sure no other threads are overwriting our objects
         for (uint32 i=0; i<q.GetNumItems(); i++) 
         {
            if (q[i]()->GetName().StartsWith(prefix) == false)
            {
               printf("ERROR, thread %p expected prefix [%s], saw [%s] at position " INT32_FORMAT_SPEC "/" UINT32_FORMAT_SPEC "\n", this, prefix(), q[i]()->GetName()(), i, q.GetNumItems());
               ExitWithoutCleanup(10);
            }
         }
 
         // Check to see if the main thread wants us to exit yet
         MessageRef r;
         while(WaitForNextMessageFromOwner(r, 0) >= 0) if (r() == NULL) keepGoing = false;
      }
   }
Exemplo n.º 6
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.º 7
0
// This program exercises the Queue class.
int main(void) 
{
   CompleteSetupSystem css;  // needed for string-count stats

#ifdef TEST_SWAP_METHOD
   while(1)
   {
      char qs1[512]; printf("Enter q1: "); fflush(stdout); if (fgets(qs1, sizeof(qs1), stdin) == NULL) qs1[0] = '\0';
      char qs2[512]; printf("Enter q2: "); fflush(stdout); if (fgets(qs2, sizeof(qs2), stdin) == NULL) qs2[0] = '\0';

      Queue<int> q1, q2;      
      StringTokenizer t1(qs1), t2(qs2);
      const char * s;
      while((s = t1()) != NULL) q1.AddTail(atoi(s));
      while((s = t2()) != NULL) q2.AddTail(atoi(s));
      printf("T1Before="); PrintToStream(q1);
      printf("T2Before="); PrintToStream(q2);
      q1.SwapContents(q2);
      printf("T1After="); PrintToStream(q1);
      printf("T2After="); PrintToStream(q2);
   }
#endif

#ifdef MUSCLE_USE_CPLUSPLUS11
   {
      Queue<int> q {1,2,3,4,5};
      if (q.GetNumItems() != 5) {printf("Oh no, initialize list constructor didn't work!\n"); exit(10);}
      q = {6,7,8,9,10,11};
      if (q.GetNumItems() != 6) {printf("Oh no, initialize list assignment operator didn't work!\n"); exit(10);}
   }
#endif

   // Test muscleSwap()
   {
      Queue<String> q1, q2;
      q1.AddTail("q1");
      q2.AddTail("q2");
      muscleSwap(q1, q2);
      if ((q1.GetNumItems() != 1)||(q2.GetNumItems() != 1)||(q1[0] != "q2")||(q2[0] != "q1"))
      {
         printf("Oh no, muscleSwap is broken for Message objects!\n");
         exit(10);
      }
      printf("muscleSwap() worked!\n");
   }


   const int testSize = 15;
   Queue<int> q;

   int vars[] = {5,6,7,8,9,10,11,12,13,14,15};

   printf("ADDTAIL TEST\n");
   {
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.AddTail(i));
         printf("len=" UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC"\n", q.GetNumItems(), q.GetNumAllocatedItemSlots());
      }
   } 

   printf("AddTail array\n");
   q.AddTailMulti(vars, ARRAYITEMS(vars));
   PrintToStream(q);

   printf("AddHead array\n");
   q.AddHeadMulti(vars, ARRAYITEMS(vars));
   PrintToStream(q);

   printf("REPLACEITEMAT TEST\n");
   {
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.ReplaceItemAt(i, i+10));
         PrintToStream(q);
      }
   } 

   printf("INSERTITEMAT TEST\n");
   {
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.InsertItemAt(i,i));
         PrintToStream(q);
      }
   }

   printf("REMOVEITEMAT TEST\n");
   {
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.RemoveItemAt(i));
         PrintToStream(q);
      }
   }

   // Check that C++11's move semantics aren't stealing values they shouldn't
   {
      Queue<String> q;
      String myStr = "Magic";
      q.AddTail(myStr);
      if (myStr != "Magic") 
      {
         printf("Error, AddTail() stole my string!\n");
         exit(10);
      }
   }

   printf("SORT TEST 1\n");
   {
      q.Clear();
      for (int i=0; i<testSize; i++)
      {
         int next = rand()%255;
         TEST(q.AddTail(next));
         printf("Added item %i = %i\n", i, q[i]);
      }
      printf("sorting ints...\n");
      q.Sort();
      for (int j=0; j<testSize; j++) printf("Now item %i = %i\n", j, q[j]);
   }

   printf("SORT TEST 2\n");
   {
      Queue<String> q2;
      for (int i=0; i<testSize; i++)
      {
         int next = rand()%255;
         char buf[64];
         sprintf(buf, "%i", next);
         TEST(q2.AddTail(buf));
         printf("Added item %i = %s\n", i, q2[i].Cstr());
      }
      printf("sorting strings...\n");
      q2.Sort();
      for (int j=0; j<testSize; j++) printf("Now item %i = %s\n", j, q2[j].Cstr());
   }

   printf("REMOVE DUPLICATES test\n");
   {
      Queue<int> q;
      const int vars[] = {9,2,3,5,8,3,5,6,6,7,2,3,4,6,8,9,3,5,6,4,3,2,1};
      if (q.AddTailMulti(vars, ARRAYITEMS(vars)) == B_NO_ERROR)
      {
         q.RemoveDuplicateItems(); 
         for (uint32 i=0; i<q.GetNumItems(); i++) printf("%u ", q[i]); printf("\n");
      }
   }

   {
      const uint32 NUM_ITEMS = 1000000;
      const uint32 NUM_RUNS  = 3;
      Queue<int> q; (void) q.EnsureSize(NUM_ITEMS, true);
      double tally = 0.0;
      for (uint32 t=0; t<NUM_RUNS; t++)
      {
         printf("SORT SPEED TEST ROUND " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC":\n", t+1, NUM_RUNS);

         srand(0); for (uint32 i=0; i<NUM_ITEMS; i++) q[i] = rand();  // we want this to be repeatable, hence srand(0)
         
         uint64 startTime = GetRunTime64();
         q.Sort();
         uint64 elapsed = (GetRunTime64()-startTime);

         double itemsPerSecond = ((double)NUM_ITEMS*((double)MICROS_PER_SECOND))/(elapsed);
         printf("   It took " UINT64_FORMAT_SPEC" microseconds to sort " UINT32_FORMAT_SPEC" items, so we sorted %f items per second\n", elapsed, NUM_ITEMS, itemsPerSecond);
         tally += itemsPerSecond;
      }
      printf("GRAND AVERAGE ITEMS PER SECOND WAS %f items per second\n", tally/NUM_RUNS);
   }

   PrintAndClearStringCopyCounts("Before String Sort Tests");
   {
      const uint32 NUM_ITEMS = 1000000;
      const uint32 NUM_RUNS  = 3;
      Queue<String> q; (void) q.EnsureSize(NUM_ITEMS, true);
      double tally = 0.0;
      for (uint32 t=0; t<NUM_RUNS; t++)
      {
         printf("STRING SORT SPEED TEST ROUND " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC":\n", t+1, NUM_RUNS);

         srand(0); for (uint32 i=0; i<NUM_ITEMS; i++) q[i] = String("FooBarBaz-%1").Arg(rand()).Pad(500);  // we want this to be repeatable, hence srand(0)
         
         uint64 startTime = GetRunTime64();
         q.Sort();
         uint64 elapsed = (GetRunTime64()-startTime);

         double itemsPerSecond = ((double)NUM_ITEMS*((double)MICROS_PER_SECOND))/(elapsed);
         printf("   It took " UINT64_FORMAT_SPEC" microseconds to sort " UINT32_FORMAT_SPEC" items, so we sorted %f items per second\n", elapsed, NUM_ITEMS, itemsPerSecond);
         tally += itemsPerSecond;
      }
      printf("STRING GRAND AVERAGE ITEMS PER SECOND WAS %f items per second\n", tally/NUM_RUNS);
   }
   PrintAndClearStringCopyCounts("After String Sort Tests");

   printf("REVERSE TEST\n");
   {
      q.Clear();
      for (int i=0; i<testSize; i++) TEST(q.AddTail(i));
      q.ReverseItemOrdering();
      for (int j=0; j<testSize; j++) printf("After reverse, %i->%i\n", j, q[j]);
   }

   printf("CONCAT TEST 1\n");
   {
      q.Clear();
      Queue<int> q2;
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.AddTail(i));
         TEST(q2.AddTail(i+100));
      }
      q.AddTailMulti(q2);
      for (uint32 j=0; j<q.GetNumItems(); j++) printf("After concat, " UINT32_FORMAT_SPEC"->%i\n", j, q[j]);
   }

   printf("CONCAT TEST 2\n");
   {
      q.Clear();
      Queue<int> q2;
      for (int i=0; i<testSize; i++) 
      {
         TEST(q.AddTail(i));
         TEST(q2.AddTail(i+100));
      }
      q.AddHeadMulti(q2);
      for (uint32 j=0; j<q.GetNumItems(); j++) printf("After concat, " UINT32_FORMAT_SPEC"->%i\n", j, q[j]);
   }
   {
      printf("GetArrayPointer() test\n");
      uint32 len = 0;
      int * a;
      for (uint32 i=0; (a = q.GetArrayPointer(i, len)) != NULL; i++)
      {
         printf("SubArray " UINT32_FORMAT_SPEC": " UINT32_FORMAT_SPEC" items: ", i, len);
         for (uint32 j=0; j<len; j++) printf("%i, ", a[j]);
         printf("\n");
      }
   }

   printf("\nStress-testing Queue::Normalize()... this may take a minute\n");
   for (uint32 i=0; i<20000; i++)
   {
      Queue<int> q;
      int counter = 0;
      for (uint32 j=0; j<i; j++)
      {
         switch(rand()%6)
         {
            case 0:  case 1: q.AddTail(counter++); break;
            case 2:  case 3: q.AddHead(counter++); break;
            case 4:          q.RemoveHead();       break;
            case 5:          q.RemoveTail();       break;
         }
      }

      int * compareArray = new int[q.GetNumItems()];
      for (uint32 j=0; j<q.GetNumItems(); j++) compareArray[j] = q[j];
      q.Normalize();

      const int * a = q.HeadPointer();
      if (memcmp(compareArray, a, q.GetNumItems()*sizeof(int)))
      {
         printf("ERROR IN NORMALIZE!\n");
         for (uint32 i=0; i<q.GetNumItems(); i++) printf("   Expected %i, got %i (qi=%i at " UINT32_FORMAT_SPEC"/" UINT32_FORMAT_SPEC")\n", compareArray[i], a[i], q[i], i, q.GetNumItems());
         MCRASH("ERROR IN NORMALIZE!");
      }

      delete [] compareArray;
   }
   printf("Queue test complete.\n");

   return 0;
}
Exemplo n.º 8
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;
}