home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / broker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  5.5 KB  |  162 lines

  1. /* The example below, Broker.c, receives input from one source, the
  2.  * controller program.  The controller program sends a CxMessage each
  3.  * time the user clicks its Enable, Disable, or Kill gadgets.  Using the
  4.  * CxMsgID() function, the commodity finds out what the command is and
  5.  * executes it.
  6.  *
  7.  * broker.c - Simple skeletal example of opening a broker
  8.  *
  9.  * compiled with SASC 5.10
  10.  * LC -b0 -cfist -v broker.c
  11.  * Blink FROM LIB:c.o,broker.o TO broker LIBRARY LIB:LC.lib,LIB:Amiga.lib
  12.  */
  13. #include <exec/libraries.h>
  14. #include <libraries/commodities.h>
  15. #include <dos/dos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/alib_protos.h>
  18. #include <clib/alib_stdio_protos.h>
  19. #include <clib/commodities_protos.h>
  20.  
  21. #ifdef LATTICE
  22. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  23. int chkabort(void) { return(0); }
  24. #endif
  25.  
  26. void main(void);
  27. void ProcessMsg(void);
  28.  
  29. struct Library *CxBase;
  30.  
  31. CxObj *broker;
  32. struct MsgPort *broker_mp;
  33. ULONG cxsigflag;
  34.  
  35. struct NewBroker newbroker = {
  36.     NB_VERSION,   /* nb_Version - Version of the NewBroker structure */
  37.     "RKM broker", /* nb_Name - Name Commodities uses to identify this commodity */
  38.     "Broker",     /* nb_Title - Title of commodity that appears in CXExchange */
  39.     "A simple example of a broker",  /* nb_Descr - Description of the commodity */
  40.     0,            /* nb_Unique - Tells CX not to launch another commodity with same name */
  41.     0,            /* nb_Flags - Tells CX if this commodity has a window */
  42.     0,            /* nb_Pri - This commodity's priority */
  43.     0,            /* nb_Port - MsgPort CX talks to */
  44.     0             /* nb_ReservedChannel - reserved for later use */
  45. };
  46.  
  47.  
  48. void main(void)
  49. {
  50.     CxMsg *msg;
  51.  
  52.     /* Before bothering with anything else, open the library */
  53.     if (CxBase = OpenLibrary("commodities.library", 37L))
  54.     {
  55.         /* Commodities talks to a Commodities application through */
  56.         /* an Exec Message port, which the application provides   */
  57.         if (broker_mp = CreateMsgPort())
  58.         {
  59.             newbroker.nb_Port = broker_mp;
  60.  
  61.             /* The commodities.library function CxBroker() adds a borker to the
  62.              * master list.  It takes two arguments, a pointer to a NewBroker
  63.              * structure and a pointer to a LONG.  The NewBroker structure contains
  64.              * information to set up the broker.  If the second argument is not
  65.              * NULL, CxBroker will fill it in with an error code.
  66.              */
  67.             if (broker = CxBroker(&newbroker, NULL))
  68.             {
  69.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  70.  
  71.                 /* After it's set up correctly, the broker has to be activated */
  72.                 ActivateCxObj(broker, 1L);
  73.  
  74.                 /* the main processing loop */
  75.                 ProcessMsg();
  76.  
  77.                 /* It's time to clean up.  Start by removing the broker from the
  78.                  * Commodities master list.  The DeleteCxObjAll() function will
  79.                  * take care of removing a CxObject and all those connected
  80.                  * to it from the Commodities network
  81.                  */
  82.                 DeleteCxObj(broker);
  83.  
  84.                 /* Empty the port of CxMsgs */
  85.                 while(msg = (CxMsg *)GetMsg(broker_mp))
  86.                     ReplyMsg((struct Message *)msg);
  87.             }
  88.             DeletePort(broker_mp);
  89.         }
  90.         CloseLibrary(CxBase);
  91.     }
  92. }
  93.  
  94.  
  95. void ProcessMsg(void)
  96. {
  97.     CxMsg *msg;
  98.  
  99.     ULONG sigrcvd, msgid, msgtype;
  100.     LONG returnvalue = 1L;
  101.  
  102.     while (returnvalue)
  103.     {
  104.         /* wait for something to happen */
  105.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  106.  
  107.         /* process any messages */
  108.         while(msg = (CxMsg *)GetMsg(broker_mp))
  109.         {
  110.             /* Extract necessary information from the CxMessage and return it */
  111.             msgid = CxMsgID(msg);
  112.             msgtype = CxMsgType(msg);
  113.             ReplyMsg((struct Message *)msg);
  114.  
  115.             switch(msgtype)
  116.             {
  117.                 case CXM_IEVENT:
  118.                     /* Shouldn't get any of these in this example */
  119.                     break;
  120.                 case CXM_COMMAND:
  121.                     /* Commodities has sent a command */
  122.                     printf("A command: ");
  123.                     switch(msgid)
  124.                     {
  125.                         case CXCMD_DISABLE:
  126.                             printf("CXCMD_DISABLE\n");
  127.                             /* The user clicked Commodities Exchange disable
  128.                              * gadget better disable
  129.                              */
  130.                             ActivateCxObj(broker, 0L);
  131.                             break;
  132.                         case CXCMD_ENABLE:
  133.                             /* user clicked enable gadget */
  134.                             printf("CXCMD_ENABLE\n");
  135.                             ActivateCxObj(broker, 1L);
  136.                             break;
  137.                         case CXCMD_KILL:
  138.                             /* user clicked kill gadget, better quit */
  139.                             printf("CXCMD_KILL\n");
  140.                             returnvalue = 0L;
  141.                             break;
  142.                     }
  143.                     break;
  144.                 default:
  145.                     printf("Unknown msgtype\n");
  146.                     break;
  147.             }
  148.         }
  149.         /* Test to see if user tried to break */
  150.         if (sigrcvd & SIGBREAKF_CTRL_C)
  151.         {
  152.             returnvalue = 0L;
  153.             printf("CTRL C signal break\n");
  154.         }
  155.     }
  156. }
  157.  
  158.  
  159. /* Notice that Broker.c uses Ctrl-C as a break key.  The break key for
  160. ** any commodity should be Ctrl-C.
  161. */
  162.