/*
 * $RCSfile: blast_control.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: blast_control.c,v $
 * Revision 1.3  1996/01/29 22:07:36  slm
 * Updated copyright and version.
 *
 * Revision 1.2  1995/08/03  19:14:21  davidm
 * 64-bit cleanup and x3.2->x3.3 conversion.
 *
 * Revision 1.1  1995/07/28  22:08:26  slm
 * Initial revision
 *
 * Revision 1.18.1.1.1.1  1994/11/23  17:28:47  hkaram
 * New branch
 *
 * Revision 1.18.1.1  1994/04/23  00:11:16  menze
 * Added checkLen to CHAN_RETRANSMIT op
 */

#include "xkernel.h"
#include "blast_internal.h"

int
blastControlProtl(self, opcode, buf, len)
    Protl self;
    int opcode;
    char *buf;
    int len;
{
    PState	*pstate;
    int 	new_size;
    int		diff;
    
    xTrace1(blastp, TR_EVENTS, "blast_controlprotl, opcode: %d", opcode);
    pstate = (PState *)self->state;
    
    switch (opcode) {
      /* free storage associated with ticket returned by push */
      case FREERESOURCES:
	checkLen(len, sizeof(int));
	xTrace1(blastp, TR_MORE_EVENTS, "blast killticket called with id %d",
		*(int *)buf);
        return blast_freeSendSeq(pstate, *(int *)buf);
      case CHAN_RETRANSMIT:
	checkLen(len, sizeof(int));
	xTrace1(blastp, TR_MORE_EVENTS, "blast killticket called with id %d",
		*(int *)buf);
        return blast_Retransmit(pstate, *(int *)buf);
      case BLAST_SETOUTSTANDINGMSGS:
	/* Set the number of outstanding messages to be the integer passed
         * in buf.
         */
	checkLen(len, sizeof(int));
	new_size = *(int *)buf;
	xTrace1(blastp, TR_MORE_EVENTS,
		"set outstanding messages to %d", new_size);
	if (new_size <= 0)
	    return -1;
	diff = new_size - pstate->max_outstanding_messages;
	for (; diff < 0; diff++) 
	    semWait(&pstate->outstanding_messages);
	for (; diff > 0; diff--) 
	    semSignal(&pstate->outstanding_messages);
	pstate->max_outstanding_messages = new_size;
	return 0;
	
      case BLAST_GETOUTSTANDINGMSGS:
	/* Return the current number of allowed outstanding messages */
	checkLen(len, sizeof(int));
	*(int *)buf = pstate->max_outstanding_messages;
	return 0;
	
      default:
        return xControlProtl(xGetProtlDown(self, 0), opcode, buf, len);
    } 
}
  

int
blastControlSessn(s, opcode, buf, len)
    Sessn s;
    int opcode;
    char *buf;
    int len;
{
    SState	*state;
    PState	*pstate;
    
    xTrace1(blastp, TR_EVENTS, "in blast_control with session=%lx", (long) s);
    state = (SState *) s->state;
    pstate = (PState *) s->myprotl->state;
    switch (opcode) {
	
	/* free storage associated with ticket returned by push */
      case FREERESOURCES:
	checkLen(len, sizeof(int));
	xTrace1(blastp, TR_MORE_EVENTS, "blast killticket called with id %d",
		*(int *)buf);
        return blast_freeSendSeq(pstate, *(int *)buf);

      case CHAN_RETRANSMIT:
	xTrace1(blastp, TR_MORE_EVENTS, "blast killticket called with id %d",
		*(int *)buf);
        return blast_Retransmit(pstate, *(int *)buf);
	
      case GETMYPROTO:
      case GETPEERPROTO:
	checkLen(len, sizeof(long));
	*(long *)buf = state->prot_id;
	return sizeof(long);
	
      case GETOPTPACKET:
	checkLen(len, sizeof(int));
	*(int *)buf = state->fragmentSize * BLAST_MAX_FRAGS;
	return sizeof(int);

      case GETMAXPACKET:
	checkLen(len, sizeof(int));
	*(int *)buf = state->fragmentSize * BLAST_MAX_FRAGS;
	return sizeof(int);

      default:
        return xControlSessn(xGetSessnDown(s, 0), opcode, buf, len);
    }
}
