//************************************************************************
//
//	DOserver.m.  
//
//		Distributed Objects server which communicates with MODEM tool
//		during Manual dial.
//
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************

#import "GKdefs.h"
#import "DOserver.h"
#import "OptionsEditor.h"
#import "HLDelegate.h"
#import "Coordinator.h"
#import "SwapView.h"
#import <appkit/nextstd.h>
#import <remote/NXProxy.h>


@implementation DOserver

//************************************************************************
//
//		show Dialer panel 
//
//************************************************************************

- showDialPanel 
{
    if(!gatePanel)
		[NXApp loadNibSection:"Inspector.nib" owner:self withNames:NO];
    [theInspector inspectName:"FirstInspector"];		// swap inspectors
	[gatePanel setFrameUsingName:"gatePanel"];			// remem loc/size
	[gatePanel setFrameAutosaveName:"gatePanel"];		// remem loc/size
    [gatePanel makeKeyAndOrderFront:self];	
	[phoneNumField setStringValue:NXGetDefaultValue([NXApp appName], 	
														"lastNumDialed")];
	[phoneNumField selectText:self];
	
    return self;
}
//*****************************************************************************
//
// 		called when GateTool's mach port becomes invalid 
//
//*****************************************************************************

- senderIsInvalid:sender 
{
	if(gatePanel)
		[gatePanel close];
	if(!connecting)							// if not starting pppd, cleanup
		[delegate DOFinished];
	connecting = NO;						// pppd will be started yes/NO
	
    return self;
}
//*****************************************************************************
//
// 		send to the gatetool server the phone num to dial 
//
//*****************************************************************************

- Dial:sender  
{
char dialStr[56] = {"SEND "};

	shouldExit = NO;						
	strncat(dialStr, NXGetDefaultValue([NXApp appName], "dialPrefix"), 8);
	strcat(dialStr, [phoneNumField stringValue]);
		// store last num dialed in defaults database
	if(!NXWriteDefault([NXApp appName], "lastNumDialed", 
												[phoneNumField stringValue]))
		[[NXApp delegate] showAlert:"ddbWriteError"];
	[gateToolProxy enScript:strcat(dialStr, " ")];
    [theInspector inspectName:"SecondInspector"];		// swap inspectors

    return self;
}
//*****************************************************************************
//
// 		send to the gatetool server quit tip instruction 
//
//*****************************************************************************

- cancelDial:sender  
{
char minBuffer[] = {" "};
	
	shouldExit = YES;						
	minBuffer[0] = CINTR;
	[gateToolProxy writePty:minBuffer];

    return self;
}
//*****************************************************************************
//
// 		send to the gatetool server quit tip instruction 
//
//*****************************************************************************

- hangUp:sender  
{
char prefix[16] = {"PASS "}, minBuffer[3] = {"b "}, dialStr[16];
	
	minBuffer[0] = CEOF;
	strcpy(dialStr, prefix);
	[gateToolProxy enScript:strcat(dialStr, minBuffer)];
    [theInspector inspectName:"FirstInspector"];		// swap inspectors

    return self;
}
//*****************************************************************************
//
// 		quit tip and launch pppd so that it can attach to serial port 
//
//*****************************************************************************

- Attach:sender  
{
	shouldExit = YES;						
	connecting = YES;								// pppd will be started
	[gateToolProxy unlockSerialPort];
	[gateToolProxy enScript:"PASS ~. "];
	[delegate linkWithFile:NULL];	

    return self;
}
//*****************************************************************************
//
// 		self activate 
//
//*****************************************************************************

- activateGateKeeper  
{
NXConnection *	myConnection;

	[NXApp activateSelf:YES];
	gateToolProxy = [NXConnection connectToName:"GateToolServer"];
	if(gateToolProxy)			// use a protocol for DO efficiency 
		{
		[gateToolProxy setProtocolForProxy:@protocol(serverProtocol)];
				// return local NXconnection so that we can register for 	
				// invalidation of the proxy's port
		myConnection = [gateToolProxy connectionForProxy];
		[myConnection registerForInvalidationNotification:self];
		}

    return self;
}
//*****************************************************************************
//
// 		return modem initialization str to use in manual Dial 
//
//*****************************************************************************

- (const char *)modemInit  
{
char dialStr[MAXPATHLEN + 1] = {"SEND "};

	strncat(dialStr, NXGetDefaultValue([NXApp appName], DIALINIT), 
												MAXPATHLEN - strlen(dialStr));
	strncat(dialStr, " EXPECT OK ", MAXPATHLEN - strlen(dialStr));

    return dialStr;
}
//*****************************************************************************
//
// 		return the name of modem's serial port  
//
//*****************************************************************************

- (const char *)modemPort  
{
int fd;
												// if it exists
	if(fd = open("/etc/ppp/options", O_RDONLY) != -1)	
		{		
		close(fd);								// parse global options file
		[[[NXApp delegate] optionsEditor] parseOptions:"/etc/ppp/options"];
		}
								// parse selected Gatedoc options file
    return [[[[NXApp delegate] optionsEditor] parseOptions:
								[[[NXApp delegate] hotListDelegate] 
											selGateDocOptionsPath]] devName];
}
//*****************************************************************************
//
// 		return the DTE speed of the serial port  
//
//*****************************************************************************

- (const char *)modemSpeed  
{
int fd;
												// if it exists
	if(fd = open("/etc/ppp/options", O_RDONLY) != -1)	
		{		
		close(fd);								// parse global options file
		[[[NXApp delegate] optionsEditor] parseOptions:"/etc/ppp/options"];
		}
								// parse selected Gatedoc options file
    return [[[[NXApp delegate] optionsEditor] parseOptions:
								[[[NXApp delegate] hotListDelegate] 
											selGateDocOptionsPath]] speed];
}
//*****************************************************************************
//
// 		return whether MODEM should exit
//
//*****************************************************************************

- (BOOL)shouldExit 
{
    return shouldExit;
}

- setDelegate:anObject
{
    delegate = anObject;
	connecting = NO;						// pppd will be started yes/NO
	
    return self;
}

- free:sender
{
	if(gatePanel)
		{
		[gatePanel close];
		[gatePanel free];
		}

    return [super free];
}

@end

