	PROGRAM RDQUE
C
C	THIS PROGRAM ILLUSTRATES THE NECESSARY STEPS TO READ A MESSAGE
C	FROM THE MA QUEUE.  ANY PROGRAM THAT READS FROM THE QUEUE MUST
C	BE RUN DURING SYSTEM BOOT SO THAT IT IS WAITING FOR THE "MESSAGE
C	IN QUEUE" EVENT FLAG TO BE SET.
C
C	WRITTEN BY: D. LUECK   12/22/87
C
	IMPLICIT COMPLEX (A-Z)

	PARAMETER	EFN = 1		! QIO TO QUEUE SYNC EVENT FLAG
	PARAMETER	LEFN = 2	! TASK WAKEUP EVENT FLAG
	PARAMETER	IZQ = 1		! QUEUE LUN
	PARAMETER	CNS = 6		! CONSOLE LUN
	PARAMETER	MSGLEN = 500	! MAX MESSAGE SIZE
	PARAMETER	QUESUC = "401	! SUCCESS STATUS
	PARAMETER	SUCESS = 1	! INIT SUCCESS STATUS
	PARAMETER	IEEOF = -10	! NO MESSAGES IN SUBQUEUE ERROR
	PARAMETER	IEFOP = -53	! READ NOT ACK MESSAGE ON QUEUE ERROR
	PARAMETER	IEPNT = -71	! NO MESSAGE NEEDS TO BE ACK
	
	INTEGER*2	STATUS		! QUEUE FUNCTION STATUS
	BYTE		STATB(2)	! STATUS AS 2 BYTES
	EQUIVALENCE	(STATUS,STATB)	
	CHARACTER*16	MSGID		! MESSAGE ID SUBQUEUE TO READ MSGS
					!   FROM
	BYTE		MSGBUF(MSGLEN)	! BUFFER MESSAGE READ INTO
	INTEGER*2	RECSIZ		! SIZE OF MESSAGE READ FROM QUEUE
	INTEGER*2	RNA		! READ NOT ACKNOWLEDGE ID
	INTEGER*2	I		! LOOP INDEX

	INTEGER*2	INITQ,ATCHQ,READQ,ACKQ	! FUNCTION RETURN STATUS

	DATA	MSGID	/'SAMPLE_QUEUE'/	! MESSAGE ID NAME

C 	PROGRAM START
C 	-------------

	STATUS = INITQ(IZQ)		     ! SETUP QUEUE EXIT HANDLER
	IF (STATUS.NE.SUCESS) THEN	     ! BAD STATUS
		WRITE (CNS,10) STATUS
10		FORMAT(' RDQUE -- BAD QUEUE STATUS',
	1	       ' ON INIT .. FATAL'/
	2	       '    DIRECTIVE STATUS.. ',I5)
		GOTO 9000
	ELSE
		WRITE (CNS,15)
15		FORMAT(' RDQUE -- INIT SUCCESSFUL')
	END IF		

	STATUS = ATCHQ(IZQ,EFN,MSGID,LEFN)   ! ATTACH TO MESSAGE ID SUBQUEUE
	IF (STATUS.NE.QUESUC) THEN	     ! BAD STATUS
		WRITE (CNS,20) STATB(1),STATB(2)
20		FORMAT(' RDQUE -- BAD QUEUE STATUS',
	1	       ' ON ATTACH TO MESSAGE ID .. FATAL'/
	2	       '    DIRECTIVE STATUS.. ',I5/
	3	       '    I/O STATUS........ ',I5)
		GOTO 9000
	ELSE
		WRITE (CNS,23)
23		FORMAT(' RDQUE -- ATTACH SUCCESSFUL')
	END IF		

25	CALL CLREF(LEFN)			! CLEAR WAKEUP EVENT FLAG 
		
27	STATUS = READQ(IZQ,EFN,MSGID,MSGBUF,
	1	       MSGLEN,RECSIZ,RNA)	! READ MESSAGE FROM QUEUE
	IF (STATB(2).EQ.IEEOF) GOTO 1000	! NOTHING TO READ
	IF (STATB(2).EQ.IEFOP) GOTO 100		! READ NO ACK MESSAGE ON QUEUE;
						!   TRY TO ACK IT
	IF (STATUS.NE.QUESUC) THEN	     ! BAD STATUS
		WRITE (CNS,30) STATB(1),STATB(2)
30		FORMAT(' RDQUE -- BAD QUEUE STATUS',
	1	       ' ON READ FROM QUEUE .. FATAL'/
	2	       '    DIRECTIVE STATUS.. ',I5/
	3	       '    I/O STATUS........ ',I5)
		GOTO 9000
	ELSE
		WRITE (CNS,35)
35		FORMAT(' RDQUE -- READ SUCCESSFUL')
	END IF		
		
C	DISPLAY MESSAGE

	WRITE (CNS,40) (MSGBUF(I),I=35,RECSIZ)
40	FORMAT (4X,75A1)

C	----------------- BEGIN PROCESSING THE MESSAGE --------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	-------------------------------------------------------------
C	----------------- PROCESSED THE MESSAGE ---------------------

100	STATUS = ACKQ(IZQ,EFN,MSGID,RNA)	! ACK THE MESSAG! JUST READ
	IF (STATB(2).EQ.IEPNT) GOTO 1000	! NO MESSAGE TO ACK
	IF (STATUS.NE.QUESUC) THEN	     	! BAD STATUS
		WRITE (CNS,110) STATB(1),STATB(2)
110		FORMAT(' RDQUE -- BAD QUEUE STATUS',
	1	       ' ON ACKNOWLEDGE .. FATAL'/
	2	       '    DIRECTIVE STATUS.. ',I5/
	3	       '    I/O STATUS........ ',I5)
		GOTO 9000
	ELSE
		WRITE (CNS,120)
120		FORMAT(' RDQUE -- ACKNOWLEDGE SUCCESSFUL')
	END IF		

	GOTO 27					! TRY TO READ ANOTHER

1000	CONTINUE

	CALL WAITFR(LEFN)			! WAIT FOR ANOTHER MESSAGE
	GOTO 25

9000	CALL DISCQ(IZQ)				! DISCONNECT
	CALL EXIT

	END
