home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / TestTools / Sources / TestArbitrator.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  5.5 KB  |  235 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestArbitrator.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __STDIO__
  11. #include <stdio.h>
  12. #endif
  13.  
  14. #ifndef __TESTARBITRATOR__
  15. #include "TestArbitrator.h"
  16. #endif
  17. #ifndef __LIBRARYMANAGERUTILITIES__
  18. #include <LibraryManagerUtilities.h>
  19. #endif
  20. #ifndef __STRING__
  21. #include <String.h>
  22. #endif
  23.  
  24. const unsigned long kMaxTokens = 40;
  25. const unsigned long kListSize = 10;
  26.  
  27. unsigned long numTokens;
  28. Boolean useFullID;
  29.  
  30.  
  31. void _CDECL OwnerHandler(void*, EventCode, OSErrParm, void* theData)
  32. {
  33.     TRequestToken* requestToken = ((TTokenNotification*) theData)->GetRequestToken();
  34.     TToken* theToken = ((TTokenNotification*) theData)->GetToken();
  35.  
  36.     TTestArbitrator* theTester = (TTestArbitrator*) theToken->GetObject();
  37.     theTester->SetOwnerHandlerCalled();
  38.     
  39.     requestToken->Give(theToken);    // give up "our" token
  40. }
  41.  
  42. void _CDECL RequestHandler(void*, EventCode, OSErrParm, void* theData)
  43. {
  44.     TRequestToken* requestToken = ((TTokenNotification*) theData)->GetRequestToken();
  45.     TToken* theToken = (TToken*) requestToken->GetObject();
  46.  
  47.     TTestArbitrator* theTester = (TTestArbitrator*) theToken->GetObject();
  48.     theTester->SetRequestHandlerCalled();
  49. }
  50.  
  51. /**********************************************************************
  52. ** PUBLIC Constructor/Destructor
  53. ***********************************************************************/
  54.  
  55. TTestArbitrator::TTestArbitrator()
  56. {
  57.     fTest = NULL;
  58.     fCount = 0;
  59. }
  60.  
  61. TTestArbitrator::~TTestArbitrator()
  62. {
  63.     delete fTest;
  64. }
  65.  
  66. /*******************************************************************************
  67. ** PUBLIC InitTest
  68. ********************************************************************************/
  69.  
  70. void TTestArbitrator::InitTest(BooleanParm, BooleanParm, int argc, char** argv)
  71. {
  72.     fTest = new TArbitrator(GetLocalPool(), (size_t) kListSize);
  73.     numTokens = kMaxTokens;
  74.     useFullID = false;
  75.  
  76.     while (argc > 0)
  77.     {
  78.         if ((argc >= 2) && (strcmp(*argv,"-numtokens") == 0))
  79.         {
  80.             argv++; argc--;
  81.             sscanf(*argv, "%d", &numTokens);
  82.             if (numTokens > kMaxTokens)
  83.                 numTokens = kMaxTokens;
  84.         }
  85.         else if (strcmp(*argv,"-fullid") == 0)
  86.         {
  87.             useFullID = true;
  88.         }
  89.         else
  90.         {
  91.             Printf("Usage: TestArbitrator -o -numtokens <n> -fullid\n");
  92.         }
  93.         argv++;
  94.         argc--;
  95.     }
  96. }
  97.  
  98. void TTestArbitrator::RunTestIteration(BooleanParm verbose, BooleanParm)
  99. {
  100.     char myID[128];
  101.     TToken* myToken[kMaxTokens+1];
  102.     int ndx;
  103.  
  104.     fCount++;
  105.     if (verbose)
  106.         Printf("Iteration = %d\n", fCount);
  107.  
  108.     // Register numTokens tokens
  109.     for (ndx = 1; ndx <= numTokens; ndx++)
  110.     {
  111.         SLMsprintf(myID, "LPG:test$%d", ndx);
  112.         fTest->RegisterObject(myID, NULL);
  113.         if (verbose)
  114.             Printf("RegisterObject called, ndx = %d, id = %s\n", ndx, myID);
  115.     }
  116.  
  117.     // GetToken the tokens
  118.     for (ndx = 1; ndx <= numTokens; ndx++)
  119.     {
  120.         if (useFullID)
  121.             SLMsprintf(myID, "LPG:test$%d", ndx);
  122.         else
  123.             SLMsprintf(myID, "LPG:test$");
  124.  
  125.         TToken* theToken = fTest->GetToken(myID, kExclusiveTokenRequest);
  126.         myToken[ndx] = theToken;
  127.         if (theToken)
  128.         {
  129.             unsigned long hash = theToken->Hash();
  130.             if (verbose)
  131.                 Printf("GetToken myToken[%d] = %x, id = %s, Hash = %d\n", 
  132.                         ndx, myToken[ndx], theToken->GetID(), hash); 
  133.         }
  134.         else 
  135.             if (verbose)
  136.                 Printf("GetToken failed, ndx = %d.\n", ndx);
  137.     }
  138.     
  139.     // try to get them again - should fail
  140.     for (ndx = 1; ndx <= numTokens; ndx++)
  141.     {
  142.         SLMsprintf(myID, "LPG:test$%d", ndx);
  143.         TToken* theToken = fTest->GetToken(myID, kExclusiveTokenRequest);
  144.         if (theToken)
  145.         {
  146.             if (verbose)
  147.                 Printf("GetToken exclusive request succeeded again, ndx = %d - bad news\n", ndx);
  148.         }
  149.         else
  150.             if (verbose)
  151.                 Printf("GetToken exclusive request failed, ndx = %d - good news.\n", ndx);
  152.     }
  153.     
  154.     // try RequestToken to get myToken[ndx] from the current "owner"
  155.     for (ndx = 1; ndx <= numTokens; ndx++)
  156.     {
  157.         if (myToken[ndx])
  158.         {
  159.             myToken[ndx]->SetObject(this);
  160.             fRequestHandlerCalled = false;
  161.             fOwnerHandlerCalled = false;
  162.     
  163.             // Set a notifier for "LPG:test$1"
  164.             TProcNotifier ownerNotifier(OwnerHandler);
  165.             myToken[ndx]->SetNotifier(&ownerNotifier);
  166.             
  167.             // Get a notifier for the request
  168.             TProcNotifier requestNotifier(RequestHandler);
  169.             
  170.             // Request token by id
  171.             TRequestToken* myRequest = fTest->ActiveRequest(myToken[ndx]->GetID(), kExclusiveTokenRequest, &requestNotifier);
  172.             
  173.             if (myRequest)
  174.             {
  175.                 TToken* theToken = NULL;
  176.                 if (fRequestHandlerCalled)
  177.                 {
  178.                     theToken = myRequest->Exchange();
  179.                     Printf("ActiveRequest succeeded, theToken = %x, token id = %s\n", theToken, theToken->GetID());
  180.                 }
  181.                 else
  182.                 {
  183.                     delete myRequest;
  184.                     Printf("ActiveRequest failed, RequestHandler not called\n");
  185.                 }
  186.             }
  187.             else
  188.             {
  189.                 Printf("ActiveRequest failed, myRequest not created\n");
  190.             }
  191.         }
  192.     }
  193.  
  194.     // release them
  195.     for (ndx = 1; ndx <= numTokens; ndx++)
  196.     {
  197.         TToken* theToken = myToken[ndx];
  198.         if (theToken)
  199.         {
  200.             if (verbose)
  201.                 Printf("Releasing token %x, ndx = %d\n", theToken, ndx);
  202.             theToken->Release();
  203.         }
  204.     }
  205.  
  206.     // get them again - this time unregister and delete em
  207.     for (ndx = 1; ndx <= numTokens; ndx++)
  208.     {
  209.         SLMsprintf(myID, "LPG:test$%d", ndx);
  210.         TToken* myToken = fTest->GetToken(myID, kExclusiveTokenRequest);
  211.         if (myToken)
  212.         {
  213.             unsigned long hash = myToken->Hash();
  214.             if (verbose)
  215.             {
  216.                 Printf("GetToken myToken = %x, Hash = %d, ndx = %d\t", myToken, hash, ndx);
  217.                 Printf("Releasing token %x,\t", myToken);
  218.             }
  219.             myToken->Release();
  220.             if (verbose)
  221.                 Printf("Deleting %x\n", myToken);
  222.             delete myToken;
  223.         }
  224.         else
  225.             if (verbose)
  226.                 Printf("GetToken failed, ndx = %d.\n", ndx);
  227.     }
  228. } // end RunTestIteration
  229.  
  230.  
  231. void TTestArbitrator::EndTest(BooleanParm, BooleanParm)
  232. {
  233. }
  234.  
  235.