00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __DEMO1_H
00018 #define __DEMO1_H
00019 #include "active-objects.h"
00020 using namespace std;
00021 class DemoSender;
00022 class ActiveDemoSender;
00023 #ifdef TRACE_MESSAGES
00024 #define ENTER_MSG cerr << "Start " << declaration() << "\n";
00025 #define LEAVE_MSG cerr << "Stop " << declaration() << "\n";
00026 #endif
00027 #ifndef ENTER_MSG
00028 #define ENTER_MSG ;
00029 #endif
00030 #ifndef LEAVE_MSG
00031 #define LEAVE_MSG ;
00032 #endif
00033 class DemoReceiver;
00034 class ActiveDemoReceiver;
00035 #ifdef TRACE_MESSAGES
00036 #define ENTER_MSG cerr << "Start " << declaration() << "\n";
00037 #define LEAVE_MSG cerr << "Stop " << declaration() << "\n";
00038 #endif
00039 #ifndef ENTER_MSG
00040 #define ENTER_MSG ;
00041 #endif
00042 #ifndef LEAVE_MSG
00043 #define LEAVE_MSG ;
00044 #endif
00045
00046
00047
00048
00058 class ActiveDemoSender_msg_
00059 {
00060 public:
00065 virtual elementResult run(ActiveDemoSender * )
00066 {
00067 assert(0);
00068 }
00073 virtual string declaration()
00074 {
00075 return "Unknown message";
00076 }
00077 };
00078
00088 class ActiveDemoReceiver_msg_
00089 {
00090 public:
00095 virtual elementResult run(ActiveDemoReceiver * )
00096 {
00097 assert(0);
00098 }
00103 virtual string declaration()
00104 {
00105 return "Unknown message";
00106 }
00107 };
00108
00109
00110
00111
00112
00113 class ActiveDemoSender: public ActiveObject< ActiveDemoSender_msg_* >
00114 {
00115 friend class DemoSender;
00116 DemoSender * self;
00117 virtual elementResult handle( ActiveDemoSender_msg_* cmd)
00118 {
00119 if (cmd) return cmd->run(this);
00120 else return Done;
00121 };
00122 volatile int tosend;
00123 DemoReceiver* recv;
00124 public: elementResult startSending(DemoReceiver* recv, int nr);
00125 protected: void queue_startSending(DemoReceiver* recv, int nr);
00126 protected:
00127 ActiveDemoSender(DemoSender* s, string name):
00128 ActiveObject< ActiveDemoSender_msg_ * >(name), self(s)
00129 {
00130 tosend = 20;
00131 };
00132 };
00133
00134 class ActiveDemoReceiver: public ActiveObject< ActiveDemoReceiver_msg_* >
00135 {
00136 friend class DemoReceiver;
00137 DemoReceiver * self;
00138 virtual elementResult handle( ActiveDemoReceiver_msg_* cmd)
00139 {
00140 if (cmd) return cmd->run(this);
00141 else return Done;
00142 };
00143 public: elementResult printNumber(int nr);
00144 protected: void queue_printNumber(int nr);
00145 protected:
00146 ActiveDemoReceiver(DemoReceiver* s, string name):
00147 ActiveObject< ActiveDemoReceiver_msg_ * >(name), self(s)
00148 {
00149 };
00150 };
00151
00152
00153
00154
00155
00156 class ActiveDemoSender_msg_startSending: public ActiveDemoSender_msg_
00157 {
00158 DemoReceiver* recv;
00159 int nr;
00160 public:
00161 ActiveDemoSender_msg_startSending(DemoReceiver* recv, int nr) : recv(recv), nr(nr)
00162 {
00163 };
00164 virtual string declaration()
00165 {
00166 return "DemoSender::startSending(DemoReceiver* recv, int nr)";
00167 }
00168 virtual elementResult run(ActiveDemoSender * ao)
00169 {
00170 ENTER_MSG;
00171 elementResult res = ao->startSending(recv, nr);
00172 LEAVE_MSG;
00173 return res;
00174 };
00175 };
00176
00177 class ActiveDemoReceiver_msg_printNumber: public ActiveDemoReceiver_msg_
00178 {
00179 int nr;
00180 public:
00181 ActiveDemoReceiver_msg_printNumber(int nr) : nr(nr)
00182 {
00183 };
00184 virtual string declaration()
00185 {
00186 return "DemoReceiver::printNumber(int nr)";
00187 }
00188 virtual elementResult run(ActiveDemoReceiver * ao)
00189 {
00190 ENTER_MSG;
00191 elementResult res = ao->printNumber(nr);
00192 LEAVE_MSG;
00193 return res;
00194 };
00195 };
00196
00197
00198
00199
00200
00206 class DemoSender
00207 {
00208 private:
00214 ActiveDemoSender object;
00215 public:
00226 DemoSender(string name="DemoSender"): object(this, name) {};
00227 public:
00228 void startSending(DemoReceiver* recv, int nr)
00229 {
00230 object.queue_startSending(recv, nr);
00231 };
00232 public:
00233 void donothing();
00234 };
00235
00241 class DemoReceiver
00242 {
00243 private:
00249 ActiveDemoReceiver object;
00250 public:
00261 DemoReceiver(string name="DemoReceiver"): object(this, name) {};
00262 public:
00263 void printNumber(int nr)
00264 {
00265 object.queue_printNumber(nr);
00266 };
00267 };
00268
00269
00270
00271
00272
00273 inline void ActiveDemoSender::queue_startSending(DemoReceiver* recv, int nr)
00274 {
00275 push(new ActiveDemoSender_msg_startSending(recv, nr));
00276 };
00277 inline void ActiveDemoReceiver::queue_printNumber(int nr)
00278 {
00279 push(new ActiveDemoReceiver_msg_printNumber(nr));
00280 };
00281 #endif // __DEMO1_H