20100712-OTcl Command -- Getting Started

4,488 views

Published on

Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

20100712-OTcl Command -- Getting Started

  1. 1. NS2: OTcl Command – Getting Started by Teerawat Issariyakul http://www.ns2ultimate.com July 2010 http://www.ns2ultimate.com
  2. 2. Outline <ul><li>Introduction </li></ul><ul><li>Motivation </li></ul><ul><li>What we would like to do </li></ul><ul><li>OTcl Command </li></ul>http://www.ns2ultimate.com
  3. 3. Introduction <ul><li>This is a series on how NS2 binds C++ and OTcl together. This is the second topic of the series: </li></ul><ul><li>Why Two Languages? </li></ul><ul><li>Binding C++ and OTcl classes </li></ul><ul><li>Variable binding </li></ul><ul><li>OTcl command: Invoking C++ statements from the OTcl domain </li></ul><ul><li>Eval and result: Invoking OTcl statements from the C++ domain </li></ul><ul><li>Object binding and object construction process. </li></ul>http://www.ns2ultimate.com
  4. 4. Motivation <ul><li>OTcl is the place where users do the programming </li></ul><ul><li>Sometimes, users need to call C++ function </li></ul>http://www.ns2ultimate.com MyObject programmer C++ MyOTclObject OTcl binding function binding class name delay_ show_delay(){ show “delay_” on the screen } show-delay
  5. 5. What we would like to do http://www.ns2ultimate.com <ul><li>C++: </li></ul><ul><li>class = MyObject (see above) </li></ul><ul><li>variable = delay_ </li></ul><ul><li>function: show_delay() </li></ul><ul><li>OTcl: </li></ul><ul><li>class = MyOTclObject (see above) </li></ul><ul><li>variable = none </li></ul><ul><li>OTcl command: show-delay </li></ul><ul><li>Binding class name [see here and here ] </li></ul>MyObject C++ MyOTclObject OTcl binding function binding class name (see above) delay_ show_delay(){ show “delay_” on the screen } show-delay
  6. 6. OTcl Command: Introduction <ul><li>Can be invoked from the OTcl domain ( show-delay ) </li></ul><ul><li>Is bound to an OTcl object ( object ) </li></ul><ul><li>Execute C++ domain ( show_delay() ) </li></ul>http://www.ns2ultimate.com
  7. 7. OTcl Command: C++ files <ul><li>In the following, I will use files in the previous post: otcl.cc, otcl.h, otcl.tcl </li></ul><ul><li>Add a C++ variable delay_ to class MyObject </li></ul>http://www.ns2ultimate.com class MyObject : public TclObject { public: MyObject(); virtual ~MyObject(){}; protected: int count_; double delay_; double speed_; double virtual_time_; int is_running_; int command(int argc, const char*const* argv); }; otcl.h
  8. 8. OTcl Command: C++ files <ul><li>In the following, I will use files in the previous post: otcl.cc, otcl.h, otcl.tcl </li></ul><ul><li>Add a C++ variable delay_ to class MyObject </li></ul><ul><li>Constructor: Set the initial value of delay_ to be 7.7 </li></ul><ul><li>Do not bind the variable </li></ul><ul><li>Next step to use OTcl command to show the value of the variable delay_ on the screen </li></ul>http://www.ns2ultimate.com MyObject::MyObject() { delay_ = 7.7; } otcl.cc
  9. 9. OTcl Command: Definition <ul><li>Define within C++ class (e.g. MyObject ) </li></ul><ul><li>Define by function “ command(…) ” </li></ul><ul><li>Example: Insert the following into the file otcl.cc </li></ul><ul><li>Do not forget to add a C++ variable delay_ to class MyObject </li></ul>http://www.ns2ultimate.com int MyObject::command(int argc, const char*const* argv) { if (argc==2) { if (strcmp(argv[1], &quot;show-delay&quot;) == 0) { printf(&quot;Delay is %g &quot;, delay_); return (TCL_OK); } } return TclObject::command(argc, argv); }; OTcl command name what this OTcl command do
  10. 10. Summary of Key Steps <ul><li>Use files otcl.cc and otcl.h in the previous post </li></ul><ul><li>Add the details as stated earlier </li></ul><ul><li>run “ make ” to incorporate C++ changes in to NS2 [see detail here ] </li></ul><ul><li>Create file otcl.tcl for testing </li></ul><ul><ul><li>4.1 Create an OTcl object object </li></ul></ul><ul><ul><li>4.2 Invoke the OTcl command “show-delay” associated with object </li></ul></ul><ul><ul><li>4.4 Change the C++ constructor: set delay_ to be 9.9 </li></ul></ul><ul><ul><li>4.5 Repeat 4.2 and 4.3 </li></ul></ul>http://www.ns2ultimate.com
  11. 11. OTcl Command: Invocation <ul><li>File otcl.tcl </li></ul><ul><li>Run the file otcl.tcl </li></ul>http://www.ns2ultimate.com set ns [new Simulator] set obj [new MyOTclObject ] $obj show-delay
  12. 12. OTcl Command: Invocation <ul><li>Modify the constructor </li></ul><ul><li>Run the file otcl.tcl again </li></ul>http://www.ns2ultimate.com MyObject::MyObject() { delay_ = 9.9; }
  13. 13. For more information about NS 2 Please see this book from Springer T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009 http://www.ns2ultimate.com

×