home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / commodities / broker.e next >
Text File  |  1977-12-31  |  4KB  |  114 lines

  1. -> broker.e - Simple skeletal example of opening a broker.
  2.  
  3. MODULE 'commodities',
  4.        'dos/dos',
  5.        'exec/libraries',
  6.        'exec/ports',
  7.        'libraries/commodities'
  8.  
  9. ENUM ERR_NONE, ERR_ARG, ERR_BRKR, ERR_CXERR, ERR_LIB, ERR_PORT
  10.  
  11. RAISE ERR_BRKR IF CxBroker()=NIL,
  12.       ERR_LIB  IF OpenLibrary()=NIL,
  13.       ERR_PORT IF CreateMsgPort()=NIL
  14.     
  15. DEF broker=NIL, broker_mp=NIL:PTR TO mp, cxsigflag
  16.   
  17. PROC main() HANDLE
  18.   DEF msg
  19.   -> Before bothering with anything else, open the library
  20.   cxbase:=OpenLibrary('commodities.library', 37)
  21.   -> Commodities talks to a Commodities application through an Exec Message
  22.   -> port, which the application provides
  23.   broker_mp:=CreateMsgPort()
  24.  
  25.   -> The commodities.library function CxBroker() adds a broker to the master
  26.   -> list.  It takes two arguments, a pointer to a NewBroker structure and a
  27.   -> pointer to a LONG.  The NewBroker structure contains information to set
  28.   -> up the broker.  If the second argument is not NIL, CxBroker will fill it
  29.   -> in with an error code.
  30.   broker:=CxBroker(
  31.            [NB_VERSION,   -> Version of the NewBroker object
  32.             0,  -> E-Note: pad byte
  33.            'RKM broker',  -> Name: commodities uses for this commodity
  34.            'Broker',      -> Title of commodity that appears in CXExchange
  35.            'A simple example of a broker',  -> Description
  36.             0,  -> Unique: tells CX not to launch a commodity with the same name
  37.             0,  -> Flags: tells CX if this commodity has a window
  38.             0,  -> Pri: this commodity's priority
  39.             0,  -> E-Note: pad byte
  40.             broker_mp, -> Port: mp CX talks to
  41.             0   -> ReservedChannel: reserved for later use
  42.            ]:newbroker, NIL)
  43.   cxsigflag:=Shl(1, broker_mp.sigbit)
  44.  
  45.   -> After it's set up correctly, the broker has to be activated
  46.   ActivateCxObj(broker, TRUE)
  47.  
  48.   -> The main processing loop
  49.   processMsg()
  50.  
  51. EXCEPT DO
  52.   -> It's time to clean up.  Start by removing the broker from the Commodities
  53.   -> master list.  The DeleteCxObjAll() function will take care of removing a
  54.   -> CxObject and all those connected to it from the Commodities network
  55.   IF broker THEN DeleteCxObj(broker)
  56.   IF broker_mp
  57.     -> Empty the port of CxMsgs
  58.     WHILE msg:=GetMsg(broker_mp) DO ReplyMsg(msg)
  59.     DeleteMsgPort(broker_mp) -> E-Note: C version incorrectly uses DeletePort()
  60.   ENDIF
  61.   IF cxbase THEN CloseLibrary(cxbase)
  62.   SELECT exception
  63.   CASE ERR_BRKR;  WriteF('Error: Could not create broker\n')
  64.   CASE ERR_CXERR; WriteF('Error: Could not activate broker\n')
  65.   CASE ERR_LIB;   WriteF('Error: Could not open commodities.library\n')
  66.   CASE ERR_PORT;  WriteF('Error: Could not create message port\n')
  67.   ENDSELECT
  68. ENDPROC
  69.  
  70. PROC processMsg()
  71.   DEF msg, sigrcvd, msgid, msgtype, done=FALSE
  72.   REPEAT
  73.     -> Wait for something to happen
  74.     sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag)
  75.  
  76.     -> Process any messages
  77.     WHILE msg:=GetMsg(broker_mp)
  78.       -> Extract any necessary information from the CxMessage and return it
  79.       msgid:=CxMsgID(msg)
  80.       msgtype:=CxMsgType(msg)
  81.       ReplyMsg(msg)
  82.  
  83.       SELECT msgtype
  84.       CASE CXM_IEVENT
  85.         -> Shouldn't get any of these in this example
  86.       CASE CXM_COMMAND
  87.         -> Commodities has sent a command
  88.         WriteF('A command: ')
  89.         SELECT msgid
  90.         CASE CXCMD_DISABLE
  91.           WriteF('CXCMD_DISABLE\n')
  92.           -> The user clicked CX Exchange disable gadget, better disable
  93.           ActivateCxObj(broker, FALSE)
  94.         CASE CXCMD_ENABLE
  95.           -> User clicked enable gadget
  96.           WriteF('CXCMD_ENABLE\n')
  97.           ActivateCxObj(broker, TRUE)
  98.         CASE CXCMD_KILL
  99.           -> User clicked kill gadget, better quit
  100.           WriteF('CXCMD_KILL\n')
  101.           done:=TRUE
  102.         ENDSELECT
  103.       DEFAULT
  104.         WriteF('Unknown msgtype\n')
  105.       ENDSELECT
  106.     ENDWHILE
  107.     -> Test to see if user tried to break
  108.     IF sigrcvd AND SIGBREAKF_CTRL_C
  109.       done:=TRUE
  110.       WriteF('CTRL C signal break\n')
  111.     ENDIF
  112.   UNTIL done
  113. ENDPROC
  114.