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 / Example Tools / Sources / TTokenExample.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  3.8 KB  |  107 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TTokenExample.cp
  3.  
  4.     Contains:    This module shows an example use of the TToken class
  5.  
  6.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11.  
  12. #include "TTokenExample.h"
  13.  
  14. ///————————————————————————————————————————————————————————————————————————————————————
  15. ///    CONSTANTS
  16. ///————————————————————————————————————————————————————————————————————————————————————
  17.  
  18. // The id we will use for our buffer token
  19. #define TBufferTokenID    "exam:tokn$Buff"
  20.  
  21. ///————————————————————————————————————————————————————————————————————————————————————
  22. ///    GLOBALS
  23. ///————————————————————————————————————————————————————————————————————————————————————
  24.  
  25. TArbitrator *gArbitrator = nil;
  26.  
  27. ///————————————————————————————————————————————————————————————————————————————————————
  28. /// PROTOTYPES
  29. ///————————————————————————————————————————————————————————————————————————————————————
  30.  
  31. static void write_to_buffer( TBufferToken *theToken, char *string );
  32.  
  33. /*————————————————————————————————————————————————————————————————————————————————————
  34.     main 
  35.     
  36.     This example demonstrate the use of TToken for accessing a buffer for reading and
  37.     writting to the buffer. First, it creates and then registers the token so it can
  38.     be accessed by other clients. After being registered, it request exclusive access
  39.     to the buffer. If successful, it will write to that buffer.
  40.     
  41.     Note that usually you don't need to create a token when registering an object
  42.     with the arbitrator. The usual method is to simply call RegisterObject and let
  43.     the arbitrator create the token for you. If however, you want your token to be
  44.     more then just a simple TToken, you can subclass it and call RegisterToken
  45.     instead as is done in this example. In this example the token acts like a
  46.     semaphore to the buffer.
  47. ————————————————————————————————————————————————————————————————————————————————————*/
  48.  
  49. main() 
  50. {    
  51.     TInitSLM initLibraryManager;            // initialize the shared library manager
  52.     
  53.     if( initLibraryManager.Failed() )    // if we failed, let go home
  54.         return 1;
  55.  
  56.     TBufferToken*        mytoken = nil;
  57.     
  58.     TRY
  59.         gArbitrator = new TArbitrator;    // create an arbitration
  60.         FailNULL( gArbitrator, ErrorCode(), "Sorry failed to create an arbitrator" );
  61.         
  62.         mytoken = new TBufferToken;        // create an instance of our TBufferToken
  63.         FailNULL( mytoken, ErrorCode(), "Sorry failed to create a Token" );
  64.         
  65.         mytoken->SetID( TBufferTokenID );    // set our tokens id
  66.     
  67.         gArbitrator->RegisterToken(mytoken);// Register token with our arbitrator
  68.  
  69.         cout << "Registered the token = " << (void *)mytoken << endl;
  70.         
  71.         write_to_buffer(mytoken, "Hello "); // write to the buffer
  72.  
  73.         mytoken->Release();                // Release the token
  74.  
  75.         write_to_buffer(mytoken, " World"); // write to buffer, no problem here
  76.  
  77.         write_to_buffer(mytoken, " This should fail"); // write to buffer, this should fail
  78.     ENDTRY    
  79.  
  80.     delete mytoken;
  81.     delete gArbitrator;
  82.     return 0;
  83. }
  84.  
  85. /*————————————————————————————————————————————————————————————————————————————————————
  86.     write_to_buffer
  87.     
  88.     Get exclusive access to the token and then writes string to the buffer.
  89.     For the sake of this example we don't release the example. It is up to
  90.     the caller to do that or the next call to write_to_buffer will fail.
  91. ————————————————————————————————————————————————————————————————————————————————————*/
  92. static void write_to_buffer( TBufferToken *theToken, char *string )
  93. {
  94.     // get an exclusive access to the buffer
  95.     
  96.     TBufferToken *exctoken = (TBufferToken *)gArbitrator->GetToken( theToken->GetID(), 
  97.                             kExclusiveTokenRequest );
  98.     
  99.     if( exctoken ) {
  100.         cout << "Got exclusive access to " << TBufferTokenID" token" << endl;
  101.         exctoken->BufWrite( string );
  102.         exctoken->BufRead();            // read what we wrote so far
  103.     }
  104.     else    
  105.         cout << "Sorry, unable to get access to the token" << endl;
  106.  
  107. }