HLCSTCP2 ;SFIRMFO/RSD - BI-DIRECTIONAL TCP ;09/13/2006
;;1.6;HEALTH LEVEL SEVEN;**19,43,49,57,63,64,66,67,76,77,87,109,133**;Oct 13,1995;Build 13
;Per VHA Directive 2004-038, this routine should not be modified.
;Sender
;Request connection, send outbound message(s) delimited by MLLP
;Input : HLDP=Logical Link to use
; Set up error trap
N $ETRAP,$ESTACK S $ETRAP="D ERROR^HLCSTCP2"
N HLMSG,HLPORT,HLRETRY,HLRETMG,HLTCPO,POP
;HLRETRY=number of retranmission for this link,HLRETMG=alert sent
S HLTCPO=HLDP,HLMSG="",(HLRETRY,HLRETMG)=0
;
;set IO(0) to the null device
S IO(0)=$S(^%ZOSF("OS")["OpenM":$S($$OS^%ZOSV()["VMS":"_NLA0:",$$OS^%ZOSV()["UNIX":"/dev/null",1:$P),^%ZOSF("OS")["DSM":"_NLA0:",1:$P)
O IO(0) U IO(0)
;
;persistent conection, open connection first, HLPORT=open port
I $G(HLTCPLNK)["Y" F Q:$$OPEN G EXIT:$$STOP^HLCSTCP H 1
F D QUE Q:$$STOP^HLCSTCP D:'HLMSG Q:$G(HLCSOUT)
. ;no messages to send
. D MON^HLCSTCP("Idle") H 3
. ;persistent connection, no retention
. Q:$G(HLTCPLNK)["Y"
. D MON^HLCSTCP("Retention")
. N % I 0
. ;if message comes in or ask to stop
. F %=1:1:HLTCPRET H 1 I $$STOP^HLCSTCP!$O(^HLMA("AC","O",HLDP,0)) Q
. E S HLCSOUT=2 Q
. Q:$$STOP^HLCSTCP
. D MON^HLCSTCP("Idle")
;Close port
I $D(HLPORT) D CLOSE^%ZISTCP K HLPORT
EXIT Q
;
QUE ; -- Check "OUT" queue for processing IF there is a message do it
; and then check the link if it open or not
N HL,HLN,HLARR,HLHDR,HLI,HLJ,HLMSA,HLRESP,HLRESLT,HLRETRM,HLTCP,HLTCPI,X,Z,HLREREAD
D MON^HLCSTCP("Check out")
;HLMSG=next msg, set at tag DONE
I 'HLMSG S HLMSG=+$O(^HLMA("AC","O",HLDP,0)),HLRETRY=0 Q:'HLMSG
;
;**109**
;Temporarily lock ^HLMA to flush buffer and ensure edits are complete
;L +^HLMA(HLMSG):1 I '$T S HLMSG=0 Q
;L -^HLMA(HLMSG)
;
S HLI=+$G(^HLMA(HLMSG,0)),HLJ=$O(^("MSH",0)),HLTCP=""
;don't have message text or MSH, kill x-ref and decrement 'to send'
I 'HLI!'HLJ K ^HLMA("AC","O",HLDP,HLMSG) D LLCNT^HLCSTCP(HLDP,3,1) S HLMSG=0 Q
;update msg status to 'being transmitted'; if cancelled decrement link and quit
I '$$CHKMSG(1.5) D LLCNT^HLCSTCP(HLDP,3,1) S HLMSG=0 Q
;number of retransmissions for message
S HLRETRM=+$P(^HLMA(HLMSG,"P"),U,5)
;retries exceeded, HLRETRA:action i=ignore, r=restart, s=shutdown
;quit if restart or shutdown, link is going down
I HLRETRY>HLDRETR D Q:"I"'[HLRETRA
. D MON^HLCSTCP("Error")
. ;only 1 alert per link up time, don't send if restart
. D:'HLRETMG&(HLRETRA'="R")
.. ;send alert
.. N XQA,XQAMSG,XQAOPT,XQAROU,XQAID,Z
.. ;get mailgroup from file 869.3
.. S HLRETMG=1,Z=$P($$PARAM^HLCS2,U,8) Q:Z=""
.. S XQA("G."_Z)="",XQAMSG=$$HTE^XLFDT($H,2)_" HL7 LL "_$P(^HLCS(870,HLDP,0),U)_" exceeded retries. LL will "_$S(HLRETRA="S":"shutdown.",HLRETRA="R":"restart.",1:"keep trying.")
.. D SETUP^XQALERT
. ;quit if action is ignore
. Q:"I"[HLRETRA
. ;this will shutdown this link
. S HLCSOUT=1
. ;action is shutdown, set shutdown flag so LM won't restart
. S:HLRETRA="S" $P(^HLCS(870,HLDP,0),U,15)=1
. D STATUS^HLTF0(HLMSG,4,103,"LLP Exceeded Retry Param")
I '$$OPEN Q
D MON^HLCSTCP("Send")
; -- data passed in global array, success=1
I $$WRITE(HLMSG)<0 Q
S (HLTCP,HLTCPI)=HLMSG,HLRETRY=HLRETRY+1,HLRETRM=HLRETRM+1
;update status to awaiting response, decrement link if cancelled
I '$$CHKMSG(1.7) D LLCNT^HLCSTCP(HLDP,3,1) S HLMSG=0 Q
;set transmission count, get ACKTIMEOUT override
S $P(^HLMA(HLMSG,"P"),U,5)=HLRETRM I $P(^("P"),U,7) S HLN("ACKTIME")=+$P(^("P"),U,7)
;get header of message just sent
K HLJ M HLJ=^HLMA(HLMSG,"MSH")
;first component of sending app.
S HLN("ECH")=$$P^HLTPCK2(.HLJ,2),HLN("SAN")=$P($$P^HLTPCK2(.HLJ,3),$E(HLN("ECH")))
;msg type, msg. id, commit ack, and app. ack parameter
S HLN("TYPE")=$$P^HLTPCK2(.HLJ,1),HLN("MID")=$$P^HLTPCK2(.HLJ,10),HLN("ACAT")=$$P^HLTPCK2(.HLJ,15),HLN("APAT")=$$P^HLTPCK2(.HLJ,16)
;MSA segment, message is a response, can't have an a. ack.
S Z=$$MSA^HLTP3(+^HLMA(HLMSG,0)) I Z]"" S:HLN("ACAT")="" HLN("ACAT")="NE" S HLN("APAT")="NE"
;for batch/file with commit ack, reset c. ack and a. ack variables
I "BHS,FHS"[HLN("TYPE") S Z=$E(HLJ(1,0),5),X=$$P^HLTPCK2(.HLJ,9),HLN("ACAT")=$P(X,Z,5),HLN("APAT")=$P(X,Z,6),HLN("MID")=$$P^HLTPCK2(.HLJ,11)
;get event protocol
S HLN("EID")=+$P(^HLMA(HLMSG,0),U,8),X=$G(^ORD(101,HLN("EID"),770))
;set link counter to msg sent
D LLCNT^HLCSTCP(HLDP,4)
;commit and app. ack is never, update status to complete and hang UNI-DIRECTIONAL WAIT
I HLN("ACAT")="NE",HLN("APAT")="NE" D Q
.D DONE(3)
.;
.;
.H $G(HLDWAIT)
;
;do structure is to stack error
D
. N $ETRAP,$ESTACK S $ETRAP="D RDERR^HLCSTCP2"
. ;HL*1.6*87: Read acknowledgement.
. ;Loop to re-read from buffer when receiving incorrect ack.
. F D Q:'+$G(HLREREAD)
.. S HLREREAD=1
.. ;override ack timeout
.. I $G(HLN("ACKTIME")) N HLDBACK S HLDBACK=HLN("ACKTIME")
.. ;check for response, quit if no-response, msg will be resent
.. ;HLRESP=ien 773^ien 772 for response message
.. S HLRESP=$$READ^HLCSTCP1()
.. ;if no response, decrement counter and quit
.. I 'HLRESP D Q
...D LLCNT^HLCSTCP(HLDP,4,1)
...S HLREREAD="0^No Response"
...;check if the port needs to be closed and re-opened before the next re-transmission attempt
...I $G(HLDRETRY("CLOSE")) D CLOSE^%ZISTCP K HLPORT
.. ;X 0=re-read msg, 1=commit ack, 3=app ack success, 4=error
.. S X=$$RSP^HLTP31(HLRESP,.HLN)
.. ;X=0, re-read msg. Incorrect ack (bad MSH,MSA,msg id,or sending app)
.. Q:'X
.. ;commit ack - done
.. I X=1 D S HLREREAD="0^Commit Ack" Q
... ;don't need app. ack, set status to complete
... I "NE"[HLN("APAT") D Q
....D DONE(3)
....;
... ;response is deferred, set status to awaiting ack
... D DONE(2)
...;
.. ;Error, HLRESLT=error number^error message from HLTP3
.. I X=4 D Q
... D DONE(4,+$G(HLRESLT),$P($G(HLRESLT),U,2))
...;
... S HLREREAD="0^Error"
.. ;app ack was successful
.. D DONE(3) S HLREREAD="0^App Ack"
..;
Q
;
DCSEND ;direct connect
; Set up error trap
N $ETRAP,$ESTACK S $ETRAP="D ERROR^HLCSTCP2"
;override ack timeout
I $G(HLP("ACKTIME")) N HLDBACK S HLDBACK=HLP("ACKTIME")
I $$WRITE(HLMSG)<0 D:$G(HLERROR)]"" Q ;HL*1.6*77
. D STATUS^HLTF0(HLMSG,4,$P(HLERROR,"^"),$P(HLERROR,"^",2),1) ;HL*1.6*77
. D LLCNT^HLCSTCP(HLDP,3,1)
D LLCNT^HLCSTCP(HLDP,4)
;do structure is to stack error
D
. N $ETRAP,$ESTACK S $ETRAP="D RDERR^HLCSTCP2"
. ;HLRESP=ien 773^ien 772 for response message
. S HLRESP=$$READ^HLCSTCP1()
;
D DONE(3):$G(HLRESP),DONE(4,108,$S($G(HLERROR)]"":$P(HLERROR,"^",2),1:"No response")):'$G(HLRESP)
I $G(HLERROR)']"" D
.D MON^HLCSTCP("Idle")
.I '$G(HLRESP) S HLERROR="108^No response"
;Close port
I $D(HLPORT) D CLOSE^%ZISTCP K HLPORT
Q
;
DONE(ST,ERR,ERRMSG) ;set status to complete
;ST=status, ERR=error ien, ERRMSG=error msg
D STATUS^HLTF0(HLMSG,ST,$G(ERR),$G(ERRMSG),1)
;
;**109**
D DEQUE^HLCSREP(HLDP,"O",HLMSG)
;
;check for more msg.
I $G(HLPRIO)'="I" S HLMSG=+$O(^HLMA("AC","O",HLDP,0)),HLRETRY=0
Q
;
CHKMSG(HLI) ;check status of message and update if not cancelled
;input: HLI=new status, HLMSG=ien of msg in 773
;returns 1=msg was updated, 0=msg has been canceled
N X
;
;**109**
;F L +^HLMA(HLMSG,"P"):1 Q:$T H 1
;
;
; New HL*1.6*77 code starting here...
I '$D(^HLMA(HLMSG,"P")) D Q 0
. S HLERROR="2^Missing status field"
. D STATUS^HLTF0(HLMSG,4,$P(HLERROR,U),$P(HLERROR,U,2),1)
.;
.;**109**
. D DEQUE^HLCSREP(HLDP,"O",HLMSG)
.;L -^HLMA(HLMSG,"P")
;**end 109**
;
; End of HL*1.6*77 modifications
;
;get status, quit if msg was cancelled
;
;**109**
;S X=+^HLMA(HLMSG,"P") I X=3 L -^HLMA(HLMSG,"P") Q 0
S X=+^HLMA(HLMSG,"P") Q:X=3 0
;
;update status if it is different
I $G(HLI),HLI'=X D STATUS^HLTF0(HLMSG,HLI)
;
;**109**
;L -^HLMA(HLMSG,"P")
;
Q 1
;
WRITE(HLDA) ; write message in HL7 format
; HLDA - ien of message in 773
; - start block $C(11)
; - end block $C(28)
; - record separator $C(13)
;Output(s): 1 - Successful
; -1 - Unsuccessful
;
N HLDA2,HLAR,HLI,LINENO,X
;set error trap, used when called from HLTP3
;
; New HL*1.6*77 code starts here...
N $ETRAP,$ESTACK S $ETRAP="D ERROR^HLCSTCP2"
I $G(^HLMA(HLDA,0))'>0 D Q -1
. S HLERROR="2^Message Text pointer missing"
S HLDA2=+$G(^HLMA(HLDA,0))
; End of HL*1.6*77 modifications...
;
Q:'$G(^HLMA(HLDA,0)) -1 ;HL*1.6*77
; header is in ^HLMA(, message is in ^HL(772,
S LINENO=1,HLI=0,HLAR="^HLMA(HLDA,""MSH"")"
U IO
D W $C(13) S HLAR="^HL(772,HLDA2,""IN"")",HLI=0 D
. F S HLI=$O(@HLAR@(HLI)) Q:'HLI S X=$G(^(HLI,0)) D
.. ;first line, need start block char.
.. S:LINENO=1 X=$C(11)_X
.. I X]"" W X,!
.. ;send CR for blank lines
.. I X="" W $C(13)
.. S LINENO=LINENO+1
; Sends end block for this message
S X=$C(28)_$C(13)
U IO W X,!
I $G(IO(0))'="",$G(IO(0))'=IO U IO(0) ;switch to null device if opened to prevent 'leakage'
Q 1
;
OPEN() ; -- Open TCP/IP device (Client)
;HLPORT=port, defined only if port is open
;HLPORTA=number of attempted opens
I $D(HLPORT) S IO=HLPORT D Q 1
. U IO
. I HLOS["OpenM" X "U IO:(::""-M"")" ;use packet mode on Cache'
N HLDOM,HLI,HLIP,HLPORTA
G OPENA^HLCSTCP3
;
RDERR D RDERR^HLCSTCP4 Q ; Exceeded 10,000 bytes, so split on 12/2/03-LJA
ERROR D ERROR^HLCSTCP4 Q ; Exceeded 10,000 bytes, so split on 12/2/03-LJA
;
CC(X) ;cleanup and close
D MON^HLCSTCP(X)
I $D(HLPORT) D CLOSE^%ZISTCP K HLPORT
H 2
Q
HLCSTCP2 ;SFIRMFO/RSD - BI-DIRECTIONAL TCP ;09/13/2006
+1 ;;1.6;HEALTH LEVEL SEVEN;**19,43,49,57,63,64,66,67,76,77,87,109,133**;Oct 13,1995;Build 13
+2 ;Per VHA Directive 2004-038, this routine should not be modified.
+3 ;Sender
+4 ;Request connection, send outbound message(s) delimited by MLLP
+5 ;Input : HLDP=Logical Link to use
+6 ; Set up error trap
+7 NEW $ETRAP,$ESTACK
SET $ETRAP="D ERROR^HLCSTCP2"
+8 NEW HLMSG,HLPORT,HLRETRY,HLRETMG,HLTCPO,POP
+9 ;HLRETRY=number of retranmission for this link,HLRETMG=alert sent
+10 SET HLTCPO=HLDP
SET HLMSG=""
SET (HLRETRY,HLRETMG)=0
+11 ;
+12 ;set IO(0) to the null device
+13 SET IO(0)=$SELECT(^%ZOSF("OS")["OpenM":$SELECT($$OS^%ZOSV()["VMS":"_NLA0:",$$OS^%ZOSV()["UNIX":"/dev/null",1:$PRINCIPAL),^%ZOSF("OS")["DSM":"_NLA0:",1:$PRINCIPAL)
+14 OPEN IO(0)
USE IO(0)
+15 ;
+16 ;persistent conection, open connection first, HLPORT=open port
+17 IF $GET(HLTCPLNK)["Y"
FOR
IF $$OPEN
QUIT
IF $$STOP^HLCSTCP
GOTO EXIT
HANG 1
+18 FOR
DO QUE
IF $$STOP^HLCSTCP
QUIT
IF 'HLMSG
Begin DoDot:1
+19 ;no messages to send
+20 DO MON^HLCSTCP("Idle")
HANG 3
+21 ;persistent connection, no retention
+22 IF $GET(HLTCPLNK)["Y"
QUIT
+23 DO MON^HLCSTCP("Retention")
+24 NEW %
IF 0
+25 ;if message comes in or ask to stop
+26 FOR %=1:1:HLTCPRET
HANG 1
IF $$STOP^HLCSTCP!$O(^HLMA("AC","O",HLDP,0))
QUIT
+27 IF '$TEST
SET HLCSOUT=2
QUIT
+28 IF $$STOP^HLCSTCP
QUIT
+29 DO MON^HLCSTCP("Idle")
End DoDot:1
IF $GET(HLCSOUT)
QUIT
+30 ;Close port
+31 IF $DATA(HLPORT)
DO CLOSE^%ZISTCP
KILL HLPORT
EXIT QUIT
+1 ;
QUE ; -- Check "OUT" queue for processing IF there is a message do it
+1 ; and then check the link if it open or not
+2 NEW HL,HLN,HLARR,HLHDR,HLI,HLJ,HLMSA,HLRESP,HLRESLT,HLRETRM,HLTCP,HLTCPI,X,Z,HLREREAD
+3 DO MON^HLCSTCP("Check out")
+4 ;HLMSG=next msg, set at tag DONE
+5 IF 'HLMSG
SET HLMSG=+$ORDER(^HLMA("AC","O",HLDP,0))
SET HLRETRY=0
IF 'HLMSG
QUIT
+6 ;
+7 ;**109**
+8 ;Temporarily lock ^HLMA to flush buffer and ensure edits are complete
+9 ;L +^HLMA(HLMSG):1 I '$T S HLMSG=0 Q
+10 ;L -^HLMA(HLMSG)
+11 ;
+12 SET HLI=+$GET(^HLMA(HLMSG,0))
SET HLJ=$ORDER(^("MSH",0))
SET HLTCP=""
+13 ;don't have message text or MSH, kill x-ref and decrement 'to send'
+14 IF 'HLI!'HLJ
KILL ^HLMA("AC","O",HLDP,HLMSG)
DO LLCNT^HLCSTCP(HLDP,3,1)
SET HLMSG=0
QUIT
+15 ;update msg status to 'being transmitted'; if cancelled decrement link and quit
+16 IF '$$CHKMSG(1.5)
DO LLCNT^HLCSTCP(HLDP,3,1)
SET HLMSG=0
QUIT
+17 ;number of retransmissions for message
+18 SET HLRETRM=+$PIECE(^HLMA(HLMSG,"P"),U,5)
+19 ;retries exceeded, HLRETRA:action i=ignore, r=restart, s=shutdown
+20 ;quit if restart or shutdown, link is going down
+21 IF HLRETRY>HLDRETR
Begin DoDot:1
+22 DO MON^HLCSTCP("Error")
+23 ;only 1 alert per link up time, don't send if restart
+24 IF 'HLRETMG&(HLRETRA'="R")
Begin DoDot:2
+25 ;send alert
+26 NEW XQA,XQAMSG,XQAOPT,XQAROU,XQAID,Z
+27 ;get mailgroup from file 869.3
+28 SET HLRETMG=1
SET Z=$PIECE($$PARAM^HLCS2,U,8)
IF Z=""
QUIT
+29 SET XQA("G."_Z)=""
SET XQAMSG=$$HTE^XLFDT($HOROLOG,2)_" HL7 LL "_$PIECE(^HLCS(870,HLDP,0),U)_" exceeded retries. LL will "_$SELECT(HLRETRA="S":"shutdown.",HLRETRA="R":"restart.",1:"keep trying.")
+30 DO SETUP^XQALERT
End DoDot:2
+31 ;quit if action is ignore
+32 IF "I"[HLRETRA
QUIT
+33 ;this will shutdown this link
+34 SET HLCSOUT=1
+35 ;action is shutdown, set shutdown flag so LM won't restart
+36 IF HLRETRA="S"
SET $PIECE(^HLCS(870,HLDP,0),U,15)=1
+37 DO STATUS^HLTF0(HLMSG,4,103,"LLP Exceeded Retry Param")
End DoDot:1
IF "I"'[HLRETRA
QUIT
+38 IF '$$OPEN
QUIT
+39 DO MON^HLCSTCP("Send")
+40 ; -- data passed in global array, success=1
+41 IF $$WRITE(HLMSG)<0
QUIT
+42 SET (HLTCP,HLTCPI)=HLMSG
SET HLRETRY=HLRETRY+1
SET HLRETRM=HLRETRM+1
+43 ;update status to awaiting response, decrement link if cancelled
+44 IF '$$CHKMSG(1.7)
DO LLCNT^HLCSTCP(HLDP,3,1)
SET HLMSG=0
QUIT
+45 ;set transmission count, get ACKTIMEOUT override
+46 SET $PIECE(^HLMA(HLMSG,"P"),U,5)=HLRETRM
IF $PIECE(^("P"),U,7)
SET HLN("ACKTIME")=+$PIECE(^("P"),U,7)
+47 ;get header of message just sent
+48 KILL HLJ
MERGE HLJ=^HLMA(HLMSG,"MSH")
+49 ;first component of sending app.
+50 SET HLN("ECH")=$$P^HLTPCK2(.HLJ,2)
SET HLN("SAN")=$PIECE($$P^HLTPCK2(.HLJ,3),$EXTRACT(HLN("ECH")))
+51 ;msg type, msg. id, commit ack, and app. ack parameter
+52 SET HLN("TYPE")=$$P^HLTPCK2(.HLJ,1)
SET HLN("MID")=$$P^HLTPCK2(.HLJ,10)
SET HLN("ACAT")=$$P^HLTPCK2(.HLJ,15)
SET HLN("APAT")=$$P^HLTPCK2(.HLJ,16)
+53 ;MSA segment, message is a response, can't have an a. ack.
+54 SET Z=$$MSA^HLTP3(+^HLMA(HLMSG,0))
IF Z]""
IF HLN("ACAT")=""
SET HLN("ACAT")="NE"
SET HLN("APAT")="NE"
+55 ;for batch/file with commit ack, reset c. ack and a. ack variables
+56 IF "BHS,FHS"[HLN("TYPE")
SET Z=$EXTRACT(HLJ(1,0),5)
SET X=$$P^HLTPCK2(.HLJ,9)
SET HLN("ACAT")=$PIECE(X,Z,5)
SET HLN("APAT")=$PIECE(X,Z,6)
SET HLN("MID")=$$P^HLTPCK2(.HLJ,11)
+57 ;get event protocol
+58 SET HLN("EID")=+$PIECE(^HLMA(HLMSG,0),U,8)
SET X=$GET(^ORD(101,HLN("EID"),770))
+59 ;set link counter to msg sent
+60 DO LLCNT^HLCSTCP(HLDP,4)
+61 ;commit and app. ack is never, update status to complete and hang UNI-DIRECTIONAL WAIT
+62 IF HLN("ACAT")="NE"
IF HLN("APAT")="NE"
Begin DoDot:1
+63 DO DONE(3)
+64 ;
+65 ;
+66 HANG $GET(HLDWAIT)
End DoDot:1
QUIT
+67 ;
+68 ;do structure is to stack error
+69 Begin DoDot:1
+70 NEW $ETRAP,$ESTACK
SET $ETRAP="D RDERR^HLCSTCP2"
+71 ;HL*1.6*87: Read acknowledgement.
+72 ;Loop to re-read from buffer when receiving incorrect ack.
+73 FOR
Begin DoDot:2
+74 SET HLREREAD=1
+75 ;override ack timeout
+76 IF $GET(HLN("ACKTIME"))
NEW HLDBACK
SET HLDBACK=HLN("ACKTIME")
+77 ;check for response, quit if no-response, msg will be resent
+78 ;HLRESP=ien 773^ien 772 for response message
+79 SET HLRESP=$$READ^HLCSTCP1()
+80 ;if no response, decrement counter and quit
+81 IF 'HLRESP
Begin DoDot:3
+82 DO LLCNT^HLCSTCP(HLDP,4,1)
+83 SET HLREREAD="0^No Response"
+84 ;check if the port needs to be closed and re-opened before the next re-transmission attempt
+85 IF $GET(HLDRETRY("CLOSE"))
DO CLOSE^%ZISTCP
KILL HLPORT
End DoDot:3
QUIT
+86 ;X 0=re-read msg, 1=commit ack, 3=app ack success, 4=error
+87 SET X=$$RSP^HLTP31(HLRESP,.HLN)
+88 ;X=0, re-read msg. Incorrect ack (bad MSH,MSA,msg id,or sending app)
+89 IF 'X
QUIT
+90 ;commit ack - done
+91 IF X=1
Begin DoDot:3
+92 ;don't need app. ack, set status to complete
+93 IF "NE"[HLN("APAT")
Begin DoDot:4
+94 DO DONE(3)
+95 ;
End DoDot:4
QUIT
+96 ;response is deferred, set status to awaiting ack
+97 DO DONE(2)
+98 ;
End DoDot:3
SET HLREREAD="0^Commit Ack"
QUIT
+99 ;Error, HLRESLT=error number^error message from HLTP3
+100 IF X=4
Begin DoDot:3
+101 DO DONE(4,+$GET(HLRESLT),$PIECE($GET(HLRESLT),U,2))
+102 ;
+103 SET HLREREAD="0^Error"
End DoDot:3
QUIT
+104 ;app ack was successful
+105 DO DONE(3)
SET HLREREAD="0^App Ack"
+106 ;
End DoDot:2
IF '+$GET(HLREREAD)
QUIT
End DoDot:1
+107 QUIT
+108 ;
DCSEND ;direct connect
+1 ; Set up error trap
+2 NEW $ETRAP,$ESTACK
SET $ETRAP="D ERROR^HLCSTCP2"
+3 ;override ack timeout
+4 IF $GET(HLP("ACKTIME"))
NEW HLDBACK
SET HLDBACK=HLP("ACKTIME")
+5 ;HL*1.6*77
IF $$WRITE(HLMSG)<0
IF $GET(HLERROR)]""
Begin DoDot:1
+6 ;HL*1.6*77
DO STATUS^HLTF0(HLMSG,4,$PIECE(HLERROR,"^"),$PIECE(HLERROR,"^",2),1)
+7 DO LLCNT^HLCSTCP(HLDP,3,1)
End DoDot:1
QUIT
+8 DO LLCNT^HLCSTCP(HLDP,4)
+9 ;do structure is to stack error
+10 Begin DoDot:1
+11 NEW $ETRAP,$ESTACK
SET $ETRAP="D RDERR^HLCSTCP2"
+12 ;HLRESP=ien 773^ien 772 for response message
+13 SET HLRESP=$$READ^HLCSTCP1()
End DoDot:1
+14 ;
+15 IF $GET(HLRESP)
DO DONE(3)
IF '$GET(HLRESP)
DO DONE(4,108,$SELECT($GET(HLERROR)]"":$PIECE(HLERROR,"^",2),1:"No response"))
+16 IF $GET(HLERROR)']""
Begin DoDot:1
+17 DO MON^HLCSTCP("Idle")
+18 IF '$GET(HLRESP)
SET HLERROR="108^No response"
End DoDot:1
+19 ;Close port
+20 IF $DATA(HLPORT)
DO CLOSE^%ZISTCP
KILL HLPORT
+21 QUIT
+22 ;
DONE(ST,ERR,ERRMSG) ;set status to complete
+1 ;ST=status, ERR=error ien, ERRMSG=error msg
+2 DO STATUS^HLTF0(HLMSG,ST,$GET(ERR),$GET(ERRMSG),1)
+3 ;
+4 ;**109**
+5 DO DEQUE^HLCSREP(HLDP,"O",HLMSG)
+6 ;
+7 ;check for more msg.
+8 IF $GET(HLPRIO)'="I"
SET HLMSG=+$ORDER(^HLMA("AC","O",HLDP,0))
SET HLRETRY=0
+9 QUIT
+10 ;
CHKMSG(HLI) ;check status of message and update if not cancelled
+1 ;input: HLI=new status, HLMSG=ien of msg in 773
+2 ;returns 1=msg was updated, 0=msg has been canceled
+3 NEW X
+4 ;
+5 ;**109**
+6 ;F L +^HLMA(HLMSG,"P"):1 Q:$T H 1
+7 ;
+8 ;
+9 ; New HL*1.6*77 code starting here...
+10 IF '$DATA(^HLMA(HLMSG,"P"))
Begin DoDot:1
+11 SET HLERROR="2^Missing status field"
+12 DO STATUS^HLTF0(HLMSG,4,$PIECE(HLERROR,U),$PIECE(HLERROR,U,2),1)
+13 ;
+14 ;**109**
+15 DO DEQUE^HLCSREP(HLDP,"O",HLMSG)
+16 ;L -^HLMA(HLMSG,"P")
End DoDot:1
QUIT 0
+17 ;**end 109**
+18 ;
+19 ; End of HL*1.6*77 modifications
+20 ;
+21 ;get status, quit if msg was cancelled
+22 ;
+23 ;**109**
+24 ;S X=+^HLMA(HLMSG,"P") I X=3 L -^HLMA(HLMSG,"P") Q 0
+25 SET X=+^HLMA(HLMSG,"P")
IF X=3
QUIT 0
+26 ;
+27 ;update status if it is different
+28 IF $GET(HLI)
IF HLI'=X
DO STATUS^HLTF0(HLMSG,HLI)
+29 ;
+30 ;**109**
+31 ;L -^HLMA(HLMSG,"P")
+32 ;
+33 QUIT 1
+34 ;
WRITE(HLDA) ; write message in HL7 format
+1 ; HLDA - ien of message in 773
+2 ; - start block $C(11)
+3 ; - end block $C(28)
+4 ; - record separator $C(13)
+5 ;Output(s): 1 - Successful
+6 ; -1 - Unsuccessful
+7 ;
+8 NEW HLDA2,HLAR,HLI,LINENO,X
+9 ;set error trap, used when called from HLTP3
+10 ;
+11 ; New HL*1.6*77 code starts here...
+12 NEW $ETRAP,$ESTACK
SET $ETRAP="D ERROR^HLCSTCP2"
+13 IF $GET(^HLMA(HLDA,0))'>0
Begin DoDot:1
+14 SET HLERROR="2^Message Text pointer missing"
End DoDot:1
QUIT -1
+15 SET HLDA2=+$GET(^HLMA(HLDA,0))
+16 ; End of HL*1.6*77 modifications...
+17 ;
+18 ;HL*1.6*77
IF '$GET(^HLMA(HLDA,0))
QUIT -1
+19 ; header is in ^HLMA(, message is in ^HL(772,
+20 SET LINENO=1
SET HLI=0
SET HLAR="^HLMA(HLDA,""MSH"")"
+21 USE IO
+22 Begin DoDot:1
+23 FOR
SET HLI=$ORDER(@HLAR@(HLI))
IF 'HLI
QUIT
SET X=$GET(^(HLI,0))
Begin DoDot:2
+24 ;first line, need start block char.
+25 IF LINENO=1
SET X=$CHAR(11)_X
+26 IF X]""
WRITE X,!
+27 ;send CR for blank lines
+28 IF X=""
WRITE $CHAR(13)
+29 SET LINENO=LINENO+1
End DoDot:2
End DoDot:1
WRITE $CHAR(13)
SET HLAR="^HL(772,HLDA2,""IN"")"
SET HLI=0
Begin DoDot:1
End DoDot:1
+30 ; Sends end block for this message
+31 SET X=$CHAR(28)_$CHAR(13)
+32 USE IO
WRITE X,!
+33 ;switch to null device if opened to prevent 'leakage'
IF $GET(IO(0))'=""
IF $GET(IO(0))'=IO
USE IO(0)
+34 QUIT 1
+35 ;
OPEN() ; -- Open TCP/IP device (Client)
+1 ;HLPORT=port, defined only if port is open
+2 ;HLPORTA=number of attempted opens
+3 IF $DATA(HLPORT)
SET IO=HLPORT
Begin DoDot:1
+4 USE IO
+5 ;use packet mode on Cache'
IF HLOS["OpenM"
XECUTE "U IO:(::""-M"")"
End DoDot:1
QUIT 1
+6 NEW HLDOM,HLI,HLIP,HLPORTA
+7 GOTO OPENA^HLCSTCP3
+8 ;
RDERR ; Exceeded 10,000 bytes, so split on 12/2/03-LJA
DO RDERR^HLCSTCP4
QUIT
ERROR ; Exceeded 10,000 bytes, so split on 12/2/03-LJA
DO ERROR^HLCSTCP4
QUIT
+1 ;
CC(X) ;cleanup and close
+1 DO MON^HLCSTCP(X)
+2 IF $DATA(HLPORT)
DO CLOSE^%ZISTCP
KILL HLPORT
+3 HANG 2
+4 QUIT