Tuesday, December 24, 2013

Next to keeping up on latest technology, keeping ones programming skills sharp should next on the list. Simple things like going back to the basics can be mentally rewarding.

Applying some of the basics was helpful to me as there is this system challenge at work. Seems there are problems with too much information, duplicate information, not enough information, or information going to the wrong place.

This is about duplicate information. Seems this client / server application is sending duplicate messages from point A (client) to point B (server). The real fix is to correct the problem at it's source. But after several days of getting nowhere, with the exception of learning more of system internals, the thought to prototype a solution and present it to the engineering team.

So here we go, back to basics. Essentially the problem statement is:

/*
 Send a record into the function.
 Check that record against each record in the alarmArray.

 If that incoming record is already in the alarmArray,
    1)Return with bad status.

 If that incoming record in not in the alarmArray,
    1) Shift the array down by one.
    2) Add that incoming record to the array.
    3) Return with good status.
*/

The code:
int checkDuplicateRecord(char *inStr) {

static char alarmArray[10][512];
static int  firstTime = 1;     
int i = 0;
int found = 0;

   //printf("checkDuplicateRecord: %s\n", inStr);

   /* Clear out the static char alarmArray once. */
   if(firstTime){
     memset(alarmArray,'\0', sizeof(alarmArray));
     firstTime = 0;
   }

/*
 * Search through the 10 elements of the alarmArray.
 * If it is found, it is a duplicate.
 * return 1
*/
   for(i=0;i<10;i++){
      if(!(strncmp(inStr, alarmArray[i], strlen(inStr)))){
         //printf("Duplicate record do not send: %s %s\n", alarmArray[i], inStr);
         found = 1;
         return 1;
         //break;
      }
      else{
         //printf("Non-duplicate record, continue: %s %s\n", alarmArray[i], inSt
r);
         found = 0;
      }
   }

   if(!(found)){
      /* Push all records down by one */
      strcpy(alarmArray[0], alarmArray[1]);
      strcpy(alarmArray[1], alarmArray[2]);
      strcpy(alarmArray[2], alarmArray[3]);
      strcpy(alarmArray[3], alarmArray[4]);
      strcpy(alarmArray[4], alarmArray[5]);
      strcpy(alarmArray[5], alarmArray[6]);
      strcpy(alarmArray[6], alarmArray[7]);
      strcpy(alarmArray[7], alarmArray[8]);
      strcpy(alarmArray[8], alarmArray[9]);
      strcpy(alarmArray[9], inStr);
   }

   if(!(found)){
      //printf("It is ok to send this alarm record.\n");
      return 0;
   }
}

So, I chose not to use a loop pushing down the records by one element. No real reason other than having a good visual representation of pushing down an array element.

The language is in C, though I mixed the C++ comment character //.
Though that a 10 X 512 sized array will meet the needs.

The runtime test worked out pretty well. It ran for over an hour with no problems. The next step would be to re-type this code into the client code and test. In short, keeping solutions simple, going back to programming basics can serve a programmer well in correcting system problems.