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

  1. #include "TR_Request.h"
  2. #include <iostream>
  3. #include "TR_NetworkTypes.h"
  4. #include "TR_Buffer.h"
  5. #include <stdlib.h>
  6.  
  7.  
  8. inline long    abs(long inValue)
  9. {
  10.     return (inValue < 0)?(-inValue):inValue;
  11. }
  12.  
  13.  
  14. typedef struct {
  15.     AddrBlock        address;
  16.     EntityName        name;
  17. }AddrName;
  18.  
  19.  
  20. static UInt32    DoMenu();
  21. static void        DoMath(UInt32    operation);
  22. static OSErr    NBP_Lookup(EntityName* lookup,short *maxToGet,AddrName** result);
  23. static void    SubmitTransaction(TR_NetworkAddress&    inAddress,UInt32 operation,
  24.                                 UInt32 param1,UInt32 param2);
  25.  
  26. static short LookupServers(TR_NetworkAddress*& outNameList);
  27.  
  28.  
  29. void main()
  30. {
  31.     cout << "Init" << endl;
  32.  
  33.     TR_NetworkAddress* list;
  34.     
  35.     // locate server address and submit transaction
  36.     
  37.     short num = LookupServers(list);
  38.     for(short z = 0;z<num;z++){
  39.         cout << "Send: " << z << endl;
  40.         SubmitTransaction(list[z],0,0,0);        
  41.     
  42.     }
  43.  
  44.  
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. static short LookupServers(TR_NetworkAddress*& outNameList)
  53. {
  54.     Boolean        nameIsGoodQ = false;
  55.     
  56.     // use NBP to locate the "first" "Math Server" that we find
  57.     // then fill in the TR_NetworkName with that information
  58.     // you would probably use somekind of "Network picker" to get the name
  59.     
  60.     EntityName    theName;
  61.     PLstrcpy(theName.objStr,"\p=");
  62.     PLstrcpy(theName.typeStr,"\pCDTrayServer");
  63.     PLstrcpy(theName.zoneStr,"\p*");
  64.     
  65.     short            maxToGet = 100;
  66.     AddrName        resultNames[100];
  67.     AddrName*        result = resultNames;
  68.     
  69.     if(NBP_Lookup(&theName,&maxToGet,&result) == noErr){
  70.         outNameList = new TR_NetworkAddress[maxToGet];
  71.         for(short z = 0;z<maxToGet;z++){
  72.             outNameList[z].InitializeFromATK(resultNames[z].address.aNet,resultNames[z].address.aNode,resultNames[z].address.aSocket);
  73.  
  74.             
  75.         }
  76.     }else{
  77.         maxToGet = 0;
  78.     }
  79.     
  80.     return maxToGet;
  81. }
  82.  
  83.  
  84.  
  85.  
  86. static void    SubmitTransaction(TR_NetworkAddress&    inAddress,UInt32 operation,
  87.                                 UInt32 param1,UInt32 param2)
  88. {
  89.     PO_Ref<TR_Buffer>        requestData;
  90.     PO_Ref<TR_Buffer>        responseData;
  91.     
  92.     // create request data buffer
  93.     // since we are doing the transaction sync, we can
  94.     // just use the default allocator
  95.     
  96.     requestData = TR_Buffer::New(1);
  97.     if(!requestData.IsNullQ()){
  98.     
  99.         // fill the params into the request buffer
  100.         
  101.         bool*        param = (bool*) requestData->GetBuffer();
  102.         *param = operation;
  103.  
  104.         
  105.         // create a buffer to hold the response
  106.         // again, since this is going to be async,
  107.         // we can use the default allocator
  108.         
  109.         responseData = TR_Buffer::New(sizeof(UInt32));
  110.         if(!responseData.IsNullQ()){
  111.     
  112.             // now, ask the factory to start the request
  113.             // again, we use the default allocator
  114.             
  115.             TR_OutgoingRequest*        request = TR_RequestFactory::New(inAddress,requestData,responseData);
  116.             if(request != NULL){
  117.             
  118.                 // wait until the request is complete
  119.             
  120.                 request->WaitUntilComplete();
  121.                 
  122.                 // find out if it was sucessful or not
  123.                 
  124.                 OSStatus    err = request->GetResult();
  125.                 if(err == noErr){
  126.                     
  127.                 }else{
  128.                     cout << "Error: " << err << endl << endl;
  129.                 }
  130.                 
  131.                 // we are done with the request, just delete it.
  132.                 // we don't have to worry about the buffers since they are
  133.                 // ref counted
  134.                 
  135.                 delete request;
  136.             }
  137.         }        
  138.     }
  139. }
  140.  
  141.  
  142.  
  143. static OSErr    NBP_Lookup(EntityName* lookup,short *maxToGet,AddrName** result)
  144. {
  145.     MPPParamBlock        pb;
  146.     char                entity[100];
  147.     char*                buffer = NULL;
  148.     long                bufferSize = (*maxToGet) * 104;
  149.     OSErr                err = noErr;
  150.     
  151.     
  152.     buffer = NewPtr(bufferSize);
  153.     err = MemError();
  154.     if(err == noErr){
  155.         NBPSetEntity(entity,lookup->objStr,lookup->typeStr,lookup->zoneStr);
  156.         pb.NBP.ioCompletion = NULL;
  157.         pb.NBPinterval = 3;
  158.         pb.NBPcount = 4;
  159.         pb.NBPentityPtr = entity;
  160.         pb.NBPretBuffPtr = buffer;
  161.         pb.NBPretBuffSize = bufferSize;
  162.         pb.NBPmaxToGet = *maxToGet;
  163.         
  164.         err = PLookupName(&pb,false);
  165.         if(err == noErr){
  166.             short        index;
  167.             AddrName*    array = *result;
  168.             *maxToGet = pb.NBPnumGotten;
  169.             if(array == NULL){
  170.                 array = (AddrName*)NewPtr(sizeof(AddrName)*pb.NBPnumGotten);
  171.                 err = MemError();
  172.                 *result = array;
  173.             }
  174.             
  175.             if(err == noErr){
  176.                 for(index = 1;index<=pb.NBPnumGotten;index++){
  177.                     NBPExtract(buffer,pb.NBPnumGotten,index,&array[index-1].name,&array[index-1].address);
  178.                 }
  179.             }
  180.         }
  181.     }
  182.         
  183.     if(buffer != NULL){
  184.         DisposePtr(buffer);
  185.     }
  186.             
  187.     return err;
  188. }
  189.  
  190.