{===========================================================================================}
{	LlamaTalk Echo Server}
{	}
{	Copyright © 1994 Apple Computer, Inc.}
{	All rights reserved.}
{	}
{	Modification Status}
{	YY/MM/DD	Name			Comments}
{	95/11/14	Jim Schram		Added CodeWarrior 7 IDE & PPC Universal Headers & SIOUX support, & released as 2.0a1}
{	94/09/12	Jim Schram		Added CodeWarrior support, released as 1.0a4}
{	94/07/07	Jim Schram		Released as 1.0a3}
{	94/03/17	Jim Schram		Initial Development}
{-------------------------------------------------------------------------------------------------------------}
Program LlamaTalkEchoApp;


{$IFC NOT UNDEFINED MWERKS}
	Uses
		Resources, LlamaTalk, TextUtils, SIOUX;
{$ELSEC}
	Uses
		Resources, LlamaTalk;
{$ENDC}

	Var
		gErr: OSErr;
		gSocket: Short;
		gSocketStatus: LTSocketStatusRec;
		gSocketConfigHdl: Handle;
		gSocketAddressHdl: Handle;
		gLlamaTalkGlobals: LTGlobalsHdl;
		gToolName: Str255;
		gInDataHdl: Handle;
		gOutDataHdl: Handle;
		gEvent: EventRecord;
		gSIOUXEvent: EventRecord;
		gWindow: WindowPtr;
		gMenuKey: Long;
		gQuit: Boolean;

{-------------------------------------------------------------------------------------------------------------}

	Function N2S (theNumber: Long): Str255;
		Var
			theString: Str255;
	Begin
		NumToString(theNumber, theString);
		N2S := theString;
	End;

{-------------------------------------------------------------------------------------------------------------}

	Function TextToHex (text: Handle): Handle;
		Var
			h: CharsHandle;
			x: Long;
			i: Long;
			hexTable: Str31;
	Begin
		hexTable := '0123456789ABCDEF';
		h := Nil;
		If text <> Nil Then Begin
				h := CharsHandle(NewHandle(GetHandleSize(text) * 4 + 5));
				h^^[0] := 'H';
				h^^[1] := 'e';
				h^^[2] := 'x';
				h^^[3] := ':';
				x := 4;
				For i := 0 To GetHandleSize(text) - 1 Do Begin
						h^^[x + 0] := ' ';
						h^^[x + 1] := ' ';
						h^^[x + 2] := hexTable[Ord(CharsHandle(text)^^[i]) Div 16 + 1];
						h^^[x + 3] := hexTable[Ord(CharsHandle(text)^^[i]) Mod 16 + 1];
						x := x + 4;
					End;
				h^^[x] := Chr(0);
			End;
		TextToHex := Handle(h);
	End;

{===========================================================================================}

Begin
{$IFC NOT UNDEFINED MWERKS}
	SIOUXSettings.initializeTB := TRUE;
	SIOUXSettings.standalone := FALSE;
	SIOUXSettings.setupmenus := TRUE;
	SIOUXSettings.autocloseonquit := TRUE;
	SIOUXSettings.asktosaveonclose := FALSE;
	SIOUXSettings.showstatusline := FALSE;
	SIOUXSettings.tabspaces := 0;
	SIOUXSettings.columns := 80;
	SIOUXSettings.rows := 24;
	SIOUXSettings.toppixel := 40;
	SIOUXSettings.leftpixel := 5;
	SIOUXSettings.fontid := 1;
	SIOUXSettings.fontsize := 9;
	SIOUXSettings.fontface := 0;
{$ELSEC}
	ShowText;
{$ENDC}

	writeln('Welcome to LlamaTalk!');
	writeln('Written by Jim Schram, PIE DTS.');
	writeln('Copyright © 1994-1995 Apple Computer, Inc.  All rights reserved.');
	writeln;
	writeln('This is an echo server for the LlamaTalk sample code (for debugging).');
	writeln('It receives data, converts each byte to a hex string, and sends it back.');
	writeln;
	writeln('Yea, this interface sucks, but this IS sample code after all...');
	writeln('');
	writeln('Press Command-Period to Quit.');
	writeln;

	gLlamaTalkGlobals := Nil;
	gSocket := 0;
	gQuit := FALSE;
	gToolName := '';

	gErr := LTOpenLlamaTalk(1, gLlamaTalkGlobals);
	If gErr <> noErr Then
		writeln(concat('Error ', N2S(gErr), ' occured opening LlamaTalk!'))
	Else
		Repeat
			LTIdle(gLlamaTalkGlobals);

			If LTIsValidSocket(gLlamaTalkGlobals, gSocket) Then Begin
					gInDataHdl := LTRead(gLlamaTalkGlobals, gSocket);
					If gInDataHdl <> Nil Then Begin
							writeln('Echo!');
							gOutDataHdl := TextToHex(gInDataHdl);
							LTWrite(gLlamaTalkGlobals, gSocket, gOutDataHdl);
							DisposeHandle(gInDataHdl);
						End
					Else
						Case LTGetSocketState(gLlamaTalkGlobals, gSocket) Of
							cLTStateError:  Begin
									writeln('Socket is in error state, disposing of socket...');
									LTDisposeSocket(gLlamaTalkGlobals, gSocket);
									gSocket := 0;
								End;
						End
				End

			Else Begin
					gToolName := 'AppleTalk ADSP Tool';
					gSocketAddressHdl := GetNamedResource('LAMA', 'ADSP Address');
					gSocketConfigHdl := GetNamedResource('LAMA', 'ADSP Configuration');
					If gSocketConfigHdl = Nil Then Begin

							gToolName := 'Apple Modem Tool';
							gSocketAddressHdl := GetNamedResource('LAMA', 'MNP Address');
							gSocketConfigHdl := GetNamedResource('LAMA', 'MNP Configuration');
							If gSocketConfigHdl = Nil Then

								If LTChoose(gToolName, gSocketConfigHdl) Then
									;
						End;

{$IFC NOT UNDEFINED MWERKS}
					SIOUXSetTitle(gToolName);
{$ENDC}
					If gSocketConfigHdl <> Nil Then Begin
							gErr := LTNewSocket(gLlamaTalkGlobals, gToolName, gSocketConfigHdl, gSocketAddressHdl, 10, 10, gSocket);
							If gErr = noErr Then Begin
									writeln;
									writeln('Waiting for connection...');
									LTListen(gLlamaTalkGlobals, gSocket);
								End
							Else
								writeln(concat('Error ', N2S(gErr), ' occured creating the new LlamaTalk socket!'));
						End;
				End;

			If GetNextEvent(everyEvent, gEvent) Then Begin
					{Filter Command-Period because SIOUX will ExitToShell if it sees it, without letting us clean up.}
					If (gEvent.what = keyDown) & (BAND(gEvent.modifiers, $1000 + $0800 + $0200 + $0100) = $0100) & (BAND(gEvent.message, $000000FF) = $2E) Then		{command}
						{period}
						gQuit := TRUE;

{$IFC NOT UNDEFINED MWERKS}
					If Not gQuit Then Begin
							gSIOUXEvent := gEvent;
							If SIOUXHandleOneEvent(gSIOUXEvent) = 0 Then
								;

							{The following hacks allow us to handle a Command-Q or Menu Bar Quit within SIOUX.}

							If (gEvent.what = keyDown) & (BAND(gEvent.modifiers, $1000 + $0800 + $0200 + $0100) = $0100) Then
								gMenuKey := MenuKey(Chr(BAND(gEvent.message, $000000FF)))
							Else If (gEvent.what = mouseDown) & (FindWindow(gEvent.where, gWindow) = inMenuBar) Then
								gMenuKey := MenuChoice
							Else
								gMenuKey := 0;

							If (BSR(gMenuKey, 16) = 32001) & (BAND(gMenuKey, $0000000F) = 9) Then						{File}
				{Quit}
								gQuit := TRUE;

							HiliteMenu(0);
						End;
{$ENDC}
				End;

		Until gQuit;

	LTCloseLlamaTalk(gLlamaTalkGlobals);

{===========================================================================================}

End.