Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

nr.cc

Go to the documentation of this file.
00001 // 
00002 // nr.cc         : Network Routing
00003 // authors       : Dan Coffin, John Heidemann, Dan Van Hook
00004 // authors       : Fabio Silva
00005 // 
00006 // Copyright (C) 2000-2002 by the University of Southern California
00007 // $Id: nr.cc,v 1.6 2002/11/26 22:45:40 haldar Exp $
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License,
00011 // version 2, as published by the Free Software Foundation.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License along
00019 // with this program; if not, write to the Free Software Foundation, Inc.,
00020 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021 //
00022 //
00023 
00024 #include <stdio.h>   // for null
00025 #include <string.h>  // for strdup
00026 #include <algorithm>  // for stl min
00027 #include "nr.hh"
00028 
00029 NRSimpleAttributeFactory<int> NRScopeAttr(NRAttribute::SCOPE_KEY, NRAttribute::INT32_TYPE);
00030 NRSimpleAttributeFactory<int> NRClassAttr(NRAttribute::CLASS_KEY, NRAttribute::INT32_TYPE);
00031 NRSimpleAttributeFactory<float> LatitudeAttr(NRAttribute::LATITUDE_KEY, NRAttribute::FLOAT32_TYPE);
00032 NRSimpleAttributeFactory<float> LongitudeAttr(NRAttribute::LONGITUDE_KEY, NRAttribute::FLOAT32_TYPE);
00033 NRSimpleAttributeFactory<char *> RouteAttr(NRAttribute::ROUTE_KEY, NRAttribute::STRING_TYPE);
00034 NRSimpleAttributeFactory<char *> SourceRouteAttr(NRAttribute::SOURCE_ROUTE_KEY, NRAttribute::STRING_TYPE);
00035 
00036 NRAttributeFactory *NRAttributeFactory::first_ = NULL;
00037 
00038 NRAttribute::NRAttribute(int key, int type, int op, int len, void *val)
00039    : key_(key), type_(type), op_(op), len_(len)
00040 {
00041    if (val != NULL){
00042       val_ = (void *) new char[len_];
00043       memcpy(val_, val, len_);
00044    }
00045 }
00046 
00047 NRAttribute::NRAttribute() :    
00048    key_(0), type_(0), op_(0), len_(0), val_(NULL) 
00049 {}
00050 
00051 
00052 NRAttribute::NRAttribute(const NRAttribute &rhs)
00053 {
00054    key_ = rhs.key_;
00055    type_ = rhs.type_;
00056    op_ = rhs.op_;
00057    len_ = rhs.len_;
00058    val_ = new char[len_];
00059    memcpy(val_, rhs.val_, len_);
00060 }
00061 
00062 NRAttribute::~NRAttribute()
00063 {
00064 }
00065 
00066 NRAttribute * NRAttribute::find_key_from(int key, NRAttrVec *attrs,
00067                                          NRAttrVec::iterator start,
00068                                          NRAttrVec::iterator *place) {
00069    
00070    NRAttrVec::iterator i;
00071    
00072    for (i = start; i != attrs->end(); ++i) {
00073       if ((*i)->getKey() == key) {
00074          if (place)
00075             *place = i;
00076          return (*i);
00077       };
00078    };
00079    return NULL;
00080 }
00081 
00082 bool NRAttribute::isEQ(NRAttribute *attr) {
00083 
00084    // Keys need to be the same
00085    if (!isSameKey(attr))
00086       abort();
00087 
00088    switch (type_){
00089 
00090    case INT32_TYPE:
00091       return (*(int32_t *) val_ == *(int32_t *) attr->getGenericVal());
00092       break;
00093 
00094    case FLOAT32_TYPE:
00095       return (*(float *) val_ == *(float *) attr->getGenericVal());
00096       break;
00097 
00098    case FLOAT64_TYPE:
00099       return (*(double *) val_ == *(double *) attr->getGenericVal());
00100       break;
00101 
00102    case STRING_TYPE:
00103       if (len_ != attr->getLen())
00104         return false;
00105       return (!strncmp((char *) val_, (char *) attr->getGenericVal(), len_));
00106       break;
00107 
00108    case BLOB_TYPE:
00109       if (len_ != attr->getLen())
00110         return false;
00111       return (!memcmp((void *) val_, (void *) attr->getGenericVal(), len_));
00112       break;
00113 
00114    default:
00115       abort();
00116       break;
00117    }
00118 }
00119 
00120 bool NRAttribute::isGT(NRAttribute *attr) {
00121 
00122    int cmplen, cmp;  // must be here for initialization reasons
00123 
00124    // Keys need to be the same
00125    if (!isSameKey(attr))
00126       abort();
00127 
00128    switch (type_) {
00129 
00130    case INT32_TYPE:
00131       return (*(int32_t *) val_ > *(int32_t *) attr->getGenericVal());
00132       break;
00133 
00134    case FLOAT32_TYPE:
00135       return (*(float *) val_ > *(float *) attr->getGenericVal());
00136       break;
00137 
00138    case FLOAT64_TYPE:
00139       return (*(double *) val_ > *(double *) attr->getGenericVal());
00140       break;
00141 
00142    case STRING_TYPE:
00143       return strncmp((char *) val_, (char *) attr->getGenericVal(),
00144                      min(len_, attr->getLen()));
00145       break;
00146 
00147    case BLOB_TYPE:
00148       cmplen = min(len_, attr->getLen());
00149       cmp = memcmp((void *) val_, (void *) attr->getGenericVal(), cmplen);
00150       
00151       // We are greater than attr
00152       if (cmp > 0)
00153          return true;
00154       // We are equal to attr up to len_, but we are longer
00155       if (cmp == 0 && (len_ > attr->getLen()))
00156          return true;
00157       return false;
00158       break;
00159 
00160    default:
00161       abort();
00162       break;
00163    }
00164 }
00165 
00166 bool NRAttribute::isGE(NRAttribute *attr) {
00167 
00168    int cmplen, cmp;  // must be here for initialization reasons
00169 
00170    // Keys need to be the same
00171    if (!isSameKey(attr))
00172       abort();
00173 
00174    switch (type_) {
00175 
00176    case INT32_TYPE:
00177       return (*(int32_t *) val_ >= *(int32_t *) attr->getGenericVal());
00178       break;
00179 
00180    case FLOAT32_TYPE:
00181       return (*(float *) val_ >= *(float *) attr->getGenericVal());
00182       break;
00183 
00184    case FLOAT64_TYPE:
00185       return (*(double *) val_ >= *(double *) attr->getGenericVal());
00186       break;
00187 
00188    case STRING_TYPE:
00189       return (strcmp((char *) val_, (char *) attr->getGenericVal()) >= 0);
00190       break;
00191 
00192    case BLOB_TYPE:
00193       cmplen = min(len_, attr->getLen());
00194       cmp = memcmp((void *) val_, (void *) attr->getGenericVal(), cmplen);
00195       
00196       // We are greater or equal to attr
00197       if (cmp >= 0)
00198          return true;
00199       return false;
00200       break;
00201 
00202    default:
00203       abort();
00204       break;
00205    }
00206 }
00207 
00208 NRSimpleAttribute<char *>::NRSimpleAttribute(int key, int type, int op, char *val, int size) :
00209    NRAttribute(key, type, op, (strlen(val) + 1))
00210 {
00211    assert(type == STRING_TYPE);
00212    val_ = NULL;
00213    setVal(val);
00214 }
00215 
00216 void NRSimpleAttribute<char *>::setVal(char *value) {
00217    delete [] (char *) val_;
00218    len_ = strlen(value) + 1;
00219    val_ = (void *) new char[len_ + 1];
00220    memcpy(val_, value, len_);
00221 }
00222 
00223 NRSimpleAttribute<void *>::NRSimpleAttribute(int key, int type, int op, void *val, int size) :
00224    NRAttribute(key, type, op, size)
00225 {
00226    assert(type == BLOB_TYPE);
00227    val_ = NULL;
00228    setVal(val, len_);
00229 }
00230 
00231 void NRSimpleAttribute<void *>::setVal(void *value, int len) {
00232    delete [] (char *) val_;
00233    len_ = len;
00234    val_ = (void *) new char[len_];
00235    memcpy(val_, value, len_);
00236 }
00237 
00238 void NRAttributeFactory::verify_unique(NRAttributeFactory *baby) {
00239    
00240    NRAttributeFactory *i = first_, *last = NULL;
00241    while (i) {
00242       if (baby->key_ == i->key_) {
00243          // identical factories are ok
00244          assert(baby->type_ == i->type_);
00245          return;  // don't enlist duplictates
00246       }
00247       last = i;
00248       i = i->next_;
00249    }
00250    // must not exist, add it
00251    if (last)
00252       last->next_ = first_;
00253    first_ = baby;
00254 }

Generated on Tue Apr 20 12:14:25 2004 for NS2.26SourcesOriginal by doxygen 1.3.3