testParallel.c 4.17 KB
/*=============================================================
 * testParallel.c
 * Test concurrent access of DD_Server
 *  May 2015, V.1.0, Renard
 *=============================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <DD.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>

//Structrure used to define a request
typedef struct {
   char ViName[100];
   char StartTime[17];
   char TimeInt[17];
   char TimeName[20];
   char ParName[20];
} RequestData;

//Number of requests to run
#define NB_REQUESTS 60

//Array of all requests data
RequestData requestDataList[NB_REQUESTS];

/*-------------------------*/
#define IMF_VI    "ace:imf:all\0"
#define IMF_PAR   "IMF\0"
#define IMF_TIME  "Time\0"
#define IMF_START "2008000000000000\0"
#define IMF_TI    "0000030000000000\0"

#define DST_VI    "ground:based:dst\0"
#define DST_PAR   "DST\0"
#define DST_TIME  "Time\0"
#define DST_START "2008000000000000\0"
#define DST_TI    "0000060000000000\0"

#define MESMAG_VI    "mes:mag:orb\0"
#define MESMAG_PAR   "B_MSO\0"
#define MESMAG_TIME  "Time\0"
#define MESMAG_START "2012000000000000\0"
#define MESMAG_TI    "0000004000000000\0"
/*-------------------------*/

/*
 * Init requests data
 */
void initRequestDataList()
{
   memset(&requestDataList, 0, NB_REQUESTS*sizeof(RequestData));

   int i;
   for (i = 0; i < NB_REQUESTS; ++i)
   {
      if (i%3 == 0)
      {
         strcpy(requestDataList[i].ViName,MESMAG_VI);
         strcpy(requestDataList[i].StartTime,MESMAG_START);
         strcpy(requestDataList[i].TimeInt,MESMAG_TI);
         strcpy(requestDataList[i].TimeName,MESMAG_TIME);
         strcpy(requestDataList[i].ParName,MESMAG_PAR);
      }
      else if (i%2 == 0)
      {
         strcpy(requestDataList[i].ViName,DST_VI);
         strcpy(requestDataList[i].StartTime,DST_START);
         strcpy(requestDataList[i].TimeInt,DST_TI);
         strcpy(requestDataList[i].TimeName,DST_TIME);
         strcpy(requestDataList[i].ParName,DST_PAR);
      }
      else
      {

         strcpy(requestDataList[i].ViName,IMF_VI);
         strcpy(requestDataList[i].StartTime,IMF_START);
         strcpy(requestDataList[i].TimeInt,IMF_TI);
         strcpy(requestDataList[i].TimeName,IMF_TIME);
         strcpy(requestDataList[i].ParName,IMF_PAR);
      }
   }
}

/*
 * Run a request
 */
int runRequest(char* ViName, char* StartTime, char* TimeInt, int NbPar, char** ParNames)
{
   int ID, error;
   double RealTime;
   DD_data_t *data;

   ID = DD_SetVariable(ViName);
   if(ID < 0)
   {
     error = DD_Close(99);
     return 0;
   }

   error = DD_SetTimeInfo(ID, StartTime, &RealTime);   
   if(error < 0)
   {
     error = DD_Close(ID);
     return 0;
   }

   do
   {
      error = DD_GetMultiData(ID, NbPar, ParNames, TimeInt, &data, 1);
      if(error < 0)
      {
         error = DD_Close(ID);
         return 0;
      }
      if(error == MOREDELAY)
      {
         error = MOREDATA;
      }
   }
   while(error == MOREDATA);

   error = DD_Close(ID);

   return 1;
}

/*
 * Main
 */
int main()
{
   initRequestDataList();

   pid_t pid;
   int crtRequest = 0;

   do {
      pid = fork();
      if (pid == 0)
      {
         char *ParNames[2] = {(char *)requestDataList[crtRequest].TimeName, (char *)requestDataList[crtRequest].ParName};
         printf("Run Request %s %s %s %s\n",requestDataList[crtRequest].ViName,requestDataList[crtRequest].ParName,requestDataList[crtRequest].StartTime,requestDataList[crtRequest].TimeInt);
         if (!runRequest(requestDataList[crtRequest].ViName, requestDataList[crtRequest].StartTime, requestDataList[crtRequest].TimeInt, 2, ParNames))
           printf("Error detected %s %s %s %s\n",requestDataList[crtRequest].ViName,requestDataList[crtRequest].ParName,requestDataList[crtRequest].StartTime,requestDataList[crtRequest].TimeInt);
         else
           printf("OK %s %s %s %s\n",requestDataList[crtRequest].ViName,requestDataList[crtRequest].ParName,requestDataList[crtRequest].StartTime,requestDataList[crtRequest].TimeInt);
      }
      else
      {
        ++crtRequest;
        if (crtRequest == NB_REQUESTS)
        {
           wait(0);
        }
      }
   } while ((pid != 0) && (crtRequest < NB_REQUESTS));

   return 1;
}