Node creation or apperance at run time in Omnet (INET) - c++

I need to create a node at run time, with similar parameters as the other nodes. For that I am creating a dynamic node in ned file as:-
host_send4: meshnode {
parameters:
#dynamic;
#display("p=1000,535;r=200,green;i=device/smallrouter");
}
To implement this node in C++ file, I add this code:-
cModuleType *meshnode1 = cModuleType::get("inet.networklayer.manetrouting.PASER.meshnode");
cModule *mod = meshnode1->createScheduleInit("host_send4", this);
cDisplayString& dispstr = mod->getDisplayString();
dispstr.parse("p=1000,535;r=200,green;i=device/smallrouter");
mod->buildInside();
mod->scheduleStart(simTime()+5*beaconInterval);
But I am not able to build it properly. I think I am in need of any example on this. Can anybody help me to point out an example in INETMANET of mixim or any other oment framework, where this functionality is already implemented.
Thanks for your help.
I have also though of creating a node statically, which would appear in simulation at later point of time. Is it possible and is there any example with runtime appearance and disappearance of node in INET or other OMNET framework.

The OMNeT++ User Manual has a section dedicated to this. According to this you don't need buildInside() and scheduleStart() when using createScheduleInit().
An example how this is performed can be seen in the Veins framework - more precisely in the TraCIScenarioManager. The important lines for you are probably:
cModule* parentmod = getParentModule();
if (!parentmod) error("Parent Module not found");
cModuleType* nodeType = cModuleType::get(type.c_str());
if (!nodeType) error("Module Type \"%s\" not found", type.c_str());
cModule* mod = nodeType->create(name.c_str(), parentmod, nodeVectorIndex, nodeVectorIndex);
mod->finalizeParameters();
mod->getDisplayString().parse(displayString.c_str());
mod->buildInside();
mod->scheduleStart(simTime() + updateInterval);

Related

Visualization of dynamically created modules

I am building a dynamic multi agent simulation in OMNeT and for this I have to create new modules at runtime. The module creation is working, however, the modules created at runtime are not appearing in the 3D visualization.
module "node" is created sucessfully
Does anyone know how to make the module appear in the visualization? Do I have to update the visualization module?
omnet.ini:
[General]
network = AgentNetwork
*.visualizer.osgVisualizer.typename = "IntegratedOsgVisualizer"
*.visualizer.*.mobilityVisualizer.animationSpeed = 1
*.visualizer.osgVisualizer.sceneVisualizer.typename = "SceneOsgEarthVisualizer"
*.visualizer.osgVisualizer.sceneVisualizer.mapFile = "hamburg.earth"
AgentSpawner:
void AgentSpawner::initialize()
{
cMessage *timer = new cMessage("timer");
scheduleAt(1.0, timer);
}
void AgentSpawner::handleMessage(cMessage *msg)
{
cModuleType *moduleType = cModuleType::get("simulations.Agent");
cModule *module = moduleType->create("node", getParentModule());
// set up parameters and gate sizes before we set up its submodules
module->par("osgModel") = "3d/glider.osgb.(20).scale.0,0,180.rot";
module->getDisplayString().parse("p=200,100;i=misc/aircraft");
module->finalizeParameters();
// create internals, and schedule it
module->buildInside();
module->callInitialize();
module->scheduleStart(simTime()+5.0);
}
The OSG visualization info is maintained totally separately from the actual simulation model module object (that's because the visualization must be ALWAYS optional in the simulation, so make sure your simulation builds fine with OSG totally turned off). This means that an entirely different data structure is built during initialization time from the existing network nodes. As this is done only once during the initialization, dynamically created modules will not have their visualization counterpart data structure.
The code which created the corresponding objects is here.
The solution would be to look up the NetworkNodeOsgVisualizer module in your AgentSpawner code then create and add the corresponding data structures (NetworkNodeOsgVisualization objects). The needed methods (create and add) are there, but sadly they are protected, so you many need to modify the INET code and make them public to be able to call them.

Eclipse CDT: Modify and save AST

I'm exploring ways to alter the AST of a C/C++ code (e.g., rename a node, add a new variable) and apply these changes to the source file.
I did a lot of reading here in SO and in Eclipse forums. However I didn't find a minimal working example.
It seems that the correct way to make changes in an AST is by using the ASTRewrite class.
A similar question was asked in SO a few months ago, but it is still pending.
Here is where I'm stuck at the moment:
//get the factory
INodeFactory nodeFactory = myAST.getASTNodeFactory();
//create a new function declarator
IASTNode n = nodeFactory.newFunctionDeclarator(nodeFactory.newName("testMe"));
//get the rewriter
ASTRewrite rewriter = ASTRewrite.create(mainAST);
//replace node with n, node is not null
rewriter.replace(node, n, null);
//make the changes
Change c = rewriter.rewriteAST();
c.perform(new NullProgressMonitor());
When I run this code snippet, I get a
java.lang.NoClassDefFoundError: org/eclipse/ltk/core/refactoring/Change
Any hints are appreciated.

xerces_3_1 adoptNode() method returns NULL

i'm currently working with xerces 3.1 in visual studio 2010.
I've written this (very simple) piece of code:
XMLPlatformUtils::Initialize();
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(L"XML 1.0");
DOMDocument* doc1 = impl->createDocument(L"nsURI", L"abc:root1", 0);
DOMDocument* doc2 = impl->createDocument(0, L"root2", 0);
DOMElement* root1 = doc1->getDocumentElement();
DOMElement* root2 = doc2->getDocumentElement();
DOMElement* el1 = doc1->createElement(L"el1");
root1->appendChild(el1);
DOMNode* tmpNode = doc2->adoptNode(el1); //tmpNode is null after this line
root2->appendChild(tmpNode);
doc1->release();
doc2->release();
xercesc::XMLPlatformUtils::Terminate();
The problem is, the adoptNode(...) method will always return a null-pointer no matter what. I really don't understand what's going on here, please help me!
PS: I know i could use the importNode(...) method and remove and release the old node from the old document, but i was hoping there was a way to fix my problem with adoptNode(...)!
The xerces api states the following for adoptNode(DOMNode* source):
Changes the ownerDocument of a node, its children, as well as the attached attribute nodes if there are any.
After some research i took a look at the implementation of adoptNode in xerces 3.1 and the sad truth is that it's not possible. Quoting the sourcecode:
if(sourceNode->getOwnerDocument()!=this)
{
// cannot take ownership of a node created by another document, as it comes from its memory pool
// and would be delete when the original document is deleted
return 0;
}
EDIT:
There is a workaround for this method but it requires some knowledge of the DOM-Implementation (especially when using UserData). You can import the node with importNode(...) and delete the other node out of the old document.
The old nodes should be released in order to not waste memory!
If you've got userdata attached to the old nodes, the new document has to have some UserDataHandler which adopts the userdata from the old node to the new node!
Please note that possible references on the old nodes do not point onto the new nodes now. They'll have to be changed manually (or with some UserDataHandler workaround)

Error - Cannot access display string yet for new node creation in Omnet

I am trying to create a node at run time in my module in Omnet. I am able to create it with this code and its working fine.
cModule* parentmod = getParentModule();
cModule* grantParentMod = parentmod->getParentModule();
cModule* grantParentMod1 = grantParentMod->getParentModule();
// To check if the module is already created
for (cSubModIterator iter(*grantParentMod1); !iter.end(); iter++)
{
EV << iter()->getFullName()<<endl;
if (iter()->getFullName() == "host_send4")
return;
}
cModuleType *meshnode1 = cModuleType::get("inet.networklayer.manetrouting.PASER.meshnode");
cModule *mod = meshnode1->create("host_send4", grantParentMod1);
cDisplayString& dispstr = getDisplayString();
dispstr.parse("p=1000,535;r=200,green");
mod->finalizeParameters();
mod->buildInside();
mod->scheduleStart(simTime()+2*beaconInterval);
However this module is not generated at desired place in simulation output (the coordinates and the display). I believe the display string created here is not attached to the module and hence I tried to do it by this :-
cDisplayString& dispstr = getDisplayString();
dispstr.parse("p=1000,535;r=200,green");
mod->getDisplayString().set(dispstr);
But with this I encounter following error at run time :- Cannot access display string yet: Parameters not yet set up . I know the problem is in mod->getDisplayString().set(dispstr);
So is there any other way to assign the parameter or am I doing some minor error.
Thanks for this help.
Make sure you are following the module creation procedure as given in the OMNeT++ manual.
If you navigate to the The Detailed Procedure sub-section you will notice a comprehensive list which tells what step should be performed where:
Find the factory object;
Create the module;
Set up its parameters and gate sizes as needed;
Tell the (possibly compound) module to recursively create its internal submodules and connections;
Schedule activation message(s) for the new simple module(s).
Step 3 I believe is the one you are looking for. Little below is given a detailed explanation of what should be done for step 3:
If you want to set up parameter values or gate vector sizes (Step
3.), the code goes between the create() and buildInside() calls:
// create
cModuleType *moduleType = cModuleType::get("foo.nodes.WirelessNode");
cModule *module = moduleType->create("node", this);
// set up parameters and gate sizes before we set up its submodules
module->par("address") = ++lastAddress;
module->finalizeParameters();
module->setGateSize("in", 3);
module->setGateSize("out", 3);
// create internals, and schedule it
module->buildInside();
module->scheduleStart(simTime());
Be aware of the usage of the module->par("<parameter_name>") function.
PS: I was writing my answer, and in meanwhile you answered your own question. This answer can be left there for future reference, if useful.
Well I modified the code as :-
cModuleType *meshnode1 = cModuleType::get("inet.networklayer.manetrouting.PASER.meshnode");
cModule *mod = meshnode1->create("host_send4", grantParentMod1);
mod->finalizeParameters();
std::string displayString = "p=1000,535;r=200,green;i=device/smallrouter";
mod->getDisplayString().parse(displayString.c_str());
mod->buildInside();
mod->scheduleStart(simTime()+2*beaconInterval);
and then its working perfect. According to my understanding, I should add mod->finalizeParameters(); before changing the display setting and display string should be a simple string but not the cDisplayString object.

Add a custom shape to a Maya scene from C++

I'm in the process of creating a custom import plugin for maya. I already wrote some import code and created a custom MPxSurfaceShape class (I'm mainly interested in drawing the surface from within the viewport).
The shape gets crated by a MPxCommand which reads a file from the disk. Now I would like to add this object to my maya scene from within the plugin. But unfortunately I can't find a function that takes a MPxNode/MPxSurfaceShape and adds it to Maya so that it can be displayed.
In all examples I've seen the node is instantiated from within mel. But I want to link this instance a file. Which prevents me from just creating the node and then editing it.
A similar solution might be found in either the apiMeshShape example in the maya plugin folder or here: https://github.com/ADN-DevTech/Maya-Locator/ (also supports the loading of external data).
Here's something I hope will help.
MDagModifier dagMod;
MObject newNode = dagMod.MDGModifier::createNode("Node Name")
dagMod.doIt()
or
MDagModifier dagMod;
MObject newNode = dagMod.MDGModifier::createNode(Node::id)
dagMod.doIt()
From there you have an MObject you can make into other things.
//Dag Node example.
MFnDagNode new_MDagNode(newNode);
//Dependency Node.
MFnDependencyNode new_DependNode(newNode);
The MPxNode also has thisMObject() which will give you the current MObject in the MPxNode.
http://download.autodesk.com/us/maya/2010help/API/class_m_px_node.html#9608c582da0945e792c3f9893661404d
Again I'm not sure I fully understand the question, but I hope this helps.