c************
c Description:	Definitions for Remote Midas InterFace
c
c @author Jeff Schoen
c @version $Id: rmif.inc,v 1.12 2009/10/20 22:52:03 ntn Exp $
c
c Feb 2005 KCS:
c    Added lastfunc so we don't have to log every single DBUF
c    Added reopen logical for DR 512208
c    Added timeData to sync up with quickly recycling remotes
c    Move timeOut here to include in COMMON
c Oct 2009 JC4/NTN:
c    Added addr_to_string function prototype for BUG 1879
c    Added validRemoteIndex function prototype for BUG 1880
c************
	implicit none
	include 'types.inc'
	include 'midas.inc'
	include 'tables.inc'
	include 'headers.inc'
	include 'qmessages.inc'

c   Defines types of library functions (function prototype)
	integer*4 initialize, compress, uncompress

	integer*4 openSocket, sendPacket, recvPacket
	integer*4 checkRemotes, formAddr, procModify
	integer*4 indexProperty, indexMember
	integer*4 indexRemote, indexRemoteAddr
	integer*4 addr_to_string
	logical*4 validRemoteIndex
	character*8 f2s

	integer*4 BUFSIZE,PKTSIZE,IPADDRSIZE
	integer*4 MAXCHANNEL,MAXREMOTE
	integer*4 MAXPROPERTY,MAXMEMBER
	integer*4 MAXKEY,L_kmsg,QPROPS
	parameter (BUFSIZE=32768, PKTSIZE=8+32768, IPADDRSIZE=8)
	parameter (MAXCHANNEL=128, MAXREMOTE=32)
	parameter (MAXPROPERTY=128, MAXMEMBER=32)
	parameter (MAXKEY=16, L_kmsg=L_mqd, QPROPS=4)

	integer*4 PROP_PIPE, PROP_RESULT, PROP_KEY, PROP_MSG
	parameter (PROP_PIPE=1, PROP_RESULT=2, PROP_KEY=3, PROP_MSG=4)
	integer*4 TYPE_XML, TYPE_RAW, TYPE_ASCII, TYPE_CHECK
	parameter (TYPE_XML=1, TYPE_RAW=2, TYPE_ASCII=3, TYPE_CHECK=4)

	integer*4 SET,GET,RET,ACK,OPEN,CLOSE,SEND,RECV,ADDR,OPENED,CLOSED
	integer*4 PING,PONG,DHDR,DBUF,GETPROP,RETPROP,MODIFY,REPORT
	integer*4 DBUFC,DBUFD,RECEIPT,LINKTEST,DROPPED
	parameter (SET=101, GET=102)
	parameter (RET=103, ACK=104)
	parameter (OPEN=105, CLOSE=106)
	parameter (SEND=107, RECV=108)
	parameter (PING=109, PONG=110)
	parameter (DHDR=111, DBUF=112)
	parameter (ADDR=113, MODIFY=114)
	parameter (OPENED=115, CLOSED=116)
	parameter (DBUFC=117, DBUFD=118)
	parameter (GETPROP=121, RETPROP=122)
	parameter (RECEIPT=123, LINKTEST=124)
	parameter (DROPPED=0)

	integer*4 UDP, RDP
	parameter (UDP=1, RDP=0)

! this is the structure in a packet used to turn UDP into a mix of RDP and TCP.
! Important content must be marked as reliable, the assumption is not.
	STRUCTURE /PACKETSTRUCT/
	  byte		func		! function (SET|GET|RET|ACK|OPEN...)
	  byte		flag		! version / modifiers (BINARY|XML|ASCII)
	  byte		info		! packet info field
	  byte		rep		! raw data representation
	  byte		seq		! packet sequence number (0-127)
	  byte		try		! number of retries
	  byte		rpt		! packet report
	  byte		adj		! bytes in adjunct header
	  UNION
	  MAP
	  byte		data(BUFSIZE)	! data buffer
	  ENDMAP
	  MAP
	  character	string*(BUFSIZE)! data buffer
	  ENDMAP
	  ENDUNION
	ENDSTRUCTURE

! a property is a server quantity to be sent to clients automatically
	STRUCTURE /PROPMEMBSTRUCT/
	  byte		dec,count		! pipe decimation
	  byte		comp,flag		! 0=none
	  integer*4	plotwidth		! width for blocking
	  integer*4	trim1,trim2		! trim frame
	ENDSTRUCTURE

! a property is a server quantity to be sent to clients automatically
	STRUCTURE /PROPERTYSTRUCT/
	  integer*4	type			! pipe, result, message
	  integer*4	members			! number of clients
	  byte		member(MAXMEMBER)	! points to remote client
	  byte		channel(MAXMEMBER)	! chan on the client side
	  character*31	name			! property name
	  character*1	rtype			! result type
	  integer*4	phcb			! pipe header
	  integer*4	len			! result length
	  byte		value(40)		! result last value
	  record /PROPMEMBSTRUCT/ pms(MAXMEMBER)
	ENDSTRUCTURE

! a remote is a client/server intending to open one or more channels
	STRUCTURE /REMOTESTRUCT/
	  character*40	name		! remote node:port
	  integer*4	addr(4)		! IP binary address
	  character*4	machine		! machine data representation
	  byte		state		! current status
	  byte		rep		! raw data representation
	  byte		origin		! origin of connection
	  byte		maxRetry	! max retry
	  byte		maxWindow	! max sliding window
	  byte		fill(7)		! alignment
	  real*8	timePong,timeRetry,latency,timeData	! last reply
	  integer*4	sendOff,sendSeq,sendChk
	  integer*4	sendQueue,sendDrop,sendOther
	  integer*4	sendOffset(128)	! send offset
	  integer*4	sendLength(128)	! send length
	  real*8	sendTime(128)	!
	  byte		sendBuffer(65536) ! send data
	  integer*4	recvOff,recvSeq,recvChk
	  integer*4	recvQueue,recvLost,recvReOrder
	  integer*4	recvOffset(128)	! recv offset
	  integer*4	recvLength(128)	! recv length
	  byte		recvBuffer(65536) ! recv data
	  real*8	recvTime(128)	!
	ENDSTRUCTURE

! a channel is an open connection to a remote property
	STRUCTURE /CHANNELSTRUCT/
	  integer*4	type		! pipe, result, message
	  character*32	name		! property name
	  integer*4	phcb		! local pipe header
	  integer*4	ir		! index of remote
	  integer*4	state		! current state
	  integer*2	dec,index	! decimators
	  character*32	rname		! remote property name
	ENDSTRUCTURE

! the KEYMSG structures for X-Midas
	STRUCTURE /KEYNVSTRUCT/
	  character*40	name
	  character*40	value
	ENDSTRUCTURE
	STRUCTURE /KEYMSGSTRUCT/
	  character*40  addr
	  UNION
	  MAP
	  record /KEYNVSTRUCT/ key(MAXKEY)
	  ENDMAP
	  MAP
	  character*8	func
	  character*(L_mqd-48) data
	  ENDMAP
	  END UNION
	ENDSTRUCTURE

! local/global variables for the RMIF X-Midas primitive
	byte localrep, lastfunc
	integer*4 socket, socketport, nproperty, nremote, nchannel
	integer*4 status, msgmod, msgid, initpipes, logunit
	integer*4 ipaddr(4), maxretry, maxwindow
        logical*4 reopen
        real*8    timeOut
	character timestr*20,logtime*20, machine*4, prelabel*4

	record /KEYMSGSTRUCT/ kmsg
	record /PACKETSTRUCT/ pack
	record /REMOTESTRUCT/ remote(0:MAXREMOTE)
	record /CHANNELSTRUCT/ channel(0:MAXCHANNEL)
	record /PROPERTYSTRUCT/ property(0:MAXPROPERTY)

	common /REMOTES/ remote, nremote
	common /RMICOM/ timeOut, socket, socketport,
     &		property, nproperty, nchannel, channel,
     &		maxretry, maxwindow, prelabel, msgmod, msgid, initpipes,
     &		logunit, logtime, reopen, localrep, lastfunc

! lookup tables for compression algorithms
	byte ctdeltalo(0:255), ctdeltahi(0:255)
	byte utdeltalo(0:255), utdeltahi(0:255)
	common /TABLES/ ctdeltalo, ctdeltahi, utdeltalo, utdeltahi

	real*8 time1,time2,time
	common /TIMECOM/ time, time1, time2
