home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM System Amiga C Club */
- /* Chapter: Messages Tulevagen 22 */
- /* File: Example1B.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-05-02 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This program will create a message of type NrMessage. */
- /* It will then try to find a message port called "NrPort". */
- /* If it finds that port it will send a message to it, and */
- /* wait for the other task to reply. */
-
-
-
- #include <exec/types.h> /* STRPTR */
- #include <exec/ports.h> /* struct Message */
- #include <exec/memory.h> /* MEMF_PUBLIC */
- #include <exec/nodes.h> /* NT_MESSAGE */
-
-
-
- /* Declare a pointer to our message and reply port: */
- struct MsgPort *msgp, *replymsgp;
-
- /* The message will be in this form: */
- struct NrMessage
- {
- struct Message SystemMsg;
- int number;
- };
-
- /* Declare a pointer to the message: */
- struct NrMessage *nrmsg, *msg;
-
-
-
- /* Declare the functions: */
- void clean_up();
- void main();
-
-
- void main()
- {
- /* Allocate memory for the message: */
- /* (Make it public and clear it.) */
- nrmsg = (struct NrMessage *)
- AllocMem( sizeof( struct NrMessage ), MEMF_PUBLIC|MEMF_CLEAR );
-
- /* Check if we have allocated the memory successfully: */
- if( !nrmsg )
- clean_up( "Not enough memory for message!" );
-
-
-
- /* Try to find the message port "NrPort": */
- msgp = (struct MsgPort *) FindPort( "NrPort" );
-
- /* Have we found the message port? */
- if( !msgp )
- clean_up( "Could not find the message port!" );
-
-
-
- /* Create a reply port: (When the other task replies */
- /* we will receive a message at this port.) */
- replymsgp = (struct MsgPort *) CreatePort( "NrReplyPort", 0 );
-
- /* Have we received a reply port? */
- if( !replymsgp )
- clean_up( "Could not create the reply port!" );
-
-
-
- /* Initialize the message: */
- /* Set type to NT_MESSAGE: */
- nrmsg->SystemMsg.mn_Node.ln_Type = NT_MESSAGE;
-
- /* Give the message a pointer to our reply port: */
- nrmsg->SystemMsg.mn_ReplyPort = replymsgp;
-
- /* Set the length of our message: */
- nrmsg->SystemMsg.mn_Length = sizeof( struct NrMessage );
-
- /* Set the message itself: */
- nrmsg->number = 12345;
-
-
-
- /* Inform the user: */
- printf( "B: We have now successfully prepared everything!\n" );
- printf( "B: Lets send the value: %d\n", nrmsg->number );
-
- /* Send the message: */
- PutMsg( msgp, nrmsg );
-
- /* After we have sent our message and until we */
- /* receive a reply we are not allowed to read */
- /* or alter the message any more! */
- printf( "B: Message sent, lets wait...\n" );
-
-
-
- /* Wait at our reply port: */
- WaitPort( replymsgp );
-
-
-
- /* Try to receive as many messages as possible */
- /* from the reply port. (In this example we will */
- /* only receive one reply, but you never know.) */
- while( msg = (struct NrMessage *) GetMsg( replymsgp ))
- {
- /* We may now examine the message: */
- printf( "B: We have received a reply!\n" );
- printf( "B: The value is now: %d\n", msg->number );
- }
-
-
-
- /* The End! */
- clean_up( "The End!" );
- }
-
-
-
- void clean_up( text )
- STRPTR text;
- {
- /* If we have successfully allocated memory for a message */
- /* deallocate it. (Note that we may not deallocate this */
- /* message if we have sent it to another task's message */
- /* port, and have not received any reply!) */
- if( nrmsg )
- FreeMem( nrmsg, sizeof( *nrmsg ) );
-
- /* If we have we successfully created a reply */
- /* port, close it: */
- if( replymsgp )
- DeletePort( replymsgp);
-
- /* Print any message: */
- printf( "B: %s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-