/*
 * query.c
 *
 *    connects to running lbpi application and prints current status
 */

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "query.h"


void main(int argc, char *argv[]) {
  char       port_name[MPI_MAX_PORT_NAME];
  MPI_Comm   servcomm;
  long       infos[4];
  long       b_size, b_total, b_ready, hits; 
  double     pi;
  MPI_Status status;

  MPI_Init(&argc, &argv);
  
  MPI_Lookup_name(SERVICE_NAME, MPI_INFO_NULL, port_name);
  printf("client: trying to get connection\n");
  MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &servcomm);

  printf("client: connected\n");

  MPI_Recv(infos, 4, MPI_LONG, 0, 0, servcomm, &status);
  MPI_Comm_disconnect(&servcomm);
  printf("client: disconnected\n");
    
  b_size  = infos[0];
  b_total = infos[1];
  b_ready = infos[2];
  hits    = infos[3];

  printf("client: %d blocks of %d ready\n", b_ready, b_total);
  pi = 4 * hits/(double)(b_ready * b_size);
  printf("client: preliminary result: PI = %lf\n", pi);

  MPI_Finalize();
  return;
}