/* 
 * $RCSfile: blast_internal.h,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: blast_internal.h,v $
 * Revision 1.2  1996/02/02 15:51:03  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/28  22:08:26  slm
 * Initial revision
 *
 * Revision 1.38.2.1.1.1  1994/11/23  17:28:57  hkaram
 * New branch
 *
 * Revision 1.38.2.1  1994/04/25  21:34:02  menze
 * changed field name spelling
 */

#ifndef blast_internal_h
#define blast_internal_h
 
#include "xkernel.h"
#include "blast.h"
#include "blast_mask32.h"
#include "blast_stack.h"
#include "chan.h"

#define BLASTHLEN  sizeof(BLAST_HDR) 

/*
 * Trace levels
 */
#define REXMIT_T TR_MAJOR_EVENTS

/* 
 * opcodes in the message header
 */
#define BLAST_SEND 1
#define BLAST_NACK 2
#define BLAST_RETRANSMIT 3

#define BLAST_OK 2
#define BLAST_FAIL 2
 
typedef unsigned int	BlastSeq;
#define BLAST_MAX_SEQ	0xffffffff

typedef struct  {
    xk_u_int32 		prot_id;
    BlastSeq 		seq;
    xk_u_int32 		len;
    xk_u_int16		num_frag;
    xk_u_int8 		op;
    xk_int8		pad[BLAST_PADDING];
    BlastMask 		mask;
} BLAST_HDR;

typedef struct {
    BLAST_HDR 	hdr;
    Msg 	frags[BLAST_MAX_FRAGS+1];
    BlastMask 	mask;
    BlastMask 	old_mask;
    u_int 	wait;
    Binding 	binding;
    struct blast_state *state;
    Event 	event;
    int 	nacks_sent;
    int 	nacks_received;
    bool	canChangeBackoff;
    int		rcnt;
} MSG_STATE;


typedef struct blast_state {
    u_int	ircnt;    
    long	prot_id;
    BLAST_HDR 	short_hdr;
    BLAST_HDR 	cur_hdr;
    Sessn 	self;
    int 	fragmentSize;
    int		per_packet;
    Map 	rec_map;
} SState;

typedef struct {
    Map 	active_map;
    Semaphore	createSem;
    Map 	passive_map;
    BlastSeq	max_seq;
    Semaphore 	outstanding_messages;
    int 	max_outstanding_messages;
    Stack	mstateStack;
    Map 	send_map;
} PState;

typedef struct {
        long	prot;
	Sessn	lls;
} ActiveID;

typedef int PassiveID;
	
/*
 * timeout calculations:
 */
#define PER_PACKET	10 * 1000
#define BACKOFF 	2
#define MAX_PER_PACKET	(INT_MAX/BACKOFF)
#define SEND_CONST 	1000 * 1000
#define MAX_WAIT	1000 * 1000
#define NACK_LIMIT	3


/* Default number of concurrent outstanding messages allowed
 * (per protocol instantiation)
 */
#define OUTSTANDING_MESSAGES 64

#define BLAST_ACTIVE_MAP_SZ	101
#define BLAST_SEND_MAP_SZ	101
#define BLAST_REC_MAP_SZ	101
#define BLAST_PASSIVE_MAP_SZ	13
#define BLAST_MSTATE_STACK_SZ	(2 * OUTSTANDING_MESSAGES)


/* If BLAST_SIM_DROPS is defined, BLAST will occasionally drop a fragment
 * before it gets to the appropriate BLAST session.
 */
/* #define BLAST_SIM_DROPS */


extern BlastMask	blastFullMask[];
extern int 		traceblastp;

#ifdef __STDC__

XkHandle	blastPush( Sessn, Msg * );
int		blastControlSessn( Sessn, int, char *, int );
int		blastControlProtl( Protl, int, char *, int );
void		blastDecIrc( Sessn );
MSG_STATE *	blastNewMstate( Sessn );
XkReturn	blastPop( Sessn, Sessn, Msg *, VOID * );
XkReturn	blastDemux( Protl, Sessn, Msg * );
Sessn		blastCreateSessn( Protl, Protl, Protl, ActiveID * );
void		blast_mapFlush( Map );
int		blast_freeSendSeq( PState *, int );
int		blast_Retransmit( PState *, int );
long		blastHdrLoad( void *, char *, long, void * );
void		blastHdrStore( void *, char *, long, void * );
XkReturn	blastSenderPop( Sessn, Msg *, BLAST_HDR * );
int		blast_mask_to_i( BLAST_MASK_PROTOTYPE );

void	blast_phdr( BLAST_HDR * );
char *	blastOpStr( int );
void	blastShowActiveKey( ActiveID *key, char *message );
void	blastShowMstate( MSG_STATE *m, char *message );
char *	blastShowMask( BLAST_MASK_PROTOTYPE );


#else

XkHandle	blastPush();
int		blastControlSessn();
int		blastControlProtl();
void		blastDecIrc();
XkReturn	blastPop();
XkReturn	blastDemux();
Sessn		blastCreateSessn();
void		blast_mapFlush();
int		blast_freeSendSeq();
int		blast_Retransmit();
long		blastHdrLoad();
void		blastHdrStore();
MSG_STATE *	blastNewMstate();
XkReturn	blastSenderPop();
int		blast_mask_to_i();

#ifdef XK_DEBUG
void	blast_phdr();
char *	blastOpStr();
void	blastShowActiveKey();
void	blastShowMstate();
char *	blastShowMask();
#endif /* XK_DEBUG */

#endif /* __STDC__ */

#endif

