Scopira  20080306
context.h
1 
2 /*
3  * Copyright (c) 2005-2007 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_AGENT_CONTEXT_H__
15 #define __INCLUDED_SCOPIRA_AGENT_CONTEXT_H__
16 
17 #include <typeinfo>
18 
19 #include <scopira/tool/export.h>
20 #include <scopira/tool/uuid.h>
21 #include <scopira/tool/binflow.h>
22 #include <scopira/agent/task.h>
23 #include <scopira/agent/agent.h>
24 
25 namespace scopira
26 {
27  namespace agent
28  {
29  class service_broadcast;
30 
31  class send_msg;
32  class recv_msg;
33 
34  class task_context; // rename to task_context
35 
45  }
46 }
47 
59 {
60  public:
68  service_broadcast(const scopira::tool::uuid &serviceid) : m_serviceud(serviceid) { }
69 
71  const scopira::tool::uuid & get_serviceid(void) const { return m_serviceud; }
72 
73  private:
74  scopira::tool::uuid m_serviceud;
75 };
76 
83 {
84  public:
104  SCOPIRA_EXPORT send_msg(scopira::agent::task_context &ctx, int dest);
105 
114  SCOPIRA_EXPORT send_msg(scopira::agent::task_context &ctx, const service_broadcast &targets);
115 
117  SCOPIRA_EXPORT virtual ~send_msg();
118 
119  private:
120  // in the future, change the implementation to use direct-to-wire
121  scopira::tool::uuid dm_src, dm_dest;
123 
124  bool dm_service_bcast;
125 };
126 
133 {
134  public:
154  SCOPIRA_EXPORT recv_msg(scopira::agent::task_context &ctx, int src);
163  SCOPIRA_EXPORT recv_msg(scopira::agent::task_context &ctx, const uuid_query &Q);
164 
166  SCOPIRA_EXPORT virtual ~recv_msg();
167 
173  scopira::tool::uuid get_source(void) const { return dm_lastsrc; }
174 
175  private:
176  scopira::tool::uuid dm_lastsrc, dm_dest;
179 };
180 
187 {
188  public:
194  SCOPIRA_EXPORT task_context(void);
207  SCOPIRA_EXPORT task_context(taskmsg_reactor_i *reac);
208  // internal ctor for scopira use, DO NOT USE
209  SCOPIRA_EXPORT task_context(int myindex, const scopira::basekit::narray<scopira::tool::uuid> &peers);
211  SCOPIRA_EXPORT ~task_context();
212 
218  bool failed(void) const { return agent_i::instance()->failed(); }
219 
233  int find_services(scopira::tool::uuid &serviceid, scopira::basekit::narray<scopira::tool::uuid> &out);
234 
241  int universe_size(void)
242  { return agent_i::instance()->universe_size(); }
243 
249  scopira::tool::uuid get_agent_id(void) { return agent_i::instance()->get_agent_id(); }
250 
269  template <class M>
271  { return agent_i::instance()->launch_task(typeid(M), where); }
273  SCOPIRA_EXPORT scopira::tool::uuid launch_task(const std::string &classname, scopira::tool::uuid where = scopira::tool::uuid());
274 
276  //scopira::tool::uuid launch_task(const std::type_info &t)
277  //{ return agent_i::instance()->launch_task(t); }
278 
292  template <class M>
294  { return agent_i::instance()->launch_group(numps, typeid(M)); }
296  SCOPIRA_EXPORT scopira::tool::uuid launch_group(int numps, const std::string &classname);
297 
299  //scopira::tool::uuid launch_group(int numps, const std::type_info &t)
300  //{ return agent_i::instance()->launch_group(numps, t); }
301 
316  template <class M>
317  void launch_slaves(int numps) // doesnt block
318  { launch_slaves_impl(numps, typeid(M)); }
319  SCOPIRA_EXPORT void launch_slaves(int numps, const std::string &classname);
320 
330  { agent_i::instance()->kill_task(ps); }
331 
341  bool wait_task(scopira::tool::uuid ps, int msec = 0)
342  { return agent_i::instance()->wait_task(ps, msec); }
343 
350  { return agent_i::instance()->is_alive_task(ps); }
351 
363  bool is_killed_task(void)
364  { return agent_i::instance()->is_killed_task(dm_peers[dm_myindex]); }
365 
375  SCOPIRA_EXPORT bool wait_msg(scopira::tool::uuid src, int timeout = 0);
386  SCOPIRA_EXPORT bool wait_msg(int src = -1, int timeout = 0);
396  SCOPIRA_EXPORT bool wait_msg(const scopira::agent::uuid_query &Q, int timeout = 0);
397 
405  SCOPIRA_EXPORT bool has_msg(scopira::tool::uuid src);
413  SCOPIRA_EXPORT bool has_msg(int src = -1);
420  SCOPIRA_EXPORT bool has_msg(const scopira::agent::uuid_query &Q);
421 
429  scopira::tool::uuid get_source(void) const { return dm_lastsrc; }
430 
436  int get_index(void) const { assert(dm_myindex>=0); return dm_myindex; }
443  SCOPIRA_EXPORT int get_index(scopira::tool::uuid id) const;
448  int get_group_size(void) const { return static_cast<int>(dm_peers.size()); }
453  scopira::tool::uuid get_id(void) const { return dm_peers[dm_myindex]; }
459  scopira::tool::uuid get_id(int idx) const { return dm_peers[idx]; }
465  const scopira::basekit::narray<scopira::tool::uuid> & get_group(void) const { return dm_peers; }
466 
472  SCOPIRA_EXPORT void barrier_group(void);
473 
479  SCOPIRA_EXPORT void wait_group(void);
480 
481  private:
482  SCOPIRA_EXPORT void launch_slaves_impl(int numtotalps, const std::type_info &nfo);
483 
484  private:
487  scopira::tool::count_ptr<agent_i> dm_server_link;
488 
489  // for wait_msg*
490  scopira::tool::uuid dm_lastsrc;
491 
492  // group stuff... [0] is the master... the group size is always atleast one
493  // [dm_myindex] is me
495  int dm_myindex;
496 
497  // dont even try it (disabled)
498  task_context(const task_context &) { }
499 
500  friend class scopira::agent::send_msg;
501  friend class scopira::agent::recv_msg;
502 };
503 
504 #endif
505 
Definition: binflow.h:154
const scopira::tool::uuid & get_serviceid(void) const
returns the service id
Definition: context.h:71
Definition: binflow.h:131
scopira::tool::uuid launch_task(scopira::tool::uuid where=scopira::tool::uuid())
Definition: context.h:270
Definition: archiveflow.h:20
scopira::tool::uuid get_id(void) const
Definition: context.h:453
Definition: context.h:186
Definition: task.h:45
static agent_i * instance(void)
Definition: agent.h:184
Definition: uuid.h:183
Definition: context.h:82
int get_index(void) const
Definition: context.h:436
Definition: agent.h:160
int universe_size(void)
Definition: context.h:241
virtual bool failed(void) const =0
void launch_slaves(int numps)
the core variant
Definition: context.h:317
scopira::tool::uuid get_agent_id(void)
Definition: context.h:249
int get_group_size(void) const
Definition: context.h:448
void run_task(scopira::agent::task_context &ctx, scopira::agent::agent_task_i &t)
service_broadcast(const scopira::tool::uuid &serviceid)
Definition: context.h:68
void kill_task(scopira::tool::uuid ps)
Definition: context.h:329
Definition: agent.h:49
bool is_killed_task(void)
Definition: context.h:363
scopira::tool::uuid get_id(int idx) const
Definition: context.h:459
bool is_alive_task(scopira::tool::uuid ps)
Definition: context.h:349
scopira::tool::uuid get_source(void) const
Definition: context.h:429
bool failed(void) const
Definition: context.h:218
Definition: context.h:58
const scopira::basekit::narray< scopira::tool::uuid > & get_group(void) const
Definition: context.h:465
Definition: context.h:132
scopira::tool::uuid get_source(void) const
Definition: context.h:173
bool wait_task(scopira::tool::uuid ps, int msec=0)
Definition: context.h:341
scopira::tool::uuid launch_group(int numps)
the core variant
Definition: context.h:293