00001
00002
00003 #ifndef _BCP_MESSAGE_H
00004 #define _BCP_MESSAGE_H
00005
00006
00007
00008 #include <utility>
00009
00010 #include "BCP_string.hpp"
00011 #include "BCP_message_tag.hpp"
00012 #include "BCP_vector.hpp"
00013
00014
00015
00016
00017 class BCP_buffer;
00018 class USER_initialize;
00019
00020
00021
00029 class BCP_proc_id {
00030 public:
00035 virtual ~BCP_proc_id() {}
00043 virtual bool is_same_process(const BCP_proc_id* other_process) const = 0;
00050 virtual BCP_proc_id* clone() const = 0;
00052 };
00053
00054
00055 const BCP_proc_id* const BCP_AnyProcess = 0;
00056
00057
00058
00061 class BCP_proc_array {
00062 private:
00066 BCP_proc_array(const BCP_proc_array&);
00068 BCP_proc_array& operator=(const BCP_proc_array&);
00070 private:
00074 BCP_vec<BCP_proc_id*> _procs;
00076 BCP_vec<BCP_proc_id*> _free_procs;
00078 public:
00082 BCP_proc_array() : _procs(), _free_procs() {}
00085 ~BCP_proc_array() {}
00091 inline bool is_free_proc(const int index) {
00092 const BCP_proc_id* proc = _procs[index];
00093 int i;
00094 for (i = _free_procs.size() - 1; i >= 0; --i)
00095 if (_free_procs[i]->is_same_process(proc))
00096 break;
00097 return (i >= 0);
00098 }
00101 inline int index_of_proc(const BCP_proc_id* proc) {
00102 int i;
00103 for (i = _procs.size() - 1; i >= 0; --i)
00104 if (_procs[i]->is_same_process(proc))
00105 break;
00106 return i;
00107 }
00109 inline BCP_vec<BCP_proc_id*>& procs() { return _procs; }
00111 inline const BCP_vec<BCP_proc_id*>& procs() const { return _procs; }
00113 inline const BCP_proc_id* process(const int i) { return _procs[i]; }
00115 inline int size() const { return _procs.size(); }
00117 inline int free_num() const { return _free_procs.size(); }
00119 inline int busy_num() const { return _procs.size() - _free_procs.size(); }
00121 BCP_proc_id* get_free_proc() {
00122 if (_free_procs.size() > 0) {
00123 BCP_proc_id* proc = _free_procs.back();
00124 _free_procs.pop_back();
00125 return proc;
00126 }
00127 return 0;
00128 }
00134 inline void clear() {
00135 purge_ptr_vector(_procs);
00136 _free_procs.clear();
00137 }
00140 inline void add_procs(BCP_vec<BCP_proc_id*>::const_iterator first,
00141 BCP_vec<BCP_proc_id*>::const_iterator last) {
00142 _procs.insert(_procs.end(), first, last);
00143
00144 _free_procs.insert(_free_procs.end(), first, last);
00145 }
00149 inline void delete_proc(const int index) {
00150 const BCP_proc_id* proc = _procs[index];
00151 int i;
00152 for (i = _free_procs.size() - 1; i >= 0; --i)
00153 if (_free_procs[i]->is_same_process(proc))
00154 break;
00155 if (i >= 0)
00156 _free_procs.erase(_free_procs.entry(i));
00157 _procs.erase(_procs.entry(index));
00158 }
00159
00160
00164 void set_proc_free(BCP_proc_id* proc) {
00165 _free_procs.push_back(proc);
00166 }
00168 };
00169
00170
00171
00179 class BCP_message_environment{
00180 public:
00185 virtual ~BCP_message_environment() {}
00192 virtual BCP_proc_id* register_process(USER_initialize* user_init) = 0;
00199 virtual BCP_proc_id* parent_process() = 0;
00206 virtual bool alive(const BCP_proc_id* pid) = 0;
00210 virtual BCP_vec<BCP_proc_id*>::iterator
00211 alive(const BCP_proc_array& parray) = 0;
00218 virtual void send(const BCP_proc_id* const target,
00219 const BCP_message_tag tag) = 0;
00222 virtual void send(const BCP_proc_id* const target,
00223 const BCP_message_tag tag,
00224 const BCP_buffer& buf) = 0;
00231 virtual void multicast(const BCP_proc_array* const target,
00232 const BCP_message_tag tag) = 0;
00235 virtual void multicast(const BCP_proc_array* const target,
00236 const BCP_message_tag tag,
00237 const BCP_buffer& buf) = 0;
00244 virtual void multicast(BCP_vec<BCP_proc_id*>::const_iterator beg,
00245 BCP_vec<BCP_proc_id*>::const_iterator end,
00246 const BCP_message_tag tag) = 0;
00249 virtual void multicast(BCP_vec<BCP_proc_id*>::const_iterator beg,
00250 BCP_vec<BCP_proc_id*>::const_iterator end,
00251 const BCP_message_tag tag,
00252 const BCP_buffer& buf) = 0;
00255
00268 virtual void receive(const BCP_proc_id* const source,
00269 const BCP_message_tag tag, BCP_buffer& buf,
00270 const double timeout) = 0;
00277 virtual bool probe(const BCP_proc_id* const source,
00278 const BCP_message_tag tag) = 0;
00286 virtual BCP_proc_id* start_process(const BCP_string& exe,
00287 const bool debug) = 0;
00289 virtual BCP_proc_id* start_process(const BCP_string& exe,
00290 const BCP_string& machine,
00291 const bool debug) = 0;
00293 virtual BCP_proc_array* start_processes(const BCP_string& exe,
00294 const int proc_num,
00295 const bool debug) = 0;
00298 virtual BCP_proc_array* start_processes(const BCP_string& exe,
00299 const int proc_num,
00300 const BCP_vec<BCP_string>& machines,
00301 const bool debug) = 0;
00304
00305
00306
00310 virtual BCP_proc_id* unpack_proc_id(BCP_buffer& buf) = 0;
00312 virtual void pack_proc_id(BCP_buffer& buf, const BCP_proc_id* pid) = 0;
00318 virtual int num_procs() { return 0; }
00323 };
00324
00325 #endif