Discovery.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include <config.h>
00018 
00019 #include <oasys/util/Options.h>
00020 #include <oasys/util/StringBuffer.h>
00021 #include "IPDiscovery.h"
00022 
00023 #ifdef OASYS_BLUETOOTH_ENABLED
00024 #include "BluetoothDiscovery.h"
00025 #endif
00026 
00027 #include "Announce.h"
00028 #include "Discovery.h"
00029 
00030 namespace dtn {
00031 
00032 Discovery::Discovery(const std::string& name,
00033                      const std::string& af)
00034     : oasys::Logger("Discovery","/dtn/discovery/%s",af.c_str()),
00035       name_(name), af_(af)
00036 {
00037 }
00038 
00039 Discovery*
00040 Discovery::create_discovery(const std::string& name,
00041                             const std::string& af,
00042                             int argc, const char* argv[])
00043 {
00044     Discovery* disc = NULL;
00045     if (af == "ip")
00046     {
00047         disc = new IPDiscovery(name);
00048     }
00049 #ifdef OASYS_BLUETOOTH_ENABLED 
00050     else
00051     if (af == "bt")
00052     {
00053         disc = new BluetoothDiscovery(name);
00054     }
00055 #endif
00056     else
00057     {
00058         // not a recognized address family
00059         return NULL;
00060     }
00061 
00062     if (! disc->configure(argc,argv))
00063     {
00064         delete disc;
00065         return NULL;
00066     }
00067 
00068     return disc;
00069 }
00070 
00071 Discovery::~Discovery()
00072 {
00073     for (iterator i = list_.begin(); i != list_.end(); i++)
00074     {
00075         delete (*i);
00076     }
00077 }
00078 
00079 void
00080 Discovery::dump(oasys::StringBuffer* buf)
00081 {
00082     buf->appendf("%s af %s: %zu announce %s\n",
00083                  name_.c_str(),af_.c_str(),list_.size(),to_addr_.c_str());
00084     for (iterator i = list_.begin(); i != list_.end(); i++)
00085     {
00086         buf->appendf("\tannounce %s type %s advertising %s every %d sec\n",
00087                      (*i)->name().c_str(),
00088                      (*i)->type().c_str(),
00089                      (*i)->local_addr().c_str(),
00090                      (*i)->interval()/1000);
00091     }
00092 }
00093 
00094 bool
00095 Discovery::announce(const char* name, int argc, const char* argv[])
00096 {
00097     iterator iter;
00098     if (find(name,&iter))
00099     {
00100         log_err("discovery for %s already exists",name);
00101         return false;
00102     }
00103 
00104     if (argc < 1)
00105     {
00106         log_err("cl type not specified");
00107         return false;
00108     }
00109 
00110     const char* cltype = argv[0];
00111     ConvergenceLayer* cl = ConvergenceLayer::find_clayer(cltype);
00112     if (cl == NULL)
00113     {
00114         log_err("invalid convergence layer type (%s)",cltype);
00115         return false;
00116     }
00117 
00118     Announce* announce = Announce::create_announce(name,cl,argc,argv);
00119     if (announce == NULL)
00120     {
00121         log_err("no announce implemented for %s convergence layer",cltype);
00122         return false;
00123     }
00124 
00125     list_.push_back(announce);
00126     // alert derived classes to new registration
00127     handle_announce();
00128 
00129     return true;
00130 }
00131 
00132 bool
00133 Discovery::remove(const char* name)
00134 {
00135     iterator iter;
00136 
00137     if (! find(name,&iter))
00138     {
00139         log_err("error removing announce %s: no such object",name);
00140         return false;
00141     }
00142 
00143     Announce *announce = *iter;
00144     list_.erase(iter);
00145     delete announce;
00146     return true;
00147 }
00148 
00149 void
00150 Discovery::handle_neighbor_discovered(const std::string& type,
00151                                       const std::string& cl_addr,
00152                                       const EndpointID& remote_eid)
00153 {
00154     for (iterator i = list_.begin(); i != list_.end(); i++)
00155     {
00156         if ((*i)->type() == type)
00157         {
00158             (*i)->handle_neighbor_discovered(cl_addr,remote_eid);
00159         }
00160     }
00161 }
00162 
00163 bool
00164 Discovery::find(const char* name, Discovery::iterator* iter)
00165 {
00166     Announce* announce;
00167     for (*iter = list_.begin(); *iter != list_.end(); (*iter)++)
00168     {
00169         announce = **iter;
00170         if (announce->name() == name)
00171         {
00172             return true; 
00173         }
00174     }
00175     return false;
00176 }
00177 
00178 };

Generated on Sat Sep 8 08:36:16 2007 for DTN Reference Implementation by  doxygen 1.5.3