home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / xi-1 / broker.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  6KB  |  174 lines

  1. ;/* broker.c - Simple skeletal example of opening a broker compiled with SAS/C 6.56
  2. sc DATA=FAR NMINC STRMERGE NOSTKCHK IGNORE=73 broker.c
  3. slink FROM LIB:c.o,broker.o TO broker LIBRARY LIB:sc.lib,LIB:amiga.lib
  4. quit
  5.  */
  6. /*
  7. Copyright (c) 1991 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga,
  10. Inc. for use with the Amiga Mail Volume II technical publication.
  11. Amiga Mail Volume II contains additional information on the correct
  12. usage of the techniques and operating system functions presented in
  13. these examples.  The source and executable code of these examples may
  14. only be distributed in free electronic form, via bulletin board or
  15. as part of a fully non-commercial and freely redistributable
  16. diskette.  Both the source and executable code (including comments)
  17. must be included, without modification, in any copy.  This example
  18. may not be published in printed form or distributed with any
  19. commercial product. However, the programming techniques and support
  20. routines set forth in these examples may be used in the development
  21. of original executable software products for Commodore Amiga
  22. computers.
  23.  
  24. All other rights reserved.
  25.  
  26. This example is provided "as-is" and is subject to change; no
  27. warranties are made.  All use is at your own risk. No liability or
  28. responsibility is assumed.
  29. */
  30.  
  31. #include <exec/libraries.h>
  32. #include <libraries/commodities.h>
  33. #include <dos/dos.h>
  34. #include <clib/exec_protos.h>
  35. #include <clib/alib_protos.h>
  36. #include <clib/alib_stdio_protos.h>
  37. #include <clib/commodities_protos.h>
  38.  
  39. #ifdef LATTICE
  40. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  41. int chkabort(void) { return(0); }
  42. #endif
  43.  
  44. struct Library *CxBase;
  45.  
  46. void main(void);
  47. LONG ProcessMsg(void);
  48.  
  49. CxObj *broker;
  50.  
  51. struct MsgPort *broker_mp;
  52.  
  53. struct NewBroker newbroker = {
  54.     NB_VERSION, /* nb_Version - Version of the NewBroker structure */
  55.     "AmigaMail broker", /* nb_Name - Name CX uses to identify this commodity */
  56.     "Broker",   /* nb_Title - Title of commodity that appears in CXExchange */
  57.     "A simple example of a broker",  /* nb_Descr - Description of the commodity */
  58.     0,          /* nb_Unique - Tells CX not to launch new commodity with same name */
  59.     0,          /* nb_Flags - Tells CX if this commodity has a window */
  60.     0,          /* nb_Pri - This commodity's priority */
  61.     0,          /* nb_Port - MsgPort CX talks to */
  62.     0           /* nb_ReservedChannel - reserved for later use */
  63. };
  64.  
  65. ULONG cxsigflag;
  66.  
  67. void main()
  68. {
  69.     /* Before bothering with anything else, open the library */
  70.     if (CxBase = OpenLibrary("commodities.library", 37L))
  71.     {
  72.         /* Commodities talks to a Commodities application through
  73.          * an Exec Message port, which the application provides
  74.          */
  75.         if (broker_mp = CreateMsgPort())
  76.         {
  77.             newbroker.nb_Port = broker_mp;
  78.  
  79.             /* The commodities.library function CxBroker() adds a
  80.              * broker to the master list.  It takes two arguments,
  81.              * a pointer to a NewBroker structure and a pointer to
  82.              * a LONG.  The NewBroker structure contains information
  83.              * to set up the broker.  If the second argument is not
  84.              * NULL, CxBroker will fill it in with an error code.
  85.              */
  86.             if (broker = CxBroker(&newbroker, NULL))
  87.             {
  88.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  89.  
  90.                 /* After it's set up correctly, the broker
  91.                  * has to be activated
  92.                  */
  93.                 ActivateCxObj(broker, 1L);
  94.  
  95.                 /* the main processing loop */
  96.                 while (ProcessMsg());
  97.  
  98.                 /* It's time to clean up.  Start by removing
  99.                  * the broker from the Commodities master list.
  100.                  * The DeleteCxObjAll() function will take care
  101.                  * of removing a CxObject and all those connected
  102.                  * to it from the Commodities network
  103.                  */
  104.                 DeleteCxObj(broker);
  105.             }
  106.             DeletePort(broker_mp);
  107.         }
  108.         CloseLibrary(CxBase);
  109.     }
  110. }
  111.  
  112. LONG ProcessMsg(void)
  113. {
  114.     CxMsg *msg;
  115.  
  116.     ULONG sigrcvd, msgid, msgtype;
  117.     LONG returnvalue = 1L;
  118.  
  119.     /* wait for something to happen */
  120.     sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  121.  
  122.     /* process any messages */
  123.     while(msg = (CxMsg *)GetMsg(broker_mp))
  124.     {
  125.         /* Extract necessary information from the CxMessage and return it */
  126.         msgid = CxMsgID(msg);
  127.         msgtype = CxMsgType(msg);
  128.         ReplyMsg((struct Message *)msg);
  129.  
  130.         switch(msgtype)
  131.         {
  132.             case CXM_IEVENT:
  133.                 /* Shouldn't get any of these in this example */
  134.                 break;
  135.             case CXM_COMMAND:
  136.                 /* Commodities has sent a command */
  137.                 printf("A command: ");
  138.                 switch(msgid)
  139.                 {
  140.                     case CXCMD_DISABLE:
  141.                         printf("CXCMD_DISABLE\n");
  142.                         /* The user clicked Commodities Exchange disable
  143.                          * gadget better disable
  144.                          */
  145.                         ActivateCxObj(broker, 0L);
  146.                         break;
  147.                     case CXCMD_ENABLE:
  148.                         /* user clicked enable gadget */
  149.                         printf("CXCMD_ENABLE\n");
  150.                         ActivateCxObj(broker, 1L);
  151.                         break;
  152.                     case CXCMD_KILL:
  153.                         /* user clicked kill gadget, better quit */
  154.                         printf("CXCMD_KILL\n");
  155.                         returnvalue = 0L;
  156.                         break;
  157.                 }
  158.                 break;
  159.             default:
  160.                 printf("Unknown msgtype\n");
  161.                 break;
  162.         }
  163.     }
  164.  
  165.     /* Test to see if user tried to break */
  166.     if (sigrcvd & SIGBREAKF_CTRL_C)
  167.     {
  168.         returnvalue = 0L;
  169.         printf("CTRL C signal break\n");
  170.     }
  171.     return(returnvalue);
  172. }
  173.  
  174.