/*
 * File         : mpi_Status.c
 * Headerfile   : mpi_Status.h 
 * Author       : Sung-Hoon Ko, Xinying Li
 * Created      : Thu Apr  9 12:22:15 1998
 * Revision     : $Revision: 1.4 $
 * Updated      : $Date: 1999/09/14 15:07:53 $
 * Copyright: Northeast Parallel Architectures Center
 *            at Syracuse University 1998
 */

#include <malloc.h>
#include <mpi.h>
#include "mpi_Status.h"

#include "handle.h"
#include "ID.h"

DECLARE_CLASS("mpi/Status", status_class)
DECLARE_FIELD_ID(status_class, "handle", "J", stathandleID)
DECLARE_FIELD_ID(status_class, "source", "I", sourceID)
DECLARE_FIELD_ID(status_class, "tag", "I", tagID)
DECLARE_FIELD_ID(status_class, "count", "I", countID)
DECLARE_FIELD_ID(status_class, "index", "I", indexID)

DECLARE_CLASS("mpi/Datatype", datatype_class)
DECLARE_FIELD_ID(datatype_class, "handle", "J", DatatypehandleID)

/*
 * Class:     mpi_Status
 * Method:    init
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_mpi_Status_init
  (JNIEnv *env, jclass jthis)
{
}


/*
 * Class:     mpi_init_handle
 * Method:    init_handle
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_mpi_Status_init_1handle
  (JNIEnv *env, jobject jthis)
{
  MPI_Status *stat = malloc(sizeof(MPI_Status));
  (*env)->SetLongField(env, jthis, stathandleID, handle_to_jlong(stat));
}


/*
 * Class:     mpi_Status
 * Method:    get_count
 * Signature: (Lmpi/Datatype;)I
 */
JNIEXPORT jint JNICALL Java_mpi_Status_get_1count
  (JNIEnv *env, jobject jthis, jobject type)
{
   int count;

   MPI_Status *stat; /*shko*/

   stat=(MPI_Status *)jlong_to_handle((*env)->GetLongField(env,jthis,stathandleID)); /*shko*/

   /*
   stat.st_count=(*env)->GetIntField(env,jthis,countID);
   stat.MPI_SOURCE=(*env)->GetIntField(env,jthis,sourceID);
   stat.MPI_TAG=(*env)->GetIntField(env,jthis,tagID);
   */

   /* stat.MPI_ERROR=(*env)->GetIntField(env,jthis,errorID); */
   /*printf(" in STATUS count = %d \n",stat->count);*/

  MPI_Get_count( stat, 
       (MPI_Datatype)((*env)->GetLongField(env,type,DatatypehandleID)), 
       &count );
  /*  printf(" count = %d \n",count);*/

   return count;

}

/*
 * Class:     mpi_Status
 * Method:    Test_cancelled
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_mpi_Status_Test_1cancelled
  (JNIEnv *env, jobject jthis)
{
   int flag;
   MPI_Status *stat;  /*shko*/

   stat=(MPI_Status *)jlong_to_handle((*env)->GetLongField(env,jthis,stathandleID));/*shko*/
   /*
   stat.st_count=(*env)->GetIntField(env,jthis,countID);
   stat.MPI_SOURCE=(*env)->GetIntField(env,jthis,sourceID);
   stat.MPI_TAG=(*env)->GetIntField(env,jthis,tagID);
   */
   /* stat.MPI_ERROR=(*env)->GetIntField(env,jthis,errorID); */

   MPI_Test_cancelled(stat, &flag);
   if(flag==0)
        return JNI_FALSE;
    else
        return JNI_TRUE;
}

/*
 * Class:     mpi_Status
 * Method:    get_elements
 * Signature: (Lmpi/Datatype;)I
 */
JNIEXPORT jint JNICALL Java_mpi_Status_get_1elements
  (JNIEnv *env, jobject jthis, jobject type)       
{
   int count;
   MPI_Status *stat;

   stat=(MPI_Status *)jlong_to_handle((*env)->GetLongField(env,jthis,stathandleID));

   MPI_Get_elements( stat,
    (MPI_Datatype)((*env)->GetLongField(env,type,DatatypehandleID)), 
    &count );

   return count;
}

/*
 * Class:     mpi_Status
 * Method:    finalize
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_mpi_Status_finalize
  (JNIEnv *env, jobject jthis) {
   MPI_Status *stat;

   stat=(MPI_Status *)jlong_to_handle((*env)->GetLongField(env,jthis,stathandleID));

   free(stat) ;
}


