00001
00002
00003
00004
00005 #define bool int
00006
00007 class Message {
00008 public:
00009 Message();
00010 ~Message();
00011
00012 void AppendMessage( Message* msg );
00013 void DeleteMessages();
00014
00015 protected:
00016 Message* next;
00017 };
00018
00019 class TrainMessage : public Message {
00020 public:
00021 TrainMessage();
00022 ~TrainMessage();
00023
00024 protected:
00025 TrainInfo* new_train_info;
00026 int request;
00027 int nbRequests;
00028 };
00029
00030 class TrainInfo : public Info {
00031 public:
00032 TrainInfo();
00033 TrainInfo( int ID, char* name, ... );
00034
00035 TrainInfo( TrainInfo& info );
00036 ~TrainInfo();
00037
00038 protected:
00039 int ID;
00040 char* name;
00041 char* color;
00042
00043
00044 double speed;
00045 bool direction;
00046 int prev_track;
00047 int current_track;
00048 int next_track;
00049
00050 int serial_id;
00051 int serial_speed;
00052 bool serial_done;
00053
00054 Position* origin;
00055 Position* destination;
00056
00057 bool running;
00058
00059 TrainInfo* prev;
00060 TrainInfo* next;
00061
00062 TrainInfo* prevState;
00063 TrainInfo* nextState;
00064
00065 TrainGUI* traingui;
00066
00067 Timer* timer;
00068
00069
00070 long action;
00071
00072 double last_update;
00073 double normal_update;
00074
00075 }
00076
00077 class Train : public Thread {
00078 public:
00079 Train();
00080 Train( int ID );
00081 ~Train();
00082 void run();
00083
00084 protected:
00085 (void*)functions[26]( TrainInfo* info );
00086
00087 void initfunctions();
00088
00089 int train_ID;
00090 TrainInfo* current_train_info;
00091 TrainInfo* buffer_;
00092 };
00093
00094 Train::Train() {
00095 train_ID = -1;
00096 initfunctions();
00097 }
00098
00099 void Train::initfunctions() {
00100
00101
00102 *functions[0] = Train::nop( ... );
00103 *functions[1] = Train::change_speed( ... );
00104 *functions[2] = Train::change_direction( ... );
00105 }
00106
00107 void Train::run()
00108 {
00109 pid_t pid;
00110 int task = 0;
00111
00112 while(1)
00113 {
00114 const char* response;
00115
00116 if((pid = Receive(0, buffer_, sizeof(buffer_))) < 0)
00117 {
00118
00119 printf("ERROR--code %d\n", errno);
00120 return;
00121 } else {
00122 task = buffer_->action;
00123 if ( task >= 0 && task < 26 ) {
00124 functions[task]( buffer_ );
00125 } else {
00126 printf("Illegal task message request: %d", task );
00127 }
00128 }
00129
00130 printf("Got from %d: %s\n", pid, buffer_);
00131
00132 Reply(pid, response, strlen(response)+1);
00133 }
00134
00135 Reply(pid, ok_response, sizeof(ok_response));
00136 }
00137
00138