home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 19 / AACD19.BIN / AACD / Programming / YAEC / modules / test / ipc.e
Encoding:
Text File  |  2001-02-23  |  2.8 KB  |  134 lines

  1. OPT MODULE   -> ipc classes by Leif S 2000-2001
  2.              -> simplifies handling messages and ports.
  3.  
  4. MODULE 'exec/nodes'
  5. MODULE 'exec/ports'
  6. MODULE 'dos/dos'
  7.  
  8. EXTERN 'exec'
  9.  
  10. ->----------NEWMSG---------
  11.  
  12. EXPORT OBJECT newMsg OF mn
  13.    sendertask
  14.    type
  15.    cmnd
  16.    data
  17.    reply
  18. ENDOBJECT
  19.  
  20. EXPORT OBJECT newPort OF mp
  21.    active:INT
  22.    oldmp:PTR TO mp
  23. ENDOBJECT
  24.  
  25. PROC getType() OF newMsg
  26. ENDPROC self.type
  27.  
  28. PROC getCmnd() OF newMsg
  29. ENDPROC self.cmnd
  30.  
  31. PROC getData() OF newMsg
  32. ENDPROC self.data
  33.  
  34. PROC do(newPort:PTR TO newPort, type, cmnd, data) OF newMsg
  35.    self.ln.type := NT_MESSAGE
  36.    self.length := SIZEOF newMsg
  37.    self.type := IF type = NIL THEN self.sendertask ELSE type
  38.    self.cmnd := cmnd
  39.    self.data := data
  40.    IF newPort.active = FALSE THEN RETURN -1
  41.    PutMsg(newPort, self)
  42.    Wait(SIGBREAKF_CTRL_F)
  43. ENDPROC self.reply
  44.  
  45. PROC doName(portname, type, cmnd, data) OF newMsg
  46.    DEF newport:PTR TO newPort
  47.    newport := FindPort(portname)
  48.    IF newport THEN self.do(newport, type, cmnd, data)
  49. ENDPROC newport
  50.  
  51. PROC reply(reply) OF newMsg
  52.    self.reply := reply
  53.    Signal(self.sendertask, SIGBREAKF_CTRL_F)
  54. ENDPROC
  55.  
  56. PROC newMsg() OF newMsg
  57.    self.sendertask:=FindTask(0)
  58. ENDPROC
  59.  
  60. PROC getReply() OF newMsg
  61. ENDPROC self.reply
  62.  
  63. PROC end() OF newMsg
  64. ENDPROC NIL
  65.  
  66. /* msg TYPES */
  67. EXPORT CONST NMT_CTRL=-10, NMT_STREAM=-20, NMT_MISC=-30, NMT_ERRORS=-40
  68.  
  69. EXPORT PROC doMsgQ(newPort, type, command, data)
  70.    DEF msg:newMsg, er
  71. ENDPROC msg.do(newPort, type, command, data)
  72.  
  73.  
  74.  
  75. ->-------NEWPORT------------------
  76.  
  77. PROC newPort(name, pri) OF newPort
  78.    DEF mp:PTR TO mp
  79.    /* create port */
  80.    mp := CreateMsgPort()
  81.    IF mp = NIL THEN RETURN NIL
  82.    mp.ln.name := name
  83.    mp.ln.pri := pri
  84.    /* save original mp */
  85.    self.oldmp := mp
  86.    /* here we become the port in a nasty way :) */
  87.    IF mp.ln.pred THEN mp.ln.pred.succ := self
  88.    IF mp.ln.succ THEN mp.ln.succ.pred := self
  89.    CopyMem(mp, self, SIZEOF mp)
  90.    /* add if desired */
  91.    IF name THEN AddPort(self)
  92.    /* we are in action */
  93.    self.active := TRUE
  94. ENDPROC self
  95.  
  96. PROC collect() OF newPort
  97. ENDPROC GetMsg(self)
  98.  
  99. PROC clear() OF newPort
  100.    DEF newMsg:PTR TO newMsg
  101.    WHILE newMsg:=self.collect()
  102.       IF newMsg THEN newMsg.reply(NIL)
  103.    ENDWHILE
  104. ENDPROC
  105.  
  106. PROC end() OF newPort
  107.    self.active := FALSE
  108.    IF self.ln.pred THEN self.ln.pred.succ := self.oldmp
  109.    IF self.ln.succ THEN self.ln.succ.pred := self.oldmp
  110.    IF self.ln.name THEN RemPort(self.oldmp)
  111.    self.clear()
  112.    /* delete old mp */
  113.    IF self.oldmp THEN DeleteMsgPort(self.oldmp)
  114. ENDPROC
  115.  
  116. PROC isActive() OF newPort
  117. ENDPROC self.active
  118.  
  119. PROC wait() OF newPort
  120. ENDPROC WaitPort(self)
  121.  
  122. PROC collectlast() OF newPort
  123.    DEF msg, lastmsg=NIL:PTR TO newMsg
  124.    WHILE (msg:=self.collect())
  125.       IF lastmsg THEN lastmsg.reply(NIL)
  126.       lastmsg:=msg
  127.    ENDWHILE
  128. ENDPROC lastmsg
  129.  
  130. PROC getSigF() OF newPort
  131. ENDPROC Shl(1, self.sigbit)
  132.  
  133.  
  134.