/********************************************************
 * search.c
 *
 * This is a sample driver for running a near neighbor
 * experiment.  This particular driver simply sets up
 * communication with the data set oracle, call the 
 * algorithm preprocessor, and then calls the algorithm
 * query function for each query point.
 *
 * Sixth Annual DIMACS Implementation Challenge
 * Author:  Michael Goldwasser (wass@cs.princeton.edu)
 * Created: Jan 16, 1998
 *******************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <values.h>
#include "algorithm.h"
#include "client.h"
#include "oracle.h"


#define DEFAULT_PORT  0x1234


#define LEN 70
static char usage[][LEN] = {
" Usage:                                                              ",
"                                                                     ",
"   -help           this message",
"   -host <host>    oracle is at <host> (default is $HOST)",
"   -port <port>    oracle uses <port> (default is 0x1234)",
"   -q <num>        process at most <num> queries",
"                                                                     ",
"                                                                     "
};
 

Print_Usage()
{
    int lines,i,j;
 
    lines = sizeof(usage)/(LEN*sizeof(char));
 
    for (i=0; i<lines; i++) {
        for (j=0; j<LEN; j++) {
            printf("%c",usage[i][j]);
        }
        printf("\n");
    }
}
 
 


main(argc, argv)
  int argc;
  char **argv;
{
  Oracle*      ora;
  AbstractDS*  ads; 
  int          i,q;

  int       port;
  char      *host;
  int       max_query;
 

  /*--- initialize variables ----*/
  port = DEFAULT_PORT;
  host = getenv("HOST");
  max_query = 10;


  /*--- parse command line args ---*/
  for (i=1; i<argc; i++) {
    if (strcmp(argv[i],"-host")==0) {
      host = argv[++i];
      printf("Hostname: %s\n",host);
    } else if (strcmp(argv[i],"-port")==0) {
      port = atoi(argv[++i]);
    } else if (strcmp(argv[i],"-q")==0) {
      max_query = atoi(argv[++i]);
    } else if ((strcmp(argv[i],"-help")==0) ||
               (strcmp(argv[i],"-h")==0)) {
      Print_Usage();
      exit(1);
    } else {
      printf("unrecognized option: %s (ignored)\n",argv[i]);
    }
  }



  /*------------------------------------------
    --------------- let's begin --------------
    ----------------------------------------*/


  /*-- open the communication with oracle --*/
  ora = OracleConnect(host,port);

  /*-- Let the algorithm preprocess --*/
  ads = Preprocess(ora);


  /*-- start the queries --*/
  q = InqNumQuery(ora);
  if (max_query > q) max_query=q;

  for (i=0; i<max_query; i++) {
    FindNeighbor(ora,ads,i);
  }


  /*-- report accounting --*/
  printf("Overall preprocessing cost: %d\n",CostPreprocess(ads));
  printf("Overall query cost: %d\n",CostQueries(ads));


  /*-- all done --*/
  DestroyAbstractDS(ads);
  OracleClose(ora);
}

