《OPNET Worker99培训在OPNET中使用C++ C语言编程.ppt》由会员分享,可在线阅读,更多相关《OPNET Worker99培训在OPNET中使用C++ C语言编程.ppt(40页珍藏版)》请在三一办公上搜索。
1、Programming in C+in OPNET,Jerome Plun,Agenda,File type suffixesReview of building process models in CBuilding process models with C+Basic SimulationUse of Variables based on Object ClassesExceptions and DebuggingWriting C+pipeline stagesUsing external C+filesGenerating C+EMA programC+limitations,OPN
2、ET File Type Suffixes,Process ModelPipeline StageExternal CodeEMA,Model.pr.m,C.pr.c.ps.c.ex.c.em.c,C+.pr.cpp.ps.cpp.ex.cpp.em.cpp,Object*.pr.o.ps.o.ex.o.em.o,*Environment attribute arch_suffix set to false,Compiling C-based Process Models,Use op_mko program(invoked by in Process Editor)Example:op_mk
3、o-m sink-type prGenerate intermediary C file and resulting object fileSpecific C compiler invoked based on comp_progAdditional compiler options specified with comp_flags,op_mko,Host C compiler,Compiling C+Process Models,Use op_mko programExample:op_mko-m HLA_RTI_Gateway-type prGenerate intermediary
4、C+file and resulting object fileSpecific C+compiler invoked based on comp_prog_cppAdditional compiler options specified with comp_flags_cpp,op_mko,Host C+compiler,C or C+Compilation,C/C+distinction based on tokenOPC_COMPILE_CPP in Header Block of process model:,C+Compilation Environment Variables,So
5、laris with Suns CC compiler:comp_prog_cpp:comp_unix_cppUnix with g+:comp_prog_cpp:comp_g+Windows NT with Visual C+:comp_prog_cpp:comp_msvccomp_flags_cpp:/GX,Linking Simulations,Dynamic Simulationop_runsim programbind_shobj_prog:linking scriptbind_shobj_flags:additional linking flagsbind_shobj_libs:a
6、dditional linking libraries inStatic Simulationop_mksim programbind_static_prog:linking scriptbind_static_flags:additional linking flagsbind_static_libs:additional linking libraries,Linking Simulations with C+Process Models,Require appropriate C+support librariesSolaris with Sun CC:Dynamic Simulatio
7、n:bind_shobj_prog:bind_so_CCStatic Simulation:bind_static_prog:bind_CC,Linking Simulations with C+Process Models(cont.),Windows NT with Visual C+:Dynamic Simulation:bind_shobj_prog:bind_cppso_msvcStatic Simulation:bind_static_prog:bind_cpp_msvcFor more complete details,refer toExternal Interfaces ma
8、nualProgram Descriptions chapterDevelopment Environment Attributes section,C+Code in Process Models,C+code allowed inState Enter and Exit ExecutivesState VariablesTemporary VariablesHeader BlockFunction BlockDiagnostic BlockTermination Block,Lab 1:Goals,Create Process Model containing C+codeCompile
9、Process ModelBuild Network Simulation with Process ModelRun Simulation,Lab 1:Create Process Model,In a new Process EditorCreate one stateEnter the following in the Enter Executive of the statecout Save the Process Model as show_idCompile Process Model(),Labs 1:Create Node Model,In a new Node EditorA
10、dd one processor()Set the process model to show_idEnable the begsim intrpt attributeSave the Node Model as show_id,Lab 1:Run Network Simulation,Open the cpp_programming ProjectMake sure the lab1_show_id scenario is visibleit contains 3 show_id nodesOpen that project and run the simulation()then clic
11、k on A Command Prompt window should appear with the output of the simulation,Lab 1:Simulation Output,The end of the output should contain something like,|-|Initializing results|-|-|Progress:Time(0 sec.),Events(0)|Speed:Average(0 events/sec.),Current(0 events/sec.)|Time:Elapsed(0 sec.)|-|Process ID=4
12、Process ID=5Process ID=6|-|Simulation Completed-Collating Results.|Simulated:Time(1 sec.),Events(3)|Time:Elapsed(1sec.)|-|,State Variables,Created when the corresponding process is createdInitialization phase for root processesDuring simulation for dynamically created processesConstructors can call
13、KPs(result is well-defined)Root processes never destroyedDynamically created processes potentially destroyedCorresponding state variable(s)destroyedTermination Block executed before variables destructorsDestructors can call KPs,Temporary Variables,Created when invokingProcess ModelDiagnostic BlockTe
14、rmination BlockDestroyed when exitingProcess ModelDiagnostic BlockTermination BlockConstructor and Destructor can call KPs,Invocation Start,Invocation End,Global variables,Any variable declared in Header or Function BlocksCreated when Process Model is loaded by the operating system before simulation
15、Created before Simulation packages are initializedStatically allocated variables:Constructors must not use KPs,Global Variables:Example of Error,Example of dangerous use of KPs in constructors,class Init_Error public:Init_Error()mem_handle=op_prg_pmo_define(Data,100,10);private:Pmohandle mem_handle;
16、Init_Error will_cause_error;Init_Error*will_not_cause_error;,Global Variables:Example of Error(cont.),The previous header block results inWith error from constructor,Variable Creation/Destruction,Created,Destroyed,Variable,Object code loaded in memory,Program terminated,Global,Process instance creat
17、ed,Process instance destroyed,State,Process invocation started,Process invocation ended,Temporary,Lab 2-Goals,Review creation and destruction of variablesProvided:Process Model show_varsClass Show_Status(defined in Header Block)Prints a message when instance createdPrints a message when instance des
18、troyedShow_Status instances asGlobal Variable in Header Block(header_var)Global Variable in Function Block(function_var)State Variable(state_var)Temporary Variable(tmp_var),Lab 2-Simulation,States with printout statements in Enter and Exit ExecutivesBegin simulation interrupt enabled for process mod
19、elSelf-interrupt scheduled in Enter Executive of initIn the cpp_programming project,switch to scenario lab2_show_vars and run itCan you guess how many constructions and destructions will occur?,Lab 2:Answers,5 variable creations:header,function,state,temp twice4 variable destructions:header,function
20、,temp twice,Lab 2:Result of Execution,Object code loading by Operating System,|-|Loading scenario model library|-*Variable header_var created*Variable function_var created*,Begin Simulation Interrupt,*Variable tmp_var created*Enter Executive of unforced state init*Variable tmp_var destroyed*,Creatio
21、n of process state,|-|Creating node contents.|-*Variable state_var created*,Lab 2:Result of Execution(cont.),Self-interrupt,*Variable tmp_var created*Exit Executive of unforced state initEnter Executive of forced state middleExit Executive of forced state middleEnter Executive of unforced state last
22、*Variable tmp_var destroyed*,|Simulation Completed-Collating Results.|Simulated:Time(1 sec.),Events(1).|Time:Elapsed(1 sec.)|-Press to continue.*Variable function_var destroyed*Variable header_var destroyed*,Termination of Simulation,Process state never destroyed,C+Exceptions,KPs do not throw any ex
23、ceptionsExceptions unhandled by process model abort the simulationExample:Process Model bad_exceptionEnter Executive of init state isthrow this exception is not caught;Running a simulation with this Process Model generates,*Time:.*Program:op_runsim(Version 6.0.L PL5)*Error:Unhandled C+exception in p
24、rocess model(bad_exception),Unhandled C+Exception,m3_vuerr output,*Time:.*Program:op_runsim(Version 6.0.L PL5)*Package:*Function:bad_exception()init enter execs*Error:Unhandled C+exception in process model(bad_exception)*Function call stack:(builds down)-Call Block Count Line#Function-0)1 118 main(a
25、rgc,argv)1)1 469 sim_main(prog_name,argc,argv,dynamic_sim,def_net_name,num_procs,proc_set_ptr,num_pk_meths,pk_set_ptr,num_pk_fd_meths,pk_fd_set_ptr)2)1 884 sim_ev_loop()3)1 476 sim_obj_qps_intrpt(simev_ptr)4)1 27 bad_exception()init enter execs 5)1 346 Vos_Error_Print(level,package,error0,error1,err
26、or2)-,Source-level Debugging,Most powerful tool for debugging C/C+codeRequires knowledge of code layoutType structuresVariable namesFunction namesUseful to understand C+code generation,Source-level Debugging:Code Generation,Example:Process Model tcp_connProcess data stored as C+class tcp_conn_stateS
27、tate variables data members of tcp_conn_stateTCP_Connection connection;double duration;Useful methods of tcp_conn_stateState-Transition:tcp_conn()Diagnostic Block:tcp_conn_diag()Termination Block:tcp_conn_state(),Source-level Debugging:Data Access,In code,op_sv_ptr represents the current state,State
28、 Variables,OPNET Internals,Writing C+Pipeline Stage,Simulation Kernel expectsSpecific prototype for each stageInterface function named as base name of fileC linkage of interface functionExample:transmission delay for point-to-point pipelineFile tx_delay.ps.cpp-Interface function tx_delayC linkage wi
29、th extern“C”around prototype,/Prototypeextern“C”void tx_delay(Packet*pkptr);void tx_delay(Packet*pkptr).,Other functions in pipeline stage dont need C linkage,Compiling Pipeline Stages,External Files,EMA,Use op_mko program for Pipeline Stages and External FilesPipeline Stage:op_mko-m dra_error-type
30、psExternal File:op_mko-m link_delay-type exUse m3_mkema program for EMAm3_mkema-m rnode.c file compiled using comp_prog and comp_flags.cpp file compiled using comp_prog_cpp and comp_flags_cpp.o object file generated,Conflicting File Extensions,Exactly one file with extension.c or.cpp must be foundCo
31、mpilation fails if both extensions foundop_mko-m dra_error-type ps will report if dra_error.ps.c and dra_error.ps.cpp are foundSimilar errors for external files and EMA,*Error:Pipeline stage model compilation failed Both C and C+source code files found,Linking C/C+EMA Programs,m3_mkema program links
32、.em.o file with OPNET libraries.em.x executable produced usingbind_static_progbind_static_flagsbind_static_libs,C+Limitations,C+compilers vary greatlySupport different C+featuresg+2.7.2:no hierarchical exceptionsVisualC+4.2:no exception signature for templatesGenerate different symbols in object fil
33、es,C+Symbols,C+function symbol representation of function prototype“Mangled”name of a functionNo standard definition of name“mangling”Different compilers generate different mangled namesint square(int v)return v*v;Solaris CC 4.2:_0FGsquareiSolaris g+2.7.2:square_FiFunction square in an external file
34、 compiled with CC cant be used by a process model compiled with g+,Distributing C+OPNET Components,Components in source form(.m,.cpp)Specify which compilers are known to compile without errorsComponents in binary form(.o)Specify which compiler was used to generate binary,Summary,Support for use of C+inProcess ModelsPipeline StagesExternal Code FilesEMA programsCode and binaries usually not compatible on different C+compilers on same platform,