| EventStudio 2.5 Sequence diagram based system design and modeling | |
#include <Routing_Table.h> In this routing table, a mapping is maintained from the Node Id to the Link Id. Each routing table entry is specified as a pair of Node Id and Link Id. Given a Node Id, the Routing Table returns the Link Id. We could write pretty much the same code as Terminal_Manager to implement it, but we will take a different route to implement the same functionality. This time we will use another feature of STL, the iterator.
Think of iterators as pointers on steroids. Iterators are special types defined by the map that give the appearance of a pointer. Iterators allow you to iterate through the complete data structure. For example the iterator in a map allows you to iterate through the complete map. The iterator points to a structure containing the key (marked as first) and the actual value stored in the as second.
Definition at line 28 of file Routing_Table.h.
Public Member Functions | |
| void | Print () |
| This method prints all the routing table entries. | |
| Status | Add_Routing_Entry (Node_Id node_Id, Link_Id link_Id) |
| Add a new entry to the routing table. | |
| Status | Remove_Routing_Entry (Node_Id node_Id) |
| Remove an entry from the routing table. | |
| Link_Id | Route (Node_Id node_Id) |
| This method performs the actual routing from Node_Id to Link_Id. | |
Private Types | |
| typedef map< Node_Id, Link_Id > | Routing_Map |
| The Routing_Map type is defined in terms of a map. | |
Private Attributes | |
| Routing_Map | m_routing_Map |
| STL based type Routing_Map is used to define the routing table map. | |
|
The Routing_Map type is defined in terms of a map. The map is keyed with the Node_Id and stores the Link_Id. The map provides Node_Id to Link_Id mapping where the Node_Id can be any arbitrary value. Most of the time the map can be accessed using array type syntax. The main advantages of using map are:
Definition at line 41 of file Routing_Table.h. |
|
This method prints all the routing table entries. STL interators are used to loop through the map and print all entries.
struct Pair
{
Node_Id first;
Link_Id second;
};
Definition at line 63 of file Routing_Table.h. 00064 {
00065 // STL defines a nested type iterator. Here we have declared an instance
00066 // of this nested type.
00067 Routing_Map::iterator it;
00068
00069 // Iterator it "points" to a structure defined as:
00070 // struct Pair
00071 // {
00072 // Node_Id first;
00073 // Link_Id second;
00074 // };
00075
00076 // map supports predefined begin() and end() markers. begin() "points"
00077 // to the first entry in the map. end() points BEYOND the last entry
00078 // in the map (i.e. the last entry is the entry just before end()
00079
00080 // STL provides pointer like symantics to iterators, thus it++ moves
00081 // the iterator to the next entry in the map.
00082
00083 for (it = m_routing_Map.begin(); it != m_routing_Map.end(); it++)
00084 {
00085 printf ("Node_Id = %d, Link_Id = %d\n", it->first, it->second);
00086 }
00087 }
|
| ||||||||||||
Add a new entry to the routing table. The routine goes through the following steps:
Definition at line 99 of file Routing_Table.h. 00100 {
00101 Status status;
00102
00103 // First check if the entry already exists
00104 if (m_routing_Map.find(node_Id) != m_routing_Map.end())
00105 {
00106 // Iterator returns a value other than end, thus this
00107 // is a duplicate addition, return failure
00108 status = FAILURE;
00109 }
00110 else
00111 {
00112 // The end iterator was returned, signifying that
00113 // no entry currently exists, so a new one can be added.
00114 m_routing_Map[node_Id] = link_Id;
00115
00116 status = SUCCESS;
00117 }
00118 }
|
|
Remove an entry from the routing table. The routine goes through the following steps:
Definition at line 127 of file Routing_Table.h. 00128 {
00129 Status status;
00130 // Check if the terminal is present
00131 if (m_routing_Map.find(node_Id) != m_routing_Map.end())
00132 {
00133 // Erase the entry from the map.
00134 m_routing_Map.erase(node_Id);
00135 status = SUCCESS;
00136 }
00137 else
00138 {
00139 status = FAILURE;
00140 }
00141
00142 return status;
00143 }
|
|
This method performs the actual routing from Node_Id to Link_Id. This method will be called for every message that needs to be routed.
Definition at line 153 of file Routing_Table.h. 00154 {
00155 Link_Id link_Id = INVALID_LINK_ID;
00156 // Check if the route entry is present
00157 if (m_routing_Map.find(node_Id) != m_routing_Map.end())
00158 {
00159 link_Id = m_routing_Map[node_Id];
00160 }
00161 return link_Id;
00162 }
|
|
STL based type Routing_Map is used to define the routing table map. All node id to link id mappings are stored inside this map. Definition at line 45 of file Routing_Table.h. |
1.3.4 | |
| Copyright © 2000-2005 EventHelix.com Inc. All Rights Reserved. |