/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_FILEreceive_data[] = "@(#)FILEreceive.c	1.12 10/19/92 13:25:01  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/27/90

	ROUTINE:	receive_data
	Version:	__ROr_FILEreceive_data

PURPOSE:
	This routine receives data "from" a file


IN:
	instance - send state

OUT:
	instance - Send state and Exception state are updated.


CALLED ROUTINES:
	SDataRequest

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>

#include <stdio.h>

#define FILEPART	(instance->subsystem->specific.file)
#define QBUFFER		(instance->receive_state.buffers)

int __ROr_FILEreceive_data(instance)

   __RO_Instance_Record *	instance;


{
   int Octet;
   int done_avail;

#ifndef __RO_PRODUCTION
   if (instance->subsystem->platform != __ROm_platform_FILE)
      __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_PLATFORMNOTFILE);
#endif
   done_avail = instance->receive_state.done_available;
   instance->receive_state.done_available = 0;

   if (!QBUFFER)
   {
      QBUFFER =
          (__RO_Receivebuffers)malloc(sizeof(struct __RO_Receivebuffer_struct));
      if (!QBUFFER)
         __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
      QBUFFER->forward = NULL;
      QBUFFER->backward = NULL;
   }
   if (fscanf((FILE *)FILEPART.infile,"%x",&Octet) != 1)
   {
      if (instance->receive_state.buffers)
         free(instance->receive_state.buffers);
      if (done_avail && feof((FILE *)FILEPART.infile))
      {
         __RO_RETURN_DONE(instance);
      }
      else
      {
         __RO_RETURN_RUNTIME(instance,__ROx_GET_UNEXPECTDONE);
      }
   }
   else
   {
      QBUFFER->data_length = 1;
      QBUFFER->data_ptr = QBUFFER->data_bytes;
      QBUFFER->data_bytes[0] = Octet;
   }
   if (__ROr_log6receive(instance))
      return(EXCEPTION);

   return(SUCCESS);
}



