LIBTable.cc

Go to the documentation of this file.
00001 //
00002 // (C) 2005 Vojtech Janota
00003 // (C) 2003 Xuan Thang Nguyen
00004 //
00005 // This library is free software, you can redistribute it
00006 // and/or modify
00007 // it under  the terms of the GNU Lesser General Public License
00008 // as published by the Free Software Foundation;
00009 // either version 2 of the License, or any later version.
00010 // The library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00013 // See the GNU Lesser General Public License for more details.
00014 //
00015 
00016 #include <iostream>
00017 #include "LIBTable.h"
00018 #include "XMLUtils.h"
00019 #include "RoutingTableAccess.h"
00020 
00021 Define_Module(LIBTable);
00022 
00023 void LIBTable::initialize(int stage)
00024 {
00025     if (stage==0)
00026         maxLabel = 0;
00027 
00028     // we have to wait until routerId gets assigned in stage 3
00029     if (stage==4)
00030     {
00031         RoutingTableAccess routingTableAccess;
00032         IRoutingTable *rt = routingTableAccess.get();
00033         routerId = rt->getRouterId();
00034 
00035         // read configuration
00036 
00037         readTableFromXML(par("conf").xmlValue());
00038 
00039         WATCH_VECTOR(lib);
00040     }
00041 }
00042 
00043 void LIBTable::handleMessage(cMessage *)
00044 {
00045     ASSERT(false);
00046 }
00047 
00048 bool LIBTable::resolveLabel(std::string inInterface, int inLabel,
00049         LabelOpVector& outLabel, std::string& outInterface, int& color)
00050 {
00051     bool any = (inInterface.length() == 0);
00052 
00053     for (unsigned int i = 0; i < lib.size(); i++)
00054     {
00055         if (!any && lib[i].inInterface != inInterface)
00056             continue;
00057 
00058         if (lib[i].inLabel != inLabel)
00059             continue;
00060 
00061         outLabel = lib[i].outLabel;
00062         outInterface = lib[i].outInterface;
00063         color = lib[i].color;
00064 
00065         return true;
00066     }
00067     return false;
00068 }
00069 
00070 int LIBTable::installLibEntry(int inLabel, std::string inInterface, const LabelOpVector& outLabel,
00071             std::string outInterface, int color)
00072 {
00073     if (inLabel == -1)
00074     {
00075         LIBEntry newItem;
00076         newItem.inLabel = ++maxLabel;
00077         newItem.inInterface = inInterface;
00078         newItem.outLabel = outLabel;
00079         newItem.outInterface = outInterface;
00080         newItem.color = color;
00081         lib.push_back(newItem);
00082         return newItem.inLabel;
00083     }
00084     else
00085     {
00086         for (unsigned int i = 0; i < lib.size(); i++)
00087         {
00088             if (lib[i].inLabel != inLabel)
00089                 continue;
00090 
00091             lib[i].inInterface = inInterface;
00092             lib[i].outLabel = outLabel;
00093             lib[i].outInterface = outInterface;
00094             lib[i].color = color;
00095             return inLabel;
00096         }
00097         ASSERT(false);
00098         return 0; // prevent warning
00099     }
00100 }
00101 
00102 void LIBTable::removeLibEntry(int inLabel)
00103 {
00104     for (unsigned int i = 0; i < lib.size(); i++)
00105     {
00106         if (lib[i].inLabel != inLabel)
00107             continue;
00108 
00109         lib.erase(lib.begin() + i);
00110         return;
00111     }
00112     ASSERT(false);
00113 }
00114 
00115 void LIBTable::readTableFromXML(const cXMLElement* libtable)
00116 {
00117     ASSERT(libtable);
00118     ASSERT(!strcmp(libtable->getTagName(), "libtable"));
00119     checkTags(libtable, "libentry");
00120     cXMLElementList list = libtable->getChildrenByTagName("libentry");
00121     for (cXMLElementList::iterator it=list.begin(); it != list.end(); it++)
00122     {
00123         const cXMLElement& entry = **it;
00124 
00125         checkTags(&entry, "inLabel inInterface outLabel outInterface color");
00126 
00127         LIBEntry newItem;
00128         newItem.inLabel = getParameterIntValue(&entry, "inLabel");
00129         newItem.inInterface = getParameterStrValue(&entry, "inInterface");
00130         newItem.outInterface = getParameterStrValue(&entry, "outInterface");
00131         newItem.color = getParameterIntValue(&entry, "color", 0);
00132 
00133         cXMLElementList ops = getUniqueChild(&entry, "outLabel")->getChildrenByTagName("op");
00134         for (cXMLElementList::iterator oit=ops.begin(); oit != ops.end(); oit++)
00135         {
00136             const cXMLElement& op = **oit;
00137             const char *val = op.getAttribute("value");
00138             const char *code = op.getAttribute("code");
00139             ASSERT(code);
00140             LabelOp l;
00141 
00142             if (!strcmp(code, "push"))
00143             {
00144                 l.optcode = PUSH_OPER;
00145                 ASSERT(val);
00146                 l.label = atoi(val);
00147                 ASSERT(l.label > 0);
00148             }
00149             else if (!strcmp(code, "pop"))
00150             {
00151                 l.optcode = POP_OPER;
00152                 ASSERT(!val);
00153             }
00154             else if (!strcmp(code, "swap"))
00155             {
00156                 l.optcode = SWAP_OPER;
00157                 ASSERT(val);
00158                 l.label = atoi(val);
00159                 ASSERT(l.label > 0);
00160             }
00161             else
00162                 ASSERT(false);
00163 
00164             newItem.outLabel.push_back(l);
00165         }
00166 
00167         lib.push_back(newItem);
00168 
00169         ASSERT(newItem.inLabel > 0);
00170 
00171         if (newItem.inLabel > maxLabel)
00172             maxLabel = newItem.inLabel;
00173     }
00174 }
00175 
00176 LabelOpVector LIBTable::pushLabel(int label)
00177 {
00178     LabelOpVector vec;
00179     LabelOp lop;
00180     lop.optcode = PUSH_OPER;
00181     lop.label = label;
00182     vec.push_back(lop);
00183     return vec;
00184 }
00185 
00186 LabelOpVector LIBTable::swapLabel(int label)
00187 {
00188     LabelOpVector vec;
00189     LabelOp lop;
00190     lop.optcode = SWAP_OPER;
00191     lop.label = label;
00192     vec.push_back(lop);
00193     return vec;
00194 }
00195 
00196 LabelOpVector LIBTable::popLabel()
00197 {
00198     LabelOpVector vec;
00199     LabelOp lop;
00200     lop.optcode = POP_OPER;
00201     lop.label = 0;
00202     vec.push_back(lop);
00203     return vec;
00204 }
00205 
00206 std::ostream & operator<<(std::ostream & os, const LabelOpVector& label)
00207 {
00208     os << "{";
00209     for (unsigned int i = 0; i < label.size(); i++)
00210     {
00211         switch(label[i].optcode)
00212         {
00213             case PUSH_OPER:
00214                 os << "PUSH " << label[i].label;
00215                 break;
00216 
00217             case SWAP_OPER:
00218                 os << "SWAP " << label[i].label;
00219                 break;
00220 
00221             case POP_OPER:
00222                 os << "POP";
00223                 break;
00224 
00225             default:
00226                 ASSERT(false);
00227         }
00228 
00229         if (i < label.size() - 1)
00230             os << "; ";
00231         else
00232             os << "}";
00233     }
00234     return os;
00235 }
00236 
00237 std::ostream & operator<<(std::ostream & os, const LIBTable::LIBEntry & lib)
00238 {
00239     os << "inLabel:" << lib.inLabel;
00240     os << "    inInterface:" << lib.inInterface;
00241     os << "    outLabel:" << lib.outLabel;
00242     os << "    outInterface:" << lib.outInterface;
00243     os << "    color:" << lib.color;
00244     return os;
00245 }