home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / CD-Rom Drive you crazy / Source / CDTrayServer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-26  |  3.7 KB  |  150 lines  |  [TEXT/CWIE]

  1. #include "CDTrayServer.h"
  2. #include "TR_NetworkTypes.h"
  3. #include "TR_Buffer.h"
  4. #include "CDTrayControl.h"
  5.  
  6. const UInt32        kRequestBufferSize = 1;        // size of expected request
  7. const UInt32        kResponseDataSize = 1;                    // size of response
  8. const UInt32        kInitialPoolSize = 5;                                // initial number of simultanious requests
  9. const UInt32        kMinPoolFree = 1;                                    // limit at which we will grow the pool
  10. const UInt32        kPoolGrowSize = 5;                                    // size we will grow the pool by
  11. const UInt32        kMaxPoolSize = 15;                                    // max size of the pool
  12.  
  13.  
  14. CDTrayServer::CDTrayServer(OSStatus& outErr)
  15.     :    fRespBufferPool(kResponseDataSize,kInitialPoolSize,kMinPoolFree,kPoolGrowSize,
  16.                         kMaxPoolSize)
  17. {
  18.     fResponder = NULL;
  19.     fActiveQ = false;
  20.     // create a name for my server
  21.     // this will use the "sharing setup" name with the NBP type of "Math Server"
  22.     
  23.     TR_NetworkName        myName("\pCDTrayServer");
  24.     
  25.     // ask the factory to construct me a responder.
  26.     
  27.     outErr = TR_ResponderFactory::New(myName,kInitialPoolSize,kMinPoolFree,kPoolGrowSize,
  28.                                             kMaxPoolSize,kRequestBufferSize,NULL,
  29.                                             fResponder);
  30.     
  31.     // if things are good, attach myself to the responder as an event receiver
  32.     
  33.     if(outErr == noErr){
  34.         fResponder->AttachEventReceiver(this);
  35.     }
  36. }
  37.  
  38.  
  39. CDTrayServer::~CDTrayServer()
  40. {
  41.     // we are closing down, so lets remove ourselves from the responder
  42.     // and delete it.
  43.     
  44.     if(fResponder != NULL){
  45.         fResponder->RemoveEventReceiver(this);
  46.         delete fResponder;
  47.     }
  48. }
  49.  
  50.  
  51.  
  52. void    CDTrayServer::Idle()
  53. {
  54.     // give the responder idle time
  55.     
  56.     if(fResponder != NULL){
  57.         fResponder->Idle();
  58.     }
  59.     
  60.     // give any objectpools idle time
  61.     // (this is being use by the TR_BufferPool object that is managing my response buffers)
  62.     
  63.     PO_ObjectPool::Idle();
  64.     
  65.     
  66.     
  67.     // NOTE if we were not processing requests when they come in,
  68.     // we could be using the TR_RequestIterator object
  69.     // to process them here....
  70.     
  71.     if(fActiveQ){
  72.         StartTrayCycle();
  73.     }else{
  74.         StopTrayCycle();
  75.     }
  76.     
  77.     IdleTray();
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. void    CDTrayServer::NotifyNewRequest(TR_Responder* inResponder,TR_RequestCookie inRequest)
  85. {
  86.     // this function gets called at interrupt when a new request comes in.
  87.     
  88.     // first off, we will ask to handle the request by trying to "commit" to it
  89.     // if we get back a non NULL pointer, then we OWN the request.
  90.     // if we get back a NULL pointer, then someone else got it already...
  91.  
  92.  
  93.     TR_IncommingRequest*    thisRequest = inResponder->CommitRequest(inRequest);
  94.     
  95.     if(thisRequest != NULL){
  96.         
  97.         // ok, we got the request, now lets fetch the associated request buffer
  98.         // we see what kind of request we are going to handle.
  99.     
  100.         PO_Ref<TR_Buffer>        reqBuffer = thisRequest->GetRequestBuffer();
  101.         
  102.         // I assert that the size of the request should be
  103.         // the size of my param structure.
  104.         
  105.         
  106.         
  107.         // ok, now cast the buffer to my param structure and examine the contents.
  108.         // we support two operations:  Adding, multiplying and fetching the "status"
  109.         // which is really just the current "pool size" of the responder.
  110.         
  111.         // for the sake of illustration, we will handle
  112.         // adding and status right here and now, but we
  113.         // will defer multiplying to a "safe" time using the
  114.         // TR_DeferredAction class
  115.         
  116.         bool*        param = (bool*) reqBuffer->GetBuffer();
  117.         
  118.         fActiveQ = *param;
  119.         
  120.         PO_Ref<TR_Buffer>        respBuffer;
  121.                     
  122.         if(fRespBufferPool.Allocate(respBuffer,1)){
  123.                     
  124.             // stuff in the result in a way that would make any "C" programmer
  125.             // proud!
  126.         
  127.             *((char*) respBuffer->GetBuffer()) = 0;
  128.             
  129.  
  130.             thisRequest->SendResponse(respBuffer,NULL,true);
  131.         }
  132.     }
  133. }
  134.  
  135.  
  136. void    CDTrayServer::NotifyResponseComplete(TR_Responder* inResponder,
  137.                             TR_IncommingRequest* inRequest,
  138.                             void* inUserData)
  139. {
  140.     // we don't care about this stuff
  141.     
  142.     (void) inResponder;
  143.     (void) inRequest;
  144.     (void) inUserData;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.