InterfaceTable.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "InterfaceTable.h"
00040 #include "conv_layers/ConvergenceLayer.h"
00041 #include "oasys/util/StringBuffer.h"
00042 
00043 namespace dtn {
00044 
00045 InterfaceTable* InterfaceTable::instance_ = NULL;
00046 
00047 InterfaceTable::InterfaceTable()
00048     : Logger("InterfaceTable", "/dtn/interface/table")
00049 {
00050 }
00051 
00052 InterfaceTable::~InterfaceTable()
00053 {
00054     NOTREACHED;
00055 }
00056 
00061 bool
00062 InterfaceTable::find(const std::string& name,
00063                      InterfaceList::iterator* iter)
00064 {
00065     Interface* iface;
00066     for (*iter = iflist_.begin(); *iter != iflist_.end(); ++(*iter)) {
00067         iface = **iter;
00068         
00069         if (iface->name() == name) {
00070             return true;
00071         }
00072     }        
00073     
00074     return false;
00075 }
00076 
00082 bool
00083 InterfaceTable::add(const std::string& name,
00084                     ConvergenceLayer* cl,
00085                     const char* proto,
00086                     int argc, const char* argv[])
00087 {
00088     InterfaceList::iterator iter;
00089     
00090     if (find(name, &iter)) {
00091         log_err("interface %s already exists", name.c_str());
00092         return false;
00093     }
00094     
00095     log_info("adding interface %s (%s)", name.c_str(), proto);
00096 
00097     Interface* iface = new Interface(name, proto, cl);
00098     if (! cl->interface_up(iface, argc, argv)) {
00099         log_err("convergence layer error adding interface %s", name.c_str());
00100         delete iface;
00101         return false;
00102     }
00103 
00104     iflist_.push_back(iface);
00105 
00106     return true;
00107 }
00108 
00112 bool
00113 InterfaceTable::del(const std::string& name)
00114 {
00115     InterfaceList::iterator iter;
00116     Interface* iface;
00117     bool retval = false;
00118     
00119     log_info("removing interface %s", name.c_str());
00120 
00121     if (! find(name, &iter)) {
00122         log_err("error removing interface %s: no such interface",
00123                 name.c_str());
00124         return false;
00125     }
00126 
00127     iface = *iter;
00128     iflist_.erase(iter);
00129 
00130     if (iface->clayer()->interface_down(iface)) {
00131         retval = true;
00132     } else {
00133         log_err("error deleting interface %s from the convergence layer.",
00134                 name.c_str());
00135         retval = false;
00136     }
00137 
00138     delete iface;
00139     return retval;
00140 }
00141 
00145 void
00146 InterfaceTable::list(oasys::StringBuffer *buf)
00147 {
00148     InterfaceList::iterator iter;
00149     Interface* iface;
00150 
00151     for (iter = iflist_.begin(); iter != iflist_.end(); ++(iter)) {
00152         iface = *iter;
00153         buf->appendf("%s: Convergence Layer: %s\n",
00154                      iface->name().c_str(), iface->proto().c_str());
00155         iface->clayer()->dump_interface(iface, buf);
00156         buf->append("\n");
00157     }
00158 }
00159 
00160 } // namespace dtn

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1