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 static const char rcsid[] =
00039 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/baytcp/ftpc.cc,v 1.5 2001/09/06 21:01:18 johnh Exp $ (LBL)";
00040
00041 #include "tcp-full-bay.h"
00042 #include "tclcl.h"
00043 #include "trace.h"
00044 #include "random.h"
00045 #include "ftp.h"
00046
00047
00048 static class FtpClientClass : public TclClass {
00049 public:
00050 FtpClientClass() : TclClass("Agent/BayTcpApp/FtpClient") {}
00051 TclObject* create(int, const char*const*) {
00052 return (new FtpClientAgent());
00053 }
00054 } class_ftpcli;
00055
00056 FtpClientAgent::FtpClientAgent() : BayTcpAppAgent(PT_NTYPE), running_(0), newfile_timer_(this)
00057 {
00058 }
00059
00060 void FtpClientAgent::start()
00061 {
00062 running_ = 1;
00063 newfile_timer_.resched(0.);
00064 }
00065
00066 void FtpClientAgent::stop()
00067 {
00068 running_ = 0;
00069 }
00070
00071 void FtpClientAgent::timeout(int event_type)
00072 {
00073 if (running_)
00074 if(event_type == NEW_FILE) {
00075 if(sendget()) {
00076 state_ = REQ_SENT;
00077 start_trans_ = now();
00078 }
00079 else {
00080 printf("ftpclient:timeout erroneous tcp state\n");
00081 }
00082 }
00083 }
00084
00085
00086 int FtpClientAgent::sendget()
00087 {
00088 return tcp_->advance(80, 0);
00089 }
00090
00091
00092
00093
00094 void FtpClientAgent::recv(Packet*, BayFullTcpAgent*, int code)
00095 {
00096
00097
00098 if(running_ && code == DATA_PUSH) {
00099 state_ = DATA_RCVD;
00100 newfile_timer_.resched(5.0);
00101 }
00102 else if(running_ && code == CONNECTION_END) {
00103 state_ = END_RCVD;
00104 newfile_timer_.cancel();
00105 newfile_timer_.resched(.0);
00106 }
00107 }
00108
00109 int FtpClientAgent::command(int argc, const char*const* argv)
00110 {
00111 Tcl& tcl = Tcl::instance();
00112 if (argc == 2) {
00113 if (strcmp(argv[1], "start") == 0) {
00114 start();
00115 return(TCL_OK);
00116 } else if (strcmp(argv[1], "stop") == 0) {
00117 stop();
00118 return(TCL_OK);
00119 }
00120 } else if(argc == 3) {
00121 if(strcmp(argv[1], "tcp") == 0) {
00122 tcp_ = (BayFullTcpAgent*)TclObject::lookup(argv[2]);
00123 if(tcp_ == 0) {
00124 tcl.resultf("no such agent %s", argv[2]);
00125 return(TCL_ERROR);
00126 }
00127 return(TCL_OK);
00128 }
00129 }
00130 return (Agent::command(argc, argv));
00131 }
00132
00133
00134 void NewFileTimer::expire(Event *e) {
00135 a_->timeout(NEW_FILE);
00136 }