home *** CD-ROM | disk | FTP | other *** search
- #include <nAppletalk.h>
- #include <stdio.h>
-
- /* Sample code - how to use the preferred appletalk interface
- with Think C 4.0
-
-
- Name Binding Protocol Tricks and Traps
-
- By J. Laurens Troost
- (212) 988-8213
-
- internet: jtt@cunixd.columbia.edu (thanks, James!)
- ^^^^^^
- don't overload him.
- or
- NYMUG bbs (212)645-9484 or 9509 mail to 'Rens Troost'
-
- flames > dev/null
-
- Disclaimer: this is just a hack. It works, though!
-
- documentation in IM is BAD, and this is not much better, but
- at least it's working source.
- */
-
- #define MAXNAMES 15
- /* maximum # of names to return */
-
- main()
- {
-
- MPPParamBlock pblock;
- OSErr errCode;
- Str32 objStr,typeStr,zoneStr;
- EntityName it;
- char myEntity[150],anotherEntity[150],doomedEntity[150];
- short bufSize = 20000;
- char buffer[20000];/* really big buffer! */
- Byte savedSelfSend,aSocket=80;
- int count=0;
- AddrBlock address;
- Boolean restore = TRUE, remove = TRUE;
-
- ToolBoxInit(); /* console is convinced this is necessary */
-
- printf("\n");
-
- if(errCode = MPPOpen()) { /* open the driver, or face the music*/
- puts("error opening driver. Click mouse to exit");
- while (!Button());
- exit(0);
- }
- puts("driver opened");
-
- /*** enable self-send ***/
- pblock.MPPnewSelfFlag = 1; /* non - zero value for TRUE */
- if (!PSetSelfSend((Ptr)&pblock,FALSE)) {
- savedSelfSend = pblock.MPPoldSelfFlag; /* let's be nice and restore this*/
- puts("self-send enabled");
- }
- else restore = FALSE;
-
- strcpy((char *)objStr,"\pDorkus the Mystic");
- strcpy((char *)typeStr,"\pRJS NetScanner");
- strcpy((char *)zoneStr,"\p*");
- NBPSetNTE((Ptr)&myEntity,objStr,typeStr,zoneStr,aSocket);
- /** SetNTE for name registration, SetEntity for lookup and removal.
- DO NOT FILL THE FIELDS IN DIRECTLY or they will bite you! (see TN) */
-
-
- /***** note - you cannot run more than one of these at once,
- since the name is hard-coded. Unique names required. If you feel
- ambitious replace objStr with the choosername. */
-
- pblock.MPPinterval = 8;
- pblock.MPPcount = 3;
- pblock.MPPentityPtr = (Ptr)&myEntity;
- pblock.MPPverifyFlag = TRUE; /* ALWAYS verify, or NBPLookup chokes */
- puts("name check commenced");
- if (errCode = PRegisterName(&pblock,FALSE)) {
- printf("Error number %d registering name. Click mouse.\n", errCode);
- while (!Button());
- if (errCode != nbpDuplicate)
- exit(0);
- else
- puts("someone else is checking you out! continuing...");
- }
- if (!errCode) puts("name registered"); else remove = FALSE;
-
- NBPSetEntity((Ptr)&doomedEntity,objStr,typeStr,zoneStr);
-
- strcpy((char *)objStr,"\p=");
- strcpy((char *)typeStr,"\p=");
- strcpy((char *)zoneStr,"\p*");
- NBPSetEntity((Ptr)&anotherEntity,objStr,typeStr,zoneStr);
- /* we must not overwrite this NVE's name info, so use another. */
-
- pblock.MPPinterval = 8;
- pblock.MPPcount = 10;
- pblock.MPPentityPtr = (Ptr)&anotherEntity;
- pblock.MPPretBuffPtr =(Ptr)&buffer;
- pblock.MPPretBuffSize =bufSize;
- pblock.MPPmaxToGet = MAXNAMES;
- pblock.MPPnumGotten = 0;
-
- puts("click mouse for lookup.");
- while(!Button()) ;
-
- puts("looking up names");
- if (errCode = PLookupName((Ptr)&pblock,FALSE)) {
- printf("Error number %d looking up names. Click mouse to exit.\n", errCode);
- while (!Button());
- exit(0);
- }
-
- printf(" %d names gotten!\n\n",pblock.MPPnumGotten);
- for (count =1;count<=pblock.MPPnumGotten;count++){
- if (NBPExtract((Ptr)&buffer,pblock.MPPnumGotten,count,&it, &address))
- puts("Out of bounds. Goalie kick (click).");
- else
- printf("%#s:%#s@%#s\n",it.objStr,it.typeStr,it.zoneStr);
- }
-
- if (remove) {
- pblock.MPPinterval = 8;
- pblock.MPPcount = 3;
- pblock.MPPentityPtr = (Ptr)&doomedEntity;
- if (errCode = PRemoveName((Ptr)&pblock,FALSE)) {
- printf("\nError number %d removing name.\n", errCode);
- while (!Button());
- exit(0);
- }
- puts("\nname removed");
- }
-
- if (restore) {
- pblock.MPPnewSelfFlag = savedSelfSend;
- PSetSelfSend((Ptr)&pblock,FALSE);
- puts("self-send restored\n");
- }
-
- puts("Sucess!");
- while (!Button());
- }
-
- ToolBoxInit()
- {
- InitGraf( &thePort);
- InitFonts();
- FlushEvents ( everyEvent, 0 );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- }
-