[ns] Brief Functionality of Node.cc in ns2...

Mubashir Rehmani mshrehmani at gmail.com
Wed Feb 25 11:14:01 PST 2009


Hi Dinesh Nagare,

You can find the brief functionality of some functions of node.cc in ns2. I
hope it gives you an overall idea how things are going on in ns2.


*
Functionality of Node.cc in ns2
================================*



00090 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm>
static class NodeClass
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm> :
public TclClass
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classTclClass.htm> {


00091 public:
00092 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm#NodeClassa0>
        NodeClass
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm#NodeClassa0>()
: TclClass <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classTclClass.htm>("Node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm>") {}

00093 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm#NodeClassa1>
        TclObject
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classTclObject.htm>*
create <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNodeClass.htm#NodeClassa1>(int,
const char*const*) {


00094                 return (new Node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm>);
00095         }
00096 } class_node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/node_8cc.htm#a1>;

*
Here class NodeClass is derived from TclClass. All the classes in ns2 should
drive from TclClass and then at line 00096, an object of class node is
created.*

00105 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea0>
Node::Node <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea0>()
:


00106         address_(-1), nodeid_ (-1), namChan_(0),
00107         rtnotif_(NULL),
00108 #ifdef HAVE_STL
00109         nixnode_(NULL),
00110 #endif //HAVE_STL
00111         energy_model_(NULL), location_(NULL)


00112 {
00113         LIST_INIT
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/bsd-list_8h.htm#a2>(&ifhead_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep9>);


00114         LIST_INIT
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/bsd-list_8h.htm#a2>(&linklisthead_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep10>);


00115         insert
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea11>(&(Node::nodehead_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodes1>));
// insert self into static list of nodes


00116 #ifdef HAVE_STL
00117         // Mods for Nix-Vector routing
00118         if (NixRoutingUsed
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/node_8cc.htm#a2> < 0) {


00119                 // Find out if nix routing is in use
00120                 Tcl& tcl = Tcl::instance();
00121                 tcl.evalf("Simulator set nix-routing");


00122                 tcl.resultAs(&NixRoutingUsed
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/node_8cc.htm#a2>);
00123         }
00124         if (NixRoutingUsed
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/node_8cc.htm#a2>) {


00125                 // Create the NixNode pointer
00126                 if(0)printf("Nix routing in use, creating NixNode\n");

00127                 nixnode_ = new NixNode
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classNixNode.htm>();
00128         }
00129 #endif //HAVE_STL

00130         neighbor_list_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodeo0>
= NULL;
00131 }

*A constructor of class node is created to initialize variables.*


00133 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea1>
Node::~Node <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea1>()


00134 {
00135         LIST_REMOVE
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/bsd-list_8h.htm#a6>(this,
entry);
00136 }
00137

*A destructor is created.*


00139 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea5>
Node::command <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#Nodea5>(int
argc, const char*const* argv)


00140 {
00141         Tcl& tcl = Tcl::instance();
00142         if (argc == 2) {
00143 #ifdef HAVE_STL
00144                 // Mods for Nix-Vector Routing


00145                 if(strcmp(argv[1], "populate-objects") == 0) {
00146                         if (nixnode_) {

00147                                 nixnode_->PopulateObjects();
00148                         }
00149                         return TCL_OK;
00150                 }
00151                 // End mods for Nix-Vector routing


00152 #endif // HAVE_STL
00153                 if(strcmp(argv[1], "address?") == 0) {

00154                         tcl.resultf("%d", address_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep6>);
00155                         return TCL_OK;


00156                 }
00157
00158
00159         } else if (argc == 3) {
00160 #ifdef HAVE_STL

00161                 // Mods for Nix-Vector Routing
00162                 if (strcmp(argv[1], "get-nix-vector") == 0) {

00163                         if (nixnode_) {
00164                                 nixnode_->GetNixVector(atol(argv[2]));
00165                         }
00166                         return TCL_OK;


00167                 }
00168 #endif //HAVE_STL
00169                 if (strcmp(argv[1], "set-neighbor") == 0) {

00170 #ifdef HAVE_STL
00171                         if (nixnode_) {
00172                                 nixnode_->AddAdj(atol(argv[2]));

00173                         }
00174 #endif //HAVE_STL
00175                         return(TCL_OK);
00176                 }

00177                 if (strcmp(argv[1], "addr") == 0) {
00178                         address_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep6>
= Address::instance
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classAddress.htm#Addresse0>().str2addr
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classAddress.htm#Addressa7>(argv[2]);


00179 #ifdef HAVE_STL
00180                         if (nixnode_) {
00181                                 nixnode_->Id(address_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep6>);


00182                         }
00183 #endif //HAVE_STL
00184                         return TCL_OK;
00185                 // End mods for Nix-Vector routing


00186                 } else if (strcmp(argv[1], "nodeid") == 0) {
00187                         nodeid_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep7>
= atoi(argv[2]);


00188                         return TCL_OK;
00189                 } else if(strcmp(argv[1], "addlinkhead") == 0) {

00190                         LinkHead
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classLinkHead.htm>* slhp
= (LinkHead <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classLinkHead.htm>*)TclObject::lookup(argv[2]);


00191                         if (slhp == 0)
00192                                 return TCL_ERROR;
00193                         slhp->insertlink
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classLinkHead.htm#SatLinkHeada10>(&linklisthead_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep10>);


00194                         return TCL_OK;
00195                 } else if (strcmp(argv[1], "addenergymodel") == 0) {

00196                         energy_model_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep12>=(EnergyModel
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classEnergyModel.htm>*)TclObject::lookup(argv[2]);


00197                         if(!energy_model_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep12>)
00198                                 return TCL_ERROR;


00199                         return TCL_OK;
00200                 } else if (strcmp(argv[1], "namattach") == 0) {

00201                         int mode;
00202                         namChan_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep8>
= Tcl_GetChannel(tcl.interp(), (char*)argv[2],


00203                                                   &mode);
00204                         if (namChan_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep8>
== 0) {


00205                                 tcl.resultf("node: can't attach
%s", argv[2]);
00206                                 return (TCL_ERROR);

00207                         }
00208                         return (TCL_OK);
00209                 } else if (strcmp(argv[1], "add-neighbor") == 0) {


00210                         Node * node = (Node *)TclObject::lookup(argv[2]);
00211                         if (node == 0) {
00212                                 tcl.resultf("Invalid node %s", argv[2]);


00213                                  return (TCL_ERROR);
00214                         }
00215                         addNeighbor
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea15>(node);


00216                         return TCL_OK;
00217                 }
00218         }
00219         return ParentNode::command(argc,argv);
00220 }


*Command is the most important function in ns2. All the commands that we
usually declare in our tcl script are first come to Command and then string
matching is done. After that particular function is called. For instance,
when we delcare
*

*add-neighbor
*

* in our tcl script, then the command pointer comes to this function, and
*

* addNeighbor <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea15>(node);*

* is called to add the neighbors.*

00262 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea15>
void Node::addNeighbor
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea15>(Node
* neighbor) {


00263
00264         neighbor_list_node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structneighbor__list__node.htm>*
nlistItem = (neighbor_list_node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structneighbor__list__node.htm>
*)malloc(sizeof(neighbor_list_node
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structneighbor__list__node.htm>));


00265         nlistItem->nodeid
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structneighbor__list__node.htm#neighbor__list__nodeo0>
= neighbor->nodeid
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea7>();


00266         nlistItem->next
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structneighbor__list__node.htm#neighbor__list__nodeo1>
= neighbor_list_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodeo0>;


00267         neighbor_list_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodeo0>=nlistItem;
00268 }

*This function is used to add neighbors in the neighor list of nodes.*

00314 // A class static method. Return the node instance from the
static node list
00315 <http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodee2>
Node* Node::get_node_by_address
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodee2>
(nsaddr_t id)


00316 {
00317         Node * tnode = nodehead_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/classnodehead__.htm>.lh_first;
00318         for (; tnode; tnode = tnode->nextnode
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodea12>())
{


00319                 if (tnode->address_
<http://www-rp.lip6.fr/ns-doc/ns226-doc/html/structNode.htm#SatNodep6>
== id ) {
00320                         return (tnode);


00321                 }
00322         }
00323         return NULL;
00324 }

*As mentioned in the code, this function returns the node instance from the
static node list.*


Regards
-- 
Mubashir Husain Rehmani

Lip6, UPMC, Paris, France
ASAP, INRIA, France

www-npa.lip6.fr/~rehmani <http://www-npa.lip6.fr/%7Erehmani>


More information about the Ns-users mailing list