//
//	Xanthus
//	Copyright (c) 1992, 1993, 1994 Xanthus International AB.  
//	All rights reserved. 
//

#import "app.h"

static int Debug = 1;

@implementation FD_AppDelegate

+ initialize;
{
	const char *v = NXGetDefaultValue([NXApp appName], "FeedDemoDebug");
	
	if(v == NULL) return self;	// Use compiled value
	
	Debug = (v != NULL && strcasecmp(v, "Yes") == 0);
	
	if(Debug)
		NXLogError("NOTE: Debugging turned on!");
	
	return self;
}

- reportError: (const char * ) format, ...;
{
	va_list a; 
	char alert[2048];

	va_start(a, format);
	vsprintf(alert, format, a);
	va_end(a);
	
	NXRunAlertPanel("Error", alert, "OK", NULL, NULL);
	
	return self;
}

//
// Startup
//

- appWillInit:sender;
{
	theClients = [[List allocFromZone:[self zone]] init];
	
	[self startServer];
	
	return self;
}

- startServer;
{
	id c;
	
	if(Debug)
		NXLogError("Listening on '%s'!", FEED_SERVER);

	c = [NXConnection registerRoot: self
				withName: FEED_SERVER];
	[c runFromAppKit];	

	return self;
}

// 
// Admin
//

- findValueCellForVariable: (const char *)name;
{
	int i, size;
	id valueCells = [theValues cellList];
	id varCells = [theVariables cellList];
	id valueCell;
	id varCell;
	
	size = [varCells count];
	for(i=0; i<size; i++) {

		varCell = [varCells objectAt: i];
		valueCell = [valueCells objectAt: i];
		
		if(strcmp([varCell stringValue], name) == 0)
			return valueCell;

	}
	
	return nil;
}

//
// API
//

- (int)getDoubleValue: (double *)aDoubleStar
	forVariable: (const char *)name;
{	
	id cell = [self findValueCellForVariable: name];
	
	if(Debug)
		NXLogError("getDoubleValue for '%s'.", name);

	if(cell == nil) return 0;
	
	*aDoubleStar = [cell doubleValue];
	
	return 1;	
}

- senderIsInvalid:sender;
{
	int i, size = [theClients count];
	
	if(Debug)
		NXLogError("senderIsInvalid '%s'.", [sender name]);
		
	for(i=size-1; i>=0; i--) {

		id aClient = [theClients objectAt: i];
		
		if([aClient connectionForProxy] == sender)
			[self checkOut:aClient];

	}
	
	return self;
}

- checkIn:aClient;
{
	id connectionToClient;
	
	if(Debug)
		NXLogError("checkIn <%d> '%s'.", aClient, [aClient name]);

    	connectionToClient = [aClient connectionForProxy];
    	[connectionToClient registerForInvalidationNotification:self];
      	  
    	[aClient setProtocolForProxy:@protocol(FeedDemoClientProtocol)];

	[theClients addObjectIfAbsent:aClient];
	
	return self;
}

- checkOut:aClient;
{
	if(Debug)
		NXLogError("checkOut client <%d>", aClient);

	[theClients removeObject:aClient];
	return self;
}
 
- debug:sender;
{
	Debug = !Debug;
	return self;
}

//
// UI
//

- valueChanged:sender; 		// Tell clients to update
{
	int error = 0;
	
	NX_DURING
		[theClients makeObjectsPerform:@selector(feedDemoUpdate)];
	NX_HANDLER
		error = NXLocalHandler.code;
	NX_ENDHANDLER
	
	if(error) {
		
		NXLogError("%s: Exception %d for 'feedDemoUpdate'", 
					[self name], 
					error);
		return nil;
	}
	
	return self;		
}

- variableChanged:sender;	// Tell clients to update
{
	[self valueChanged:sender];
	return self;		
}


@end

