CPN
Computational Process Networks
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
CPN::NodeBase Class Referenceabstract

The definition common to all nodes in the process network. More...

#include <NodeBase.h>

+ Inheritance diagram for CPN::NodeBase:
+ Collaboration diagram for CPN::NodeBase:

Public Member Functions

 NodeBase (Kernel &ker, const NodeAttr &attr)
 
virtual ~NodeBase ()
 
const std::string & GetTypeName () const
 
KernelGetKernel ()
 Return a pointer to the kernel that his node is running under. More...
 
std::string GetParam (const std::string &key) const
 
bool HasParam (const std::string &key) const
 
template<typename T >
GetParam (const std::string &key) const
 
template<typename T >
GetParam (const std::string &key, T def) const
 
void Start ()
 For use by the CPN::Kernel to start the node. More...
 
void Shutdown ()
 Perform actions (like joining a thread) before destruction. More...
 
bool IsPurePseudo ()
 
void LogState ()
 For debugging ONLY! More...
 
const std::string & GetName () const
 
Key_t GetKey () const
 
shared_ptr< QueueReaderGetIQueue (const std::string &portname)
 This method is for use by the user to aquire a reader endpoint. This function blocks until the CPN::Kernel hands this node the queue associated with the endpoint. More...
 
shared_ptr< QueueWriterGetOQueue (const std::string &portname)
 This method is for use by the user to aquire a writer endpoint. This function blocks until the CPN::Kernel hands this node the queue associated with the endpoint. More...
 
void CreateReader (shared_ptr< QueueBase > q)
 for use by the CPN::Kernel to create a new read endpoint. More...
 
void CreateWriter (shared_ptr< QueueBase > q)
 for use by the CPN::Kernel to create a new writer endpoint. More...
 
void NotifyTerminate ()
 Called by the kernel when it is shutting down. More...
 

Protected Member Functions

virtual void Process ()=0
 Override this method to implement a node. More...
 

Protected Attributes

Kernelkernel
 
Logger logger
 

Private Member Functions

void * EntryPoint ()
 

Private Attributes

const std::string type
 
auto_ptr< Pthreadthread
 
std::map< std::string,
std::string > 
params
 

Detailed Description

The definition common to all nodes in the process network.

A node is a thread of execution which lasts the lifetime of the node object.

Definition at line 69 of file NodeBase.h.

Constructor & Destructor Documentation

CPN::NodeBase::NodeBase ( Kernel ker,
const NodeAttr attr 
)

Definition at line 35 of file NodeBase.cc.

References CreatePthreadFunctional(), EntryPoint(), and thread.

36  : PseudoNode(attr.GetName(), attr.GetKey(), ker.GetContext()),
37  kernel(ker),
38  type(attr.GetTypeName()),
39  params(attr.GetParams())
40  {
42  }
PseudoNode(const std::string &name_, Key_t k, shared_ptr< Context > ctx)
Definition: PseudoNode.cc:31
const std::string type
Definition: NodeBase.h:121
PthreadFunctional * CreatePthreadFunctional(T *obj, void *(T::*meth)(void))
void * EntryPoint()
Definition: NodeBase.cc:72
std::map< std::string, std::string > params
Definition: NodeBase.h:123
auto_ptr< Pthread > thread
Definition: NodeBase.h:122
Kernel & kernel
Definition: NodeBase.h:117

+ Here is the call graph for this function:

CPN::NodeBase::~NodeBase ( )
virtual

Definition at line 44 of file NodeBase.cc.

44  {
45  }

Member Function Documentation

void CPN::PseudoNode::CreateReader ( shared_ptr< QueueBase q)
inherited

for use by the CPN::Kernel to create a new read endpoint.

Definition at line 55 of file PseudoNode.cc.

References ASSERT, CPN::PseudoNode::cond, CPN::PseudoNode::d4rnode, CPN::PseudoNode::lock, CPN::PseudoNode::readermap, and Sync::ReentrantCondition::Signal().

55  {
57  Key_t readerkey = q->GetReaderKey();
58  d4rnode->AddReader(q);
59  q->SetReaderNode(d4rnode);
60  q->SignalReaderTagChanged();
61  ASSERT(readermap.find(readerkey) == readermap.end(), "The reader already exists");
62  shared_ptr<QueueReader> reader;
63  reader = shared_ptr<QueueReader>(new QueueReader(this, q));
64  readermap.insert(std::make_pair(readerkey, reader));
65  cond.Signal();
66  }
shared_ptr< D4R::Node > d4rnode
Definition: PseudoNode.h:89
Sync::ReentrantLock lock
Definition: PseudoNode.h:85
uint64_t Key_t
Definition: common.h:79
ReaderMap readermap
Definition: PseudoNode.h:93
Sync::ReentrantCondition cond
Definition: PseudoNode.h:86
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void CPN::PseudoNode::CreateWriter ( shared_ptr< QueueBase q)
inherited

for use by the CPN::Kernel to create a new writer endpoint.

Definition at line 68 of file PseudoNode.cc.

References ASSERT, CPN::PseudoNode::cond, CPN::PseudoNode::d4rnode, CPN::PseudoNode::lock, Sync::ReentrantCondition::Signal(), and CPN::PseudoNode::writermap.

68  {
70  Key_t writerkey = q->GetWriterKey();
71  d4rnode->AddWriter(q);
72  q->SetWriterNode(d4rnode);
73  q->SignalWriterTagChanged();
74  ASSERT(writermap.find(writerkey) == writermap.end(), "The writer already exists.");
75  shared_ptr<QueueWriter> writer;
76  writer = shared_ptr<QueueWriter>(new QueueWriter(this, q));
77  writermap.insert(std::make_pair(writerkey, writer));
78  cond.Signal();
79  }
shared_ptr< D4R::Node > d4rnode
Definition: PseudoNode.h:89
WriterMap writermap
Definition: PseudoNode.h:94
Sync::ReentrantLock lock
Definition: PseudoNode.h:85
uint64_t Key_t
Definition: common.h:79
Sync::ReentrantCondition cond
Definition: PseudoNode.h:86
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void * CPN::NodeBase::EntryPoint ( )
private

Definition at line 72 of file NodeBase.cc.

References Logger::ERROR, CPN::Kernel::GetContext(), CPN::PseudoNode::GetKey(), CPN::PseudoNode::GetName(), Logger::Info(), kernel, CPN::PseudoNode::logger, Logger::Name(), CPN::Kernel::NodeTerminated(), Process(), and CPN::Kernel::SwallowBrokenQueueExceptions().

Referenced by NodeBase().

72  {
73  try {
74  kernel.GetContext()->SignalNodeStart(GetKey());
75  Process();
76  } catch (const CPN::ShutdownException &e) {
77  // Forced shutdown
78  } catch (const CPN::BrokenQueueException &e) {
80  throw;
81  }
82  } catch (const D4R::DeadlockException &e) {
83  // A true deadlock was detected, die
85  logger.Name(GetName().c_str());
86  logger.Info("DEADLOCK detected at %s\n", GetName().c_str());
87  }
89  return 0;
90  }
Logger object that is used for forwarding log messages.
Definition: Logger.h:57
shared_ptr< Context > GetContext() const
Definition: Kernel.h:227
The exception thrown when true deadlock is detected.
An exception indicating that the node tried to write to a shutdown queue.
Definition: Exceptions.h:65
Key_t GetKey() const
Definition: PseudoNode.h:43
void Info(const char *fmt,...)
Definition: Logger.cc:177
bool SwallowBrokenQueueExceptions()
Whether the node should by default swallow the broken queue exceptions or let them propigate as an er...
Definition: Kernel.cc:656
const std::string & Name() const
Definition: Logger.cc:88
void NodeTerminated(Key_t key)
Called by the node in the cleanup routine. TO BE CALLED ONLY BY THE CPN INTERNALS.
Definition: Kernel.cc:495
const std::string & GetName() const
Definition: PseudoNode.h:40
An exception indicating that the Kernel has shut down.
Definition: Exceptions.h:38
virtual void Process()=0
Override this method to implement a node.
Kernel & kernel
Definition: NodeBase.h:117

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

shared_ptr< QueueReader > CPN::PseudoNode::GetIQueue ( const std::string &  portname)
inherited

This method is for use by the user to aquire a reader endpoint. This function blocks until the CPN::Kernel hands this node the queue associated with the endpoint.

Parameters
portnamethe port name of the reader to get.
Returns
a shared pointer to a reader for he given endpoint name

Definition at line 43 of file PseudoNode.cc.

References CPN::PseudoNode::context, CPN::PseudoNode::GetReader(), and CPN::PseudoNode::nodekey.

43  {
44  context->CheckTerminated();
45  Key_t ekey = context->GetCreateReaderKey(nodekey, portname);
46  return GetReader(ekey);
47  }
const Key_t nodekey
Definition: PseudoNode.h:88
shared_ptr< QueueReader > GetReader(Key_t ekey)
Definition: PseudoNode.cc:132
uint64_t Key_t
Definition: common.h:79
shared_ptr< Context > context
Definition: PseudoNode.h:96

+ Here is the call graph for this function:

Kernel* CPN::NodeBase::GetKernel ( )
inline

Return a pointer to the kernel that his node is running under.

Definition at line 82 of file NodeBase.h.

82 { return &kernel; }
Kernel & kernel
Definition: NodeBase.h:117
Key_t CPN::PseudoNode::GetKey ( ) const
inlineinherited
Returns
the process network wide unique id for this node

Definition at line 43 of file PseudoNode.h.

Referenced by EntryPoint().

43 { return nodekey; }
const Key_t nodekey
Definition: PseudoNode.h:88

+ Here is the caller graph for this function:

const std::string& CPN::PseudoNode::GetName ( ) const
inlineinherited
Returns
the unique name of this node

Definition at line 40 of file PseudoNode.h.

Referenced by EntryPoint(), and GetParam().

40 { return name; }
const std::string name
Definition: PseudoNode.h:87

+ Here is the caller graph for this function:

shared_ptr< QueueWriter > CPN::PseudoNode::GetOQueue ( const std::string &  portname)
inherited

This method is for use by the user to aquire a writer endpoint. This function blocks until the CPN::Kernel hands this node the queue associated with the endpoint.

Parameters
portnamethe port name fo the writer to get.
Returns
a shared pointer to a writer for the given endpoint name.

Definition at line 49 of file PseudoNode.cc.

References CPN::PseudoNode::context, CPN::PseudoNode::GetWriter(), and CPN::PseudoNode::nodekey.

49  {
50  context->CheckTerminated();
51  Key_t ekey = context->GetCreateWriterKey(nodekey, portname);
52  return GetWriter(ekey);
53  }
const Key_t nodekey
Definition: PseudoNode.h:88
shared_ptr< QueueWriter > GetWriter(Key_t ekey)
Definition: PseudoNode.cc:147
uint64_t Key_t
Definition: common.h:79
shared_ptr< Context > context
Definition: PseudoNode.h:96

+ Here is the call graph for this function:

std::string CPN::NodeBase::GetParam ( const std::string &  key) const

Definition at line 47 of file NodeBase.cc.

References CPN::PseudoNode::GetName(), and params.

47  {
48  std::map<std::string, std::string>::const_iterator entry =
49  params.find(key);
50  if (entry == params.end()) {
51  throw std::invalid_argument("Required parameter \"" + key + "\" missing"
52  " for node \"" + GetName() + "\"");
53  }
54  return entry->second;
55  }
const std::string & GetName() const
Definition: PseudoNode.h:40
std::map< std::string, std::string > params
Definition: NodeBase.h:123

+ Here is the call graph for this function:

template<typename T >
T CPN::NodeBase::GetParam ( const std::string &  key) const
inline

Definition at line 89 of file NodeBase.h.

References CPN::NodeBasePrivate::CoerceParam< T >::Coerce().

89  {
91  }
static T Coerce(const std::string &key, const std::string &param)
Definition: NodeBase.h:45
std::string GetParam(const std::string &key) const
Definition: NodeBase.cc:47

+ Here is the call graph for this function:

template<typename T >
T CPN::NodeBase::GetParam ( const std::string &  key,
def 
) const
inline

Definition at line 94 of file NodeBase.h.

94  {
95  if (HasParam(key)) {
96  return GetParam<T>(key);
97  }
98  return def;
99  }
bool HasParam(const std::string &key) const
Definition: NodeBase.cc:57
const std::string& CPN::NodeBase::GetTypeName ( ) const
inline
Returns
the type id of this node

Definition at line 78 of file NodeBase.h.

78 { return type; }
const std::string type
Definition: NodeBase.h:121
bool CPN::NodeBase::HasParam ( const std::string &  key) const

Definition at line 57 of file NodeBase.cc.

References params.

57  {
58  std::map<std::string, std::string>::const_iterator entry =
59  params.find(key);
60  return entry != params.end();
61  }
std::map< std::string, std::string > params
Definition: NodeBase.h:123
bool CPN::NodeBase::IsPurePseudo ( )
virtual

Reimplemented from CPN::PseudoNode.

Definition at line 93 of file NodeBase.cc.

93  {
94  return false;
95  }
void CPN::NodeBase::LogState ( )
virtual

For debugging ONLY!

Reimplemented from CPN::PseudoNode.

Definition at line 97 of file NodeBase.cc.

References Logger::Error(), CPN::PseudoNode::logger, CPN::PseudoNode::LogState(), and thread.

97  {
99  logger.Error("Thread id: %llu, %s", (unsigned long long)((pthread_t)(*thread.get())),
100  (thread->Done() ? "done" : "running"));
101  }
virtual void LogState()
For debugging ONLY!
Definition: PseudoNode.cc:167
void Error(const char *fmt,...)
Definition: Logger.cc:159
auto_ptr< Pthread > thread
Definition: NodeBase.h:122

+ Here is the call graph for this function:

void CPN::PseudoNode::NotifyTerminate ( )
inherited

Called by the kernel when it is shutting down.

Definition at line 123 of file PseudoNode.cc.

References CPN::PseudoNode::cond, CPN::PseudoNode::lock, CPN::PseudoNode::readermap, Sync::ReentrantCondition::Signal(), and CPN::PseudoNode::writermap.

123  {
125  cond.Signal();
126  WriterMap::iterator witr = writermap.begin();
127  while (witr != writermap.end()) { (witr++)->second->NotifyTerminate(); }
128  ReaderMap::iterator ritr = readermap.begin();
129  while (ritr != readermap.end()) { (ritr++)->second->NotifyTerminate(); }
130  }
WriterMap writermap
Definition: PseudoNode.h:94
Sync::ReentrantLock lock
Definition: PseudoNode.h:85
ReaderMap readermap
Definition: PseudoNode.h:93
Sync::ReentrantCondition cond
Definition: PseudoNode.h:86

+ Here is the call graph for this function:

virtual void CPN::NodeBase::Process ( )
protectedpure virtual

Override this method to implement a node.

Referenced by EntryPoint().

+ Here is the caller graph for this function:

void CPN::NodeBase::Shutdown ( )
virtual

Perform actions (like joining a thread) before destruction.

Reimplemented from CPN::PseudoNode.

Definition at line 67 of file NodeBase.cc.

References CPN::PseudoNode::Shutdown(), and thread.

67  {
68  thread->Join();
70  }
virtual void Shutdown()
Perform actions (like joining a thread) before destruction.
Definition: PseudoNode.cc:81
auto_ptr< Pthread > thread
Definition: NodeBase.h:122

+ Here is the call graph for this function:

void CPN::NodeBase::Start ( )

For use by the CPN::Kernel to start the node.

Definition at line 63 of file NodeBase.cc.

References thread.

63  {
64  thread->Start();
65  }
auto_ptr< Pthread > thread
Definition: NodeBase.h:122

Member Data Documentation

Kernel& CPN::NodeBase::kernel
protected

Definition at line 117 of file NodeBase.h.

Referenced by EntryPoint().

Logger CPN::PseudoNode::logger
protectedinherited

Definition at line 76 of file PseudoNode.h.

Referenced by EntryPoint(), CPN::PseudoNode::LogState(), and LogState().

std::map<std::string, std::string> CPN::NodeBase::params
private

Definition at line 123 of file NodeBase.h.

Referenced by GetParam(), and HasParam().

auto_ptr<Pthread> CPN::NodeBase::thread
private

Definition at line 122 of file NodeBase.h.

Referenced by LogState(), NodeBase(), Shutdown(), and Start().

const std::string CPN::NodeBase::type
private

Definition at line 121 of file NodeBase.h.


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