Create an agent/myagent Process

Source: Internet
Author: User
Tags ping set

A simple example illustrates how TCL maps to C ++. The purpose of a program is to create a new network object class "myagent", which is a subclass of "agent.

Below I paste the code here:

// This is a simple agent just for show
# Include <stdio. h>
# Include <string. h>
# Include "agent. H"

Class myagent: public agent {
Myagent ();
Int command (INT argc, const char * const * argv );
Int my_val1;
Double my_val2;
Void myprivfunc (void );
// ================================================ ==================================
// Assume that the class "myagent" of a new network object is created in C ++, which is derived from the "agent" class,
// Then it can create an OTCL object instance. To implement this function, we must define
// The connected object "myagentclass", derived from tclclass. This join object creates
// You can specify an OTCL object ("agent/myagentotcl" in this example) and create a connection.
// Between the OTCL object and the C ++ object ("myagent"), its instance runs the "Create" member function to specify
// Program
// ================================================ ==========================================
Static class myagentclass: Public tclclass {// This is the ing object class; it is a static class; inherits the tclclass
Myagentclass (): tclclass ("agent/myagentotcl "){}
// Create an OTCL object named "agent/myagentotcl"
Tclobject * Create (INT, const char * const *){
Return (New myagent (); // in this way, the OTCL and C ++ object are connected, which is the Startup Program of the instance.
} Class_my_agent; // an instance is created when the NS is started. class_my_agent is a static variable.

Myagent: myagent (): Agent (pt_udp) {// This function outputs the C ++ class member variables to OTCL
BIND ("my_val1_otcl", & my_val1); // bind the class member variable in otcl to the c ++ member variable. You can use OTCL to control the C ++ member variable, it is recommended to set the default value in the ns-lib.tcl, otherwise there is warning
BIND ("my_val2_otcl", & my_val2 );

Int myagent: Command (INT argc, const char * const * argv) {// output the C ++ object control command to OTCL, which defines a command interpreter
If (argc = 2 ){
If (strcmp (argv [1], "Call-my-priv-func") = 0 ){
Myprivfunc ();
Return (tcl_ OK );
Return (AGENT: Command (argc, argv); // If no result is found, the command function of the parent and parent classes is called.

Void myagent: myprivfunc (void) {// execute the OTCL command in the C ++ object, which is complementary to the above function
TCL & TCL = TCL: instance (); // generate a Tcl instance and PASS commands through the function. For example, the following two functions are called:
TCL. eval ("puts \" Message from myprivfunc \"");
TCL. evalf ("puts \" my_val1 = % d \ "", my_val1 );
TCL. evalf ("puts \" my_val2 = % d \ "", my_val2 );

1. Create a New file and place it in ~ /Ns2.29 directory.

2. Open ~ /Tcl/lib/tcl-default.tcl file, assign default values to member variables:

# Agent set seqno _ 0 now is gone
# Agent set class _ 0 now is gone

Agent/myagentotcl set my_val1_otcl 1
Agent/myagentotcl set my_val2_otcl 0.0001

Agent/ping set packetsize _ 64

3. Add ex-linkage.o \ In makefile. In \

4. Final./configure make

Attach the Tcl test script:

# Create myagent
Set myagent [new agent/myagentotcl]

$ Myagent set my_val1_otcl 2
$ Myagent set my_val2_otcl 3.14

$ Myagent call-my-priv-func


To sum up, the Tcl ing mainly consists of five steps.

    1. Create a network object class

    2. Create object ing class public tclclass

    3. Bind C ++ member variables and OTCL Variables)

    4. Control of C ++ member variables (command or member function)

    5. Compile the. CC file (make)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.