CPN
Computational Process Networks
NodeBase.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
24 #include "common_priv.h"
25 #include <cpn/NodeBase.h>
26 #include <cpn/Kernel.h>
27 #include <cpn/Exceptions.h>
28 #include <cpn/Context.h>
32 
33 namespace CPN {
34 
35  NodeBase::NodeBase(Kernel &ker, const NodeAttr &attr)
36  : PseudoNode(attr.GetName(), attr.GetKey(), ker.GetContext()),
37  kernel(ker),
38  type(attr.GetTypeName()),
39  params(attr.GetParams())
40  {
42  }
43 
45  }
46 
47  std::string NodeBase::GetParam(const std::string &key) const {
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  }
56 
57  bool NodeBase::HasParam(const std::string &key) const {
58  std::map<std::string, std::string>::const_iterator entry =
59  params.find(key);
60  return entry != params.end();
61  }
62 
63  void NodeBase::Start() {
64  thread->Start();
65  }
66 
68  thread->Join();
70  }
71 
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  }
91 
92 
94  return false;
95  }
96 
99  logger.Error("Thread id: %llu, %s", (unsigned long long)((pthread_t)(*thread.get())),
100  (thread->Done() ? "done" : "running"));
101  }
102 }
103 
Logger object that is used for forwarding log messages.
Definition: Logger.h:57
shared_ptr< Context > GetContext() const
Definition: Kernel.h:227
The base definition of all nodes.
Attributes for a node.
Definition: NodeAttr.h:58
void LogState()
For debugging ONLY!
Definition: NodeBase.cc:97
Definition for the kernel object.
virtual void LogState()
For debugging ONLY!
Definition: PseudoNode.cc:167
The Context abstract data type.
The exception thrown when true deadlock is detected.
void Error(const char *fmt,...)
Definition: Logger.cc:159
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
virtual void Shutdown()
Perform actions (like joining a thread) before destruction.
Definition: PseudoNode.cc:81
The exceptions specified for the CPN network.
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
PthreadFunctional * CreatePthreadFunctional(T *obj, void *(T::*meth)(void))
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
void * EntryPoint()
Definition: NodeBase.cc:72
bool IsPurePseudo()
Definition: NodeBase.cc:93
const std::string & GetName() const
Definition: PseudoNode.h:40
void Start()
For use by the CPN::Kernel to start the node.
Definition: NodeBase.cc:63
An exception indicating that the Kernel has shut down.
Definition: Exceptions.h:38
void Shutdown()
Perform actions (like joining a thread) before destruction.
Definition: NodeBase.cc:67
std::map< std::string, std::string > params
Definition: NodeBase.h:123
virtual void Process()=0
Override this method to implement a node.
std::string GetParam(const std::string &key) const
Definition: NodeBase.cc:47
virtual ~NodeBase()
Definition: NodeBase.cc:44
An exception thrown when a true deadlock is detected.
auto_ptr< Pthread > thread
Definition: NodeBase.h:122
The Kernel declaration.
Definition: Kernel.h:55
Kernel & kernel
Definition: NodeBase.h:117
NodeBase(Kernel &ker, const NodeAttr &attr)
Definition: NodeBase.cc:35
bool HasParam(const std::string &key) const
Definition: NodeBase.cc:57