00001
00002 #ifndef NODE_H
00003 #define NODE_H
00004
00005 #include <limits>
00006 #include <iostream>
00007 #include <sstream>
00008 using namespace std;
00009 #include <boost/shared_ptr.hpp>
00010 #include <boost/enable_shared_from_this.hpp>
00011
00012 #include "sim_time.hpp"
00013 #include "communication_layer.hpp"
00014 #include "location.hpp"
00015
00016 class Event;
00017 typedef boost::shared_ptr<Event> EventPtr;
00018
00020
00022
00027 class NodeId
00028 {
00034 public:
00035
00039 NodeId(t_uint numericValue)
00040 : m_numericValue(numericValue)
00041 { }
00042
00051 NodeId(const t_uchar* byteArray, t_uint byteCount)
00052 {
00053 m_numericValue = 0;
00054 for(t_uint i = 0; i < byteCount; i++) {
00055 if(i > sizeof(t_uint))
00056 assert(byteArray[i] == 0);
00057 m_numericValue += (byteArray[i] << (8 * i));
00058 }
00059 }
00060
00062 virtual ~NodeId() { }
00063
00071 inline void writeToByteArray(t_uchar* byteArray,
00072 t_uint byteCount) const
00073 {
00074 t_uint id = getNumericValue();
00075 assert(sizeof(t_uint) <= byteCount);
00076
00077 fill(byteArray, &byteArray[byteCount], 0);
00078 t_uint i = 0;
00079 while(id > 0) {
00080 assert(i < byteCount);
00081 byteArray[i++] = id & 0xFF;
00082 id >>= 8;
00083 }
00084 }
00085
00091 static inline t_uint broadcastDestination()
00092 {
00093 return numeric_limits<t_uint>::max();
00094 }
00095
00100 virtual string getStringValue() const
00101 {
00102 ostringstream streamValue;
00103 if(m_numericValue == broadcastDestination())
00104 streamValue << "BROADCAST";
00105 else
00106 streamValue << m_numericValue;
00107 return streamValue.str();
00108 }
00109
00114 virtual t_uint getNumericValue() const
00115 {
00116 return m_numericValue;
00117 }
00118
00123 inline bool operator== (const NodeId& rhs) const
00124 {
00125 return m_numericValue == rhs.m_numericValue;
00126 }
00127
00133 inline bool operator< (const NodeId& rhs) const
00134 {
00135 return m_numericValue < rhs.m_numericValue;
00136 }
00137
00138 protected:
00139
00143 t_uint m_numericValue;
00144
00145 };
00146
00153 class Node : boost::noncopyable,
00154 public boost::enable_shared_from_this<Node> {
00155
00156
00157
00158
00159 public:
00161 typedef boost::shared_ptr<Node> NodePtr;
00162
00164 virtual ~Node();
00165
00172 static inline NodePtr create(const Location& location,
00173 const NodeId& nodeId);
00174
00179 inline Location getLocation() const;
00180
00185 inline NodeId getNodeId() const;
00186
00193 SimTime currentTime() const;
00194
00203 bool scheduleEvent(EventPtr eventToSchedule,
00204 const SimTime& eventDelay);
00205
00212 bool cancelEvent(EventPtr eventToCancel);
00213
00214 protected:
00215
00219 Node(const Location& location, const NodeId& nodeId);
00220
00221 private:
00222
00225 Location m_location;
00226
00229 NodeId m_nodeId;
00230
00231 };
00232 typedef boost::shared_ptr<Node> NodePtr;
00233
00235
00237
00238 inline NodePtr Node::create(const Location& location,
00239 const NodeId& nodeId)
00240 {
00241 NodePtr p(new Node(location, nodeId));
00242 return p;
00243 }
00244
00245 inline Location Node::getLocation() const
00246 {
00247 return m_location;
00248 }
00249
00250 inline NodeId Node::getNodeId() const
00251 {
00252 return m_nodeId;
00253 }
00254
00256
00258
00259 inline ostream& operator<< (ostream& s, const Node& node)
00260 {
00261 return s<< "Node state (pointer=" << &node << ")";
00262 }
00263
00264 inline ostream& operator<< (ostream& s, const NodeId& nodeId)
00265 {
00266 return s<< nodeId.getStringValue();
00267 }
00268
00269 #endif // NODE_H
00270