Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/Group5/Message.cpp

Go to the documentation of this file.
00001 /***
00002    NAME
00003      message
00004    PURPOSE
00005      Message stuff.  Runs a message loop.
00006    NOTES
00007      Messages have the following format:
00008 
00009      int msgnum;
00010      char args[1];
00011 
00012      msgnum 0 means that the thread should terminate.  
00013     It takes no arguments.
00014 ***/
00015 
00016 #ifndef __J2K__Train__Message_CPP__
00017 #define __J2K__Train__Message_CPP__
00018 
00019 #include "message.hh"
00020 #include <sys/kernel.h>
00021 #include <unistd.h>
00022 #include <stdio.h>
00023 
00024 // Overall message format
00025 struct message
00026 {
00027     int msgnum;
00028     char args[1];               // really dependent on what's there
00029 };
00030 
00031 /*
00032  * Purpose:  Built a message thread.
00033  *
00034  * Input:    None.
00035  *
00036  * Output:   None.
00037  */
00038 
00039 Message_thread::Message_thread() : Schedule_thread() { }
00040 
00041 /*
00042  * Purpose:  Runs the message loop and dispatches to the proper
00043  *           function.
00044  *
00045  * Input:    None
00046  *
00047  * Output:   None
00048  */
00049 void Message_thread::run()
00050 {
00051     message* msg;
00052     pid_type pid;
00053     int status;
00054 
00055     while(1)
00056     {
00057         if((pid = Receive(0, buffer_, sizeof(buffer_))) < 0)
00058             continue;           // try again
00059         // convert message
00060         msg = (message*)buffer_;
00061 
00062         // message 0 is destroy message
00063         if(msg->msgnum == 0)
00064             break;
00065 
00066         // filter out message
00067         if(message_filter(msg->msgnum, msg->args, pid))
00068             continue;
00069         // Get entry and call it
00070         status = dispatch(this, msg->msgnum, msg->args, pid);
00071         // when we get >= 0, it means that's the status code
00072         if(status >= 0)
00073             Reply(pid, &status, sizeof(status));
00074     }
00075 
00076     // send confirmation for destruction
00077     Reply(pid, NULL, 0);
00078 }
00079 
00080 /*
00081  * Purpose:  Dispatch message into given table entry
00082  */
00083 int Message_thread::dispatch(Message_thread* self,
00084                              int msg,
00085                              char* buffer,
00086                              pid_type pid)
00087 {
00088     const message_table_entry* entry;
00089 
00090 //    printf("In dispatch: msg = %d, pid = %d\n", msg, pid);
00091 //    fflush(stdout);
00092     entry = self->find_entry(msg);
00093     if(!entry) return 0;
00094 //    fflush(stdout);
00095 //    printf("In dispatch: find_entry returned marshal %p function %p\n",
00096 //           entry->marshal, entry->function);
00097     return (*entry->marshal)(self, entry->function, msg, buffer, pid);
00098 }
00099 
00100 /*
00101  * Purpose:  Send message to thread
00102  *
00103  * Input:    buffer  : message to send
00104  *           length  : how long it is
00105  *           reply   : reply buffer
00106  *           rlength : how long it is
00107  *
00108  * Output:   None.
00109  */
00110 void Message_thread::send(const void* buffer, int length,
00111                           void* reply, int rlength)
00112 {
00113     if(pid_ < 0) { return; }
00114     Send(pid_, buffer, reply, length, rlength);
00115 }
00116 
00117 /*
00118  * Purpose:  Stops the current thread (if running)
00119  *
00120  */
00121 int Message_thread::stop()
00122 {
00123     static const int terminate_message = 0;
00124     if(pid_ < 0) { return -1; }
00125 
00126     // try to send message 0
00127     send(&terminate_message, sizeof(terminate_message), NULL, 0);
00128 
00129     pid_ = -1;
00130     return 0;
00131 }
00132 
00133 #endif

Generated on Sun Oct 14 18:46:35 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001