home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / exec_library / ports / port2.e < prev   
Text File  |  1977-12-31  |  2KB  |  70 lines

  1. -> port2.e - Port and message example, run at the same time as port1.e
  2.  
  3. MODULE 'amigalib/ports',
  4.        'dos/dos',
  5.        'exec/memory',
  6.        'exec/nodes',
  7.        'exec/ports'
  8.  
  9. ENUM ERR_NONE, ERR_FINDPORT, ERR_CREATEPORT
  10.  
  11. OBJECT xyMessage
  12.   msg:mn
  13.   x:INT
  14.   y:INT
  15. ENDOBJECT
  16.  
  17. PROC main() HANDLE
  18.   DEF xyreplyport=NIL:PTR TO mp, xymsg=NIL:PTR TO xyMessage,
  19.       reply:PTR TO xyMessage
  20.   -> Using createPort() with no name because this port need not be public.
  21.   IF NIL=(xyreplyport:=createPort(NIL, 0)) THEN Raise(ERR_CREATEPORT)
  22.   xymsg:=NewM(SIZEOF xyMessage, MEMF_PUBLIC OR MEMF_CLEAR)
  23.   xymsg.msg.ln.type:=NT_MESSAGE       -> Make up a message, including the
  24.   xymsg.msg.length:=SIZEOF xyMessage  -> reply port.
  25.   xymsg.msg.replyport:=xyreplyport
  26.   xymsg.x:=10  -> Our special message information
  27.   xymsg.y:=20
  28.  
  29.   WriteF('Sending to port1: x = \d y = \d\n', xymsg.x, xymsg.y)
  30.  
  31.   -> port2 will simply try to put one message to port1, wait for the reply,
  32.   -> and then exit
  33.   IF FALSE=safePutToPort(xymsg, 'xyport') THEN Raise(ERR_FINDPORT)
  34.  
  35.   WaitPort(xyreplyport)
  36.   IF reply:=GetMsg(xyreplyport)
  37.     -> We don't ReplyMsg since WE initiated the message.
  38.     WriteF('Reply contains: x = \d y = \d\n', xymsg.x, xymsg.y)
  39.   ENDIF
  40.  
  41.   -> Since we only use this private port for receiving replies, and we sent
  42.   -> only one and got one reply there is no need to cleanup.  For a public port,
  43.   -> or if you pass a pointer to the port to another process, it is a very
  44.   -> good habit to always handle all messages at the port before you delete it.
  45. EXCEPT DO
  46.   IF xymsg THEN Dispose(xymsg)  -> E-Note: not really necessary
  47.   IF xyreplyport THEN deletePort(xyreplyport)
  48.   SELECT exception
  49.   CASE ERR_CREATEPORT
  50.     WriteF('Couldn''t create "xyreplyport"\n')
  51.   CASE ERR_FINDPORT
  52.     WriteF('Can''t find "xyport"; start port1 in a separate shell\n')
  53.   CASE "MEM"
  54.     WriteF('Couldn''t get memory\n')
  55.   ENDSELECT
  56. ENDPROC
  57.  
  58. PROC safePutToPort(message, portname)
  59.   DEF port:PTR TO mp
  60.   Forbid()
  61.   port:=FindPort(portname)
  62.   IF port THEN PutMsg(port, message)
  63.   Permit()
  64.   -> Once we've done a Permit(), the port might go away and leave us with an
  65.   -> invalid port address.  So we return just a boolean to indicate whether
  66.   -> the message has been sent or not.
  67.   -> E-Note: Be careful - if FindPort() automatically raised an exception
  68.   ->         you might forget to Permit()!
  69. ENDPROC port<>NIL  -> FALSE if the port was not found
  70.