Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

mainFileServer.cpp

Go to the documentation of this file.
00001 #include "Basis/CmdOptions.h"
00002 #include "Util/ChannelServer.h"
00003 #include "Util/Channel.h"
00004 #include "Util/IOBufferFile.h"
00005 
00006 namespace Impala
00007 {
00008 namespace Application
00009 {
00010 
00011 
00012 using namespace Util;
00013 
00014 /******************************************************************
00015  Instances of this class serve files to clients.
00016 *******************************************************************/
00017 class FileServer : public ChannelServer
00018 {
00019 
00020 public: 
00021 
00022     FileServer(int port, int nrPorts, CString passwordFile)
00023         : ChannelServer(port, nrPorts, false, passwordFile)
00024     {
00025         Init();
00026     }
00027 
00028 protected:
00029 
00030     virtual int
00031     AcceptRequest(char* buf, int len, int bufSize, CString conn, int port)
00032     {
00033         String curCon = ConnectionDescr();
00034 
00035         int rcBase = ChannelServer::AcceptRequest(buf, len, bufSize, conn, port);
00036         if (rcBase >= 0)
00037             return rcBase;
00038 
00039         else if (strncmp(buf, "get ", 4) == 0)
00040         {
00041             
00042             ILOG_DEBUG("Doing get");
00043             String fileName(buf+4);
00044             
00045             ILOG_DEBUG("name : [" << fileName << "]");
00046             mIoBuf = new IOBufferFile(fileName, true, false);
00047             if (!mIoBuf->Valid())
00048             {
00049                 delete mIoBuf;
00050                 mIoBuf = 0;
00051                 sprintf(buf, "ERROR : unable to open file %s\0",
00052                         fileName.c_str());
00053                 return strlen(buf)+1;
00054             }
00055             int nrToSend = Min<Int64>(Channel::DATA_BUFFER_SIZE, mIoBuf->Size());
00056             mIoBuf->Read(buf, nrToSend);
00057             if (nrToSend != Channel::DATA_BUFFER_SIZE)
00058             {
00059                 delete mIoBuf;
00060                 mIoBuf = 0;
00061             }
00062             return nrToSend;
00063         }
00064 
00065         else if (strncmp(buf, "getmore", 7) == 0)
00066         {
00067             
00068             ILOG_DEBUG("Doing getmore");
00069             if (mIoBuf == 0)
00070             {
00071                 sprintf(buf, "ERROR : no open file\0");
00072                 return strlen(buf)+1;
00073             }
00074             int nrToSend = Min<Int64>(Channel::DATA_BUFFER_SIZE,
00075                                       mIoBuf->Available());
00076             mIoBuf->Read(buf, nrToSend);
00077             if (nrToSend != Channel::DATA_BUFFER_SIZE)
00078             {
00079                 delete mIoBuf;
00080                 mIoBuf = 0;
00081             }
00082             return nrToSend;
00083         }
00084 
00085         else if (!AllowWrite(port))
00086         {
00087             sprintf(buf, "ERROR : permission denied\0");
00088             return strlen(buf)+1;
00089         }
00090 
00091         else if (strncmp(buf, "put ", 4) == 0)
00092         {
00093             
00094             ILOG_DEBUG("Doing put");
00095             String fileName(buf+4);
00096             
00097             ILOG_DEBUG("name : [" << fileName << "]");
00098             mIoBuf = new IOBufferFile(fileName, false, false);
00099             if (!mIoBuf->Valid())
00100             {
00101                 delete mIoBuf;
00102                 mIoBuf = 0;
00103                 sprintf(buf, "ERROR : unable to open file %s\0",
00104                         fileName.c_str());
00105                 return strlen(buf)+1;
00106             }
00107             sprintf(buf, "OK\0");
00108             return strlen(buf)+1;
00109         }
00110 
00111         else if (strncmp(buf, "putmore", 7) == 0)
00112         {
00113             
00114             ILOG_DEBUG("Doing putmore");
00115             if (mIoBuf == 0)
00116             {
00117                 sprintf(buf, "ERROR : no open file\0");
00118                 return strlen(buf)+1;
00119             }
00120             mIoBuf->Write(buf+8, len-8);
00121             if (len != Channel::DATA_BUFFER_SIZE)
00122             {
00123                 delete mIoBuf;
00124                 mIoBuf = 0;
00125             }
00126             sprintf(buf, "OK\0");
00127             return strlen(buf)+1;
00128         }
00129 
00130         else
00131             return -1;
00132     }
00133 
00134 
00135 private:
00136 
00137     void 
00138     Init()
00139     {
00140         mIoBuf = 0;
00141     }
00142 
00143     IOBufferFile* mIoBuf;
00144 
00145     ILOG_VAR_DEC;
00146 
00147 }; // class
00148 
00149 ILOG_VAR_INIT(FileServer, Impala.Application);
00150 
00151 int
00152 mainFileServer(int argc, char* argv[])
00153 {
00154     CmdOptions& options = CmdOptions::GetInstance();
00155     options.Initialise(true, true, true);
00156     // multiple ports require a lot more bookkeeping for multipart messages
00157     //if (! options.ParseArgs(argc, argv, "port [nrPorts]", 1))
00158     if (! options.ParseArgs(argc, argv, "port", 1))
00159         return 1;
00160 
00161     int port = atol(options.GetArg(0));
00162 
00163     //if (options.GetNrArg() > 1)
00164     //    nrPorts = atol(options.GetArg(1));
00165     int nrPorts = 1;
00166 
00167     String passwordFile = options.GetString("passwordFile");
00168     Application::FileServer fileServer(port, nrPorts, passwordFile);
00169     bool doIdle = false;
00170     fileServer.Start(doIdle);
00171 
00172     return 0;
00173 }
00174 
00175 } // namespace Application
00176 } // namespace Impala
00177 
00178 int
00179 main(int argc, char* argv[])
00180 {
00181     return Impala::Application::mainFileServer(argc, argv);
00182 }

Generated on Fri Mar 19 09:30:28 2010 for ImpalaSrc by  doxygen 1.5.1