FTPService Example

LANSA Integrator

FTPService Example

This example program will perform the following steps:

1.  Performs a series of calls necessary to load the FTPService

2.  Connects and logs on to the remote FTP server you specify using credentials that you specify (you need to modify the source code)

3.  Retrieves the name of the current directory from the remote FTP server and writes it to the joblog

4.  Changes the current directory on the remote server to the directory LIFTPIN

5.  Sends the shipped order.xml file to the remote server, placing it in the LIFTPIN directory

6.  Quits the ftp session, unloads the service and closes the connection to the JSM server.

Note:
  • To test this example, you need to create the folder /LIFTPIN in the IFS on the remote server.
  • You must specify your own values for the remote server name, user id and password before compiling and running the example.  These are all contained in constants near the beginning of the source code.
  • You may change the directory and file names used in the example if you wish.

Refer to the comments and code in the example for more information.

1.   Create and run the ILE RPG example program

Copy and paste the source provided below into a source file member.  Then modify the constant values for server, user id and password as directed above and in the source code.

To create the program, you need to use the CRTRPGMOD and CRTPGM commands.  Make sure that you use the parameter values specified in the source member.

*************************************************                   

* FTP:     example in RPG ILE of using the LANSA Integrator         

*          FTPService                                               

*                                                                   

* Note:    This is an example program containing only               

*          rudimentary exception handling                           

*                                                                   

* To create this program you must execute the following commands,   

* supplying the indicated parameter values and any others that are  

* necessary in your installation:                                   

*                                                                   

*   CRTRPGMOD MODULE(<modlib>/FTP)                                  

*             SRCFILE(<srclib>/<srcfil>)                            

*                                                                   

*   CRTPGM    PGM(<pgmlib>/FTP)                                     

*             MODULE(<modlib>/FTP)                                  

*             BNDSRVPGM(<jsmpgmlib>/DCXS882X)                       

*             ACTGRP(*CALLER)                                       

*************************************************                   

                                                       

* You MUST replace the value of these constants        

* before compiling and running this example            

                                                       

d ftpserver       c                   '<your server>'   

d ftpuser         c                   '<user id>'       

d ftppassword     c                   '<password>'      

                                                    

* IFS file and folder names used by this program    

* - to try this program you need to create the      

*   folder /LIFTPIN in your IFS                     

* - this example assumes the shipped ORDER.XML file 

*   is present in the JSM instance folder           

                                                    

d flrtarget       c                   '/LIFTPIN'     

d ftpsource       c                   'ORDER.XML'    

d ftptarget       c                   'ORDER.XML'    

                                                                      

* Declare variables for the JSM calls                                

                                                                      

d jsmsrv          s             50a   inz(*blanks)                    

d jsmsts          s             20a   inz(*blanks)                    

d jsmmsg          s            255a   inz(*blanks)                    

d jsmcmd          s            255a   inz(*blanks)                    

d bytelength      s             10i 0 inz(*zero)                      

                                                                      

* Completion messages                                                

                                                                      

d CompMsg01       c                   'JSMOPEN call completed.'       

d CompMsg02       c                   '  SERVICE_LOAD call completed.'

d CompMsg10       c                   '  CONNECT call completed.'     

d CompMsg20       c                   '  LOGIN call completed.'       

d CompMsg30       c                   '  GETDIR call completed.'      

d CompMsg31       c                   '  - current directory on +     

d                                          remote server is: '        

d CompMsg40       c                   '  CHGDIR call completed.'      

d CompMsg50       c                   '  BINARY call completed.'      

d CompMsg60       c                   '  PUT call completed.'            

d CompMsg70       c                   '  QUIT call completed.'           

d CompMsg98       c                   '  SERVICE_UNLOAD call completed.' 

d CompMsg99       c                   'JSMCLOSE call completed.'         

                                                                         

* Procedure prototypes                                                  

                                                                         

d CheckResult     pr                                                     

d  crjsts                             const like(jsmsts)                 

d  crjmsg                             const like(jsmmsg)                 

                                                                         

d SendMessage     pr                                                     

d  smText                      512a   VALUE                              

d  smType                       10a   VALUE                              

                                                                         

* Prototypes for the JSM calls                                          

                                                                         

/COPY QRPGLESRC,JSM_PROC.H                                              

                                                                         

* Open a connection to the default JSM server                           

* - because the server parameter is blank, details of the default  

*   JSM server are obtained from the data area JSMCLTDTA on IBM i

*   or from the file jsmcltdta.txt on other supported platforms)   

                                                                    

c                   callp     p_jsmopen(jsmsrv:jsmsts:jsmmsg)       

c                   callp     CheckResult(jsmsts:jsmmsg)            

c                   callp     SendMessage(CompMsg01:'*COMP')        

                                                                    

* Load the FTPService                                              

* - this example explicitly turns tracing on, overriding the       

*   settings in the manager.properties file                        

                                                                    

c                   eval      jsmcmd = 'SERVICE_LOAD'               

c                             + ' SERVICE(FTPSERVICE) TRACE(*YES)'  

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)        

c                   callp     CheckResult(jsmsts:jsmmsg)            

c                   callp     SendMessage(CompMsg02:'*COMP')        

                                                                    

* Connect to FTP server                                            

                                                                    

c                   eval      jsmcmd = 'CONNECT'                       

c                             + ' HOST(' + %trim(ftpserver) + ')'      

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)           

c                   callp     CheckResult(jsmsts:jsmmsg)               

c                   callp     SendMessage(CompMsg10:'*COMP')           

                                                                       

* Login to FTP server                                                 

                                                                       

c                   eval      jsmcmd = 'LOGIN'                         

c                             + ' USER(' + %trim(ftpuser) + ')'        

c                             + ' PASSWORD(' + %trim(ftppassword) + ')'

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)           

c                   callp     CheckResult(jsmsts:jsmmsg)               

c                   callp     SendMessage(CompMsg20:'*COMP')           

                                                                       

* Get the current directory on the remote server                      

* - the current directory is returned in the jsmmsg field             

                                                                       

c                   eval      jsmcmd = 'GETDIR'                        

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)           

c                   callp     CheckResult(jsmsts:jsmmsg)            

c                   callp     SendMessage(CompMsg30:'*COMP')        

                                                                    

* ... output the current directory name into the joblog ...        

                                                                    

c                   callp     SendMessage(CompMsg31 + %trim(jsmmsg) 

c                                         :'*COMP')                 

                                                                    

* Change the current directory on the remote server                

                                                                    

c                   eval      jsmcmd = 'CHGDIR'                     

c                             + ' PATH(' + %trim(flrtarget) + ')'   

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)        

c                   callp     CheckResult(jsmsts:jsmmsg)            

c                   callp     SendMessage(CompMsg40:'*COMP')        

                                                                    

* Set binary mode                                                  

                                                                    

c                   eval      jsmcmd = 'BINARY'                     

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)        

c                   callp     CheckResult(jsmsts:jsmmsg)         

c                   callp     SendMessage(CompMsg50:'*COMP')     

                                                                 

* Put file onto remote server                                   

                                                                 

c                   eval      jsmcmd = 'PUT'                     

c                             + ' FROM(' + %trim(ftpsource) + ')'

c                             + '   TO(' + %trim(ftptarget) + ')'

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)     

c                   callp     CheckResult(jsmsts:jsmmsg)         

c                   callp     SendMessage(CompMsg60:'*COMP')     

                                                                 

* Quit the FTP session                                          

                                                                 

c                   eval      jsmcmd = 'QUIT'                    

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)     

c                   callp     CheckResult(jsmsts:jsmmsg)         

c                   callp     SendMessage(CompMsg70:'*COMP')     

                                                                 

* Unload the FTPService                                         

                                                               

c                   eval      jsmcmd = 'SERVICE_UNLOAD'        

c                   callp     p_jsmcmd(jsmcmd:jsmsts:jsmmsg)   

c                   callp     CheckResult(jsmsts:jsmmsg)       

c                   callp     SendMessage(CompMsg98:'*COMP')   

                                                               

* Close the connection to the JSM server and finish           

                                                               

c                   callp     p_jsmclose(jsmsts:jsmmsg)        

c                   callp     CheckResult(jsmsts:jsmmsg)       

c                   callp     SendMessage(CompMsg99:'*COMP')   

c                   eval      *inlr = *on                      

c                   return                                     

                                                               

*************************************************             

* Procedure to check the result of a Java Service Manager call

*************************************************             

p CheckResult     b                                            

d CheckResult     pi                                           

d  crjsts                             const like(jsmsts)       

d  crjmsg                             const like(jsmmsg)            

d  crText         s            512a                                 

d  crMsg1         c                   const('JSM Status : ')        

d  crMsg2         c                   const('JSM Message: ')        

d  crMsg3         c                   const('JSM Service error has +

d                                            occurred')             

c                   if        crjsts <> 'OK'                        

c                   eval      crText = crMsg1 + crjsts              

c                   callp     SendMessage(crText:'*DIAG')           

c                   eval      crText = crMsg2 + crjmsg              

c                   callp     SendMessage(crText:'*DIAG')           

c                   callp     SendMessage(crMsg3:'*ESCAPE')         

c                   endif                                           

p CheckResult     e                                                 

                                                                    

*************************************************                  

* Procedure to send a program message                              

*************************************************                  

p SendMessage     b                                                 

                                                                    

d SendMessage     pi                                             

d smText                       512a   VALUE                      

d smMsgT                        10a   VALUE                      

                                                                 

d smMsgI          s              7a   inz('CPF9897')             

d smMsgF          s             20a   inz('QCPFMSG   *LIBL     ')

d smDtaL          s             10i 0 inz(%size(smText))         

d smStkE          s             10a   inz('*')                   

d smStkC          s             10i 0 inz(1)                     

d smMsgK          s              4a                              

d smErrC          s             10i 0 inz(0)                     

                                                                 

c                   if        smMsgT = '*ESCAPE'                 

c                   eval      smMsgI = 'CPF9898'                 

c                   endif                                        

                                                                 

c                   call      'QMHSNDPM'                         

c                   parm                    smMsgI               

c                   parm                    smMsgF               

c                   parm                    smText               

c                   parm                    smDtaL

c                   parm                    smMsgT

c                   parm                    smStkE

c                   parm                    smStkC

c                   parm                    smMsgK

c                   parm                    smErrC

                                                  

p                 e