Using ASP to communicate to a mainframe Page 3

By ServerWatch Staff (Send Email)
Posted Oct 27, 1998


<HTML>

<TITLE>PREMIUM RESULTS</TITLE>

<BODY>

<form METHOD="GET" >
<%
POLICY_NBR = REQUEST ("POLICY_NBR")
PASSWORD = REQUEST("PASS")

APPLICATION.LOCK     'Lock the application variable so no other user can access it
FREE_FILE = APPLICATION("PORT_NBR_OPEN")
I = 1
FOUND_ONE = 0  'FOUND_ONE will contain 1 or 2 out of loop, 2 is no open ports
DO WHILE FOUND_ONE > 0   
      IF MID(FREE_FILE, I, 1) = "0" THEN     'find the first open port(the first "0")
           MID(FREE_FILE, I, 1) = "1"
           FOUND_ONE = 1
      ELSE
           I = I + 1
           IF I > 10 THEN
               FOUND_ONE = 2
           END IF
      END IF
LOOP
APPLICATION("PORT_NBR_OPEN") = FREE_FILE  'reset the application var to show we
APPLICATION.UNLOCK                                              'have this port, then unlock the appl

IF FOUND_ONE = 2 THEN  'oops no open ports to use
    RESPONSE.WRITE("SERVER BUSY, TRY YOUR REQUEST AT A LATER TIME")
ELSE

FILE_NBR = I
ON ERROR RESUME NEXT  'we want to handle our own errors
SET FS = server.CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")

FILE_NAME = "PORT_FILE" & FILE_NBR & ".INP" 'the file name will be PORT_FILE#.inp

SET A = FS.OPENTEXTFILE(FILE_NAME) 'as an added precaution see if the file exists
                                                                         'if we can open to read, it already exists


IF ERR.NUMBER = 0 THEN           'Oops a .inp file already exists for this port, even though the
    RESPONSE.WRITE("SERVER ERROR")  'the application variable said it was open
ELSE

ERR.CLEAR    'We wanted the error, so just clear it and go on
SET A = FileObject.CreateTextFile (FILE_NAME, True)  'Creates a new .inp file

'Using a *  as a delimiter between each field
LINE = "001*" & POLICY_NBR & "*" & PASSWORD

A.WRITELINE(LINE)      'write the line to the file and close it
SET A = NOTHING

FILE_NAME = "PORT_FILE" & FILE_NBR & ".OUT"  ' the response will be in the file
                                                                                         'PORT_FILE#.OUT


DONE = 0   'This loop will check for the file every 1 second.
START = NOW()
TOTAL_WAIT = 0    'used to limit the max wait for a response
MAX_WAIT = 10
DO WHILE DONE = 0
      SET INSTREAM= FileObject.OpenTextFile (file_name, 1, False, False)
      IF ERR.NUMBER <> 0 THEN     'If there is an error then the file is not present
         AGAIN = NOW()
         DONE2 = 0
         DO WHILE DONE2 = 0
                ERR.CLEAR
                AGAIN2 = NOW()
                SECS = DATEDIFF("S",START,AGAIN)  'DateDiff returns the difference between
                 IF ABS(SECS) > 1 THEN    '2 date/times, the"S" returns # of seconds
                     DONE2 = 1
                     TOTAL_WAIT = TOTAL_WAIT + 1
                     START = NOW()     'Reset the start time
                 END IF
          LOOP
          IF TOTAL_WAIT > MAX_WAIT   THEN 
               DONE = 2     'Max wait has passed lets get out and error
          END IF
      ELSE
         DONE = 1
      END IF
LOOP
IF DONE = 1 THEN
     RESPONSE.WRITE "<PRE>"    ' the response is preformatted
     WHILE NOT INSTREAM.AtEndOfStream    'Read thru the file and write it back out
                  RESPONSE.WRITE INSTREAM.READLINE & "<BR>"
     WEND
      RESPONSE.WRITE "</PRE>"
      INSTREAM.CLOSE
      SET INSTREAM = NOTHING
ELSE
     SET INSTREAM = NOTHING   'If we get here we never got a response
     RESPONSE.WRITE "Server ERROR pipe #"& file_nbr & "<br>"
END IF

FILE_NAME = "PIPE_NBR" & FILE_NBR & ".DNE" 'Now I write a done file to let
                                                                            'the application know I am done with this file

Set A = FileObject.CreateTextFile (file_name, True)
LINE = "DONE"

A.WRITELINE(LINE)
SET A = NOTHING
APPLICATION.LOCK   'now free up the port in the application
FREE_FILE = APPLICATION("PORT_NBR_OPEN")
MID(FREE_FILE, FILE_NBR, 1) = "0"
APPLICATION("PORT_NBR_OPEN") = FREE_FILE
APPLICATION.UNLOCK

END IF
END IF

'Note: the above method is not full proof. An inp file could be left not processed on a session 'abandon. More security would need to be added in a real world release. Some examples would be 'to write the requesting IP address in the inp file and rewrite it in the out file. If the IP address does 'not match the user waiting for the out file we would not show the results to that user. Also could put 'some code in the global.asa file for Session_OnEnd to free up any port files the user may have in 'their possession. You could also add another application variable to track the time when a port_file 'was taken, then if a user gets a no open port error then check to see if a port has been taken for a 'long time(since we only allow 10 seconds for a response, a long time should mean anything over 10 'seconds) and free it up for reuse.

%>
</FORM>

</BODY>

</HTML>

Page 3 of 4


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.


 

 


Thanks for your registration, follow us on our social networks to keep up-to-date