Ryan's District Boards

Computer, programming, and webmaster help , support , tips and tricks => Tutorials Zone! => Internet webmaster computer programming technology tips and tricks => Visual Basic => Topic started by: ben2ong2 on October 07, 2006, 04:40:10 PM

Title: FTP-EDI with GE Global eXchange services (VB6)
Post by: ben2ong2 on October 07, 2006, 04:40:10 PM
Recently, I was required to setup a ‘Trading Partner’ relationship with GE Global EDI (Electronic Document Interchange) services. GE EDI is arguably the largest EDI exchange service in the world and the opportunity to create a direct interface was exciting.

NOTE: I found some software that makes windows function much better: Firefox with Google Toolbar

Architecture:

GE EDI uses an FTP client connection over a VPN (Virtual Private Network) when connecting to GE via TCP/IP. If the FTP connection is over a frame-relay, then the VPN is not required (this fact is not published). Since I was developing for use with TCP, I applied the VPN. GE uses "CheckPoint" client software to secure the VPN. The CheckPoint client is included in your GE EDI account, but requires download, install and configuration to the machine conducting the communication. (www.checkpoint.com)

GE EDI has a 3-folder system to manage document interchange. GE refers to these folders as INBOX (receive), OUTBOX (send) and REPORTS. The FTP commands for navigation are ‘cd /receive’, ‘cd /send’ and cd /reports. Navigation between folders is direct, in that, it is not possible to step-up a directory tree and down-to the next folder. Issue the FTP command and you are in the designated folder.

FTP files uploaded to the OUTBOX are automatically routed by GE EDI services based on the ‘Trading Partner Sender’ and ‘Trading Partner Receiver’ information contained within the document. FTP files downloaded are automatically removed from the INBOX after confirmation and status can be found in the REPORTS folder.

The VB6 Class to handle communications will transfer all files located in a designated SEND local directory and RECEIVE all remote files listed by the ‘dir’-directory FTP command into a designated RECEIVE local directory.

VPN:

The CheckPoint VPN software used with GE EDI is not explicitly mentioned within the GE user manual. This is the reason for this article, as it was EXTREMEMLY aggravating to integrate the VPN without this knowledge. After installation, configuration of ports, adding the FTP site name and a Restart, double-click the envelope-key in the SysTray. Navigate to menu Passwords / Configure SSO. SSO will allow the VPN to login with NT account login, which is a huge advantage in development. After, navigate Passwords / Enable SSO, when prompted, click ‘OK’.

Application:

The program is based around a single class with 1 public and 3 private functions. The program’s function is to send all files in a local ‘send’ directory and receive remote files. Additionally, we will move SEND/* files to a SENT/ directory to keep a good delineation between the two. The application uses the ‘Microsoft Internet Control’ ActiveX control and the File System Object. Both of these controls are late bound in class functions, so that we can compile as a DLL and not have to support a Form in the DLL.

    * Example:
    * Set oFTP = CreateObject("Inetctls.Inet")
    * Set oFSO = CreateObject("Scripting.FileSystemObject")

FTP:

When the oFTP object is instantiated, you have FTP client ready to send commands. To visualize the object, open the DOS prompt and type ‘ftp’. In fact, this is how we will first test your ftp connection to GE.

    * From the FTP prompt ‘ftp>’ in DOS type: ftp.interchange.gegxs.com

If your VPN is not working, the DNS will resolve the IP address, but the request will time-out. If the VPN is set, you will be prompted for USERNAME and PASSWORD. REMEMBER, CASE IS EXTREMLY IMPORTANT!

    * From the ‘ftp>’ prompt type: dir

Take the time to manually issue different commands, switch directories, send and receive files from GE EDI.

CODE:

In code processing, the most important item to remember is asynchronous communications.

    * FTP is asynchronous. There is a function ‘IMWaiting’ to let the current command to execute and complete. Without ‘IMWaiting’, the program would continue to issue commands without waiting for a response.
    * Second, the program must read and evaluate the FTP server response with the ‘oFTP.GetChunk(1024, icString)’ method.
    * Third, each command confirms the previous command. For example, a ‘dir’ command following a ‘put’ command confirms the files receipt.

Take special note of the response to the GetChunk method, when receiving files. The program reads the remote files by splitting the response into an array and issuing individual ‘put’ commands for each file.

Public Function strProcessFtpFiles(strFTPsite As String, strSendDirectory As String, _
     strRecDirectory As String, strUser As String, strPassword As String) As String
    'Returns a string. String is "," delimited within a ";" structure.
    'Create arrays and Split on ";",
    'then Split on "," to retrieve names of files sent and receive.
    'Check Point software for VPN must be active for the GE eXchange FTP server.
    On Error GoTo ErrHandler
    Dim strResponse As String
    Dim oFTP As Object
    ' Set a reference to the Internet transfer control and set its properites.
    ' This is a late bound ActiveX Control and we can use it in a class.
    Set oFTP = CreateObject("Inetctls.Inet")
    oFTP.Protocol = icFTP
    oFTP.URL = strFTPsite '"ftp.interchange.gegxs.com"
    oFTP.UserName = strUser 'Username
    oFTP.Password = strPassword 'Password
    'Directory listing executes the login.
    oFTP.Execute , "dir"
    Call IMWaiting(oFTP)
    strResponse = oFTP.GetChunk(1024, icString)
    Call IMWaiting(oFTP)
    'Send Files
    strProcessFtpFiles = "Sent Files;"
    strProcessFtpFiles = strProcessFtpFiles & SendFiles(oFTP, strSendDirectory) & ";"
    'Get Files
    strProcessFtpFiles = strProcessFtpFiles & "Received files;"
    strProcessFtpFiles = strProcessFtpFiles & GetFiles(oFTP, strRecDirectory) & ";"
    'Close and Exit. "dir" command comfirms 'Send' or 'Receive'
    oFTP.Execute , "dir"
    Call IMWaiting(oFTP)
    oFTP.Execute , "CLOSE"
    Exit Function
ErrHandler:
    strProcessFtpFiles = "A transfer error occured,"
End Function

Private Function SendFiles(oFTP As Object, strSendDirectory As String) As String
   'Execute sending of all files in the local Send folder.
   'Returns a comma delimited string of sent file names
    Dim folder As Object, filename As Object, strCommand As String
    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    'Switch to remote Send directory
    oFTP.Execute , "cd /send"
    Call IMWaiting(oFTP)
    strResponse = oFTP.GetChunk(1024, icString)
    Call IMWaiting(oFTP)
    'Iterate through the local Send Folder.
    Set folder = oFSO.GetFolder(strSendDirectory)
    For Each filename In folder.Files
        strCommand = "put " & filename.Path & " /" & filename.Name
        oFTP.Execute , strCommand
        Call IMWaiting(oFTP)
        filename.Move (strSendDirectory & "Sent\" & filename.Name)
        SendFiles = SendFiles & filename.Name & ","
    Next
End Function

Private Function GetFiles(oFTP As Object, strRecDirectory As String) As String
    'Execute receiving and naming files from the GE recieve folder.
    'Returns comma delimited string of files received.
    Dim strFileName As String, strResponse As String, strLine As String
    Dim aryResponse As Variant, aryTemp As Variant, aryLine As Variant
    Dim xCount As Integer, yCount As Integer
    'Switch to the remote receive directlory
    oFTP.Execute , "cd /receive"
    Call IMWaiting(oFTP)
    oFTP.Execute , "dir"
    Call IMWaiting(oFTP)
    strResponse = oFTP.GetChunk(50124, icString)
    Call IMWaiting(oFTP)
    'Parse the remote files listing returned from "dir" to get the file names.
    strResponse = Replace(strResponse, " ", ";")
    aryResponse = Split(strResponse, Chr(13))
    For xCount = 1 To UBound(aryResponse)
        aryTemp = Split(aryResponse(xCount), ";")
        For yCount = 0 To UBound(aryTemp)
            If (aryTemp(yCount) <> "") Then
                strLine = strLine & CStr(aryTemp(yCount)) & ";"
            End If
        Next
        aryLine = Split(strLine, ";")
        If (UBound(aryLine) > 3) Then
            '5th column contains the file name.
            oFTP.Execute , "GET " & aryLine(4) & " " & strRecDirectory & aryLine(4)
            GetFiles = GetFiles & aryLine(4) & ","
        End If
        Call IMWaiting(oFTP)
        strLine = ""
    Next
End Function

Private Function IMWaiting(obj)
    Do While obj.StillExecuting
        DoEvents
    Loop
End Function



Article By, Matthew P. Burnett.