/*****************************************************************/
/**                                                             **/
/**             (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_STDinit_instance[] = "@(#)STDinit_inst.c	1.65 10/19/92 13:48:02 LLNL ";

static char sccs_ISODEPLATFORM[] = 
#ifdef __RO_PLATFORM_ISODE
"@(#)PLATFORM: isode PRESENT"
#else
"@(#)PLATFORM: NO isode"
#endif
;

static char sccs_PURESOCKETSPLATFORM[] = 
#ifdef __RO_PLATFORM_PURESOCKETS
"@(#)PLATFORM: puresockets PRESENT"
#else
"@(#)PLATFORMS: NO puresockets"
#endif
;

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	7/5/90

	ROUTINE:	init_instance
	Version:	__ROr_STDinit_instance

PURPOSE:
	This routine creates and initializes the instance record prior
	to a bind call.

IN:
	info - interface specification specific information for the
		initialization process
OUT:
	instance_ptr - Pointer to the instance record pointer


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	Added sending_PPDUs field to instance record (ls 920414)


*/


#include <elros/instance.h>

#include <malloc.h>
extern char *getenv();

/* THE FOLLOWING ARE ELROS GLOBAL VARIABLES */

__RO_Debugstate __ROv_global_debug_state = { NULL, NULL, 0,
				       NULL, NULL, 0 };
int __ROv_global_debug_state_initialized = 0;

__RO_Instance_Info *__ROv_current_interface_information = NULL;



int __ROr_STDinit_instance(instance_ptr,info,declared_class)

	__RO_Instance_Record **instance_ptr;
	__RO_Instance_Info *info;
	__RO_Instance_Class declared_class;

{
   __RO_Instance_Record *inst;


	/* If the global interface is undefined, define it, for now */

   if (!__ROv_current_interface_information)
      __ROv_current_interface_information = info;


	/* Allocate the memory and Set up the signature */

   inst = *instance_ptr =
	(__RO_Instance_Record *)malloc(sizeof(__RO_Instance_Record));
   if (!inst)
   {
	/* This needs to be dummied up for the exception to work! */
      static __RO_Instance_Record special_static_instance;
      *instance_ptr = &special_static_instance;
      special_static_instance.signature = &special_static_instance;
      __RO_RETURN_RUNTIME(*instance_ptr,__ROx_GENERAL_MALLOCFAILED);
   }
   inst->signature = (void *)inst;

	/* Handle the global debug state setup */

   if (!__ROv_global_debug_state_initialized)
   {
      char *ename;

      if (ename=getenv("RO_LOG6FILE"))
         if (__ROr_log6start(ename))
            __RO_RETURN_RUNTIME(inst,__ROx_LOG_START6);

      if (ename=getenv("RO_LOG7FILE"))
         if (__ROr_log7start(ename))
            __RO_RETURN_RUNTIME(inst,__ROx_LOG_START7);

      __ROv_global_debug_state_initialized = 1;
   }


   inst->subsystem = (__RO_Instance_Address *)malloc(sizeof(__RO_Instance_Address));
   if (!inst->subsystem)
      __RO_RETURN_RUNTIME(inst,__ROx_GENERAL_MALLOCFAILED);
   inst->subsystem->timeout = 0;
   SemInit(inst->exclusion,1);
   inst->decl_class = declared_class;
   inst->bound_rules = __ROm_rules_NORULES;

   inst->received_invokeID = -1;
   inst->received_linkedID = -1;
   inst->sending_invokeID = 1;
   inst->sending_nesting = 0;
   inst->sending_flushed = 0;

   if (declared_class == __ROm_class_GIVEACCEPT)
     inst->sending_PPDUs = 1;
   else
     inst->sending_PPDUs = 0;

   inst->except_state.number = 0;
   inst->except_state.customize = NULL;
   inst->except_state.entry = NULL;
   inst->except_state.location.raised_file = 
       inst->except_state.location.goto_file = "***not set***";
   inst->except_state.location.raised_line =
       inst->except_state.location.goto_line = -1;

   inst->log6on = 0;
   inst->log7on = 0;

	/* Mark 'new' in the log file, if needed. */
   __ROr_log6new(inst);
   __ROr_log7new(inst);

   inst->ISO_Contexts.appl_context_name = info->acn;
   inst->ISO_Contexts.prsnt_context_name = info->pcn;

   inst->send_state.index = 0;
   inst->send_state.buffer_maximum = info->sending_buffersize;
   inst->send_state.buffer = 
	(unsigned char *) malloc(info->sending_buffersize);
   if (!inst->send_state.buffer)
      __RO_RETURN_RUNTIME(inst,__ROx_GENERAL_MALLOCFAILED);
   inst->send_state.boundary_flag = 0;

   inst->receive_state.buffers = NULL;
   inst->receive_state.buffer_maximum = info->receiving_buffersize;
   inst->receive_state.done_available = 0;
   inst->receive_state.done_taken = 0;

   inst->get_state.buffer = NULL;
   inst->get_state.buffer_size = 0;
   inst->get_state.bytecounter = 0;
   inst->get_state.definite.top = 0;
   inst->get_state.definite.maximum = info->defstksize;
   inst->get_state.definite.stack = 
	(unsigned long *)malloc(sizeof(unsigned long) * (info->defstksize+1));
   if (!inst->get_state.definite.stack)
      __RO_RETURN_RUNTIME(inst,__ROx_GENERAL_MALLOCFAILED);

   inst->get_state.opoid_maximum = info->maxoidsize;
   inst->get_state.opoidspace =
	(struct __RO_oid_skeleton *)
	 malloc(sizeof(int) + sizeof(long)*(info->maxoidsize));
   if (!inst->get_state.opoidspace)
      __RO_RETURN_RUNTIME(inst,__ROx_GENERAL_MALLOCFAILED);
   inst->get_state.ppdu_state = __ROm_ppdu_state1;

   inst->give_table = info->give_table_ptr;
   inst->accept_tables = info->accept_tables_ptr;
   inst->type_information = info->type_information_ptr;

   inst->capable = info->rules_capable;
   inst->extensor_type = __ROm_extensor_NONE; /* not valid */
   inst->extensor.NOEXTENSOR = NULL;

   return(SUCCESS);
}


/********************************************************
	ROUTINE:	__ROr_STDfree_instance

PURPOSE:
	This routine unbinds the instance

IN:
	instance - pointer to instance record.

OUT:
	instance - All fields are inaccessible -- pointer is freed.


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

int __ROr_STDfree_instance(instance)

	__RO_Instance_Record *instance;
   
{
   if (__ROr_log6endi(instance))
      return(EXCEPTION);
   if (__ROr_log7endi(instance))
      return(EXCEPTION);


	/* If tasking clean up the socket structure */
#ifdef __RO_TASKING
   if (instance->subsystem->platform == __ROm_platform_ISODE)
   {
#ifdef __RO_PLATFORM_ISODE
      if( (instance->subsystem->specific.isode.association >= 0) && 
          (instance->subsystem->specific.isode.association <= FD_SETSIZE) )
      {
          __ROr_ClearSocket( instance->subsystem->specific.isode.association, 
				__ROm_READDIR);
          __ROr_ClearSocket( instance->subsystem->specific.isode.association, 
				__ROm_WRITEDIR);
          __ROr_ClearSocket( instance->subsystem->specific.isode.association, 
				__ROm_ERRORDIR);
      }
#endif
   }
   else if (instance->subsystem->platform == __ROm_platform_PURESOCKETS)
   {
#ifdef __RO_PLATFORM_PURESOCKETS
      if( (instance->subsystem->specific.puresockets.socket >= 0) && 
          (instance->subsystem->specific.puresockets.socket <= FD_SETSIZE) )
      {
          __ROr_ClearSocket( instance->subsystem->specific.puresockets.socket,
				__ROm_READDIR);
          __ROr_ClearSocket( instance->subsystem->specific.puresockets.socket, 
				__ROm_WRITEDIR);
          __ROr_ClearSocket( instance->subsystem->specific.puresockets.socket, 
				__ROm_ERRORDIR);
      }
#endif
   }
#endif

	/* FREE the instance record components */

   free(instance->subsystem);
   free(instance->send_state.buffer);
   if (__ROr_freeQ(instance->receive_state.buffers))
      return(EXCEPTION);
   free(instance->get_state.definite.stack);
   free(instance->get_state.opoidspace);
   switch (instance->extensor_type)
   {
      case __ROm_extensor_NONE:
         break;
      default:
         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_MUTUAL);
   }

	/* Invalidate the signature */
   instance->signature = 0;

   free(instance);

   return(SUCCESS);
}


/*
	ROUTINE:	freeQ
	Version:	__ROr_freeQ

PURPOSE:
	This routine unbinds the instance

IN:
	instance - pointer to instance record.

OUT:
	instance - All fields are inaccessible -- pointer is freed.


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/


int __ROr_freeQ(q)

   __RO_Receivebuffers q;

{

   if (q)
   {
      while (q->forward != NULL)
         q = q->forward;
      while (q)
      {
         __RO_Receivebuffers to_be_freed;

         to_be_freed = q;
         q = q->backward;
         free(to_be_freed);
      }
   }
   return(SUCCESS);
}


