OTLookupNameTest.c

/*
    File:       OTLookupNameTest.c
 
    Contains:   A simple illustration of how to use OTLookupName.
 
    Written by:     Quinn "The Eskimo!" 
 
    Copyright:  Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                2/12/2001   MK              Carbonized and updated to CodeWarrior 6.1
                7/22/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
/////////////////////////////////////////////////////////////////////
// Carbon.h
 
#include <Carbon.h>
 
/////////////////////////////////////////////////////////////////////
// Standard C prototypes
 
#include <stdio.h>
 
/////////////////////////////////////////////////////////////////////
 
static UInt32 gLastPrinted = 0;
 
 
static void MoreAssertQ(Boolean mustBeTrue)
{
    if ( ! mustBeTrue ) {
        DebugStr("\pMoreAssertQ: Assertion failure.");
    }
}
 
 
static pascal void YieldingNotifier(EndpointRef ep, OTEventCode code, 
                                       OTResult result, void* cookie)
    // This notifier calls printf on a period basis in response
    // to a kOTSyncIdleEvent.  By the wonders of SIOUX, printf calls
    // WaitNextEvent for us, and hence our synchronous calls to 
    // OT will yield to other processes.
{
    #pragma unused(ep)
    #pragma unused(result)
    #pragma unused(cookie)
    
    switch (code) {
        case kOTSyncIdleEvent:
            if (TickCount() > gLastPrinted + 10) {
                printf(".");
                fflush(stdout);
                gLastPrinted = TickCount();
            }
            break;
        default:
            // do nothing
            break;
    }
}
 
/////////////////////////////////////////////////////////////////////
 
static void PrintAddress( DDPAddress *addr )
    // Prints a DDPAddress in a nicely formatted manner.
{
    MoreAssertQ( addr->fAddressType == AF_ATALK_DDP );
    printf("Net = $%04x, Node = $%02x, Socket = $%02x ", 
                addr->fNetwork, 
                addr->fNodeID, 
                addr->fSocket);
}
 
static void PrintName(const char *name, UInt32 length)
    // Prints on NBP name in a nicely formatted manner.
{
    char nameForPrinting[256];
    
    OTMemzero(nameForPrinting, 256);
    OTMemcpy(nameForPrinting, name, length);
 
    printf("Ò%sÓ", nameForPrinting);
}
 
enum {
    kResponseBufferSize = 8192
};
 
static OSStatus LookupAndPrint(char *requestAddress)
    // requestAddress must be an NBP address of the form
    // <name>:<type>@<zone>.  This routine opens an NBP mapper
    // provider, switches it into synchronous/blocking mode
    // and uses sync idle events to yield time to other processes.
    // It then issues an NBP lookup request using OTLookupName.
    // When the request completes, it prints out the resulting
    // addresses and names.
{
    OSStatus err;
    OSStatus junk;
    MapperRef nbpMapper;
    TLookupRequest lookupRequest;
    TLookupReply lookupReply;
    void *responseBuffer;
    TLookupBuffer *currentLookupReplyBuffer;
    UInt32 nameIndex;
    
    err = noErr;
    nbpMapper = kOTInvalidMapperRef;
    
    // Create the responseBuffer.
    
    responseBuffer = OTAllocMemInContext(kResponseBufferSize, NULL);
    if (responseBuffer == nil) {
        err = kENOMEMErr;
    }
    
    // Create an NBP mapper and set it to up for threaded processing.
    
    if (err == noErr) {
        nbpMapper = OTOpenMapperInContext(OTCreateConfiguration(kNBPName), 0, &err, NULL);
    }
    if (err == noErr) {
        junk = OTSetSynchronous(nbpMapper);
        MoreAssertQ(junk == noErr);
        junk = OTSetBlocking(nbpMapper);
        MoreAssertQ(junk == noErr);
        junk = OTUseSyncIdleEvents(nbpMapper, true);
        MoreAssertQ(junk == noErr);
        junk = OTInstallNotifier(nbpMapper, YieldingNotifier, nil);
        MoreAssertQ(junk == noErr);
    }
    
    // Issue the OTLookupName synchronously.
    
    if (err == noErr) {
        
        // Set up the TLookupRequest structure.
 
        OTMemzero(&lookupRequest, sizeof(lookupRequest));
        lookupRequest.name.buf = (UInt8 *) requestAddress;
        lookupRequest.name.len = OTStrLength(requestAddress);
        lookupRequest.timeout = 1000;                       // 1 second in milliseconds
        lookupRequest.maxcnt = kResponseBufferSize / kNBPEntityBufferSize;
 
        // Set up the TLookupReply structure.
        
        OTMemzero(&lookupReply, sizeof(lookupReply));
        lookupReply.names.buf = responseBuffer;
        lookupReply.names.maxlen = kResponseBufferSize;
        
        // Call OT synchronously.
        
        err = OTLookupName(nbpMapper, &lookupRequest, &lookupReply);
    }
    
    // Print out the contents of the responseBuffer.
    
    if (err == noErr) {
        printf("\n");
        
        // Start by pointing currentLookupReplyBuffer to point to the
        // beginning of the response buffer.
        
        currentLookupReplyBuffer = (TLookupBuffer *) responseBuffer;
        
        // For each response in the buffer...
        
        for (nameIndex = 0; nameIndex < lookupReply.rspcount; nameIndex++) {
 
            // ... print the name and address and...
        
            printf("%3d ", (int) nameIndex);
            PrintAddress( (DDPAddress *) &currentLookupReplyBuffer->fAddressBuffer[0]);
            PrintName( (char *) &currentLookupReplyBuffer->fAddressBuffer[currentLookupReplyBuffer->fAddressLength], currentLookupReplyBuffer->fNameLength);
            printf("\n");
            
            // ... use OTNextLookupBuffer to get from the current buffer to the next.
            
            currentLookupReplyBuffer = OTNextLookupBuffer(currentLookupReplyBuffer);
        }
    }
    
    // Clean up.
    
    if (responseBuffer != nil) {
        OTFreeMem(responseBuffer);
    }
    if (nbpMapper != kOTInvalidMapperRef) {
        junk = OTCloseProvider(nbpMapper);
        MoreAssertQ(junk == noErr);
    }
    
    return (err);
}
 
/////////////////////////////////////////////////////////////////////
 
int main(void)
{
    OSStatus err = 0;
    char requestAddress[] = "=:AFPServer@*";
    
    printf("Hello Cruel World!\n");
    
    err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
    
    if (err == noErr) {
    
        err = LookupAndPrint(requestAddress);
        
        CloseOpenTransportInContext(NULL);
    }
    
    if (err == noErr) {
        printf("Success.\n");
    } else {
        printf("Failed with error %d.\n", (int)err);
    }
    printf("Done.  Press command-Q to Quit.\n");
        
    return err;
}