Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: VENPCCP

VENPCCP.m

Go to the documentation of this file.
VENPCCP ; IHS/OIT/GIS - PRINT DEAMON - MANAGE BACKGROUND PRINTING ; [ 03/03/09   5:46 PM ]
 ;;2.6;PCC+;**1,3**;APR 03, 2012;Build 24
 ;
 ; 
 ;
TASK ; EP - PRINT DEAMON ; THERE SHOULD ONLY BE 1 PRINT DEAMON PROCESS RUNNING AT A TIME
 I $D(^TMP("VEN TASK",$J)) X ("J K"_"T^VENPCCP") Q  ; IT LOOKS LIKE THERE ALREADY IS A PRINT DEAMON ALREADY RUNNING IN THIS PARTITION!
 E  D KILLTASK ; INITIAL CLEANUP ; MAKE SURE NO OTHER PRINT DEAMON PROCESSES ARE RUNNING
 D ^VENPCCP1
 K ^TMP("VEN TASK",$J) D KILLTASK Q  ; FINAL CLEANUP ; STOP ALL PRINT DEAMON PROCESSES
 ; 
 ; ---------------------------------------------------------
 ;
SEND(SOCKET,IP,IPA) ; EP - FOR SENDING A RECORD TO THE PRINT SERVICE
 ; SEND(SOCKET,IP,IPA,PGIEN,MRPFLAG) ; EP FOR SENDING A RECORD TO THE PRINT SERVICE
 ; REC MUST EXIST AS AN ARRAY (VER 2.5) OR A LOCAL VARIABLE (PREVIOUS VERSIONS)
 ; IP IS THE TCP/IP ADDRESS.  IPA IS THE ALTERNATE IP ADDRESS (IPA IS OPTIONAL)
 ; SOCKET IS THE TCP SOCKET NUMBER ON THIS MACHINE
 ; REC IS THE RECORD
 ; THE RECORD MUST BE '^' DELIMITED.  FIRST PIECE: HEADER NAME/MNEMONIC.  SECOND PIECE: TEMPLATE NAME/MNEMONIC.  THIRD PIECE: PRINTER DESTINATION.  FOUTH PIECE: ENVIRONMANTAL INFO  OTHER PIECES: DATA
 ; THIS FUNCTION RETURNS '1' IF THE TRANSMISSION WAS SUCCESSFUL OR AN ERROR CODE IF IT WAS UNSUCCESSFUL
 ; UPDATED TO SUPPERT THE VER 2.5 PRINT SERVICE
 ; 
CHK I $G(SOCKET),$L(IP),$D(REC)
 E  D REG^VENPCCP2(10,"MISSING/INVALID CONFIGURATION PARPMETERS") Q 0
 N %,N,ACK,ERR,STOP,LEN,PCE,SEX,SSN,VCN,ELIG,RSTG,OK,I
 N MAXPCE,CKSUM,TRY,CACHE,BUFF,BLANK,LASTPCE,STOP,ECNT
 S ECNT=0 ; INITIALIZE THE ERROR COUNTER
 S CACHE=($$VEN^VENPCCU=2),BUFF=$S(CACHE:500,1:500)
 I $D(SWAP) S (IP,IPA)=SWAP
JUMP25 I $G(VER25) G EVAR ; VER 2.5: THE REC ARRAY HAS ALREDY BEEN CREATED & CKSUM CALCULATED. NO BLANK LINE NECESSARY.
ADDBLANK S BLANK="",N=$L(REC,U) S $P(BLANK,U,N)="" S BLANK=$C(13,10)_BLANK,CKSUM=$L(REC)+$L(BLANK)
SPLIT F I=1:1 S %=$E(REC,1,BUFF),REC=$E(REC,(BUFF+1),99999) Q:%=""  S REC(I)=%,LASTPCE=I+1
 F I=LASTPCE:1 S %=$E(BLANK,1,BUFF),BLANK=$E(BLANK,(BUFF+1),99999) Q:%=""  S REC(I)=%,MAXPCE=I
EVAR D VAR ; GET ENVIRONMENTAL VARIABLES
TRY S POP=$$OTCP^VENPCCP(IP,SOCKET) ; TRY TO OPEN TCP SOCKET TO THE PRINT SERVER
 I POP,$D(SWAP)!(IP=IPA) D REG^VENPCCP2(1,"UNABLE TO ACCESS ANY PRINT SERVERS") Q 0
 I POP D REG^VENPCCP2(4,"UNABLE TO ACCESS ONE OF THE PRINT SERVERS ("_IP_")") S (IP,SWAP)=IPA S @TMP="Unable to access one of the Print Servers" G TRY
 S TRY=0
START W ("START"_U_CKSUM) W:$G(CACHE) ! K ACK ; START THE TRANSMISSION TO THE PRINT SERVER
 R ACK:60
 I $G(ACK)=1 G LOOP ; SUCCESSFUL START!
 ; IF YOU GET TO THIS POINT, SOMETHING HAS GONE WRONG... 
 I $G(ACK)=-5 D  G:'$D(@TMP) NOTOPEN G:TRY'>20 START Q
 . S TRY=TRY+1
 . I TRY>20 Q
 . H 5
 . I '$D(@TMP) Q
 . S @TMP="Print Service busy.  Will attempt to connect for another "_(100-(5*TRY))_" seconds"
 . Q
NOTOPEN D CTCP ; CLOSE TCP SOCKET AFTER START FAILURE
 I '$D(@TMP) Q "EXTERNAL HALT"
 I $G(ACK)="" D REG^VENPCCP2(1,"Unable to connect to a Print Server") Q 0
 I $G(ACK)=-5 D REG^VENPCCP2(2,"Print Service was busy, and it timed out") G N1
 D REG^VENPCCP2(2,"Print service reports a startup error ("_$G(ACK)_")")
N1 I $G(SWAP)!(IP=IPA) Q 0  ; FAILED TO START PRINT PROCESS!
 S (SWAP,IP)=IPA G TRY
 ; 
LOOP S PCE=0 K STOP ; DATA TRANSMISSION FROM RPMS TO PCC+ PRINT SVC
 S @TMP="Transmitting data to PCC+ print service..."
 F  S PCE=$O(REC(PCE)) Q:'PCE  D  I $D(STOP) Q  ; TRANSMISSION LOOP: DATA SENT IN CHUNKS.
 . S REC=REC(PCE)
 . W REC W:CACHE ! K ACK
 . R ACK:60 ; TYPICALLY, WE GET SUB-SECOND RESPONSE TIME
 . I '$D(ACK) S STOP=0 Q
 . I $E(ACK)=1 F  Q:$E(ACK,2)'=1  S ACK=$E(ACK,2,99)
 . I ACK'=1 S STOP=1
 . Q
LOOPFAIL I $D(STOP) D  Q 0  ; PRINT SERVICE STARTS RECEPTION, BUT DATA TRANSMISSION FAILS.  MANAGE THE ERROR.
 . D CTCP
 . I 'STOP D REG^VENPCCP2(1,"Print Service starts but then does not respond") Q
 . D REG^VENPCCP2(1,"Print Service starts, but then it reports a data transmission failure")
 . Q
MERGE S @TMP=(MSG_"  Starting mail merge") ; START MAIL MERGE
 W "STOP" W:CACHE ! K ACK ; SEND MESSAGE: "STOP" = ALL DATA SENT, START TO GENERATE THE MERGED DOCUMENT
 R ACK:60 ; IF THINGS ARE WORKING NORMALLY, THE ACK SHOULD COME BACK WITHIN A FEW SECONDS
CLOSESVS D CTCP ; CLOSE THE TCP CONNECTION
SUCCESS I $G(ACK)=0 S @TMP=(MSG_"  Success!!!") Q 1
RETRY I ACK=-1 S ECNT=ECNT+1 I ECNT<2 D  G TRY ; PROBABLY A COM PROBLEM.  TRY 2 TIMES ; PATCHED BY GIS/OIT 5/15/08 ; PCC+ 2.6 PATCH 1 ; WORK AROUND FOR COM ERROR
 . S @TMP=(MSG_"  Retry #"_ECNT)
 . I $G(IPA)'="",IPA'=IP S IP=IPA ; IF POSSIBLE, TRY A DIFFERENT PRINT SERVER
 . H 1
 . Q
MERGFAIL D MERGFAIL^VENPCCP2($G(ACK),$G(MRPFLAG),PGIEN) ; MERGE HAS FAILED.  MANAGE THE ERROR.
 Q 0
 ;
VAR ; EP-GET MISC VARS REQUIRED FOR ERROR PROCESSING
 ; GET ENVIRONMENTAL VARS: VISIT, DUZ, DUZ(2), DFN, DUZ(0), VENDEV, DEFEF, MRPFLG
 I '$G(VER25) D VARS Q
 N CNT,STG,X,Y,Z,%,VE,VD,VP
 S (VE,VD,VP)=""
 S (CNT,CKSUM,MAXPCE)=0
 F  S CNT=$O(REC(CNT)) Q:'CNT  D
 . S CKSUM=CKSUM+$L(REC(CNT))
 . S MAXPCE=CNT
 . S STG=REC(CNT)_$G(REC(CNT+1))
 . I 'VE F X="PRINTER","printer" D  ; GET ENVIRONMENTAL VARIABLES
 .. S Y="^"_X_$C(30)
 .. I STG'[Y Q
 .. S %=$P(STG,Y,2),%=$P(%,U) ; DATA STRING
 .. I $L(%,";")'>5 Q  ; INCOMPLETE DATA STRING - WAIT FOR NEXT ITERATION
 .. S VISIT=$P(%,";"),DFN=$P(%,";",4),VENDEV=$P(%,U,6),VE=1
 .. S %=$C(68,85,90)
 .. S @%=$P(%,";",2),@%@(2)=$P(%,";",3),@%@(0)=$P(%,U,5)
 .. Q
 . I 'VD F X="TEMPLATE","template" D  ; GET TEMPLATE
 .. S Y="^"_X_$C(30)
 .. I STG'[Y Q
 .. S %=$P(STG,Y,2) ; DATA STRING
 .. I %'[U Q  ; INCOMPLETE, WAIT FOR NEXT ITERATION
 .. S %=$P(%,U)
 .. S DEFEF="",VD=1
 .. I '$L(%) Q
 .. S Z=0
 .. F  S Z=$O(^VEN(7.41,Z)) Q:'Z  I $P($G(^(Z,0)),U,3)=% S DEFEF=Z Q
 .. Q
 . I 'VP F X="GROUP","group" D  ; GET PRINT GROUP
 .. S Y="^"_X_$C(30)
 .. I STG'[Y Q
 .. S %=$P(STG,Y,2) ; DATA STRING
 .. I %'[U Q  ; INCOMPLETE, WAIT FOR NEXT ITERATION
 .. S %=$P(%,U)
 .. S PGIEN="",MRPFLAG=0,VP=1
 .. S PGIEN=$O(^VEN(7.4,"B",%,0)) I 'PGIEN Q
 .. I $$MRP^VENPCCU=$P($G(^VEN(7.4,PGIEN,0)),U) S MRPFLAG=1
 .. Q
 . Q
 Q
 ; 
VARS ; EP-OLD WAY OF GETTING ENVIRONMENTAL VARS TO POPULATE ERROR MSG FILE
 N %,X,Z
FMVARS S %=$P(REC(1),U,4)
 S Z=$C(68,85,90)
 S VISIT=$P(%,";"),@Z=$P(%,";",2),@Z@(2)=$P(%,";",3),DFN=$P(%,";",4),@Z@(0)=$P(%,U,5),VENDEV=$P(%,U,6)
DEFEF S %=$P(REC(1),U,2),DEFEF="",Z=0   ; GET DEFAULT TEMPLTE IEN
 F  S Z=$O(^VEN(7.41,Z)) Q:'Z  I $P($G(^(Z,0)),U,3)=% S DEFEF=Z Q
PGIEN S %=$P(REC(1),U,3),PGIEN="",MRPFLAG=0 ; GET PRINT GROUP IEN
 I $L(%) S PGIEN=$O(^VEN(7.4,"B",%,0)) ; IDENTIFY PRINT GROUP IEN
MRPFLAG I $$MRP^VENPCCU=$P($G(^VEN(7.4,+$G(PGIEN),0)),U) S MRPFLAG=1 ; THIS DOCUMENT IS GOING TO THE MED REC PRINTER
 Q
 ; 
 ; -------------------------------------------------------------
 ; 
 ; GENERIC PRINT DEAMON FUNCTIONS USED BY MANY ROUTINES
 ;
OPN(PATH,FILE,RW,EX) ; EP-CLEAN HFS OPEN, USE, EXECUTE, CLOSE
 N DEV,POP,TRY,F,CACHE
 S F=PATH_FILE,POP=1,TRY=0,CACHE=($$VEN^VENPCCU=2)
OPN1 I 'CACHE X ("F DEV=51:1:54 O DEV:(F:RW):2 I $T S POP=0 U DEV X EX C DEV Q")
 I CACHE D  Q POP
 . S RW=$S(RW="W":"WNS",1:"RS"),DEV=F
 . X ("O"_" F:(RW):2")
 . X ("I  S POP=0 "_"U"_" F X EX "_"C"_" F")
 . Q
 I 'POP Q 0
 ; STOP TRYING IF STOP OPTION HAS BEEN SELECTED
 I '$D(^TMP("VEN TASK",$J)) Q 2
 S TRY=TRY+1 I TRY>5 Q 1
 H 2 G OPN1
 ; 
FIRST(PATH) ; EP-FIRST FILE IN THE PATH
 I $$VEN^VENPCCU=2 X ("S RESULT=$"_"Z"_"SEARCH("""_PATH_"*.TXT"")") Q $P(RESULT,"\",$L(RESULT,"\"))
 I $$OS^VENPCCU S RESULT=$$UNIX(PATH,"S RESULT="""" F  R % Q:%=""""  I %["".TXT""!(%["".txt"") S RESULT=% Q") Q RESULT
 N X,PF
 S PF=PATH_"*.TXT"
 D FUNC("FIRST")
 Q $P(X,U)
 ; 
DEL(PATH,F) ; EP-FROM VENPCC2A - DELETE A FILE FROM THE PRINT QUEUE
 I $$VEN^VENPCCU=2,$$OS^VENPCCU=1 X ("I $"_"Z"_"F(-1,""rm "_PATH_F_""")") Q  ; UNIX/CACHE
 I $$VEN^VENPCCU=2 X ("I $"_"Z"_"F(-1,""del "_PATH_F_""")") Q  ; NT/CACHE
 I $$OS^VENPCCU D UCMD("rm "_PATH_F) Q  ; UNIX/MSM
 N PF,X,%
 S PF=PATH_F
 D FUNC("DEL") ; NT/MSM
 Q
 ; 
COUNT(PATH) ; EP-COUNT FILES WAITING TO BE PROCESSED
 N FILE,CNT
 I $$VEN^VENPCCU=2 X ("F CNT=0:1 S FILE=$S('CNT:(PATH_""*.TXT""),1:"""") S FILE=$"_"Z"_"SEARCH(FILE) I '$L(FILE) Q") Q CNT
 I $$OS^VENPCCU N RESULT,X S RESULT=$$UNIX(PATH,"S RESULT=0 F  R X Q:'$L(X)  I X["".TXT""!(X["".txt"") S RESULT=RESULT+1") Q RESULT
 N PF,CNT
 S PF=PATH_"*.TXT"
 D FUNC("FIRST")
 I '$L(X) Q 0
 S CNT=1
COUNT1 D FUNC("NEXT")
 I X="" Q CNT
 S CNT=CNT+1
 G COUNT1
 ; 
FIND(PATH,TARGET) ;EP-RETURN A '1' IF A FILE EXISTS
 N FILE
 I $$VEN^VENPCCU=2 X ("S TARGET=$"_"Z"_"SEARCH(PATH_TARGET)") Q ($L(TARGET)>0)
 I $$OS^VENPCCU N RESULT,X S RESULT=$$UNIX(PATH,"S RESULT=0 F  R X Q:'$L(X)  I $P(X,U)=TARGET S RESULT=1 Q") Q RESULT
 N PF
 S PF=PATH_TARGET
 D FUNC("FIRST")
 I '$L(X) Q 0
 I $P(X,U)=TARGET Q 1
FIND1 X "S X=$"_"Z"_"OS(13,X)"
 I X="" Q 0
 I $P(X,U)=TARGET Q 1
 G FIND1
 ; 
UCMD(CMD) ;EP-UNIX COMMANDS
 Q:'$L($G(CMD))
 X ("I $$JOB"_"WAIT"_U_"%HOST"_"CMD(CMD)")
 Q
 ; 
UNIX(PATH,EX) ; EP-EXAMINE A UNIX DIRECTORY
 N PATH2,PATHX,FILE,CMD,RESULT
 S PATH2=$G(^VEN(7.5,CFIGIEN,3)),PATHX=$E(PATH,1,$L(PATH)-1),RESULT=""
 S FILE="ven_"_+$J_".temp",CMD="ls "_PATHX_" > "_PATH2_FILE
 D UCMD(CMD)
 S POP=$$OPN(PATH2,FILE,"R",EX) I POP Q ""
 D DEL(PATH2,FILE)
 Q RESULT
 ; 
OTCP(IP,SOCKET) ; EP-OPEN TCP
 N IO,IOF,IOT,IOST,POP
 I $$VEN^VENPCCU<2 D CALL^%ZISTCP(IP,SOCKET,10) Q POP
 S POP=1 X ("O"_" ""|TCP|"_SOCKET_""":"""_IP_""":5") I $T S POP=0 X ("U"_" ""|TCP|"_SOCKET_"""")
 Q POP
 ; 
CTCP ; EP-CLOSE TCP
 I $$VEN^VENPCCU<2 X ("C"_" 56") Q
 I '$G(SOCKET) N SOCKET S SOCKET=$P($G(^VEN(7.5,$$CFG^VENPCCU,11)),U,3)
 X ("C"_" ""|TCP|"_SOCKET_"""")
 Q
 ; 
FUNC(ACT) ; EP-MSM/NT FUNCTIONS 
 N %,Y
 S %="S X=$"_"Z"_"O"
 I ACT="FIRST" X (%_"S(12,PF,0)") Q
 I ACT="NEXT" X (%_"S(13,X)") Q
 I ACT="DEL",PF'["*." X (%_"S(2,PF)") Q
 I ACT'="DEL" Q
 D FUNC("FIRST") I X="" Q
 I X[".txt"!(X[".TXT") S Y=X,PF=PATH_$P(X,U) D FUNC("DEL") S X=Y
 F  D FUNC("NEXT") Q:X=""  I X[".txt"!(X[".TXT") S Y=X,PF=PATH_$P(X,U) D FUNC("DEL") S X=Y
 Q
 ; 
HOLD(FILE,PATH) ; EP-IF TRANSMISSION FAILS, PUT THE FILE SET AT THE BACK OF THE LINE
 N X,N,M,%,F,R,CMD,STG,%
 S N=$E(FILE,2,99)
 F M="e","g","h","E","G","H" S F=(M_N) I $$FIND(PATH,F) D
 . S R="z"_$E(F)_$E(F,3,99),STG=" "_PATH_F_" "_PATH_R
 . I $$VEN^VENPCCU=2 S CMD="copy"_STG X ("S %=$"_"Z"_"F(-1,CMD)") D DEL(PATH,F) Q
 . I $$OS^VENPCCU S CMD="cp"_STG D UCMD(CMD) D DEL(PATH,F) Q
 . X ("S %=$"_"ZO"_"S(3,PATH_F,PATH_R)")
 . Q
 Q
 ; 
KT H 3
KILLTASK ; EP - DEFINITIVE WAY TO STOP ALL INSTANCES OF THE PRINT DEAMON
 N X
 S X=0
 F  S X=$O(^TMP("VEN TASK",X)) Q:'X  D  ; MAKE SURE ALL PRINT DEAMON PROCESSES ARE SHUT DOWN
 . K ^TMP("VEN TASK",X) ; INDIRECT WAY TO STOP ANOTHER PROCESS ; MSM
 . I $P($G(^VEN(7.5,$$CFG^VENPCCU,0)),"^",5)=2 X ("S %=$Z"_"UTIL(4,X)") Q  ; DIRECTLY STOP ANOTHER PRINT DEAMON PROCESS; CACHE ONLY
 . Q
 Q
 ;