master.c


/*
** $RCSfile: master.c $
** $Revision: 1.6 $
** $Author: doomer $
** $Date: 1996/02/08 11:44:14 $
** Copyright (c) 1996 John Dumais
*/

#include "sharedMemory.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUM_ARRAY_ELEMENTS 1440
#define PI 3.1415927
#define N_SEGMENT_ENTRIES 4
#define DEFAULT_ENTRY "defaultEntry"

static double cosineArray[NUM_ARRAY_ELEMENTS];
static double sineArray[NUM_ARRAY_ELEMENTS];

void initializeSineArray(void) {
	long i;
	double sampleInterval;
	
	sampleInterval=2*PI/(NUM_ARRAY_ELEMENTS -1);
	
	for (i=0; i<NUM_ARRAY_ELEMENTS; i++) {
		sineArray[i]=sin(i*sampleInterval);
	}
		
	return;
}

void initializeCosineArray(void) {
	long i;
	double sampleInterval;

	sampleInterval=2*PI/(NUM_ARRAY_ELEMENTS -1);
	
	for (i=0; i<NUM_ARRAY_ELEMENTS; i++) {
		cosineArray[i]=cos(i*sampleInterval);
	}
	return;	
}

void main(void) {
	short i;
	char *segmentNames[]= {
		DEFAULT_ENTRY,
		DEFAULT_ENTRY,
		DEFAULT_ENTRY,
		DEFAULT_ENTRY	
	};
	long nDataElements[N_SEGMENT_ENTRIES];
	short dataTypes[N_SEGMENT_ENTRIES];
	long nSegments;
	FILE *segmentFile;
		
	initializeSineArray();
	initializeCosineArray();
	
	if (createDoubleShare("demo", NUM_ARRAY_ELEMENTS) < 0) {
		printf("Couldn't create shared data segment\n");
		exit(-1);
	}
	
	nSegments=enumerateSharedMemory(segmentNames, nDataElements,
			dataTypes, N_SEGMENT_ENTRIES);
			 
	if (nSegments > 0) {
		segmentFile=fopen("\\tmp\\segment.dat", "w");
		if (segmentFile) {
			fprintf(segmentFile, "%ld\n", nSegments);
			for (i=0; i<nSegments; i++) {
				fprintf(segmentFile, "%s\n%ld\n%d\n",
						segmentNames[i], nDataElements[i],
						dataTypes[i]);
			}
			
			fclose(segmentFile);
		}
	}
	
	for (i=0; i<100; i++) {
		if ((i % 2) == 0) {
			printf("Writing   sine data on iteration: %ld\r", i);
			writeDoubleData("demo", sineArray, NUM_ARRAY_ELEMENTS);
		}
		else {
			printf("Writing cosine data on iteration: %ld\r", i);
			writeDoubleData("demo", cosineArray, NUM_ARRAY_ELEMENTS);
		}

		Sleep(1000);
	}
	
	destroyAllSharedMemory();
	
	exit(0);	
}

/*
** $Log: master.c $
** Revision 1.6  1996/02/08 11:44:14  doomer
** changed i % 2L to i % 2.  I is a short, don't want a type conversion
** unless it's necessary.
** Revision 1.5  1996/02/08 11:37:56  doomer
** removed call to fmod() and just used % to figure out which data to write.
** Revision 1.4  1996/02/07 16:38:00  doomer
** added smarts to let the two programs know about the shared segments by
** saving the relevant information in a file.  The two programs agree
** on this file as the source of information about existing shared segments.
** Revision 1.3  1996/02/06 11:40:14  doomer
** spiffed the demo up a little.
** Revision 1.2  1996/02/02 11:01:34  doomer
** first demo that worked
*/