INET Framework for OMNeT++/OMNEST
InterfaceStateManager.cc
Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2009 Martin Danko
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00019 #include "InterfaceStateManager.h"
00020 #include "InterfaceTableAccess.h"
00021 
00022 Define_Module(InterfaceStateManager);
00023 
00024 void InterfaceStateManager::initialize()
00025 {
00026     ift = InterfaceTableAccess().get();
00027     if (ift == NULL){
00028        throw cRuntimeError("AnsaInterfaceTable not found");
00029     }
00030 }
00031 
00032 void InterfaceStateManager::handleMessage(cMessage *msg)
00033 {
00034     ASSERT(false);
00035 }
00036 
00037 /*
00038  * Method processes the XML command received from ScenarioManager
00039  * @param node          - XML element with command from ScenarioManager
00040  */
00041 
00042 void InterfaceStateManager::processCommand(const cXMLElement& node)
00043 {
00044    if (!strcmp(node.getTagName(), "interfacedown"))
00045     {
00046       InterfaceEntry *targetInt = ift->getInterfaceByName(node.getAttribute("int"));
00047       EV << "interface " << node.getAttribute("int") << " is going DOWN" << endl;
00048       if (targetInt == NULL){
00049          throw cRuntimeError("Interface %s not found", node.getAttribute("int"));
00050       }
00051       changeInterfaceState(targetInt, true);
00052     }
00053     else if(!strcmp(node.getTagName(), "interfaceup"))
00054     {
00055       InterfaceEntry *targetInt = ift->getInterfaceByName(node.getAttribute("int"));
00056       EV << "interface " << node.getAttribute("int") << " is going UP" << endl;
00057       if (targetInt == NULL){
00058          throw cRuntimeError("Interface %s not found", node.getAttribute("int"));
00059       }
00060       changeInterfaceState(targetInt, false);
00061     }
00062     else
00063         ASSERT(false);
00064 
00065 }
00066 /*
00067  * Method changes the interface state to desired value
00068  * @param targetInt     - interface, that is changing the state
00069  * @param toDown        - new interface state
00070  */
00071 
00072 void InterfaceStateManager::changeInterfaceState(InterfaceEntry *targetInt, bool toDown)
00073 {
00074   Enter_Method_Silent();
00075   
00076   if(targetInt != NULL)
00077   {
00078     bool isDown = targetInt->isDown();
00079     if(!isDown && toDown)
00080       targetInt->setDown(true);
00081     else
00082       if(isDown && !toDown)
00083         targetInt->setDown(false);
00084   }
00085   else
00086     EV << "Interface NOT found\n";
00087 }
00088