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


#import "feed_demo.h"
#import <remote/NXProxy.h>




static int Debug = 1;



@implementation feed_demo

+ 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;
}


+ (const char *)functionName;
{
	return "feed_demo";
}

+ (const char *)categoryName;
{
	return "data_feed";
}

- (const char *)argumentNameNo:(int)index;
{
	switch(index) {
	case 0 : 
		return "variable";
	}
	
	return "<should never be seen>";
}

//
// 
//

- init;			// 3.1 CHANGE
{
	[super init];
	[[NXApp delegate] registerFeeder:self];
		
			// register myself as a feeder
	return self;
}

- free;			// 3.1 CHANGE	
{
	[[NXApp delegate] unregisterFeeder:self];
		
			// unregister myself as a feeder
			
	return [super free];
}

//
// @protocol Questor_DataFeedDelegate
//

- dataFeedDisabled:sender;		// 3.1 CHANGE
{
	if(Debug)
		NXLogError("<%s> dataFeedDisabled: ",  [self name]);
		
	// Example use: tell server to stop sending data	
		
		
	return self;
}

- dataFeedEnabled:sender;		// 3.1 CHANGE
{
	if(Debug)
		NXLogError("<%s> dataFeedEnabled: ",  [self name]);

	// Example use: tell server to start sending data	

	return self;
}


- appWillTerminate:sender;		// 3.1 CHANGE
{
	if(Debug)
		NXLogError("<%s> appWillTerminate: ",  [self name]);

	// Example use: tell server to stop sending data	

	return self;
}

//
//
//
//

- (int)minArg;
{
	return 1;
}

- (int)maxArg;
{
	return 1;
}

- (int)formalCount;
{
	return 1;
}

- (int)argType:(int)index;
{
	switch(index) {
	case 0 : 
		return XQ_EVAL;
	}
	return XQ_EVAL;
}

- (void *)runFunction: (void *)frame;
{
	double aDouble = -1;
	int argType = 0;
	const char *var = NULL;
	int ok = 1;
	int error = 0;
	
    // check argument type, should be a string
    
	argType = XQ_ArgType(frame, 0);
	if(argType != XQ_STRING_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_STRING_ARG);
	}
	
    // get name of variable
    
	var = (char *)XQ_GetStringValue(frame, 0);
	if(var == NULL) {
		
		NXLogError("<%s> No variable!", [self name]);
		return XQ_FalseValue();
	}
	
    // connect to FeedDemo server
    
	[self cacheServerProxy];
	if(theServer == nil)
		return XQ_FalseValue();
		
    // obtain value from server
    
    	NX_DURING
		
		ok = [theServer getDoubleValue: &aDouble
				forVariable: var];
		
    	NX_HANDLER
	
		error = NXLocalHandler.code;
	
	NX_ENDHANDLER
	
	if(error) {
		
		NXLogError("<%s> Exception %d.", [self name], error);
		return XQ_FalseValue();
	}
	
	if(!ok) {
		
		NXLogError("<%s> No such variable %s.", [self name], var);
		return XQ_FalseValue();
	}
	
	return XQ_CreateDoubleValue(frame, aDouble);
}

- cacheServerProxy;
{
	BOOL x = 1;
	id aConnection;
	int error = 0;
	
	if(theServer != nil) return theServer;
	 	
    // Check if server launched
	
	if(Debug)
		NXLogError("<%s> Trying to launch '%s'.", 
				[self name],
				FEED_APP);

	x = [[Application workspace]
			launchApplication:FEED_APP];

	if(x == NO) {
	
		if(Debug)
			NXLogError("<%s> Launching '%s'.", 
					[self name],
					FEED_APP);

		x = [[Application workspace]
				launchApplication:FEED_APP];
	
	}

	if(x == NO) {
		NXLogError("<%s> Could not launch %s!", 
				[self name], 
				FEED_APP);
		return nil;
	}
	
    // connect to server
    
	if(Debug)
		NXLogError("<%s> Connecting to '%s'.", 
				[self name],
				FEED_SERVER);

	theServer = [NXConnection connectToName: FEED_SERVER];
	
	if(theServer == nil) {
		NXLogError("<%s> Could not connect to %s.", 
				[self name], 
				FEED_SERVER);
		return nil;
	}

    	aConnection = [theServer connectionForProxy];
       
        [aConnection registerForInvalidationNotification:self];
    	[aConnection runFromAppKit];

 	if(Debug)
		NXLogError("<%s> Checking in with '%s'.", 
				[self name],
				FEED_SERVER);

   	NX_DURING
		
		[theServer checkIn:self];
		
    	NX_HANDLER
	
		error = NXLocalHandler.code;
	
	NX_ENDHANDLER
	
	if(error) {
		
		NXLogError("<%s> Exception %d for 'checkIn'.", 
					[self name], error);
		return nil;
	}
	
   	return theServer;
}	

- senderIsInvalid:sender;
{
	if(Debug)
		NXLogError("<%s> senderIsInvalid '%s'.", 
				[self name],		
				[sender name]);
		
	theServer = nil;
	return self;
}

//
//
//

- (oneway void)feedDemoUpdate;
{
	if(Debug)
		NXLogError("<%s> feedDemoUpdate", [self name]);

	if([[NXApp delegate] dataFeedEnabled])
		[[NXApp delegate] recalculateWorksheets:self];	// 3.1 CHANGE
	
}


@end
