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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include "agent.h"
00051 #include "app.h"
00052 #include "tcpapp.h"
00053
00054
00055
00056
00057 CBuf::CBuf(AppData *c, int nbytes)
00058 {
00059 nbytes_ = nbytes;
00060 size_ = c->size();
00061 if (size_ > 0)
00062 data_ = c;
00063 else
00064 data_ = NULL;
00065 next_ = NULL;
00066 }
00067
00068 CBufList::~CBufList()
00069 {
00070 while (head_ != NULL) {
00071 tail_ = head_;
00072 head_ = head_->next_;
00073 delete tail_;
00074 }
00075 }
00076
00077 void CBufList::insert(CBuf *cbuf)
00078 {
00079 if (tail_ == NULL)
00080 head_ = tail_ = cbuf;
00081 else {
00082 tail_->next_ = cbuf;
00083 tail_ = cbuf;
00084 }
00085 #ifdef TCPAPP_DEBUG
00086 num_++;
00087 #endif
00088 }
00089
00090 CBuf* CBufList::detach()
00091 {
00092 if (head_ == NULL)
00093 return NULL;
00094 CBuf *p = head_;
00095 if ((head_ = head_->next_) == NULL)
00096 tail_ = NULL;
00097 #ifdef TCPAPP_DEBUG
00098 num_--;
00099 #endif
00100 return p;
00101 }
00102
00103
00104
00105
00106 class TcpAppString : public AppData {
00107 private:
00108 int size_;
00109 char* str_;
00110 public:
00111 TcpAppString() : AppData(TCPAPP_STRING), size_(0), str_(NULL) {}
00112 TcpAppString(TcpAppString& d) : AppData(d) {
00113 size_ = d.size_;
00114 if (size_ > 0) {
00115 str_ = new char[size_];
00116 strcpy(str_, d.str_);
00117 } else
00118 str_ = NULL;
00119 }
00120 virtual ~TcpAppString() {
00121 if (str_ != NULL)
00122 delete []str_;
00123 }
00124
00125 char* str() { return str_; }
00126 virtual int size() const { return AppData::size() + size_; }
00127
00128
00129 void set_string(const char* s) {
00130 if ((s == NULL) || (*s == 0))
00131 str_ = NULL, size_ = 0;
00132 else {
00133 size_ = strlen(s) + 1;
00134 str_ = new char[size_];
00135 assert(str_ != NULL);
00136 strcpy(str_, s);
00137 }
00138 }
00139 virtual AppData* copy() {
00140 return new TcpAppString(*this);
00141 }
00142 };
00143
00144
00145 static class TcpCncClass : public TclClass {
00146 public:
00147 TcpCncClass() : TclClass("Application/TcpApp") {}
00148 TclObject* create(int argc, const char*const* argv) {
00149 if (argc != 5)
00150 return NULL;
00151 Agent *tcp = (Agent *)TclObject::lookup(argv[4]);
00152 if (tcp == NULL)
00153 return NULL;
00154 return (new TcpApp(tcp));
00155 }
00156 } class_tcpcnc_app;
00157
00158 TcpApp::TcpApp(Agent *tcp) :
00159 Application(), curdata_(0), curbytes_(0)
00160 {
00161 agent_ = tcp;
00162 agent_->attachApp(this);
00163 }
00164
00165 TcpApp::~TcpApp()
00166 {
00167
00168
00169 agent_->attachApp(NULL);
00170 }
00171
00172
00173 void TcpApp::send(int nbytes, AppData *cbk)
00174 {
00175 CBuf *p = new CBuf(cbk, nbytes);
00176 #ifdef TCPAPP_DEBUG
00177 p->time() = Scheduler::instance().clock();
00178 #endif
00179 cbuf_.insert(p);
00180 Application::send(nbytes);
00181 }
00182
00183
00184 void TcpApp::recv(int size)
00185 {
00186
00187
00188 if (curdata_ == 0)
00189 curdata_ = dst_->rcvr_retrieve_data();
00190 if (curdata_ == 0) {
00191 fprintf(stderr, "[%g] %s receives a packet but no callback!\n",
00192 Scheduler::instance().clock(), name_);
00193 return;
00194 }
00195 curbytes_ += size;
00196 #ifdef TCPAPP_DEBUG
00197 fprintf(stderr, "[%g] %s gets data size %d, %s\n",
00198 Scheduler::instance().clock(), name(), curbytes_,
00199 curdata_->data());
00200 #endif
00201 if (curbytes_ == curdata_->bytes()) {
00202
00203
00204 process_data(curdata_->size(), curdata_->data());
00205
00206 delete curdata_;
00207 curdata_ = NULL;
00208 curbytes_ = 0;
00209 } else if (curbytes_ > curdata_->bytes()) {
00210
00211
00212 while (curbytes_ >= curdata_->bytes()) {
00213 process_data(curdata_->size(), curdata_->data());
00214 curbytes_ -= curdata_->bytes();
00215 #ifdef TCPAPP_DEBUG
00216 fprintf(stderr,
00217 "[%g] %s gets data size %d(left %d)\n",
00218 Scheduler::instance().clock(),
00219 name(),
00220 curdata_->bytes(), curbytes_);
00221
00222 #endif
00223 delete curdata_;
00224 curdata_ = dst_->rcvr_retrieve_data();
00225 if (curdata_ != 0)
00226 continue;
00227 if ((curdata_ == 0) && (curbytes_ > 0)) {
00228 fprintf(stderr, "[%g] %s gets extra data!\n",
00229 Scheduler::instance().clock(), name_);
00230 Tcl::instance().eval("[Simulator instance] flush-trace");
00231 abort();
00232 } else
00233
00234 break;
00235 }
00236 }
00237 }
00238
00239 void TcpApp::resume()
00240 {
00241
00242 }
00243
00244 int TcpApp::command(int argc, const char*const* argv)
00245 {
00246 Tcl& tcl = Tcl::instance();
00247
00248 if (strcmp(argv[1], "connect") == 0) {
00249 dst_ = (TcpApp *)TclObject::lookup(argv[2]);
00250 if (dst_ == NULL) {
00251 tcl.resultf("%s: connected to null object.", name_);
00252 return (TCL_ERROR);
00253 }
00254 dst_->connect(this);
00255 return (TCL_OK);
00256 } else if (strcmp(argv[1], "send") == 0) {
00257
00258
00259
00260 int size = atoi(argv[2]);
00261 if (argc == 3)
00262 send(size, NULL);
00263 else {
00264 TcpAppString *tmp = new TcpAppString();
00265 tmp->set_string(argv[3]);
00266 send(size, tmp);
00267 }
00268 return (TCL_OK);
00269 } else if (strcmp(argv[1], "dst") == 0) {
00270 tcl.resultf("%s", dst_->name());
00271 return TCL_OK;
00272 }
00273 return Application::command(argc, argv);
00274 }
00275
00276 void TcpApp::process_data(int size, AppData* data)
00277 {
00278 if (data == NULL)
00279 return;
00280
00281
00282 if (target())
00283 send_data(size, data);
00284 else if (data->type() == TCPAPP_STRING) {
00285 TcpAppString *tmp = (TcpAppString*)data;
00286 Tcl::instance().eval(tmp->str());
00287 }
00288 }