00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00075
00076 SRAgent::SRAgent():Agent(PT_TCP), slot_(0), nslot_(0), maxslot_(-1)
00077 {
00078
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
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
00173
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
00180
00181 if (cmh->src_rt_valid)
00182 {
00183
00184
00185 if (srh->cur_addr_ == srh->num_addrs_)
00186 {
00187
00188
00189
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
00198
00199 if (node == NULL)
00200 {
00201
00202
00203 exit(0);
00204 }
00205 else
00206 {
00207
00208 cmh->src_rt_valid = '1';
00209
00210 node->recv(packet, (Handler *)0);
00211
00212 }
00213 return;
00214 }
00215 else
00216 {
00217
00218 h_node *temp_list;
00219
00220
00221 srh->cur_addr_ = 1;
00222 int connect_id = iph->flowid();
00223
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
00236
00237 for (int j = 0; j< srh->num_addrs_; j++ )
00238 {
00239 srh->addrs[j] = temp_list->route[j];
00240
00241 }
00242
00243
00244
00245 cmh->src_rt_valid = '1';
00246
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 }