home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / classsource.lha / ClassSource / Exec / Ports / Ports.c
Encoding:
C/C++ Source or Header  |  1995-02-12  |  3.5 KB  |  239 lines

  1.  
  2. #include <classes/Exec/Ports.h>
  3.  
  4. #include <pragma/exec_lib.h>
  5.  
  6. MessageC::MessageC(class PortC &replyport)
  7. {
  8.     mn_ReplyPort = replyport.port();
  9. }
  10.  
  11. MessageC::MessageC(MsgPort *replyport)
  12. {
  13.     mn_ReplyPort = replyport;
  14. }
  15.  
  16. VOID MessageC::reply()
  17. {
  18.     ReplyMsg(this);
  19. }
  20.  
  21. // *****************************************************
  22.  
  23. PortC::PortC()
  24.     : SignalHandlerC(), ShareC()
  25. {
  26.     msgport = CreateMsgPort();
  27.     if (!msgport)
  28.         throw PortX();
  29.     mask = 1L << msgport->mp_SigBit;
  30.     pub = FALSE;
  31.     own = TRUE;
  32. }
  33.  
  34. PortC::PortC(STRPTR name, BYTE pri)
  35.     : SignalHandlerC(), ShareC()
  36. {
  37.     msgport = CreateMsgPort();
  38.     if (!msgport)
  39.         throw PortX();
  40.     mask = 1L << msgport->mp_SigBit;
  41.     msgport->mp_Node.ln_Name = name;
  42.     msgport->mp_Node.ln_Pri = pri;
  43.     Forbid();
  44.     if (FindPort(name))
  45.     {
  46.         Permit();
  47.         DeleteMsgPort(msgport);
  48.         throw PortX(TRUE);
  49.     };
  50.     AddPort(msgport);
  51.     Permit();
  52.     pub = TRUE;
  53.     own = TRUE;
  54. }
  55.  
  56. PortC::PortC(MsgPort *port)
  57.     : SignalHandlerC(), ShareC()
  58. {
  59.     if (!port)
  60.         throw PortX();
  61.     msgport = port;
  62.     mask = 1L << msgport->mp_SigBit;
  63.     pub = FALSE;
  64.     own = FALSE;
  65. }
  66.  
  67. PortC::PortC(const PortC &s)
  68.     : ShareC(s)
  69. {
  70.     msgport = s.msgport;
  71.     pub = s.pub;
  72.     own = s.own;
  73. }
  74.  
  75. PortC::~PortC()
  76. {
  77.     if (only())
  78.     {
  79.         if (pub)
  80.             RemPort(msgport);
  81.         flush();
  82.         if (own)
  83.             DeleteMsgPort(msgport);
  84.     };
  85. }
  86.  
  87. PortC &PortC::operator = (const PortC &s)
  88. {
  89.     if (this != &s)
  90.     {
  91.         if (only())
  92.         {
  93.             if (pub)
  94.                 RemPort(msgport);
  95.             flush();
  96.             if (own)
  97.                 DeleteMsgPort(msgport);
  98.         };
  99.         ShareC::operator=(s);
  100.         SignalHandlerC::operator=(s);
  101.         msgport = s.msgport;
  102.         pub = s.pub;
  103.         own = s.own;
  104.     };
  105.     return *this;
  106. }
  107.  
  108. BOOL PortC::forMe(ULONG)
  109. {
  110.     return !isEmpty();
  111. }
  112.  
  113. BOOL PortC::handle(ULONG)
  114. {
  115.     MessageC *msg;
  116.     while (msg = (MessageC *) GetMsg(msgport)) {
  117.         if (handleMsg(*msg))
  118.             return TRUE;
  119.     };
  120.     return FALSE;
  121. }
  122.  
  123. VOID PortC::flush()
  124. {
  125.     MessageC *msg;
  126.     while (msg = (MessageC *) GetMsg(msgport)) {
  127.         if (msg->mn_Node.ln_Type == NT_MESSAGE)
  128.             msg->reply();
  129.     };
  130. }
  131.  
  132. BOOL PortC::handleMsg(MessageC &msg)
  133. {
  134.     if (msg.mn_Node.ln_Type == NT_MESSAGE)
  135.         msg.reply();
  136.     return FALSE;
  137. }
  138.  
  139. // ******************************************************
  140.  
  141. VOID HandlerChainC::add(MessageHandlerC &h)
  142. {
  143.     addTail(h);
  144. }
  145.  
  146. BOOL HandlerChainC::handle(MessageC &msg)
  147. {
  148.     ListCursorC lc(*this);
  149.     _exit = FALSE;
  150.     BOOL r = FALSE;
  151.     while (!lc.isDone()) {
  152.         MessageHandlerC *h = (MessageHandlerC *) lc.item();
  153.         if (h->handle(msg))
  154.         {
  155.             _exit |= h->exit();
  156.             r = TRUE;
  157.         };
  158.         lc.next();
  159.     };
  160.     return r;
  161. }
  162.  
  163. // ******************************************************
  164.  
  165. VOID HandlerPortC::add(MessageHandlerC &h)
  166. {
  167.     addTail(h)
  168. }
  169.  
  170. BOOL HandlerPortC::handleMsg(MessageC &msg)
  171. {
  172.     ListCursorC lc(*this);
  173.     BOOL r = FALSE;
  174.     while (!lc.isDone()) {
  175.         MessageHandlerC *h = (MessageHandlerC *) lc.item();
  176.         if (h->forMe(msg))
  177.         {
  178.             if (h->handle(msg))
  179.             {
  180.                 BOOL b = h->exit();
  181.                 r = r | b;
  182.             };
  183.         };
  184.         lc.next();
  185.     };
  186.     if (msg.mn_Node.ln_Type == NT_MESSAGE)
  187.         msg.reply();
  188.     return r;
  189. }
  190.  
  191. // *******************************************************
  192.  
  193. SendPortC::SendPortC(PortC &port)
  194. {
  195.     msgport = port.port();
  196.     portname = NULL;
  197. }
  198.  
  199. SendPortC::SendPortC(MsgPort *port)
  200. {
  201.     if (!port)
  202.         throw PortX();
  203.     msgport = port;
  204.     portname = NULL;
  205. }
  206.  
  207. SendPortC::SendPortC(STRPTR name)
  208. {
  209.     if (!name)
  210.         throw PortX();
  211.     portname = name;
  212.     msgport = FindPort(portname);
  213. }
  214.  
  215. SendPortC::~SendPortC()
  216. {
  217. }
  218.  
  219. BOOL SendPortC::send(MessageC &msg)
  220. {
  221.     if (!portname)
  222.     {
  223.         PutMsg(msgport,&msg);
  224.         return TRUE;
  225.     }
  226.     else {
  227.         Forbid();
  228.         msgport = FindPort(portname);
  229.         if (!msgport)
  230.         {
  231.             Permit();
  232.             return FALSE;
  233.         };
  234.         PutMsg(msgport,&msg);
  235.         Permit();
  236.         return TRUE;
  237.     };
  238. }
  239.