/*external*/
/*******************************************************************************
Copyright by Telesoft Europe AB 1990, 1991.
Copyright by TeleLOGIC Malmoe AB 1991, 1992, 1993.
Copyright by TeleLOGIC AB 1994, 1995.
Copyright by Telelogic AB 1996 - 1999.
All rights reserved. No part of this document or computer program may be 
reproduced, transmitted, transcribed, or translated into any language in 
any form by any means without prior written permission of TeleLOGIC AB,
in accordance with the terms and conditions stipulated in the agreement or
contract under which the program(s) have been supplied.
Information in this document or computer program is subject to change without
notice. 
*******************************************************************************/

/*end_external*/

/******************************************************************************
*  Headerfile defining the public interface to SDT's postmaster mechanism.
*
*
*   File: sp.h
*
*   Created by    : Per Madsen      1990-05-22
*
*   First revision: SDT2-17.3       1989-12-22
*   Second        :                 1990-02-21
*
*   1990-08-20 PM SPTOOLPARAMETERLEN increased to 200
*/
/******************************************************************************
*/

/*external*/
#ifndef _sp_h
#define _sp_h


/*****************************************************************************
* 
* XNOPROTO not defined -> use function prototypes
*/
#ifndef XPP
#ifndef XNOPROTO
#define XPP(x)  x
#else
#define XPP(x)  ()
#endif
#endif 
#ifndef MAX
#define MAX(a,b)	((a>b)?a:b)
#endif
/******************************************************************************
*  Necessary header files to include.
*/
/*#include "suos.h"		 OS dependent INT16/INT32*/
#ifndef VXWORKS_SDT
#ifndef INT16
	typedef short INT16;
#endif
#ifndef INT32
	typedef long  INT32;
#endif
#if ! defined (WINDOWS)

#ifndef BOOL
	typedef unsigned char BOOL;
#endif
#endif /*WINDOWS*/
#ifndef FALSE
#define FALSE	(BOOL)0	
#endif
#ifndef TRUE
#define TRUE 	(BOOL)1	
#endif
#endif /*VXWORKS_SDT*/

#include "spevent.h"		 /*Defined events. */

/*#include "suerrid.h"*/
/* errors from postmaster */
#define SU_POSTMASTER_ERRBASE     1000
/*
#define SPNOSESSION               (SU_POSTMASTER_ERRBASE + 1)
#define SPALREADYCONNECTED        (SU_POSTMASTER_ERRBASE + 2)
#define SPNOCHANNEL               (SU_POSTMASTER_ERRBASE + 3)
#define SPNOPOSTMASTER            (SU_POSTMASTER_ERRBASE + 4)
#define SPNOMESSAGE               (SU_POSTMASTER_ERRBASE + 5)
#define SPTIMEDOUT                (SU_POSTMASTER_ERRBASE + 6)
#define SPNOTMPDIR                (SU_POSTMASTER_ERRBASE + 7)
#define SPNOACCESS                (SU_POSTMASTER_ERRBASE + 8)

#define SPNOSUCHPID               (SU_POSTMASTER_ERRBASE + 9)
#define SPNOTCONNECTED            (SU_POSTMASTER_ERRBASE + 10)
#define SPMAXINSTANCESOFTOOL      (SU_POSTMASTER_ERRBASE + 11)
#define SPALREADYSTARTED          (SU_POSTMASTER_ERRBASE + 12)

#define SPNOMEMORY                (SU_POSTMASTER_ERRBASE + 13)
#define SPBUFFERTFULL             (SU_POSTMASTER_ERRBASE + 14)
#define SPFILENOTFOUND            (SU_POSTMASTER_ERRBASE + 15)
#define SPSYSTEMQUOTEEXCEEDED     (SU_POSTMASTER_ERRBASE + 16)

#define SPTOOLARGEMESSAGE         (SU_POSTMASTER_ERRBASE + 17)
*/


/*external*/

/******************************************************************************
* No time-out when reading a message.
*/
#define SPWAITFOREVER		-1

#ifndef NULL
#define NULL 			0
#endif

/******************************************************************************
* Error handling.
*/

/* return values from functions */
#define SPOK                    0
#define SPERROR                 -1

/* Error classes */
#define SPCLASSINFO             1
#define SPCLASSERROR            2
#define SPCLASSFATAL            3

/******************************************************************************
* Environment dependencies.
*/

#define SPMAXNOOFDEFINEDTOOLS   100   /* Assumed max no of tools types in post */
#define SPMAXNOOFINSTANCES      32    /* No of instances when using '*' */

#define SPTOOLNAMELEN           15    /* Max len of tool.  14, max in SYS5 */
#define SPTOOLPARAMETERLEN      200   /* Max len of parameter when creating a tool*/

#define SPTOOLDIR               "sdtbin"
#define SPTMPDIR                "sdttmp"
#define SPUNIXSOCKPREFIX		".sdt"
#define SPPOSTPID               "sdtpostpid"

/******************************************************************************
* Type definitions.
*/
typedef void		(*SPFUNC)();
typedef INT32		SPTIMEOUT;
typedef INT32		SPEVENT;
typedef INT32		SPSIZE;
typedef INT32		SPPID;	
typedef INT32		SPTIME;
#ifndef WINDOWS
typedef struct sockaddr_un UNIXSOCKTYPE;
#endif
#define FIXEDMESSSIZE  20 
#define INT_SIZE 10
typedef struct SPMessType {
  SPEVENT	event;		/* Event type of mess. These are 
				  defined in spevent.h */
  char         *data;           /* Handle to data. */
  SPSIZE	size;		/* Size of data. */
  SPPID 	sender;		/* Pid of the one who sent mess */
  SPTIME	timeStamp;	/* Timestamp when mess was sent by Pid*/
} SPMESSTYPE, *SPPMESSTYPE;

/******************************************************************************
* Available variables
*/

/*end_external*/
/*
SPerrno contains an error code when a call to a function in this package fails. 
*/
/*external*/
extern INT16 sperrno;

/*end_external*/ 
/*
 SPPortNo contains a descriptor to the port where incoming 
messages are found. The state of the variable prior to the call of
SPInitConnection is undefined. This facility could be used on certain
systems ( UNIX ) to determine when a message is available instead of
the asyncrounous mechanism that "func" in SPInitConnection provides.
The descriptor mustn't be used for other purposes than described above.

Currently this variable has an undefined value on MS-DOS
implementations.
*/
/*external*/
/*
extern INT16 spPortNO;
*/


/******************************************************************************
*  Functional interface.
*/

/*end_external*/
/******************************************************************************
*  SPInitConnection
*******************************************************************************
SPInitConnection initiates a session and establishes a connection with the
postmaster.
Inparameters    func    A pointer to a function. The function is later called
                        when a message is available. If this facility is not
                        used this parameter should be set to NULL.
                toolType Type of tool which connects. Used when doing external
                        connections.
                argv0   Should be argv[0] of the program otherwise NULL
Returns:        >0      The pid of the process.
                SPERROR SPInitConnection failed. sperrno is set.
Errors:         SPNOPOSTMASTER
                SPTIMEDOUT
                SPALREADYCONNECTED
*/
/*external*/
SPPID SPInitConnection XPP(( SPFUNC func, INT32 toolType, char * argv0));

/*end_external*/

/*******************************************************************************
 SPExitConnection 
*******************************************************************************
SPExitConnection exists a session and disconnects the connection with the
postmaster. Subsequent calls to SP.. functions will return the error code
SPNOSESSION until a new SPInitConenction is done.
Returns:	SPOK	Status OK.
		SPERROR	SPExitCOnenction failed sperrno is set.
Errors:		SPNOCHANNEL
		SPNOSESSION
*/
/*external*/
INT16 SPExitConnection XPP(( void ));

/*end_external*/

/*******************************************************************************
 SPClientExitConnection
*******************************************************************************
Similar to SPExitConnection, except that only the client side is closed.
Used to manage forked subprocesses, where the child process, need to close
the conenction to the postmaster, wherease the parent should still be connected.
*/
/*external*/
INT16 SPClientExitConnection XPP(( void));

/*end_external*/

/*******************************************************************************
  SPSendMessage
*******************************************************************************
SPSendMessage sends a message to the process which has process id toPid. The
memory allocated for Data is NOT deallocated within SPSendMessage.

Inparameters:	event	Type of message.
		data	Handle to an information block.
		size	Size of data.
		toPid	Pid of the message's receiver.
Returns:	SPOK	Status OK.
		SPERROR	SPSendMessage failed. sperrno is set.
Errors:		SPNOCHANNEL
		SPTIMEDOUT
		SPTOOLARGEMESSAGE
		SPNOSESSION
		+ Error codes from the memory handling. See "sgmem.h"
*/
/*external*/
INT16 SPSendMessage XPP((SPEVENT event, char * data, SPSIZE size, SPPID toPid));


/*end_external*/

/******************************************************************************
*  SPBroadcastMessage
*******************************************************************************
SPBroadcastMessage sends a message to all processes that subscribes on Event. 
The memory allocated to Data is NOT deallocated within SPBroadcastMessage
 
Inparameters:	event	Type of message.
		data	Handle to an information block.
	 	size	Size of data.
Returns:	SPOK	Status OK.
		SPERROR	SPBroadcastMessage failed. sperrno is set.
Errors:		SPNOCHANNEL
		SPNOPOSTMASTER 
		SPTIMEDOUT 
		SPTOOLARGEMESSAGE
		SPSPNOSESSION
		+ Error codes from the memory handling. See "sgmem.h"
*/
/*external*/
INT16 SPBroadcastMessage XPP((SPEVENT event, char * data, SPSIZE size));

/*end_external*/

/******************************************************************************
*  SPReadMessage
*******************************************************************************
SPReadMessage reads a message from the queue of unread messages that 
the postmaster has sent. When the message is read, it's also consumed, 
that is- the message is removed from the queue.
The function allocates the necessary amount of memory needed for the Data
component in Mess using the dynamic memory handling routines found in 
"sgmem.h" The application using this routine is responsible for freeing the
allocated memory when it's no longer needed.
The function "func", see SPInitConnection, is not called within this routine
when waiting for a message and a message arrives.
Inparameters:	timeOut Maximum amount of time the function waits for a message.
			If a message hasn't arrived, when timeOut expires, the
			function returns with return value -1 and sperrno is set
			to SPNOMESSAGE. If however when a message arrives,
			the function reads the message and returns immedeately.
			If the function should wait until a message arrives,
			which could mean forever, timeOut should be set to
			SPWAITFOREVER. 
			TimeOut is counted in milliseconds.
Outparameters:	mess	Mess is filled out with the first message in
			message queue.
Returns:	SPOK	Status OK.
		SPERROR	SPReadMessage failed. sperrno is set.
Errors:		SPNOCHANNEL
		SPNOMESSAGE
		SPNOSESSION
		SPINVALIDMESSAGE
		+ Error codes from the memory handling. See "sgmem.h"
*/
/*external*/
INT16 SPReadMessage XPP((SPPMESSTYPE mess, SPTIMEOUT timeOut));

/*end_external*/

/*******************************************************************************
  SPPeekMessage
*******************************************************************************
SPPeekMessage has the same behavior as SPReadMessage except that the message
is not removed from the message queue.
Inparameters:	timeOut Maximum amount of time the function waits for a message.
			If a message hasn't arrived, when timeOut expires, the
			function returns with return value -1 and sperrno is set
			to SPNOMESSAGE. If however when a message arrives,
			the function reads the message and returns immedeately.
			If the function should wait until a message arrives,
			which could mean forever, timeOut should be set to
			SPWAITFOREVER. 
			TimeOut is counted in milliseconds.
Outparameters:	mess	Mess is filled out with the first message in
 			message queue.
Returns:	SPOK	Status OK. 
	 	SPERROR	SPPeekMessage failed. sperrno is set.
Errors:		SPNOCHANNEL
		SPNOMESSAGE
		SPNOSESSION
		SPINVALIDMESSAGE
		+ Error codes from the memory handling. See "sgmem.h"
*/
/*external*/
INT16 SPPeekMessage XPP((SPPMESSTYPE mess, SPTIMEOUT timeOut));

/*end_external*/

/*******************************************************************************
  SPMessIsOfType
*******************************************************************************
SPMessIsOfType returns the type of message "Event" that is available without
removing the message from the message queue.
Returns:	SPERROR	SPMessIsOfType failed. sperrno is set.
		Otherwise the event type of the message.
Errors:		SPNOCHANNEL
		SPNOMESSAGE
		SPNOSESSION
		SPINVALIDMESSAGE
*/
/*external*/
SPEVENT SPMessIsOfType XPP((void));

/*end_external*/

/*******************************************************************************
  SPSkipMessage
*******************************************************************************
SPSkipMessage removes and deletes the first message in message queue, without
allocating any memeory for it.
Returns:	SPOK	Status Ok.
		SPERROR	SPSkipMessage failed sperrno is set.
Errorn		SPNOCHANNEL
		SPNOMESSAGE
		SPNOSESSION
*/
/*external*/
INT16 SPSkipMessage XPP((void));

/*end_external*/

/*******************************************************************************
  SPDecodeErrorMessage
*******************************************************************************
SPDecodeErrorMessage decodes an errormessage from postmaster. Such a message
is of type SEOPFAILED. This message is only returned to the application that
sent has sent a message to the postmaster, which the postmaster couldn't 
execute or transfer properly.
Inparameters:	mess	Error message. 'mess.event' must be SEOPFAILED.
Outparameters:	event	Event type of message that failed.
		class	Tell how serious the error was . Possible values are -
			SPCLASSINFO, SPCLASSERROR, SPCLASSFATAL.
		error	Error code that describes the error. Possible values
			are found in the error code section in this file.
		pid	If the postmaster failed in operation against another
			application, the application's pid is found in this
			variable.
		data	The first SPERRORDATALEN bytes (maximun) of the failed
			message's data component is returned in this variable.
Returns:	SPOK	Status Ok.
                SPERROR SPSkipMessage failed sperrno is set.
Errors:		SPINVALIDMESSAGE
		SPNOMEMORY
*/
/*external*/
INT16 SPDecodeErrorMessage XPP((SPPMESSTYPE mess,
                         SPEVENT * myevent,
                         INT16 * lClass,
                         INT16 * error,
                         SPPID * pid,
                         char  *hData));

/*end_external*/

/*******************************************************************************
 End of public interface to the postmaster. 
*******************************************************************************
*/

BOOL SPCGetTmpDir XPP((char  *tmpPath));

void SPMakeFixedMess XPP(( 
              char* fixedMess,
              SPEVENT event,
              SPSIZE size,
              SPPID fromPid,
              SPPID toPid,
              SPTIME timeStamp));

void SPGetFixedMess XPP((
              char* fixedMess,
              SPEVENT * event,
              SPSIZE * size,
              SPPID * fromPid,
              SPPID * toPid,
              SPTIME * timeStamp));

#ifndef WINDOWS
BOOL SPCMakeUnixSocket XPP((
             SPPID pid,
             int * s,
             UNIXSOCKTYPE *  sAddr,
             int * addrLen));
#endif

SPTIME SPCGetSysTime XPP((void));

/*external*/
#endif /* _sp_h */
/*end_external*/
