Scopira 20080306

context.h

00001 
00002 /*
00003  *  Copyright (c) 2005-2007    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED_SCOPIRA_AGENT_CONTEXT_H__
00015 #define __INCLUDED_SCOPIRA_AGENT_CONTEXT_H__
00016 
00017 #include <typeinfo>
00018 
00019 #include <scopira/tool/export.h>
00020 #include <scopira/tool/uuid.h>
00021 #include <scopira/tool/binflow.h>
00022 #include <scopira/agent/task.h>
00023 #include <scopira/agent/agent.h>
00024 
00025 namespace scopira
00026 {
00027   namespace agent
00028   {
00029     class service_broadcast;
00030 
00031     class send_msg;
00032     class recv_msg;
00033 
00034     class task_context;  // rename to task_context
00035 
00044     SCOPIRA_EXPORT void run_task(scopira::agent::task_context &ctx, scopira::agent::agent_task_i &t);
00045   }
00046 }
00047 
00058 class scopira::agent::service_broadcast
00059 {
00060   public:
00068     service_broadcast(const scopira::tool::uuid &serviceid) : m_serviceud(serviceid) { }
00069 
00071     const scopira::tool::uuid & get_serviceid(void) const { return m_serviceud; }
00072 
00073   private:
00074     scopira::tool::uuid m_serviceud;
00075 };
00076 
00082 class scopira::agent::send_msg : public scopira::tool::bin64oflow
00083 {
00084   public:
00094     SCOPIRA_EXPORT send_msg(scopira::agent::task_context &ctx, scopira::tool::uuid dest);
00104     SCOPIRA_EXPORT send_msg(scopira::agent::task_context &ctx, int dest);
00105 
00114     SCOPIRA_EXPORT send_msg(scopira::agent::task_context &ctx, const service_broadcast &targets);
00115 
00117     SCOPIRA_EXPORT virtual ~send_msg();
00118 
00119   private:
00120     // in the future, change the implementation to use direct-to-wire
00121     scopira::tool::uuid dm_src, dm_dest;
00122     scopira::tool::count_ptr<scopira::tool::bufferflow> dm_buf;
00123 
00124     bool dm_service_bcast;
00125 };
00126 
00132 class scopira::agent::recv_msg : public scopira::tool::bin64iflow
00133 {
00134   public:
00145     SCOPIRA_EXPORT recv_msg(scopira::agent::task_context &ctx, scopira::tool::uuid src);
00154     SCOPIRA_EXPORT recv_msg(scopira::agent::task_context &ctx, int src);
00163     SCOPIRA_EXPORT recv_msg(scopira::agent::task_context &ctx, const uuid_query &Q);
00164 
00166     SCOPIRA_EXPORT virtual ~recv_msg();
00167 
00173     scopira::tool::uuid get_source(void) const { return dm_lastsrc; }
00174 
00175   private:
00176     scopira::tool::uuid dm_lastsrc, dm_dest;
00177     scopira::tool::count_ptr<scopira::tool::bufferiflow> dm_buf;
00178     scopira::tool::count_ptr<scopira::tool::bufferflow> dm_realbuf;
00179 };
00180 
00186 class scopira::agent::task_context
00187 {
00188   public:
00194     SCOPIRA_EXPORT task_context(void);
00207     SCOPIRA_EXPORT task_context(taskmsg_reactor_i *reac);
00208     // internal ctor for scopira use, DO NOT USE
00209     SCOPIRA_EXPORT task_context(int myindex, const scopira::basekit::narray<scopira::tool::uuid> &peers);
00211     SCOPIRA_EXPORT ~task_context();
00212 
00218     bool failed(void) const { return agent_i::instance()->failed(); }
00219 
00233     int find_services(scopira::tool::uuid &serviceid, scopira::basekit::narray<scopira::tool::uuid> &out);
00234 
00241     int universe_size(void)
00242       { return agent_i::instance()->universe_size(); }
00243 
00249     scopira::tool::uuid get_agent_id(void) { return agent_i::instance()->get_agent_id(); }
00250 
00269     template <class M>
00270       scopira::tool::uuid launch_task(scopira::tool::uuid where = scopira::tool::uuid())
00271         { return agent_i::instance()->launch_task(typeid(M), where); }
00273     SCOPIRA_EXPORT scopira::tool::uuid launch_task(const std::string &classname, scopira::tool::uuid where = scopira::tool::uuid());
00274     
00276     //scopira::tool::uuid launch_task(const std::type_info &t)
00277       //{ return agent_i::instance()->launch_task(t); }
00278 
00292     template <class M>
00293       scopira::tool::uuid launch_group(int numps)
00294         { return agent_i::instance()->launch_group(numps, typeid(M)); }
00296     SCOPIRA_EXPORT scopira::tool::uuid launch_group(int numps, const std::string &classname);
00297 
00299     //scopira::tool::uuid launch_group(int numps, const std::type_info &t)
00300       //{ return agent_i::instance()->launch_group(numps, t); }
00301 
00316     template <class M>
00317       void launch_slaves(int numps)    // doesnt block
00318         { launch_slaves_impl(numps, typeid(M)); }
00319     SCOPIRA_EXPORT void launch_slaves(int numps, const std::string &classname);
00320 
00329     void kill_task(scopira::tool::uuid ps)
00330       { agent_i::instance()->kill_task(ps); }
00331 
00341     bool wait_task(scopira::tool::uuid ps, int msec = 0)
00342       { return agent_i::instance()->wait_task(ps, msec); }
00343 
00349     bool is_alive_task(scopira::tool::uuid ps)
00350       { return agent_i::instance()->is_alive_task(ps); }
00351 
00363     bool is_killed_task(void)
00364       { return agent_i::instance()->is_killed_task(dm_peers[dm_myindex]); }
00365 
00375     SCOPIRA_EXPORT bool wait_msg(scopira::tool::uuid src, int timeout = 0);
00386     SCOPIRA_EXPORT bool wait_msg(int src = -1, int timeout = 0);
00396     SCOPIRA_EXPORT bool wait_msg(const scopira::agent::uuid_query &Q, int timeout = 0);
00397 
00405     SCOPIRA_EXPORT bool has_msg(scopira::tool::uuid src);
00413     SCOPIRA_EXPORT bool has_msg(int src = -1);
00420     SCOPIRA_EXPORT bool has_msg(const scopira::agent::uuid_query &Q);
00421 
00429     scopira::tool::uuid get_source(void) const { return dm_lastsrc; }
00430 
00436     int get_index(void) const { assert(dm_myindex>=0); return dm_myindex; }
00443     SCOPIRA_EXPORT int get_index(scopira::tool::uuid id) const;
00448     int get_group_size(void) const { return static_cast<int>(dm_peers.size()); }
00453     scopira::tool::uuid get_id(void) const { return dm_peers[dm_myindex]; }
00459     scopira::tool::uuid get_id(int idx) const { return dm_peers[idx]; }
00465     const scopira::basekit::narray<scopira::tool::uuid> & get_group(void) const { return dm_peers; }
00466 
00472     SCOPIRA_EXPORT void barrier_group(void);
00473 
00479     SCOPIRA_EXPORT void wait_group(void);
00480 
00481   private:
00482     SCOPIRA_EXPORT void launch_slaves_impl(int numtotalps, const std::type_info &nfo);
00483 
00484   private:
00487     scopira::tool::count_ptr<agent_i> dm_server_link;
00488 
00489     // for wait_msg*
00490     scopira::tool::uuid dm_lastsrc;
00491 
00492     // group stuff... [0] is the master... the group size is always atleast one
00493     // [dm_myindex] is me
00494     scopira::basekit::narray<scopira::tool::uuid> dm_peers;
00495     int dm_myindex;
00496 
00497     // dont even try it (disabled)
00498     task_context(const task_context &) { }
00499 
00500   friend class scopira::agent::send_msg;
00501   friend class scopira::agent::recv_msg;
00502 };
00503 
00504 #endif
00505