Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

NS2 Shadow Object Construction

1,290 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

NS2 Shadow Object Construction

  1. 1. NS2: Shadow ObjectNS2: Shadow Object ConstructionConstruction by Teerawat Issariyakul http://www.ns2ultimate.com October 2010 http://www.ns2ultimate.com 1
  2. 2. OutlineOutline Introduction Creating an OTcl object Shadow Object Construction Process Summary http://www.ns2ultimate.com 2
  3. 3. IntroductionIntroduction This is a series on how NS2 binds C++ and OTcl together.This is the second topic of the series: 1.WhyTwo Languages? 2. Binding C++ and OTcl classes 3. Variable binding 4. OTcl command: Invoking C++ statements from the OTcl domain 5. Eval and result: Invoking OTcl statements from the C++ domain 6. Object binding and object construction process. http://www.ns2ultimate.com 3
  4. 4. MotivationMotivation http://www.ns2ultimate.com 4  NS2 consists of 2 languages: ◦ OTcl: A user interface language used to create objects ◦ C++: A language defining the created objects’ attributes and behaviors  Objects in NS2 ◦ A user creates an object from the OTcl domain ◦ NS2 automatically create a “shadow” object in the C++ domain  From the user perspective, these two objects are the same.
  5. 5. Suppose we have two bound classes We are going to learn how EXACTLY NS2 auto. create a shadow object. What We Would Like to Do?What We Would Like to Do? http://www.ns2ultimate.com 5 TCPAgent C++ Agent/TCP OTcl bound user obj create c_obj Auto. construction by NS2 shadow object
  6. 6. OutlineOutline Introduction Creating an OTcl object Shadow Object Construction Process Summary http://www.ns2ultimate.com 6
  7. 7. Creating an OTcl ObjectCreating an OTcl Object http://www.ns2ultimate.com 7  An OTcl is created using the following syntax <className> create <var> [<args>] which creates an object of class <className> and store the created object in the variable <var>. Note, [<args>]is optional input argument for object construction.  There are two key steps when invoking new ◦ Executing instproc “alloc” to allocate memory to hold the object ◦ Executing instproc “init” (i.e., the constructor) to initialize class variables
  8. 8. Instproc “Instproc “initinit”” http://www.ns2ultimate.com 8 It initializes class variables It is referred to as “the constructor” OOP structure ◦ Call the constructor of the base class first ◦ Use the instproc next
  9. 9. Instproc “Instproc “nextnext”” http://www.ns2ultimate.com 9 Instproc “next” executes the instproc with the same name located in the parent class. E.g., Class Agent/TCP derives from class Agent When we see the statement “eval $self next” executes to “Agent init {}”. Agent/TCP instproc init {} { eval $self next … }
  10. 10. OutlineOutline Introduction Creating an OTcl object Shadow Object Construction Process Summary http://www.ns2ultimate.com 10
  11. 11. Shadow Object ConstructionShadow Object Construction The process consists of two part ◦ Part I: OTcl—Creating an object using new ◦ Part II: C++—Shadow object construction http://www.ns2ultimate.com 11 Part I: OTcl Part II: C++
  12. 12. Shadow Object ConstructionShadow Object Construction Let’s use the following example ◦ OTcl class: Agent/TCP ◦ C++ class: TcpAgent In OTcl, create an object using new Agent/TCP In C++, create a shadow TcpAgent object http://www.ns2ultimate.com 12
  13. 13. Part I: OTclPart I: OTcl Main steps of Part I: The OTcl statement new <classname> [<args>] The OTcl statement $classname create $o $args The instproc alloc of the OTcl class <classname> The instproc init of the OTcl class <classname> The instproc init of the OTcl class SplitObject The OTcl statement $self create-shadow $args http://www.ns2ultimate.com 13
  14. 14. Part I: OTclPart I: OTcl 1. The OTcl statement new <classname> [<args>] http://www.ns2ultimate.com 14 proc new { className args } { set o [SplitObject getid] if [catch "$className create $o $args" msg] { ... } return $o } step 2
  15. 15. Part I: OTclPart I: OTcl 2.The OTcl statement $classname create $o $args Again, the instproc create invokes two instprocs in Steps 3 and 4. 3.The instproc alloc of the OTcl class <classname>: Memory allocation http://www.ns2ultimate.com 15
  16. 16. Part I: OTclPart I: OTcl 4.The instproc init of the OTcl class <classname> A general structure of the the instproc init is to invoke the instproc init of the base class until reaching the top level class, e.g., The top level class is class SplitObject  Step 5 http://www.ns2ultimate.com 16 Agent/TCP instproc init {} { eval $self next … }
  17. 17. Part I: OTclPart I: OTcl 5. The instproc init of the OTcl class SplitObject 6. The OTcl statement $self create-shadow $args:  This executes, for example, instproc create- shadow of class Agent/TCP http://www.ns2ultimate.com 17 SplitObject instproc init args { $self next if [catch "$self create-shadow $args"] { error "__FAILED_SHADOW_OBJECT_" "" } }
  18. 18. Part II: C++Part II: C++ http://www.ns2ultimate.com 18 Part I completes with the OTcl command create-shadow. In Part II, we are moving to the C++ domain. Part I: OTcl Part II: C++
  19. 19. Part II: C++Part II: C++ http://www.ns2ultimate.com 19 Let’s use an example of class TcpAgent Main steps of Part II: 1. Step 6 in Part I 2. The C++ function create-shadow(...) of class TclClass 3. The C++ function create(...) of class TcpClass 4. The C++ statement new TcpAgent()
  20. 20. Part II: C++Part II: C++ http://www.ns2ultimate.com 20 1. Step 6 in Part I: The OTcl command create-shadow is bound to The C++ function create-shadow(...) of class TclClass 2.The C++ function create-shadow(...) of class TclClass 3.The C++ function create(...) of class TcpClass
  21. 21. Part II: C++Part II: C++ http://www.ns2ultimate.com 21 3.The C++ function create(...) of class TcpClass 4. The C++ statement new TcpAgent(): Create a shadow object static class TcpClass : public TclClass { public: TcpClass() : TclClass("Agent/TCP") {} TclObject* create(int , const char*const*) { return (new TcpAgent()); } } class_tcp; Step 4
  22. 22. OutlineOutline Introduction Creating an OTcl object Shadow Object Construction Process Summary http://www.ns2ultimate.com 22
  23. 23. SummarySummary In NS2, a user creates an object from the OTcl domain using a global procedure “new” NS2 automatically creates a so-called shadow object in the C++ domain. The shadow object construction consists of 2 parts: ◦ Part 1: Execute OTcl constructors ◦ Part II: Create a shadow object in the C++ domain. http://www.ns2ultimate.com 23
  24. 24. For more information aboutFor more information about NS 2NS 2 Please see this book from Springer T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009 http://www.ns2ultimate.com 24

×