CPN
Computational Process Networks
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
CPN::LocalContext Class Reference

A local implementation of the Context interface. More...

#include <LocalContext.h>

+ Inheritance diagram for CPN::LocalContext:
+ Collaboration diagram for CPN::LocalContext:

Classes

struct  KernelInfo
 
struct  NodeInfo
 
struct  PortInfo
 

Public Types

typedef std::map< std::string,
Key_t
NameMap
 
typedef std::map< Key_t,
shared_ptr< KernelInfo > > 
KernelMap
 
typedef std::map< Key_t,
shared_ptr< NodeInfo > > 
NodeMap
 
typedef std::map< Key_t,
shared_ptr< PortInfo > > 
PortMap
 

Public Member Functions

 LocalContext ()
 
virtual ~LocalContext ()
 
virtual void Log (int level, const std::string &msg)
 Log a message to this outputer. More...
 
virtual int LogLevel () const
 
virtual int LogLevel (int level)
 
virtual Key_t SetupKernel (const std::string &name, const std::string &hostname, const std::string &servname, KernelBase *kernel)
 Called by the Kernel when it has successfully set it self up. This gives the Context a way to notify the Kernel of events and lets other Kernels look up the connection information for this Kernel. More...
 
virtual Key_t SetupKernel (const std::string &name, KernelBase *kernel)
 Called by the kernel when it is not in remote mode. More...
 
virtual Key_t GetKernelKey (const std::string &kernel)
 
virtual std::string GetKernelName (Key_t kernelkey)
 
virtual void GetKernelConnectionInfo (Key_t kernelkey, std::string &hostname, std::string &servname)
 obtain the connection information for the given kernel More...
 
virtual void SignalKernelEnd (Key_t kernelkey)
 Signal to the Context that the given kernel is dead. More...
 
virtual Key_t WaitForKernelStart (const std::string &kernel)
 Does not return until the given kernel has started. More...
 
virtual void SignalKernelStart (Key_t kernelkey)
 Signal to the context that the given kernel has started. More...
 
virtual void SendCreateWriter (Key_t kernelkey, const SimpleQueueAttr &attr)
 Tell a given kernel that it needs to create a queue write end. More...
 
virtual void SendCreateReader (Key_t kernelkey, const SimpleQueueAttr &attr)
 Tell a given kernel that it needs to create a queue read end. More...
 
virtual void SendCreateQueue (Key_t kernelkey, const SimpleQueueAttr &attr)
 Tell a given kernel that it needs to create a queue. More...
 
virtual void SendCreateNode (Key_t kernelkey, const NodeAttr &attr)
 Tell a given kernel that it needs to create a node. More...
 
virtual Key_t CreateNodeKey (Key_t kernelkey, const std::string &nodename)
 Tell the context to allocate a new node key and data structure for a node with nodename which is on kernelkey. More...
 
virtual Key_t GetNodeKey (const std::string &nodename)
 
virtual std::string GetNodeName (Key_t nodekey)
 
virtual Key_t GetNodeKernel (Key_t nodekey)
 
virtual void SignalNodeStart (Key_t nodekey)
 Called by the node startup routine to indicate that the node has started. More...
 
virtual void SignalNodeEnd (Key_t nodekey)
 Called by the node cleanup routine to indicate that the node has ended. More...
 
virtual Key_t WaitForNodeStart (const std::string &nodename)
 Waits until the node starts and returns the key, if the node is already started returns the key. More...
 
virtual void WaitForNodeEnd (const std::string &nodename)
 Waits for the given node to signal end. More...
 
virtual void WaitForAllNodeEnd ()
 Convenience method which waits until there are no nodes running. If no node have started then this will return immediately. More...
 
virtual Key_t GetCreateReaderKey (Key_t nodekey, const std::string &portname)
 Get the key associated with the given endpoint for the given node. Creates the information if it does not exist. More...
 
virtual Key_t GetReaderNode (Key_t portkey)
 
virtual Key_t GetReaderKernel (Key_t portkey)
 
virtual std::string GetReaderName (Key_t portkey)
 
virtual Key_t GetCreateWriterKey (Key_t nodekey, const std::string &portname)
 
virtual Key_t GetWriterNode (Key_t portkey)
 
virtual Key_t GetWriterKernel (Key_t portkey)
 
virtual std::string GetWriterName (Key_t portkey)
 
virtual void ConnectEndpoints (Key_t writerkey, Key_t readerkey, const std::string &qname)
 Called by the kernel when a queue is created. Note that the endpoints may have been created when the node requests them but the queue may be created long after that. More...
 
virtual Key_t GetReadersWriter (Key_t readerkey)
 
virtual Key_t GetWritersReader (Key_t writerkey)
 
virtual void Terminate ()
 Signal to the Context that the network is terminating. After this call most methods will throw a ShutdownException. More...
 
virtual bool IsTerminated ()
 
void CheckTerminated ()
 Convenience method that checks IsTerminated and if so throws a ShutdownException. More...
 
virtual bool RequireRemote ()
 Lets the kernel know that this context type requires remote activity. This overrides the kernel option for remote activity. Default value is false. More...
 
virtual unsigned CalculateGrowSize (unsigned currentsize, unsigned request)
 Calculate the new queue size when a queue needs to grow. More...
 

Static Public Member Functions

static shared_ptr< ContextLocal ()
 Create a local context. More...
 

Private Member Functions

void InternalCheckTerminated ()
 
Key_t NewKey ()
 

Private Attributes

int loglevel
 
PthreadMutex lock
 
PthreadCondition nodelivedead
 
PthreadCondition kernellivedead
 
NameMap kernelnames
 
NameMap nodenames
 
KernelMap kernelmap
 
NodeMap nodemap
 
PortMap readports
 
PortMap writeports
 
unsigned numlivenodes
 
bool shutdown
 
Key_t counter
 

Detailed Description

A local implementation of the Context interface.

Definition at line 40 of file LocalContext.h.

Member Typedef Documentation

typedef std::map< Key_t, shared_ptr<KernelInfo> > CPN::LocalContext::KernelMap

Definition at line 74 of file LocalContext.h.

typedef std::map< std::string, Key_t > CPN::LocalContext::NameMap

Definition at line 42 of file LocalContext.h.

typedef std::map< Key_t, shared_ptr<NodeInfo> > CPN::LocalContext::NodeMap

Definition at line 75 of file LocalContext.h.

typedef std::map< Key_t, shared_ptr<PortInfo> > CPN::LocalContext::PortMap

Definition at line 76 of file LocalContext.h.

Constructor & Destructor Documentation

CPN::LocalContext::LocalContext ( )

Definition at line 34 of file LocalContext.cc.

CPN::LocalContext::~LocalContext ( )
virtual

Definition at line 38 of file LocalContext.cc.

38  {
39  }

Member Function Documentation

virtual unsigned CPN::Context::CalculateGrowSize ( unsigned  currentsize,
unsigned  request 
)
inlinevirtualinherited

Calculate the new queue size when a queue needs to grow.

Returns
the new queue size

Definition at line 275 of file Context.h.

275 { return currentsize + request; }
void CPN::Context::CheckTerminated ( )
inherited

Convenience method that checks IsTerminated and if so throws a ShutdownException.

Exceptions
ShutdownException

Definition at line 44 of file Context.cc.

References CPN::Context::IsTerminated().

44  {
45  if (IsTerminated()) {
46  throw ShutdownException();
47  }
48  }
virtual bool IsTerminated()=0

+ Here is the call graph for this function:

void CPN::LocalContext::ConnectEndpoints ( Key_t  writerkey,
Key_t  readerkey,
const std::string &  qname 
)
virtual

Called by the kernel when a queue is created. Note that the endpoints may have been created when the node requests them but the queue may be created long after that.

Parameters
writerkeythe unique key for the writer endpoint
readerkeythe unique key for the reader endpoint
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 428 of file LocalContext.cc.

References InternalCheckTerminated(), lock, readports, and writeports.

428  {
431  PortMap::iterator writeentry = writeports.find(writerkey);
432  if (writeentry == writeports.end()) {
433  throw std::invalid_argument("Write port does not exist.");
434  }
435  PortMap::iterator readentry = readports.find(readerkey);
436  if (readentry == readports.end()) {
437  throw std::invalid_argument("Read port does not exist.");
438  }
439 
440  writeentry->second->opposingport = readerkey;
441  readentry->second->opposingport = writerkey;
442 
443  readentry->second->qname = qname;
444  writeentry->second->qname = qname;
445  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::CreateNodeKey ( Key_t  kernelkey,
const std::string &  nodename 
)
virtual

Tell the context to allocate a new node key and data structure for a node with nodename which is on kernelkey.

Parameters
kernelkeythe id of the kernel that the node will run on
nodenamethe name of the node
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 217 of file LocalContext.cc.

References InternalCheckTerminated(), lock, NewKey(), nodemap, and nodenames.

217  {
220  NameMap::iterator nameentry = nodenames.find(nodename);
221  Key_t key;
222  if (nameentry == nodenames.end()) {
223  shared_ptr<NodeInfo> ninfo = shared_ptr<NodeInfo>(new NodeInfo);
224  ninfo->name = nodename;
225  ninfo->started = false;
226  ninfo->dead = false;
227  ninfo->kernelkey = kernelkey;
228  key = NewKey();
229  nodenames.insert(std::make_pair(nodename, key));
230  nodemap.insert(std::make_pair(key, ninfo));
231  } else {
232  throw std::invalid_argument("Node " + nodename + " already exists.");
233  }
234  return key;
235  }
void InternalCheckTerminated()
uint64_t Key_t
Definition: common.h:79
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetCreateReaderKey ( Key_t  nodekey,
const std::string &  portname 
)
virtual

Get the key associated with the given endpoint for the given node. Creates the information if it does not exist.

Parameters
nodekeythe unique id for the node
portnamethe name of the endpoint.
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 334 of file LocalContext.cc.

References InternalCheckTerminated(), lock, NewKey(), nodemap, and readports.

334  {
337  NodeMap::iterator entry = nodemap.find(nodekey);
338  if (entry == nodemap.end()) {
339  throw std::invalid_argument("No such node");
340  }
341  NameMap::iterator portentry = entry->second->readers.find(portname);
342  if (portentry == entry->second->readers.end()) {
343  Key_t key = NewKey();
344  entry->second->readers.insert(std::make_pair(portname, key));
345  shared_ptr<PortInfo> pinfo = shared_ptr<PortInfo>(new PortInfo);
346  pinfo->name = portname;
347  pinfo->nodekey = nodekey;
348  pinfo->opposingport = 0;
349  pinfo->dead = false;
350  readports.insert(std::make_pair(key, pinfo));
351  return key;
352  } else {
353  return portentry->second;
354  }
355  }
void InternalCheckTerminated()
uint64_t Key_t
Definition: common.h:79
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetCreateWriterKey ( Key_t  nodekey,
const std::string &  portname 
)
virtual
See Also
GetCreateReaderKey

Implements CPN::Context.

Definition at line 381 of file LocalContext.cc.

References InternalCheckTerminated(), lock, NewKey(), nodemap, and writeports.

381  {
384  NodeMap::iterator entry = nodemap.find(nodekey);
385  if (entry == nodemap.end()) {
386  throw std::invalid_argument("No such node");
387  }
388  NameMap::iterator portentry = entry->second->writers.find(portname);
389  if (portentry == entry->second->writers.end()) {
390  Key_t key = NewKey();
391  entry->second->writers.insert(std::make_pair(portname, key));
392  shared_ptr<PortInfo> pinfo = shared_ptr<PortInfo>(new PortInfo);
393  pinfo->name = portname;
394  pinfo->nodekey = nodekey;
395  pinfo->opposingport = 0;
396  pinfo->dead = false;
397  writeports.insert(std::make_pair(key, pinfo));
398  return key;
399  } else {
400  return portentry->second;
401  }
402  }
void InternalCheckTerminated()
uint64_t Key_t
Definition: common.h:79
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::GetKernelConnectionInfo ( Key_t  kernelkey,
std::string &  hostname,
std::string &  servname 
)
virtual

obtain the connection information for the given kernel

Parameters
kernelkeythe unique id for the kernel
hostname(output) string to be filled with the hostname
servname(output) string to be filled with the service name
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 120 of file LocalContext.cc.

References ASSERT, InternalCheckTerminated(), kernelmap, and lock.

120  {
123  KernelMap::iterator entry = kernelmap.find(kernelkey);
124  if (entry == kernelmap.end()) {
125  throw std::invalid_argument("No such kernel");
126  }
127  ASSERT(entry->second->allowremote, "Kernel does not have remote configured.");
128  hostname = entry->second->hostname;
129  servname = entry->second->servname;
130  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131
#define ASSERT(exp,...)

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetKernelKey ( const std::string &  kernel)
virtual
Parameters
kernelthe name of the kernel
Returns
the key for the given kernel.
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 100 of file LocalContext.cc.

References InternalCheckTerminated(), kernelnames, and lock.

100  {
103  NameMap::iterator entry = kernelnames.find(kernel);
104  if (entry == kernelnames.end()) {
105  throw std::invalid_argument("No such kernel");
106  }
107  return entry->second;
108  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

std::string CPN::LocalContext::GetKernelName ( Key_t  kernelkey)
virtual
Parameters
kernelkeythe key to the kernel
Returns
the name for the kernel
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 110 of file LocalContext.cc.

References InternalCheckTerminated(), kernelmap, and lock.

110  {
113  KernelMap::iterator entry = kernelmap.find(kernelkey);
114  if (entry == kernelmap.end()) {
115  throw std::invalid_argument("No such kernel");
116  }
117  return entry->second->name;
118  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetNodeKernel ( Key_t  nodekey)
virtual
Parameters
nodekeythe unique key for the node
Returns
the key for the kernel the node is running on
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 324 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and nodemap.

Referenced by GetReaderKernel(), and GetWriterKernel().

324  {
327  NodeMap::iterator entry = nodemap.find(nodekey);
328  if (entry == nodemap.end()) {
329  throw std::invalid_argument("No such node");
330  }
331  return entry->second->kernelkey;
332  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Key_t CPN::LocalContext::GetNodeKey ( const std::string &  nodename)
virtual
Returns
the unique key associated with the given node name.
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 237 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and nodenames.

237  {
240  NameMap::iterator nameentry = nodenames.find(nodename);
241  if (nameentry == nodenames.end()) {
242  throw std::invalid_argument("No such node");
243  } else {
244  return nameentry->second;
245  }
246  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

std::string CPN::LocalContext::GetNodeName ( Key_t  nodekey)
virtual
Returns
the name associated with the given node key
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 248 of file LocalContext.cc.

References lock, and nodemap.

248  {
250  NodeMap::iterator entry = nodemap.find(nodekey);
251  if (entry == nodemap.end()) {
252  throw std::invalid_argument("No such node");
253  }
254  return entry->second->name;
255  }
PthreadMutex lock
Definition: LocalContext.h:131
Key_t CPN::LocalContext::GetReaderKernel ( Key_t  portkey)
virtual
Parameters
portkeythe unique id for the port
Returns
the key for the kernel this port is on
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 366 of file LocalContext.cc.

References GetNodeKernel(), and GetReaderNode().

366  {
367  Key_t nodekey = GetReaderNode(portkey);
368  return GetNodeKernel(nodekey);
369  }
uint64_t Key_t
Definition: common.h:79
virtual Key_t GetReaderNode(Key_t portkey)
virtual Key_t GetNodeKernel(Key_t nodekey)

+ Here is the call graph for this function:

std::string CPN::LocalContext::GetReaderName ( Key_t  portkey)
virtual
Parameters
portkeythe unique id for the port
Returns
the name of the port
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 371 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and readports.

371  {
374  PortMap::iterator entry = readports.find(portkey);
375  if (entry == readports.end()) {
376  throw std::invalid_argument("No such port");
377  }
378  return entry->second->name;
379  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetReaderNode ( Key_t  portkey)
virtual
Parameters
portkeythe unique id for the port
Returns
the key for the node this port is on
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 356 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and readports.

Referenced by GetReaderKernel().

356  {
359  PortMap::iterator entry = readports.find(portkey);
360  if (entry == readports.end()) {
361  throw std::invalid_argument("No such port");
362  }
363  return entry->second->nodekey;
364  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Key_t CPN::LocalContext::GetReadersWriter ( Key_t  readerkey)
virtual
Parameters
readerkeya unique reader key
Returns
the writer key associated with this reader key if there is one
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 447 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and readports.

447  {
450  PortMap::iterator readentry = readports.find(readerkey);
451  if (readentry == readports.end()) {
452  throw std::invalid_argument("Read port does not exist.");
453  }
454  return readentry->second->opposingport;
455  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetWriterKernel ( Key_t  portkey)
virtual
See Also
GetReaderKernel

Implements CPN::Context.

Definition at line 413 of file LocalContext.cc.

References GetNodeKernel(), and GetWriterNode().

413  {
414  Key_t nodekey = GetWriterNode(portkey);
415  return GetNodeKernel(nodekey);
416  }
virtual Key_t GetWriterNode(Key_t portkey)
uint64_t Key_t
Definition: common.h:79
virtual Key_t GetNodeKernel(Key_t nodekey)

+ Here is the call graph for this function:

std::string CPN::LocalContext::GetWriterName ( Key_t  portkey)
virtual
See Also
GetReaderName

Implements CPN::Context.

Definition at line 418 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and writeports.

418  {
421  PortMap::iterator entry = writeports.find(portkey);
422  if (entry == writeports.end()) {
423  throw std::invalid_argument("No such port");
424  }
425  return entry->second->name;
426  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::GetWriterNode ( Key_t  portkey)
virtual
See Also
GetReaderNode

Implements CPN::Context.

Definition at line 403 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and writeports.

Referenced by GetWriterKernel().

403  {
406  PortMap::iterator entry = writeports.find(portkey);
407  if (entry == writeports.end()) {
408  throw std::invalid_argument("No such port");
409  }
410  return entry->second->nodekey;
411  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Key_t CPN::LocalContext::GetWritersReader ( Key_t  writerkey)
virtual
Parameters
writerkeya unique writer key
Returns
the reader key associated with this writer
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 457 of file LocalContext.cc.

References InternalCheckTerminated(), lock, and writeports.

457  {
460  PortMap::iterator writeentry = writeports.find(writerkey);
461  if (writeentry == writeports.end()) {
462  throw std::invalid_argument("Write port does not exist.");
463  }
464  return writeentry->second->opposingport;
465  }
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::InternalCheckTerminated ( )
private
bool CPN::LocalContext::IsTerminated ( )
virtual
Returns
true if Terminate has been called

Implements CPN::Context.

Definition at line 485 of file LocalContext.cc.

References lock, and shutdown.

485  {
487  return shutdown;
488  }
PthreadMutex lock
Definition: LocalContext.h:131
shared_ptr< Context > CPN::Context::Local ( )
staticinherited

Create a local context.

Returns
a new local context.

Definition at line 29 of file Context.cc.

Referenced by CPN::Kernel::Kernel(), and CPN::VariantCPNLoader::LoadContext().

29  {
30  return shared_ptr<Context>(new LocalContext);
31  }

+ Here is the caller graph for this function:

void CPN::LocalContext::Log ( int  level,
const std::string &  msg 
)
virtual

Log a message to this outputer.

Parameters
levelthe level of this message
msgthe message

Implements LoggerOutput.

Definition at line 41 of file LocalContext.cc.

References lock, and loglevel.

41  {
43  if (level >= loglevel) {
44  std::cerr << level << ":" << msg << std::endl;
45  }
46  }
PthreadMutex lock
Definition: LocalContext.h:131
int CPN::LocalContext::LogLevel ( ) const
virtual
Returns
the current log level

Implements LoggerOutput.

Definition at line 48 of file LocalContext.cc.

References lock, and loglevel.

48  {
50  return loglevel;
51  }
PthreadMutex lock
Definition: LocalContext.h:131
int CPN::LocalContext::LogLevel ( int  level)
virtual
Parameters
levelthe new log level
Returns
the new log level

Implements LoggerOutput.

Definition at line 53 of file LocalContext.cc.

References lock, and loglevel.

53  {
55  return loglevel = level;
56  }
PthreadMutex lock
Definition: LocalContext.h:131
Key_t CPN::LocalContext::NewKey ( )
inlineprivate

Definition at line 143 of file LocalContext.h.

Referenced by CreateNodeKey(), GetCreateReaderKey(), GetCreateWriterKey(), and SetupKernel().

143 { return ++counter; }

+ Here is the caller graph for this function:

bool CPN::Context::RequireRemote ( )
virtualinherited

Lets the kernel know that this context type requires remote activity. This overrides the kernel option for remote activity. Default value is false.

Returns
true or false

Reimplemented in CPN::RemoteContextClient.

Definition at line 40 of file Context.cc.

40  {
41  return false;
42  }
void CPN::LocalContext::SendCreateNode ( Key_t  kernelkey,
const NodeAttr attr 
)
virtual

Tell a given kernel that it needs to create a node.

Parameters
kernelkeythe id of the kernel
attrthe node attribute
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 205 of file LocalContext.cc.

References ASSERT, InternalCheckTerminated(), kernelmap, lock, and CPN::KernelBase::RemoteCreateNode().

205  {
206  KernelBase *kernel;
207  {
210  shared_ptr<KernelInfo> hinfo = kernelmap[kernelkey];
211  kernel = hinfo->kernel;
212  }
213  ASSERT(kernel);
214  kernel->RemoteCreateNode(attr);
215  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void CPN::LocalContext::SendCreateQueue ( Key_t  kernelkey,
const SimpleQueueAttr attr 
)
virtual

Tell a given kernel that it needs to create a queue.

Parameters
kernelkeythe id of the kernel
attrthe queue attribute
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 193 of file LocalContext.cc.

References ASSERT, InternalCheckTerminated(), kernelmap, lock, and CPN::KernelBase::RemoteCreateQueue().

193  {
194  KernelBase *kernel;
195  {
198  shared_ptr<KernelInfo> hinfo = kernelmap[kernelkey];
199  kernel = hinfo->kernel;
200  }
201  ASSERT(kernel);
202  kernel->RemoteCreateQueue(attr);
203  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void CPN::LocalContext::SendCreateReader ( Key_t  kernelkey,
const SimpleQueueAttr attr 
)
virtual

Tell a given kernel that it needs to create a queue read end.

Parameters
kernelkeythe id of the kernel
attrthe queue attribute
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 181 of file LocalContext.cc.

References ASSERT, InternalCheckTerminated(), kernelmap, lock, and CPN::KernelBase::RemoteCreateReader().

181  {
182  KernelBase *kernel;
183  {
186  shared_ptr<KernelInfo> hinfo = kernelmap[kernelkey];
187  kernel = hinfo->kernel;
188  }
189  ASSERT(kernel);
190  kernel->RemoteCreateReader(attr);
191  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void CPN::LocalContext::SendCreateWriter ( Key_t  kernelkey,
const SimpleQueueAttr attr 
)
virtual

Tell a given kernel that it needs to create a queue write end.

Parameters
kernelkeythe id of the kernel
attrthe queue attribute
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 169 of file LocalContext.cc.

References ASSERT, InternalCheckTerminated(), kernelmap, lock, and CPN::KernelBase::RemoteCreateWriter().

169  {
170  KernelBase *kernel;
171  {
174  shared_ptr<KernelInfo> hinfo = kernelmap[kernelkey];
175  kernel = hinfo->kernel;
176  }
177  ASSERT(kernel);
178  kernel->RemoteCreateWriter(attr);
179  }
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131
#define ASSERT(exp,...)

+ Here is the call graph for this function:

Key_t CPN::LocalContext::SetupKernel ( const std::string &  name,
const std::string &  hostname,
const std::string &  servname,
KernelBase kernel 
)
virtual

Called by the Kernel when it has successfully set it self up. This gives the Context a way to notify the Kernel of events and lets other Kernels look up the connection information for this Kernel.

Parameters
namethe kernel name
hostnamethe hostname to use to connect to this kernel
servnamethe service name the kernel is listening on
kernelcallback reference
Returns
the unique key for the new kernel
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 58 of file LocalContext.cc.

References InternalCheckTerminated(), kernelmap, kernelnames, lock, and NewKey().

59  {
62  if (!kernel) { throw std::invalid_argument("Must have non null Kernel."); }
63  if (kernelnames.find(name) != kernelnames.end()) {
64  throw std::invalid_argument("Cannot create two kernels with the same name");
65  }
66  shared_ptr<KernelInfo> hinfo = shared_ptr<KernelInfo>(new KernelInfo);
67  hinfo->name = name;
68  hinfo->hostname = hostname;
69  hinfo->servname = servname;
70  hinfo->kernel = kernel;
71  hinfo->dead = false;
72  hinfo->live = false;
73  hinfo->allowremote = true;
74  Key_t key = NewKey();
75  kernelmap.insert(std::make_pair(key, hinfo));
76  kernelnames.insert(std::make_pair(name, key));
77  return key;
78  }
void InternalCheckTerminated()
uint64_t Key_t
Definition: common.h:79
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

Key_t CPN::LocalContext::SetupKernel ( const std::string &  name,
KernelBase kernel 
)
virtual

Called by the kernel when it is not in remote mode.

Parameters
namethe kernel name
kernelcallback reference
Returns
the unique key for the new kernel

Implements CPN::Context.

Definition at line 80 of file LocalContext.cc.

References InternalCheckTerminated(), kernelmap, kernelnames, lock, and NewKey().

80  {
83  if (!kernel) { throw std::invalid_argument("Must have non null Kernel."); }
84  if (kernelnames.find(name) != kernelnames.end()) {
85  throw std::invalid_argument("Cannot create two kernels with the same name");
86  }
87  shared_ptr<KernelInfo> hinfo = shared_ptr<KernelInfo>(new KernelInfo);
88  hinfo->name = name;
89  hinfo->kernel = kernel;
90  hinfo->dead = false;
91  hinfo->live = false;
92  hinfo->allowremote = false;
93  Key_t key = NewKey();
94  kernelmap.insert(std::make_pair(key, hinfo));
95  kernelnames.insert(std::make_pair(name, key));
96  return key;
97 
98  }
void InternalCheckTerminated()
uint64_t Key_t
Definition: common.h:79
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::SignalKernelEnd ( Key_t  kernelkey)
virtual

Signal to the Context that the given kernel is dead.

Parameters
kernelkeyid of the kernel that died
Exceptions
std::invalid_argument

Implements CPN::Context.

Definition at line 132 of file LocalContext.cc.

References PthreadCondition::Broadcast(), kernellivedead, kernelmap, and lock.

132  {
134  KernelMap::iterator entry = kernelmap.find(kernelkey);
135  if (entry == kernelmap.end()) {
136  throw std::invalid_argument("No such kernel");
137  }
138  entry->second->dead = true;
140  }
PthreadCondition & Broadcast(void)
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadCondition kernellivedead
Definition: LocalContext.h:133
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::SignalKernelStart ( Key_t  kernelkey)
virtual

Signal to the context that the given kernel has started.

Parameters
kernelkeythe id for the kernel
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 158 of file LocalContext.cc.

References PthreadCondition::Broadcast(), InternalCheckTerminated(), kernellivedead, kernelmap, and lock.

158  {
161  KernelMap::iterator entry = kernelmap.find(kernelkey);
162  if (entry == kernelmap.end()) {
163  throw std::invalid_argument("No such kernel");
164  }
165  entry->second->live = true;
167  }
PthreadCondition & Broadcast(void)
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadCondition kernellivedead
Definition: LocalContext.h:133
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::SignalNodeEnd ( Key_t  nodekey)
virtual

Called by the node cleanup routine to indicate that the node has ended.

Parameters
nodekeythe unique key for the node
Exceptions
std::invalid_argument

Implements CPN::Context.

Definition at line 270 of file LocalContext.cc.

References PthreadCondition::Broadcast(), lock, nodelivedead, nodemap, and numlivenodes.

270  {
272  NodeMap::iterator entry = nodemap.find(nodekey);
273  if (entry == nodemap.end()) {
274  throw std::invalid_argument("No such node");
275  } else {
276  entry->second->dead = true;
277  --numlivenodes;
279  }
280  }
PthreadCondition & Broadcast(void)
unsigned numlivenodes
Definition: LocalContext.h:140
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132

+ Here is the call graph for this function:

void CPN::LocalContext::SignalNodeStart ( Key_t  nodekey)
virtual

Called by the node startup routine to indicate that the node has started.

Parameters
nodekeythe unique key for the node
Exceptions
ShutdownException
std::invalid_argument

Implements CPN::Context.

Definition at line 257 of file LocalContext.cc.

References PthreadCondition::Broadcast(), InternalCheckTerminated(), lock, nodelivedead, nodemap, and numlivenodes.

257  {
260  NodeMap::iterator entry = nodemap.find(nodekey);
261  if (entry == nodemap.end()) {
262  throw std::invalid_argument("No such node");
263  } else {
264  entry->second->started = true;
265  ++numlivenodes;
267  }
268  }
PthreadCondition & Broadcast(void)
void InternalCheckTerminated()
unsigned numlivenodes
Definition: LocalContext.h:140
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132

+ Here is the call graph for this function:

void CPN::LocalContext::Terminate ( )
virtual

Signal to the Context that the network is terminating. After this call most methods will throw a ShutdownException.

Implements CPN::Context.

Definition at line 467 of file LocalContext.cc.

References PthreadCondition::Broadcast(), kernellivedead, kernelmap, lock, nodelivedead, and shutdown.

467  {
468  KernelMap mapcopy;
469  {
471  shutdown = true;
474  mapcopy = kernelmap;
475  }
476  KernelMap::iterator itr = mapcopy.begin();
477  while (itr != mapcopy.end()) {
478  if (!itr->second->dead) {
479  itr->second->kernel->NotifyTerminate();
480  }
481  ++itr;
482  }
483 }
std::map< Key_t, shared_ptr< KernelInfo > > KernelMap
Definition: LocalContext.h:74
PthreadCondition & Broadcast(void)
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadCondition kernellivedead
Definition: LocalContext.h:133
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132

+ Here is the call graph for this function:

void CPN::LocalContext::WaitForAllNodeEnd ( )
virtual

Convenience method which waits until there are no nodes running. If no node have started then this will return immediately.

Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 317 of file LocalContext.cc.

References lock, nodelivedead, numlivenodes, shutdown, and PthreadCondition::Wait().

317  {
319  while (numlivenodes > 0 && !shutdown) {
321  }
322  }
PthreadCondition & Wait(PthreadMutex &mutex)
unsigned numlivenodes
Definition: LocalContext.h:140
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132

+ Here is the call graph for this function:

Key_t CPN::LocalContext::WaitForKernelStart ( const std::string &  kernel)
virtual

Does not return until the given kernel has started.

Parameters
kernelthe name of the kernel (the key may not be known yet)
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 142 of file LocalContext.cc.

References InternalCheckTerminated(), kernellivedead, kernelmap, kernelnames, lock, and PthreadCondition::Wait().

142  {
145  while (true) {
146  NameMap::iterator entry = kernelnames.find(kernel);
147  if (entry != kernelnames.end()) {
148  KernelMap::iterator hentry = kernelmap.find(entry->second);
149  if (hentry->second->live) {
150  return entry->second;
151  }
152  }
155  }
156  }
PthreadCondition & Wait(PthreadMutex &mutex)
void InternalCheckTerminated()
KernelMap kernelmap
Definition: LocalContext.h:136
PthreadCondition kernellivedead
Definition: LocalContext.h:133
PthreadMutex lock
Definition: LocalContext.h:131

+ Here is the call graph for this function:

void CPN::LocalContext::WaitForNodeEnd ( const std::string &  nodename)
virtual

Waits for the given node to signal end.

Parameters
nodenamethe name of the node
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 300 of file LocalContext.cc.

References ASSERT, lock, nodelivedead, nodemap, nodenames, shutdown, and PthreadCondition::Wait().

300  {
302  while (!shutdown) {
303  NameMap::iterator nameentry = nodenames.find(nodename);
304  if (nameentry != nodenames.end()) {
305  NodeMap::iterator entry = nodemap.find(nameentry->second);
306  if (entry != nodemap.end()) {
307  if (entry->second->dead) {
308  ASSERT(entry->second->started, "Node died before it started!?");
309  return;
310  }
311  }
312  }
314  }
315  }
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132
#define ASSERT(exp,...)

+ Here is the call graph for this function:

Key_t CPN::LocalContext::WaitForNodeStart ( const std::string &  nodename)
virtual

Waits until the node starts and returns the key, if the node is already started returns the key.

Parameters
nodenamethe name of the node to wait for
Returns
the key for the node
Exceptions
ShutdownException

Implements CPN::Context.

Definition at line 282 of file LocalContext.cc.

References InternalCheckTerminated(), lock, nodelivedead, nodemap, nodenames, and PthreadCondition::Wait().

282  {
285  while (true) {
286  NameMap::iterator nameentry = nodenames.find(nodename);
287  if (nameentry != nodenames.end()) {
288  NodeMap::iterator entry = nodemap.find(nameentry->second);
289  if (entry != nodemap.end()) {
290  if (entry->second->started) {
291  return entry->first;
292  }
293  }
294  }
297  }
298  }
PthreadCondition & Wait(PthreadMutex &mutex)
void InternalCheckTerminated()
PthreadMutex lock
Definition: LocalContext.h:131
PthreadCondition nodelivedead
Definition: LocalContext.h:132

+ Here is the call graph for this function:

Member Data Documentation

Key_t CPN::LocalContext::counter
private

Definition at line 144 of file LocalContext.h.

PthreadCondition CPN::LocalContext::kernellivedead
private

Definition at line 133 of file LocalContext.h.

Referenced by SignalKernelEnd(), SignalKernelStart(), Terminate(), and WaitForKernelStart().

KernelMap CPN::LocalContext::kernelmap
private
NameMap CPN::LocalContext::kernelnames
private

Definition at line 134 of file LocalContext.h.

Referenced by GetKernelKey(), SetupKernel(), and WaitForKernelStart().

PthreadMutex CPN::LocalContext::lock
mutableprivate
int CPN::LocalContext::loglevel
private

Definition at line 130 of file LocalContext.h.

Referenced by Log(), and LogLevel().

PthreadCondition CPN::LocalContext::nodelivedead
private
NodeMap CPN::LocalContext::nodemap
private
NameMap CPN::LocalContext::nodenames
private

Definition at line 135 of file LocalContext.h.

Referenced by CreateNodeKey(), GetNodeKey(), WaitForNodeEnd(), and WaitForNodeStart().

unsigned CPN::LocalContext::numlivenodes
private

Definition at line 140 of file LocalContext.h.

Referenced by SignalNodeEnd(), SignalNodeStart(), and WaitForAllNodeEnd().

PortMap CPN::LocalContext::readports
private
bool CPN::LocalContext::shutdown
private
PortMap CPN::LocalContext::writeports
private

The documentation for this class was generated from the following files: