home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / NBP Sample / atNBP.c next >
Encoding:
C/C++ Source or Header  |  1990-11-11  |  4.1 KB  |  158 lines  |  [TEXT/KAHL]

  1. #include <nAppletalk.h>
  2. #include <stdio.h>
  3.  
  4. /* Sample code - how to use the preferred appletalk interface
  5.     with Think C 4.0
  6.     
  7.     
  8.     Name Binding Protocol Tricks and Traps
  9.     
  10.     By J. Laurens Troost
  11.     (212) 988-8213
  12.     
  13.     internet: jtt@cunixd.columbia.edu (thanks, James!)
  14.                                                ^^^^^^
  15.                                                don't overload him.
  16.     or
  17.         NYMUG bbs (212)645-9484 or 9509 mail to 'Rens Troost'
  18.         
  19.     flames > dev/null
  20.  
  21.     Disclaimer: this is just a hack. It works, though!
  22.     
  23.     documentation in IM is BAD, and this is not much better, but
  24.     at least it's working source.
  25. */
  26.  
  27. #define MAXNAMES 15
  28. /* maximum # of names to return */
  29.  
  30. main()
  31. {
  32.  
  33.     MPPParamBlock    pblock;
  34.     OSErr            errCode;
  35.     Str32            objStr,typeStr,zoneStr;
  36.     EntityName        it;
  37.     char            myEntity[150],anotherEntity[150],doomedEntity[150];    
  38.     short            bufSize = 20000;
  39.     char            buffer[20000];/* really big buffer! */
  40.     Byte            savedSelfSend,aSocket=80;
  41.     int                count=0;
  42.     AddrBlock        address;
  43.     Boolean            restore = TRUE, remove = TRUE;
  44.     
  45.     ToolBoxInit(); /* console is convinced this is necessary */
  46.     
  47.     printf("\n");
  48.     
  49.     if(errCode = MPPOpen()) { /* open the driver, or face the music*/
  50.         puts("error opening driver. Click mouse to exit");
  51.         while (!Button());
  52.         exit(0);
  53.     }
  54.     puts("driver opened");
  55.     
  56.     /*** enable self-send ***/
  57.     pblock.MPPnewSelfFlag = 1;                    /* non - zero value for TRUE */
  58.     if (!PSetSelfSend((Ptr)&pblock,FALSE)) {
  59.         savedSelfSend = pblock.MPPoldSelfFlag;     /* let's be nice and restore this*/
  60.         puts("self-send enabled");
  61.     }
  62.     else restore = FALSE;
  63.     
  64.     strcpy((char *)objStr,"\pDorkus the Mystic");
  65.     strcpy((char *)typeStr,"\pRJS NetScanner");
  66.     strcpy((char *)zoneStr,"\p*");
  67.     NBPSetNTE((Ptr)&myEntity,objStr,typeStr,zoneStr,aSocket);
  68.     /** SetNTE for name registration, SetEntity for lookup and removal.
  69.     DO NOT FILL THE FIELDS IN DIRECTLY or they will bite you! (see TN) */
  70.     
  71.     
  72.     /***** note - you cannot run more than one of these at once,
  73.     since the name is hard-coded. Unique names required. If you feel
  74.     ambitious replace objStr with the choosername. */
  75.     
  76.     pblock.MPPinterval = 8;
  77.     pblock.MPPcount = 3;
  78.     pblock.MPPentityPtr = (Ptr)&myEntity;
  79.     pblock.MPPverifyFlag = TRUE; /* ALWAYS verify, or NBPLookup chokes */
  80.     puts("name check commenced");
  81.     if (errCode = PRegisterName(&pblock,FALSE)) {
  82.         printf("Error number %d registering name. Click mouse.\n", errCode);
  83.         while (!Button());
  84.         if (errCode != nbpDuplicate)
  85.             exit(0);
  86.         else
  87.             puts("someone else is checking you out! continuing...");
  88.     }
  89.     if (!errCode) puts("name registered"); else remove = FALSE;
  90.     
  91.     NBPSetEntity((Ptr)&doomedEntity,objStr,typeStr,zoneStr);
  92.         
  93.     strcpy((char *)objStr,"\p=");
  94.     strcpy((char *)typeStr,"\p=");
  95.     strcpy((char *)zoneStr,"\p*");
  96.     NBPSetEntity((Ptr)&anotherEntity,objStr,typeStr,zoneStr);
  97.     /* we must not overwrite this NVE's name info, so use another. */
  98.     
  99.     pblock.MPPinterval = 8;
  100.     pblock.MPPcount = 10;
  101.     pblock.MPPentityPtr = (Ptr)&anotherEntity;
  102.     pblock.MPPretBuffPtr =(Ptr)&buffer;
  103.     pblock.MPPretBuffSize =bufSize;
  104.     pblock.MPPmaxToGet = MAXNAMES;
  105.     pblock.MPPnumGotten = 0;
  106.     
  107.     puts("click mouse for lookup.");
  108.     while(!Button()) ;
  109.     
  110.     puts("looking up names");
  111.     if (errCode = PLookupName((Ptr)&pblock,FALSE)) {
  112.         printf("Error number %d looking up names. Click mouse to exit.\n", errCode);
  113.         while (!Button());
  114.         exit(0);
  115.     }
  116.     
  117.     printf(" %d names gotten!\n\n",pblock.MPPnumGotten);
  118.     for (count =1;count<=pblock.MPPnumGotten;count++){
  119.     if (NBPExtract((Ptr)&buffer,pblock.MPPnumGotten,count,&it, &address))
  120.         puts("Out of bounds. Goalie kick (click).");
  121.     else
  122.         printf("%#s:%#s@%#s\n",it.objStr,it.typeStr,it.zoneStr);
  123.     }
  124.  
  125.     if (remove) {
  126.         pblock.MPPinterval = 8;
  127.         pblock.MPPcount = 3;
  128.         pblock.MPPentityPtr = (Ptr)&doomedEntity;
  129.         if (errCode = PRemoveName((Ptr)&pblock,FALSE))    {
  130.             printf("\nError number %d removing name.\n", errCode);
  131.             while (!Button());
  132.             exit(0);    
  133.         }
  134.         puts("\nname removed");
  135.     }
  136.     
  137.     if (restore) {
  138.         pblock.MPPnewSelfFlag = savedSelfSend;
  139.         PSetSelfSend((Ptr)&pblock,FALSE);
  140.         puts("self-send restored\n");
  141.     }
  142.  
  143.     puts("Sucess!");
  144.     while (!Button());
  145. }
  146.  
  147. ToolBoxInit()
  148. {
  149.         InitGraf( &thePort);
  150.         InitFonts();
  151.         FlushEvents ( everyEvent, 0 );
  152.         InitWindows();
  153.         InitMenus();
  154.         TEInit();
  155.         InitDialogs( 0L );
  156.         InitCursor();
  157. }
  158.