00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00025 struct message
00026 {
00027 int msgnum;
00028 char args[1];
00029 };
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 Message_thread::Message_thread() : Schedule_thread() { }
00040
00041
00042
00043
00044
00045
00046
00047
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;
00059
00060 msg = (message*)buffer_;
00061
00062
00063 if(msg->msgnum == 0)
00064 break;
00065
00066
00067 if(message_filter(msg->msgnum, msg->args, pid))
00068 continue;
00069
00070 status = dispatch(this, msg->msgnum, msg->args, pid);
00071
00072 if(status >= 0)
00073 Reply(pid, &status, sizeof(status));
00074 }
00075
00076
00077 Reply(pid, NULL, 0);
00078 }
00079
00080
00081
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
00091
00092 entry = self->find_entry(msg);
00093 if(!entry) return 0;
00094
00095
00096
00097 return (*entry->marshal)(self, entry->function, msg, buffer, pid);
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
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
00119
00120
00121 int Message_thread::stop()
00122 {
00123 static const int terminate_message = 0;
00124 if(pid_ < 0) { return -1; }
00125
00126
00127 send(&terminate_message, sizeof(terminate_message), NULL, 0);
00128
00129 pid_ = -1;
00130 return 0;
00131 }
00132
00133 #endif