Home | OS/2 Software | Rexx | Named Pipe Example (Intermediate to Advanced Level)

Named Pipe Example (Intermediate to Advanced Level)

Contributed by: Doug Rickman, Global Hydrology and Climate Center, MSFC, NASA

This is aimed at intermediate to advanced REXX users. The example shows how to establish a named pipe server and a client. Start NPipeServerDemo.cmd. It will start NPipeServerDemo.cmd in a separate shell. NPipeServerDemo.cmd will automatically connect to the server and execute some operations. When the prprogrammed operations are done the user may enter a single line to be sent to the server. After NPipeServerDemo is running you may also start NPipeClientDemo from the command line in another shell.



NPipeServerDemo.cmd

 

/*****************************************************************************/
/*                                                                           */
/* Sample named pipe server using REXXLIB. It simply echoes whatever         */
/* message is sent to it.  It will start EchoClient2.cmd.                    */
/*                                                                           */
/* Requires REXXLIB NMPIPE functions.                                        */
/*                                                                           */
/*****************************************************************************/
/* based on original from Quercus                                            */
/* mods by Doug Rickman Dec 19, 1997                                         */
/* this may be run with the program EchoClient2.cmd                          */

if rxfuncquery('rexxlibregister') then do         /* this will start rexxlib */
    call rxfuncadd 'rexxlibregister', 'rexxlib', 'rexxlibregister' 
    call rexxlibregister
    end
if rxfuncquery('sysloadfuncs') then do           /* this will start rexxutil */
    CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
    CALL SysLoadFuncs
    end

pipe = '\pipe\echo'

call nmpipe_create pipe, 'm', 'm', 'w'
'start NPIpeClientDemo.cmd'
do i=1
   call nmpipe_connect pipe
   if result \= 0 then exit
    say
   say 'Connection =' i

    do forever
      message = nmpipe_read(pipe)
        say 'Server received: |'||message||'|'
        select
            when message = 'end' then do
            call nmpipe_disconnect pipe
                call nmpipe_close pipe               
            exit
            end

            when message = 'releasing' | message = '' then do
            call nmpipe_disconnect pipe
                iterate i
                end
            otherwise nop
            end /* select */

      call nmpipe_write pipe, '"'message'"' 'is hereby returned to sender.' '0a'x
      end /* do forever */

    end i

return 1


NPipeClientDemo.cmd
/* Named pipe client demo and test.  Should be run with EchoServer2.cmd      */
/*                                                                           */
/* Doug Rickman Dec. 19, 1997                                                */

signal on Halt
signal on NotReady

if rxfuncquery('rexxlibregister') then do         /* this will start rexxlib */
    call rxfuncadd 'rexxlibregister', 'rexxlib', 'rexxlibregister' 
    call rexxlibregister
    end
if rxfuncquery('sysloadfuncs') then do           /* this will start rexxutil */
    CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
    CALL SysLoadFuncs
    end

/* --------------------------------------------------------------------------*/
/* --- begin MAIN                                               -------------*/

pipe = '\pipe\echo'

/* Open a pipe, write and read then write a release message to the server    */
rc= NMPIPE_OPEN(pipe,'M','W')
if rc<>0 then select
    when rc=2 then say 'Pipe 'pipe' not found.'
   when rc=4 then say 'Too many open handles.'
    otherwise say 'rc='rc 'This is an error code from NMPipe_Open.'
    end /* select */
rc=NMPipe_Write(pipe,'This is a NMPipe_Write from EchoClient2.cmd.')
if rc<>0 then select
    when rc=2 then say 'Pipe 'pipe' not found.'
    otherwise say 'rc='rc 'This is an error code from NMPipe_Write.'
    end /* select */
say NMPipe_Read(pipe)
rc= NMPipe_Write(pipe,'releasing')  /* This tells the server to disconnect */

rc=delay(2) /* give the server time to reset */

/* Open a pipe, transact some business then close the pipe's client side.    */
rc= NMPIPE_OPEN(pipe,'M','W')
say 'RC for open is 'rc
say NMPipe_Transact(pipe,'This is from the first  NMPipe_Transact.')
say NMPipe_Transact(pipe,'This is from the second NMPipe_Transact.')
say NMPipe_Transact(pipe,'This is from the third  NMPipe_Transact.')
rc=nmpipe_close(pipe)

/* Write and read  using calls                                               */
say NMPipe_Call(pipe,'Call: This is from the first  NMPipe_Call.')
say NMPipe_Call(pipe,'Call: This is from the second NMPipe_Call.')
say NMPipe_Call(pipe,'Call: This is from the third  NMPipe_Call.')

rc=delay(2) /* give the server time to reset */

/* Open a pipe, get user input, write and read then exit.                    */
rc= NMPIPE_OPEN(pipe,'M','W')
say 'RC for open is 'rc
say 'Enter something to send to the server.'
say 'Sending the string "end" will cause the server to terminate.'
parse pull stuff
rc=NMPipe_Write(pipe,stuff)
say NMPipe_Read(pipe)

/* 'EXIT */ /* This will close the shell in which this run */
exit
/* --- end MAIN                                                 -------------*/
/* --------------------------------------------------------------------------*/


/* --------------------------------------------------------------------------*/
/* --- begin subroutine - Halt:                                 -------------*/
Halt:
say 'This is a graceful exit from a Cntl-C'
exit
/* --- end  subroutine - Halt:                                  -------------*/
/* --------------------------------------------------------------------------*/
/* --- begin subroutine - NotReady:                             -------------*/
NotReady:
say 'It would seem that you are pointing at non-existant data.  Oops.  Bye!'
exit
/* --- end  subroutine - NotReady:                              -------------*/
/* --------------------------------------------------------------------------*/