/*****************************************************************/
/**                                                             **/
/**             (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_ISODEsend_data[] = "@(#)ISODEsend.c	1.21 10/19/92 13:25:23 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	6/27/90

	ROUTINE:	send_data
	Version:	__ROr_ISODEsend_data

PURPOSE:
	This routine sends the data in the Send state array.  This version
	runs over ISODE.


IN:
	instance - send state

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


CALLED ROUTINES:
	SWriteRequest

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>

#ifdef __RO_TASKING
#    include <elros/tasking.h>

#endif


int __ROr_ISODEsend_data(instance)
   __RO_Instance_Record *	instance;

#ifdef __RO_PLATFORM_ISODE

#define ISODESPEC 	instance->subsystem->specific.isode

{
   struct SSAPindication si;
   int code;
   int status;

#ifndef __RO_PRODUCTION
   if (instance->subsystem->platform != __ROm_platform_ISODE)
      __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_PLATFORMNOTISODE);
#endif

   if (__ROr_log6send(instance))
      return(EXCEPTION);

   if (instance->send_state.index > 0)
   {

			/*
			 * If out of space in uvarray, double it's	
			 * size.  Note that there must always be 1 "Null"
			 * element at the end of the array.  That's how the
			 * ISODE calling protocol terminates the array.
			 */
      if( ISODESPEC.uvindex + 1 >= ISODESPEC.uvmax )
      {
	  struct udvec *newuvarray;
	  int i;

  	
	  ISODESPEC.uvmax *= 2;
	  if( (newuvarray = (struct udvec *)malloc(sizeof(struct udvec) * (ISODESPEC.uvmax))) == NULL ) 
	  	__RO_RETURN_RUNTIME( instance, __ROx_INTERNAL_OUTOFMEM );

	  for( i=0; i<ISODESPEC.uvindex; i++)
	     newuvarray[i] = ISODESPEC.uvarray[i];
          memset(newuvarray+i,0,(sizeof(struct udvec)*(ISODESPEC.uvmax-i)));
	  free(ISODESPEC.uvarray);
	  ISODESPEC.uvarray = newuvarray;

       }


				/*
				 * Add the new buffer to the uv array.
				 */
#ifndef __RO_PRODUCTION
       if( ISODESPEC.uvindex + 1 >= ISODESPEC.uvmax )
	  __RO_RETURN_RUNTIME( instance, __ROx_INTERNAL_UVOVERFLOW )
       else
#endif
       {

          ISODESPEC.uvarray[ISODESPEC.uvindex].uv_base = 
			(caddr_t)instance->send_state.buffer;
	  ISODESPEC.uvarray[ISODESPEC.uvindex].uv_len = 
		instance->send_state.index;
	
	
					/*
					 * Get a new send_state buffer
					 */
	  if( (instance->send_state.buffer = (unsigned char *)malloc(instance->send_state.buffer_maximum)) == NULL )
	  	__RO_RETURN_RUNTIME( instance, __ROx_INTERNAL_OUTOFMEM );

	  instance->send_state.index = 0;
          ISODESPEC.uvindex++;

       }



				/*
				 * If we are at a PPDU boundary, send
				 * all of the data.
				 */
   }

   if( instance->send_state.boundary_flag )
   {
	int i;
	int done = 0;


	code = SWriteRequest( instance->subsystem->specific.isode.association,
				0, ISODESPEC.uvarray, &si ) != OK;
        if (code != OK) /* code == NOTOK */
        {
           __RO_RETURN_RUNTIME_PARM(instance,__ROx_SEND_ABORT,si.si_abort.sa_data);
           /* Put reason into parameter:  si.si_abort.sa_reason */
        }
       
				/*
				 * Call TNetAccept at least once to see if
				 * write is complete.  
				 */
#ifdef __RO_TASKING 
		
	if( __ROr_ISODEwait_for_send(instance) )
	    return(EXCEPTION);
#endif

        instance->send_state.boundary_flag = 0;

	for( i=0; i<ISODESPEC.uvindex; i++ )
	{
 	   free(ISODESPEC.uvarray[i].uv_base);
 	   ISODESPEC.uvarray[i].uv_base = NULL;
 	   ISODESPEC.uvarray[i].uv_len = 0;
	}
		
	ISODESPEC.uvindex = 0;
	
   }

   return(SUCCESS);
}

#else
{
   __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_ISODENOTLINKED);
}
#endif

