home *** CD-ROM | disk | FTP | other *** search
- #include "TR_Request.h"
- #include <iostream>
- #include "TR_NetworkTypes.h"
- #include "TR_Buffer.h"
- #include <stdlib.h>
-
-
- inline long abs(long inValue)
- {
- return (inValue < 0)?(-inValue):inValue;
- }
-
-
- typedef struct {
- AddrBlock address;
- EntityName name;
- }AddrName;
-
-
- static UInt32 DoMenu();
- static void DoMath(UInt32 operation);
- static OSErr NBP_Lookup(EntityName* lookup,short *maxToGet,AddrName** result);
- static void SubmitTransaction(TR_NetworkAddress& inAddress,UInt32 operation,
- UInt32 param1,UInt32 param2);
-
- static short LookupServers(TR_NetworkAddress*& outNameList);
-
-
- void main()
- {
- cout << "Init" << endl;
-
- TR_NetworkAddress* list;
-
- // locate server address and submit transaction
-
- short num = LookupServers(list);
- for(short z = 0;z<num;z++){
- cout << "Send: " << z << endl;
- SubmitTransaction(list[z],0,0,0);
-
- }
-
-
- }
-
-
-
-
-
-
- static short LookupServers(TR_NetworkAddress*& outNameList)
- {
- Boolean nameIsGoodQ = false;
-
- // use NBP to locate the "first" "Math Server" that we find
- // then fill in the TR_NetworkName with that information
- // you would probably use somekind of "Network picker" to get the name
-
- EntityName theName;
- PLstrcpy(theName.objStr,"\p=");
- PLstrcpy(theName.typeStr,"\pCDTrayServer");
- PLstrcpy(theName.zoneStr,"\p*");
-
- short maxToGet = 100;
- AddrName resultNames[100];
- AddrName* result = resultNames;
-
- if(NBP_Lookup(&theName,&maxToGet,&result) == noErr){
- outNameList = new TR_NetworkAddress[maxToGet];
- for(short z = 0;z<maxToGet;z++){
- outNameList[z].InitializeFromATK(resultNames[z].address.aNet,resultNames[z].address.aNode,resultNames[z].address.aSocket);
-
-
- }
- }else{
- maxToGet = 0;
- }
-
- return maxToGet;
- }
-
-
-
-
- static void SubmitTransaction(TR_NetworkAddress& inAddress,UInt32 operation,
- UInt32 param1,UInt32 param2)
- {
- PO_Ref<TR_Buffer> requestData;
- PO_Ref<TR_Buffer> responseData;
-
- // create request data buffer
- // since we are doing the transaction sync, we can
- // just use the default allocator
-
- requestData = TR_Buffer::New(1);
- if(!requestData.IsNullQ()){
-
- // fill the params into the request buffer
-
- bool* param = (bool*) requestData->GetBuffer();
- *param = operation;
-
-
- // create a buffer to hold the response
- // again, since this is going to be async,
- // we can use the default allocator
-
- responseData = TR_Buffer::New(sizeof(UInt32));
- if(!responseData.IsNullQ()){
-
- // now, ask the factory to start the request
- // again, we use the default allocator
-
- TR_OutgoingRequest* request = TR_RequestFactory::New(inAddress,requestData,responseData);
- if(request != NULL){
-
- // wait until the request is complete
-
- request->WaitUntilComplete();
-
- // find out if it was sucessful or not
-
- OSStatus err = request->GetResult();
- if(err == noErr){
-
- }else{
- cout << "Error: " << err << endl << endl;
- }
-
- // we are done with the request, just delete it.
- // we don't have to worry about the buffers since they are
- // ref counted
-
- delete request;
- }
- }
- }
- }
-
-
-
- static OSErr NBP_Lookup(EntityName* lookup,short *maxToGet,AddrName** result)
- {
- MPPParamBlock pb;
- char entity[100];
- char* buffer = NULL;
- long bufferSize = (*maxToGet) * 104;
- OSErr err = noErr;
-
-
- buffer = NewPtr(bufferSize);
- err = MemError();
- if(err == noErr){
- NBPSetEntity(entity,lookup->objStr,lookup->typeStr,lookup->zoneStr);
- pb.NBP.ioCompletion = NULL;
- pb.NBPinterval = 3;
- pb.NBPcount = 4;
- pb.NBPentityPtr = entity;
- pb.NBPretBuffPtr = buffer;
- pb.NBPretBuffSize = bufferSize;
- pb.NBPmaxToGet = *maxToGet;
-
- err = PLookupName(&pb,false);
- if(err == noErr){
- short index;
- AddrName* array = *result;
- *maxToGet = pb.NBPnumGotten;
- if(array == NULL){
- array = (AddrName*)NewPtr(sizeof(AddrName)*pb.NBPnumGotten);
- err = MemError();
- *result = array;
- }
-
- if(err == noErr){
- for(index = 1;index<=pb.NBPnumGotten;index++){
- NBPExtract(buffer,pb.NBPnumGotten,index,&array[index-1].name,&array[index-1].address);
- }
- }
- }
- }
-
- if(buffer != NULL){
- DisposePtr(buffer);
- }
-
- return err;
- }
-
-