sragent.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1996 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 /*
00035  * Contributed by Rishi Bhargava <rishi_bhargava@yahoo.com> May, 2001.
00036  */
00037 
00038 #include <assert.h>
00039 #include <math.h>
00040 #include <stdio.h>
00041 #include <signal.h>
00042 #include <float.h>
00043 
00044 #include "object.h"
00045 #include "agent.h"
00046 #include "trace.h"
00047 #include "packet.h"
00048 #include "scheduler.h"
00049 #include "random.h"
00050 
00051 #include "mac.h"
00052 #include "ll.h"
00053 #include "cmu-trace.h"
00054 
00055 #include "hdr_src.h"
00056 #include "sragent.h"
00057 #include <fstream>
00058 
00059 
00060 
00061 
00062 /*===========================================================================
00063   SRAgent OTcl linkage
00064 ---------------------------------------------------------------------------*/
00065 static class SRAgentClass : public TclClass {
00066 public:
00067   SRAgentClass() : TclClass("Agent/SRAgent") {}
00068   TclObject* create(int, const char*const*) {
00069     return (new SRAgent);
00070   }
00071 } class_SRAgent;
00072 
00073 /*===========================================================================
00074   SRAgent methods
00075 ---------------------------------------------------------------------------*/
00076 SRAgent::SRAgent():Agent(PT_TCP), slot_(0), nslot_(0), maxslot_(-1) 
00077 {
00078   //  cout << " into the constructor " << endl; 
00079 }
00080 
00081 SRAgent::~SRAgent()
00082 {
00083   delete [] slot_;
00084 }
00085 
00086 
00087 
00088 int
00089 SRAgent::command(int argc, const char*const* argv)
00090 {
00091     Tcl& tcl = Tcl::instance();     
00092 
00093     if(argc == 2)
00094       {
00095         if (strcmp(argv[1],"reset") == 0)
00096           {
00097         tcl.resultf("reset done");
00098         return(TCL_OK);
00099           }
00100       }
00101     else if(argc == 3) 
00102       {
00103         if((strcmp(argv[1], "target") == 0))
00104           {
00105         target_ = (NsObject *) TclObject::lookup(argv[2]);
00106         if (target_ == 0) {
00107           tcl.resultf("no such target %s", argv[2]);
00108           return(TCL_ERROR);
00109         }
00110         tcl.resultf(" The target is successfully set");
00111         return(TCL_OK);
00112           }
00113       }
00114     else if(argc == 4)
00115       {
00116         if((strcmp(argv[1], "install_slot") == 0))
00117           {
00118         //cout << " Installing " << argv[2] << " in " << argv[3] << endl;
00119         NsObject* obj = (NsObject*)TclObject::lookup(argv[2]);
00120         if (obj == 0) 
00121           {
00122             tcl.resultf("SRAgent: %s lookup of %s failed\n", argv[1], argv[2]);
00123             return TCL_ERROR;
00124           }
00125         else
00126           {
00127             install(atoi(argv[3]), obj);
00128             return TCL_OK;
00129           }
00130         
00131           }
00132       }
00133     else
00134       {
00135 
00136         if( (strcmp(argv[1], "install_connection") == 0))
00137           {
00138         h_node *node = (h_node *)malloc(sizeof(h_node));
00139         h_node *list;
00140         
00141         node->route_len = argc - 5;
00142         for (int tmp=0; tmp < node->route_len; tmp++)
00143           node->route[tmp] = atoi(argv[5+tmp]);
00144         node->tag = atoi(argv[2]);
00145         
00146         int hash_val = atoi(argv[2])%10;
00147         list = table[hash_val];
00148         node->next = list;
00149         table[hash_val] = node;
00150         
00151         return TCL_OK;
00152         
00153           }
00154       }
00155     return (Agent::command(argc,argv));
00156 }
00157 
00158 void SRAgent::install(int slot, NsObject* p)
00159 {
00160     if (slot >= nslot_)
00161         alloc(slot);
00162     slot_[slot] = p;
00163     if (slot >= maxslot_)
00164         maxslot_ = slot;
00165 }
00166 
00167 
00168 void
00169 SRAgent::recv(Packet* packet, Handler*)
00170 
00171 {
00172     //  int i = 0;
00173     //int n;
00174   hdr_cmn *cmh =  hdr_cmn::access(packet);
00175   hdr_src *srh =  hdr_src::access(packet);
00176   hdr_ip *iph = hdr_ip::access(packet);
00177 
00178   assert(cmh->size() >= 0);
00179   /* insert the route in the header here , also after this the target
00180    * should have been set.*/
00181   if (cmh->src_rt_valid)
00182     {
00183       //cout << "src rt packet" << endl;
00184       
00185       if (srh->cur_addr_ == srh->num_addrs_)
00186         {
00187       // supposedly dest reached
00188       // I can change the packet type and send it to the entry point once again..... this would set things right. This part of the code is managing things at the receiver also..... finally the packet goes to the right agent and not our src rt agent.
00189       //cout << " setting the packet tye as tcp" << endl; 
00190           cmh->src_rt_valid = '\0';
00191           send(packet,0);
00192       return;
00193         }
00194       
00195       int slot_no = srh->addrs[srh->cur_addr_++];
00196       NsObject *node =  slot_[slot_no];
00197       //cout << "passing to the next hop: " << slot_no << endl;
00198       
00199       if (node == NULL)
00200     {
00201       //cout << "src_rt : null node " << slot_no << " accessed" << endl;
00202       //cout << "flow id is " << iph->flowid() << endl;
00203       exit(0);
00204     }
00205       else
00206     {
00207       //cout << "packet of type" << cmh->ptype() << endl;
00208       cmh->src_rt_valid = '1';
00209       
00210       node->recv(packet, (Handler *)0);
00211       
00212     }
00213       return;
00214     }
00215   else
00216     {
00217       // packet from the TCP most probably
00218       h_node *temp_list;
00219       //cout << " Packet of type: "<< cmh->ptype();
00220 
00221       srh->cur_addr_ = 1;
00222       int connect_id = iph->flowid();
00223       //cout << " The flowid is" << connect_id << endl;
00224       temp_list = table[connect_id%10];
00225 
00226       while (temp_list && temp_list->tag != connect_id)
00227     temp_list = temp_list->next;
00228       if (temp_list == NULL)
00229     {
00230       printf(" The connection id is not in routing tabel\n");
00231       exit(0);
00232     }
00233       
00234       srh->num_addrs_ = temp_list->route_len;
00235       // cout << " total number of hops:" << srh->num_addrs_ << endl;
00236       
00237       for (int j = 0; j< srh->num_addrs_; j++ )
00238     {
00239       srh->addrs[j] = temp_list->route[j];
00240       // cout << " setting the next hop "<< srh->addrs[j];
00241     }
00242       //cout << endl;
00243       //set the target before starting anything in the tcl code
00244       
00245       cmh->src_rt_valid = '1';
00246       //      cout << "setting the type to be src_rt" << endl; 
00247       
00248       send(packet,0);
00249       return;
00250     }
00251   
00252 }
00253 
00254 void SRAgent::alloc(int slot)
00255 {
00256     NsObject** old = slot_;
00257     int n = nslot_;
00258     if (old == NULL)
00259         nslot_ = 32;
00260     while (nslot_ <= slot)
00261         nslot_ <<= 1;
00262     slot_ = new NsObject*[nslot_];
00263     memset(slot_, 0, nslot_ * sizeof(NsObject*));
00264     for (int i = 0; i < n; ++i)
00265         slot_[i] = old[i];
00266     delete [] old;
00267 }

Generated on Tue Mar 6 16:47:51 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6