00001 #include "Util/Channel.h"
00002 #include "Basis/CmdOptions.h"
00003 #include "Util/ChannelPool.h"
00004 #include "Basis/Timer.h"
00005 #include "Util/IOBufferFile.h"
00006
00007 namespace Impala
00008 {
00009 namespace Application
00010 {
00011 namespace FileClient
00012 {
00013
00014 using namespace Util;
00015
00016 int gVerbose;
00017 Channel* gChannel;
00018
00019 void
00020 DoGet(String fileName)
00021 {
00022 std::cout << "Getting " << fileName << " ... " << std::flush;
00023 Timer timer(1);
00024 char* buf = gChannel->Buffer();
00025 sprintf(buf, "get %s\0", fileName.c_str());
00026 int len = gChannel->SendRequest(strlen(buf)+1);
00027 buf[len] = 0;
00028 if ((len > 5) && (strncmp(buf, "ERROR", 5) == 0))
00029 return;
00030 IOBufferFile ioBuf(fileName, false, false);
00031 ioBuf.Write(buf, len);
00032 double b = len;
00033 while (len == Channel::DATA_BUFFER_SIZE)
00034 {
00035 sprintf(buf, "getmore\0");
00036 len = gChannel->SendRequest(strlen(buf)+1);
00037 buf[len] = 0;
00038 ioBuf.Write(buf, len);
00039 b += len;
00040 std::cout << "#" << std::flush;
00041 }
00042 double timeVal = timer.SplitTime();
00043 double bps = b / (1024.0*1024.0 * timeVal);
00044 std::cout << " done (in " << timeVal << " sec = " << bps << " Mb/sec)"
00045 << std::endl;
00046 }
00047
00048 void
00049 DoPut(String fileName)
00050 {
00051 std::cout << "Putting " << fileName << " ... " << std::flush;
00052 Timer timer(1);
00053 char* buf = gChannel->Buffer();
00054 String dstFileName = FileNameTail(fileName);
00055 sprintf(buf, "put %s\0", dstFileName.c_str());
00056 int len = gChannel->SendRequest(strlen(buf)+1);
00057 buf[len] = 0;
00058 if ((len > 5) && (strncmp(buf, "ERROR", 5) == 0))
00059 return;
00060 IOBufferFile ioBuf(fileName, true, false);
00061 double b = ioBuf.Size();
00062 while (ioBuf.Available() > 0)
00063 {
00064 sprintf(buf, "putmore\0");
00065 int nrData = Min<Int64>(Channel::DATA_BUFFER_SIZE-8, ioBuf.Available());
00066 ioBuf.Read(buf+8, nrData);
00067 len = gChannel->SendRequest(nrData+8);
00068 if ((len > 5) && (strncmp(buf, "ERROR", 5) == 0))
00069 return;
00070 std::cout << "#" << std::flush;
00071 }
00072 double timeVal = timer.SplitTime();
00073 double bps = b / (1024.0*1024.0 * timeVal);
00074 std::cout << " done (in " << timeVal << " sec = " << bps << " Mb/sec)"
00075 << std::endl;
00076 }
00077
00078 int
00079 mainFileClient(int argc, char* argv[])
00080 {
00081 CmdOptions& options = CmdOptions::GetInstance();
00082 options.Initialise(false);
00083 String usageStr = "{put|get} servername port filename(s)\n\n";
00084 if (! options.ParseArgs(argc, argv, usageStr, 4))
00085 return 1;
00086
00087 String cmd = options.GetArg(0);
00088 String server = options.GetArg(1);
00089 int port = atol(options.GetArg(2));
00090
00091 gVerbose = options.GetInt("verb");
00092
00093
00094 gChannel = ChannelPool::Instance().Get(server, port, ".passwords");
00095 if (!gChannel || !gChannel->Valid())
00096 return 1;
00097
00098 if (cmd == "get")
00099 {
00100 for (int i=3 ; i<options.GetNrArg() ; i++)
00101 DoGet(options.GetArg(i));
00102 }
00103 else if (cmd == "put")
00104 {
00105 for (int i=3 ; i<options.GetNrArg() ; i++)
00106 DoPut(options.GetArg(i));
00107 }
00108
00109
00111
00112
00113 return 0;
00114 }
00115
00116 }
00117 }
00118 }
00119
00120 int
00121 main(int argc, char* argv[])
00122 {
00123 return Impala::Application::FileClient::mainFileClient(argc, argv);
00124 }