c
c	masktrap1.f 1.4 92/07/01 15:23:50
c
c  Copyright 1991 Intel Corporation, Inc.
c  See the "Copyright" file for complete rights and liability information.
c
c  Node 0 posts a receive and sets up a handler.
c  Node n sends a message to node 0.
c  Node 0 drops into the handler when it receives the message.

      include 'fcube.h'

      integer      iam, mtime, oldmask
      character*80 buf
      external myhandler

c  Identify self.

      iam = mynode()

c  If node 0, then ...

      if(iam .eq. 0) then
         print *, 'Starting...'

c    Disable (block) send/receive traps.

         oldmask = masktrap(1)

c    Post a receive and identify a handler procedure.

         write(*, 100) iam
100      format('Node ', i2, ': Before hrecv')

         call hrecv(100, buf, len(buf), myhandler)

         write(*, 200) iam
200      format('Node ', i2, ': After hrecv')

c    Delay to ensure that node is still around when message arrives.

         mtime = mclock()
1        continue
         if(mclock() .lt. mtime + 2000) goto 1

         write(*, 300) iam
300      format('Node ', i2, ': Waiting ...')

         mtime = mclock()
2        continue
         if(mclock() .lt. mtime + 2000) goto 2

         write(*, 400) iam
400      format('Node ', i2, ': ... No hrecv interrupts can occur ...')

         mtime = mclock()
3        continue
         if(mclock() .lt. mtime + 2000) goto 3

c    Enable send/receive traps.

         write(*, 500) iam
500      format('Node ', i2, ': ... until now.')

         oldmask = masktrap(0)

         write(*, 600) iam, buf
600      format('Node ', i2, ' received: ', a)

         write(*, 700) iam
700      format('Node ', i2, ': Completed.')

c  If not node 0, then...

      else

c    Delay to ensure that hrecv is already posted.

         mtime = mclock()
4        continue
         if(mclock() .lt. mtime + 1000) goto 4

c    Send message.

         write(buf, 800) iam
800      format('Hello from node ', i2)

         call csend(100, buf, len(buf), 0, mypid())

         write(*, 900) iam, buf
900      format('Node ', i2, ' sent:     ', a)

      endif

      end
