xrootd
Server.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef SERVER_HH
00020 #define SERVER_HH
00021 
00022 #include <map>
00023 #include <string>
00024 #include <utility>
00025 #include <vector>
00026 #include <stdint.h>
00027 #include <pthread.h>
00028 
00029 namespace XrdClTests {
00030 
00031 //------------------------------------------------------------------------------
00033 //------------------------------------------------------------------------------
00034 class ClientHandler
00035 {
00036   public:
00037     //--------------------------------------------------------------------------
00039     //--------------------------------------------------------------------------
00040     ClientHandler();
00041 
00042     //--------------------------------------------------------------------------
00044     //--------------------------------------------------------------------------
00045     virtual ~ClientHandler();
00046 
00047     //--------------------------------------------------------------------------
00051     //--------------------------------------------------------------------------
00052     virtual void HandleConnection( int socket ) = 0;
00053 
00054     //--------------------------------------------------------------------------
00056     //--------------------------------------------------------------------------
00057     void UpdateSentData( char *buffer, uint32_t size );
00058 
00059     //--------------------------------------------------------------------------
00061     //--------------------------------------------------------------------------
00062     void UpdateReceivedData( char *buffer, uint32_t size );
00063 
00064     //--------------------------------------------------------------------------
00066     //--------------------------------------------------------------------------
00067     uint64_t GetSentBytes() const
00068     {
00069       return pSentBytes;
00070     }
00071 
00072     //--------------------------------------------------------------------------
00074     //--------------------------------------------------------------------------
00075     uint32_t GetSentChecksum() const
00076     {
00077       return pSentChecksum;
00078     }
00079 
00080     //--------------------------------------------------------------------------
00082     //--------------------------------------------------------------------------
00083     uint64_t GetReceivedBytes() const
00084     {
00085       return pReceivedBytes;
00086     }
00087 
00088     //--------------------------------------------------------------------------
00090     //--------------------------------------------------------------------------
00091     uint32_t GetReceivedChecksum() const
00092     {
00093       return pReceivedChecksum;
00094     }
00095 
00096 
00097   private:
00098     uint64_t    pSentBytes;
00099     uint64_t    pReceivedBytes;
00100     uint32_t    pSentChecksum;
00101     uint32_t    pReceivedChecksum;
00102 };
00103 
00104 //------------------------------------------------------------------------------
00106 //------------------------------------------------------------------------------
00107 class ClientHandlerFactory
00108 {
00109   public:
00110     //--------------------------------------------------------------------------
00112     //--------------------------------------------------------------------------
00113     virtual ~ClientHandlerFactory() {};
00114 
00115     //--------------------------------------------------------------------------
00117     //--------------------------------------------------------------------------
00118     virtual ClientHandler *CreateHandler() = 0;
00119 };
00120 
00121 //------------------------------------------------------------------------------
00122 // Forward declaration
00123 //------------------------------------------------------------------------------
00124 struct ClientHelper;
00125 
00126 //------------------------------------------------------------------------------
00128 //------------------------------------------------------------------------------
00129 class Server
00130 {
00131   public:
00132     typedef std::map<std::string, std::pair<uint64_t, uint32_t> > TransferMap;
00133 
00134     //--------------------------------------------------------------------------
00136     //--------------------------------------------------------------------------
00137     Server();
00138 
00139     //--------------------------------------------------------------------------
00141     //--------------------------------------------------------------------------
00142     ~Server();
00143 
00144     //--------------------------------------------------------------------------
00151     //--------------------------------------------------------------------------
00152     bool Setup( int port, int accept, ClientHandlerFactory *factory );
00153 
00154     //--------------------------------------------------------------------------
00156     //--------------------------------------------------------------------------
00157     bool Start();
00158 
00159     //--------------------------------------------------------------------------
00162     //--------------------------------------------------------------------------
00163     bool Stop();
00164 
00165     //--------------------------------------------------------------------------
00167     //--------------------------------------------------------------------------
00168     std::pair<uint64_t, uint32_t> GetSentStats( const std::string host ) const;
00169 
00170     //--------------------------------------------------------------------------
00172     //--------------------------------------------------------------------------
00173     std::pair<uint64_t, uint32_t> GetReceivedStats( const std::string host ) const;
00174 
00175     //--------------------------------------------------------------------------
00177     //--------------------------------------------------------------------------
00178     int HandleConnections();
00179 
00180   private:
00181 
00182     TransferMap                 pSent;
00183     TransferMap                 pReceived;
00184     pthread_t                   pServerThread;
00185     std::vector<ClientHelper*>  pClients;
00186     int                         pListenSocket;
00187     ClientHandlerFactory       *pHandlerFactory;
00188 };
00189 
00190 }
00191 
00192 #endif // SERVER_HH