/* You may freely copy, distribute and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or implied, as to
 * its fitness for any particular use.
 */ 

#import "CallServer.h"
#import "CallRecord.h"

@implementation CallServer : CallController

/* 
 * Purpose: Initialize the CallServer
 *
 * First sends an init message to super, thereby invoking CallController's 
 * version of init, which initializes the callTable HashTable.  
 * Then registers and starts the server connection.  
 */

- init
{
    [super init];
    [[NXConnection registerRoot:self 
	withName:"CallDataServer"] runFromAppKit];
    return self;
}


/*
 * Purpose: Retrieve a CallRecord for a particular name from the callTable.  
 *
 * This is one of the CallProvider protocol methods.
 * When the client wants to look up a record for a name in the database,
 * it sends this message -- through it's proxy -- to the server. 
 * CallClient sends this message from its retrieveCall: method.  
 *
 */

- (id <CallData>)lookupCall:(const char *)fetchName
{
    return [callTable valueForKey:fetchName]; 
}


/*
 * Purpose: Request a new record from the server 
 *
 * This is one of the CallProvider protocol methods.  
 * Before the client fills a new record with data, it sends this message
 * to request the record -- through the proxy -- to the server.  
 * CallClient sends this message from its saveCall: method.  
 *
 */

- (id <CallData>)newRecord
{
    return [[CallRecord alloc] init];
}


/*
 * Purpose: Save the record in the database
 *
 * This is one of the CallProvider protocol methods.  
 * Once the client fills a new record with data, it sends this message -- 
 * through the proxy -- to request that the server save it in the database.  
 * CallClient sends this message from its saveCall: method.
 */

- storeCall:(id <CallData>)theRecord 
{
    NXTypedStream *callStream;

    [callTable insertKey:[theRecord name] value:theRecord]; 
    if (callStream = NXOpenTypedStreamForFile(callFilePath, NX_WRITEONLY)) {
	NXWriteObject(callStream, callTable);
	NXCloseTypedStream(callStream);
	return self;
    }
    else return nil;
}



@end
