ScenarioManager Class Reference

#include <ScenarioManager.h>

List of all members.


Detailed Description

Scenario Manager (experimental) which executes a script specified in XML. ScenarioManager has a few built-in commands such as <set-param>, <set-channel-attr>, etc, and can pass commands to modules that implement the IScriptable interface. The <at> built-in command can be used to group commands to be carried out at the same simulation time.

See NED file for details.

See also:
IScriptable
Author:
Andras Varga

Public Member Functions

 ScenarioManager ()

Protected Member Functions

const char * getRequiredAttribute (cXMLElement *node, const char *attr)
virtual cModule * getRequiredModule (cXMLElement *node, const char *attr)
virtual cGate * getRequiredGate (cXMLElement *node, const char *modattr, const char *gateattr)
virtual void processCommand (cXMLElement *node)
virtual void processAtCommand (cXMLElement *node)
virtual void processSetParamCommand (cXMLElement *node)
virtual void processSetChannelAttrCommand (cXMLElement *node)
virtual void processCreateModuleCommand (cXMLElement *node)
virtual void processDeleteModuleCommand (cXMLElement *node)
virtual void processConnectCommand (cXMLElement *node)
virtual void processDisconnectCommand (cXMLElement *node)
virtual void processModuleSpecificCommand (cXMLElement *node)
virtual void initialize ()
virtual void handleMessage (cMessage *msg)
virtual void updateDisplayString ()

Protected Attributes

int numChanges
int numDone

Constructor & Destructor Documentation

ScenarioManager::ScenarioManager (  )  [inline]

00065 {}


Member Function Documentation

const char * ScenarioManager::getRequiredAttribute ( cXMLElement *  node,
const char *  attr 
) [protected]

Referenced by getRequiredGate(), getRequiredModule(), processSetChannelAttrCommand(), and processSetParamCommand().

00100 {
00101     const char *s = node->getAttribute(attr);
00102     if (!s)
00103         error("required attribute %s of <%s> missing at %s",
00104               attr, node->getTagName(), node->getSourceLocation());
00105     return s;
00106 }

cModule * ScenarioManager::getRequiredModule ( cXMLElement *  node,
const char *  attr 
) [protected, virtual]

Referenced by getRequiredGate(), processModuleSpecificCommand(), and processSetParamCommand().

00109 {
00110     const char *moduleAttr = getRequiredAttribute(node, attr);
00111     cModule *mod = simulation.getModuleByPath(moduleAttr);
00112     if (!mod)
00113         error("module '%s' not found at %s", moduleAttr, node->getSourceLocation());
00114     return mod;
00115 }

cGate * ScenarioManager::getRequiredGate ( cXMLElement *  node,
const char *  modattr,
const char *  gateattr 
) [protected, virtual]

Referenced by processSetChannelAttrCommand().

00118 {
00119     cModule *mod = getRequiredModule(node, modAttr);
00120     const char *gateStr = getRequiredAttribute(node, gateAttr);
00121     std::string gname;
00122     int gindex;
00123     cGate *g = parseIndexedName(gateStr, gname, gindex) ? mod->gate(gname.c_str(), gindex) : mod->gate(gname.c_str());
00124     if (!g)
00125         error("module '%s' has no gate '%s' at %s", mod->getFullPath().c_str(), gateStr, node->getSourceLocation());
00126     return g;
00127 }

void ScenarioManager::processCommand ( cXMLElement *  node  )  [protected, virtual]

Referenced by handleMessage(), and processAtCommand().

00063 {
00064     const char *tag = node->getTagName();
00065     EV << "processing <" << tag << "> command...\n";
00066 
00067     if (!strcmp(tag,"at"))
00068         processAtCommand(node);
00069     else if (!strcmp(tag,"set-param"))
00070         processSetParamCommand(node);
00071     else if (!strcmp(tag,"set-channel-attr"))
00072         processSetChannelAttrCommand(node);
00073     // else if (!strcmp(tag,"create-module"))
00074     //    processCreateModuleCommand(node);
00075     // else if (!strcmp(tag,"connect"))
00076     //    processConnectCommand(node);
00077     else
00078         processModuleSpecificCommand(node);
00079 }

void ScenarioManager::processAtCommand ( cXMLElement *  node  )  [protected, virtual]

Referenced by processCommand().

00130 {
00131     for (cXMLElement *child=node->getFirstChild(); child; child=child->getNextSibling())
00132         processCommand(child);
00133 }

void ScenarioManager::processSetParamCommand ( cXMLElement *  node  )  [protected, virtual]

Referenced by processCommand().

00152 {
00153     // process <set-param> command
00154     cModule *mod = getRequiredModule(node, "module");
00155     const char *parAttr = getRequiredAttribute(node, "par");
00156     const char *valueAttr = getRequiredAttribute(node, "value");
00157 
00158     EV << "Setting " << mod->getFullPath() << "." << parAttr << " = " << valueAttr << "\n";
00159     bubble((std::string("setting: ")+mod->getFullPath()+"."+parAttr+" = "+valueAttr).c_str());
00160 
00161     // set the parameter to the given value
00162     cPar& param = mod->par(parAttr);
00163     param.parse(valueAttr);
00164 }

void ScenarioManager::processSetChannelAttrCommand ( cXMLElement *  node  )  [protected, virtual]

Referenced by processCommand().

00167 {
00168     // process <set-channel-attr> command
00169     cGate *g = getRequiredGate(node, "src-module", "src-gate");
00170     const char *attrAttr = getRequiredAttribute(node, "attr");
00171     const char *valueAttr = getRequiredAttribute(node, "value");
00172 
00173     EV << "Setting channel attribute: " << attrAttr << " = " << valueAttr
00174        << " of gate " << g->getFullPath() << "\n";
00175     bubble((std::string("setting channel attr: ")+attrAttr+" = "+valueAttr).c_str());
00176 
00177     // make sure gate is connected at all
00178     if (!g->getNextGate())
00179         error("gate '%s' is not connected at %s", g->getFullPath().c_str(), node->getSourceLocation());
00180 
00181     // find channel (or add one?)
00182     cChannel *chan = g->getChannel();
00183     if (!chan)
00184         error("connection starting at gate '%s' has no attributes at %s", g->getFullPath().c_str(), node->getSourceLocation());
00185 
00186     // set the parameter to the given value
00187     if (!chan->hasPar(attrAttr))
00188         ; //FIXME remove this "if"
00189     cPar& param = chan->par(attrAttr);
00190     param.parse(valueAttr);
00191 }

void ScenarioManager::processCreateModuleCommand ( cXMLElement *  node  )  [protected, virtual]

00194 {
00195     // FIXME finish and test
00196 }

void ScenarioManager::processDeleteModuleCommand ( cXMLElement *  node  )  [protected, virtual]

00199 {
00200     // FIXME finish and test
00201 }

void ScenarioManager::processConnectCommand ( cXMLElement *  node  )  [protected, virtual]

00204 {
00205     // FIXME finish and test
00206 }

void ScenarioManager::processDisconnectCommand ( cXMLElement *  node  )  [protected, virtual]

00209 {
00210     // FIXME finish and test
00211 }

void ScenarioManager::processModuleSpecificCommand ( cXMLElement *  node  )  [protected, virtual]

Referenced by processCommand().

00136 {
00137     // find which module we'll need to invoke
00138     cModule *mod = getRequiredModule(node, "module");
00139 
00140     // see if it supports the IScriptable interface
00141     IScriptable *scriptable = dynamic_cast<IScriptable *>(mod);
00142     if (!scriptable)
00143         error("<%s> not understood: it is not a built-in command of %s, and module class %s "
00144               "is not scriptable (does not subclass from IScriptable) at %s",
00145               node->getTagName(), getClassName(), mod->getClassName(), node->getSourceLocation());
00146 
00147     // ok, trust it to process this command
00148     scriptable->processCommand(*node);
00149 }

void ScenarioManager::initialize (  )  [protected, virtual]

00024 {
00025     cXMLElement *script = par("script");
00026 
00027     numChanges = numDone = 0;
00028     WATCH(numChanges);
00029     WATCH(numDone);
00030 
00031     for (cXMLElement *node=script->getFirstChild(); node; node = node->getNextSibling())
00032     {
00033         // check attr t is present
00034         const char *tAttr = node->getAttribute("t");
00035         if (!tAttr)
00036             error("attribute 't' missing at %s", node->getSourceLocation());
00037 
00038         // schedule self-message
00039         simtime_t t = STR_SIMTIME(tAttr);
00040         cMessage *msg = new cMessage("scenario-event");
00041         msg->setContextPointer(node);
00042         scheduleAt(t, msg);
00043 
00044         // count it
00045         numChanges++;
00046     }
00047 
00048     updateDisplayString();
00049 }

void ScenarioManager::handleMessage ( cMessage *  msg  )  [protected, virtual]

00052 {
00053     cXMLElement *node = (cXMLElement *) msg->getContextPointer();
00054     delete msg;
00055 
00056     processCommand(node);
00057 
00058     numDone++;
00059     updateDisplayString();
00060 }

void ScenarioManager::updateDisplayString (  )  [protected, virtual]

Referenced by handleMessage(), and initialize().

00214 {
00215     char buf[80];
00216     sprintf(buf, "total %d changes, %d left", numChanges, numChanges-numDone);
00217     getDisplayString().setTagArg("t", 0, buf);
00218 }


Member Data Documentation

int ScenarioManager::numChanges [protected]

Referenced by initialize(), and updateDisplayString().

int ScenarioManager::numDone [protected]


The documentation for this class was generated from the following files:

Generated on Fri Mar 20 18:51:21 2009 for INET Framework for OMNeT++/OMNEST by  doxygen 1.5.5