Public Member Functions | Protected Member Functions | Protected Attributes

ScenarioManager Class Reference

#include <ScenarioManager.h>

List of all members.

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

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

Definition at line 38 of file ScenarioManager.h.


Constructor & Destructor Documentation

ScenarioManager::ScenarioManager (  )  [inline]

Definition at line 65 of file ScenarioManager.h.

{}


Member Function Documentation

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

Definition at line 99 of file ScenarioManager.cc.

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

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

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

Definition at line 117 of file ScenarioManager.cc.

Referenced by processSetChannelAttrCommand().

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

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

Definition at line 108 of file ScenarioManager.cc.

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

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

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

Definition at line 51 of file ScenarioManager.cc.

{
    cXMLElement *node = (cXMLElement *) msg->getContextPointer();
    delete msg;

    processCommand(node);

    numDone++;
    updateDisplayString();
}

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

Definition at line 23 of file ScenarioManager.cc.

{
    cXMLElement *script = par("script");

    numChanges = numDone = 0;
    WATCH(numChanges);
    WATCH(numDone);

    for (cXMLElement *node=script->getFirstChild(); node; node = node->getNextSibling())
    {
        // check attr t is present
        const char *tAttr = node->getAttribute("t");
        if (!tAttr)
            error("attribute 't' missing at %s", node->getSourceLocation());

        // schedule self-message
        simtime_t t = STR_SIMTIME(tAttr);
        cMessage *msg = new cMessage("scenario-event");
        msg->setContextPointer(node);
        scheduleAt(t, msg);

        // count it
        numChanges++;
    }

    updateDisplayString();
}

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

Definition at line 129 of file ScenarioManager.cc.

Referenced by processCommand().

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

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

Definition at line 62 of file ScenarioManager.cc.

Referenced by handleMessage(), and processAtCommand().

{
    const char *tag = node->getTagName();
    EV << "processing <" << tag << "> command...\n";

    if (!strcmp(tag,"at"))
        processAtCommand(node);
    else if (!strcmp(tag,"set-param"))
        processSetParamCommand(node);
    else if (!strcmp(tag,"set-channel-attr"))
        processSetChannelAttrCommand(node);
    // else if (!strcmp(tag,"create-module"))
    //    processCreateModuleCommand(node);
    // else if (!strcmp(tag,"connect"))
    //    processConnectCommand(node);
    else
        processModuleSpecificCommand(node);
}

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

Definition at line 203 of file ScenarioManager.cc.

{
    // FIXME finish and test
}

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

Definition at line 193 of file ScenarioManager.cc.

{
    // FIXME finish and test
}

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

Definition at line 198 of file ScenarioManager.cc.

{
    // FIXME finish and test
}

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

Definition at line 208 of file ScenarioManager.cc.

{
    // FIXME finish and test
}

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

Definition at line 135 of file ScenarioManager.cc.

Referenced by processCommand().

{
    // find which module we'll need to invoke
    cModule *mod = getRequiredModule(node, "module");

    // see if it supports the IScriptable interface
    IScriptable *scriptable = dynamic_cast<IScriptable *>(mod);
    if (!scriptable)
        error("<%s> not understood: it is not a built-in command of %s, and module class %s "
              "is not scriptable (does not subclass from IScriptable) at %s",
              node->getTagName(), getClassName(), mod->getClassName(), node->getSourceLocation());

    // ok, trust it to process this command
    scriptable->processCommand(*node);
}

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

Definition at line 166 of file ScenarioManager.cc.

Referenced by processCommand().

{
    // process <set-channel-attr> command
    cGate *g = getRequiredGate(node, "src-module", "src-gate");
    const char *attrAttr = getRequiredAttribute(node, "attr");
    const char *valueAttr = getRequiredAttribute(node, "value");

    EV << "Setting channel attribute: " << attrAttr << " = " << valueAttr
       << " of gate " << g->getFullPath() << "\n";
    bubble((std::string("setting channel attr: ")+attrAttr+" = "+valueAttr).c_str());

    // make sure gate is connected at all
    if (!g->getNextGate())
        error("gate '%s' is not connected at %s", g->getFullPath().c_str(), node->getSourceLocation());

    // find channel (or add one?)
    cChannel *chan = g->getChannel();
    if (!chan)
        error("connection starting at gate '%s' has no attributes at %s", g->getFullPath().c_str(), node->getSourceLocation());

    // set the parameter to the given value
    if (!chan->hasPar(attrAttr))
        ; //FIXME remove this "if"
    cPar& param = chan->par(attrAttr);
    param.parse(valueAttr);
}

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

Definition at line 151 of file ScenarioManager.cc.

Referenced by processCommand().

{
    // process <set-param> command
    cModule *mod = getRequiredModule(node, "module");
    const char *parAttr = getRequiredAttribute(node, "par");
    const char *valueAttr = getRequiredAttribute(node, "value");

    EV << "Setting " << mod->getFullPath() << "." << parAttr << " = " << valueAttr << "\n";
    bubble((std::string("setting: ")+mod->getFullPath()+"."+parAttr+" = "+valueAttr).c_str());

    // set the parameter to the given value
    cPar& param = mod->par(parAttr);
    param.parse(valueAttr);
}

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

Definition at line 213 of file ScenarioManager.cc.

Referenced by handleMessage(), and initialize().

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


Member Data Documentation

int ScenarioManager::numChanges [protected]

Definition at line 42 of file ScenarioManager.h.

Referenced by initialize(), and updateDisplayString().

int ScenarioManager::numDone [protected]

Definition at line 43 of file ScenarioManager.h.

Referenced by handleMessage(), initialize(), and updateDisplayString().


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