xrootd
XrdClStream.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 __XRD_CL_STREAM_HH__
00020 #define __XRD_CL_STREAM_HH__
00021 
00022 #include "XrdCl/XrdClPoller.hh"
00023 #include "XrdCl/XrdClStatus.hh"
00024 #include "XrdCl/XrdClURL.hh"
00025 #include "XrdCl/XrdClPostMasterInterfaces.hh"
00026 #include "XrdCl/XrdClChannelHandlerList.hh"
00027 #include "XrdCl/XrdClJobManager.hh"
00028 #include "XrdCl/XrdClInQueue.hh"
00029 
00030 #include "XrdSys/XrdSysPthread.hh"
00031 #include <list>
00032 #include <vector>
00033 #include <netinet/in.h>
00034 
00035 namespace XrdCl
00036 {
00037   class  Message;
00038   class  Channel;
00039   class  TransportHandler;
00040   class  TaskManager;
00041   struct SubStreamData;
00042 
00043   //----------------------------------------------------------------------------
00045   //----------------------------------------------------------------------------
00046   class Stream
00047   {
00048     public:
00049       //------------------------------------------------------------------------
00051       //------------------------------------------------------------------------
00052       enum StreamStatus
00053       {
00054         Disconnected    = 0,    
00055         Connected       = 1,    
00056         Connecting      = 2,    
00057         Error           = 3     
00058       };
00059 
00060       //------------------------------------------------------------------------
00062       //------------------------------------------------------------------------
00063       Stream( const URL *url, uint16_t streamNum );
00064 
00065       //------------------------------------------------------------------------
00067       //------------------------------------------------------------------------
00068       ~Stream();
00069 
00070       //------------------------------------------------------------------------
00072       //------------------------------------------------------------------------
00073       Status Initialize();
00074 
00075       //------------------------------------------------------------------------
00077       //------------------------------------------------------------------------
00078       Status Send( Message              *msg,
00079                    OutgoingMsgHandler   *handler,
00080                    bool                  stateful,
00081                    time_t                expires );
00082 
00083       //------------------------------------------------------------------------
00085       //------------------------------------------------------------------------
00086       void SetTransport( TransportHandler *transport )
00087       {
00088         pTransport = transport;
00089       }
00090 
00091       //------------------------------------------------------------------------
00093       //------------------------------------------------------------------------
00094       void SetPoller( Poller *poller )
00095       {
00096         pPoller = poller;
00097       }
00098 
00099       //------------------------------------------------------------------------
00101       //------------------------------------------------------------------------
00102       void SetIncomingQueue( InQueue *incomingQueue )
00103       {
00104         pIncomingQueue = incomingQueue;
00105         delete pQueueIncMsgJob;
00106         pQueueIncMsgJob = new QueueIncMsgJob( incomingQueue );
00107       }
00108 
00109       //------------------------------------------------------------------------
00111       //------------------------------------------------------------------------
00112       void SetChannelData( AnyObject *channelData )
00113       {
00114         pChannelData = channelData;
00115       }
00116 
00117       //------------------------------------------------------------------------
00119       //------------------------------------------------------------------------
00120       void SetTaskManager( TaskManager *taskManager )
00121       {
00122         pTaskManager = taskManager;
00123       }
00124 
00125       //------------------------------------------------------------------------
00127       //------------------------------------------------------------------------
00128       void SetJobManager( JobManager *jobManager )
00129       {
00130         pJobManager = jobManager;
00131       }
00132 
00133       //------------------------------------------------------------------------
00137       //------------------------------------------------------------------------
00138       Status EnableLink( PathID &path );
00139 
00140       //------------------------------------------------------------------------
00142       //------------------------------------------------------------------------
00143       void Disconnect( bool force = false );
00144 
00145       //------------------------------------------------------------------------
00148       //------------------------------------------------------------------------
00149       void Tick( time_t now );
00150 
00151       //------------------------------------------------------------------------
00153       //------------------------------------------------------------------------
00154       const URL *GetURL() const
00155       {
00156         return pUrl;
00157       }
00158 
00159       //------------------------------------------------------------------------
00161       //------------------------------------------------------------------------
00162       uint16_t GetStreamNumber() const
00163       {
00164         return pStreamNum;
00165       }
00166 
00167       //------------------------------------------------------------------------
00169       //------------------------------------------------------------------------
00170       void ForceConnect();
00171 
00172       //------------------------------------------------------------------------
00174       //------------------------------------------------------------------------
00175       const std::string &GetName() const
00176       {
00177         return pStreamName;
00178       }
00179 
00180       //------------------------------------------------------------------------
00182       //------------------------------------------------------------------------
00183       void OnIncoming( uint16_t  subStream,
00184                        Message  *msg,
00185                        uint32_t  bytesReceived );
00186 
00187       //------------------------------------------------------------------------
00188       // Call when one of the sockets is ready to accept a new message
00189       //------------------------------------------------------------------------
00190       std::pair<Message *, OutgoingMsgHandler *>
00191         OnReadyToWrite( uint16_t subStream );
00192 
00193       //------------------------------------------------------------------------
00194       // Call when a message is written to the socket
00195       //------------------------------------------------------------------------
00196       void OnMessageSent( uint16_t  subStream,
00197                           Message  *msg,
00198                           uint32_t  bytesSent );
00199 
00200       //------------------------------------------------------------------------
00202       //------------------------------------------------------------------------
00203       void OnConnect( uint16_t subStream );
00204 
00205       //------------------------------------------------------------------------
00207       //------------------------------------------------------------------------
00208       void OnConnectError( uint16_t subStream, Status status );
00209 
00210       //------------------------------------------------------------------------
00212       //------------------------------------------------------------------------
00213       void OnError( uint16_t subStream, Status status );
00214 
00215       //------------------------------------------------------------------------
00217       //------------------------------------------------------------------------
00218       void OnReadTimeout( uint16_t subStream );
00219 
00220       //------------------------------------------------------------------------
00222       //------------------------------------------------------------------------
00223       void OnWriteTimeout( uint16_t subStream );
00224 
00225       //------------------------------------------------------------------------
00227       //------------------------------------------------------------------------
00228       void RegisterEventHandler( ChannelEventHandler *handler );
00229 
00230       //------------------------------------------------------------------------
00232       //------------------------------------------------------------------------
00233       void RemoveEventHandler( ChannelEventHandler *handler );
00234 
00235       //------------------------------------------------------------------------
00244       //------------------------------------------------------------------------
00245       std::pair<IncomingMsgHandler *, bool>
00246         InstallIncHandler( Message *msg, uint16_t stream );
00247 
00248     private:
00249 
00250       //------------------------------------------------------------------------
00251       // Job queuing the incoming messages
00252       //------------------------------------------------------------------------
00253       class QueueIncMsgJob: public Job
00254       {
00255         public:
00256           QueueIncMsgJob( InQueue *queue ): pQueue( queue ) {};
00257           virtual ~QueueIncMsgJob() {};
00258           virtual void Run( void *arg )
00259           {
00260             Message *msg = (Message *)arg;
00261             pQueue->AddMessage( msg );
00262           }
00263         private:
00264           InQueue *pQueue;
00265       };
00266 
00267       //------------------------------------------------------------------------
00268       // Job handling the incoming messages
00269       //------------------------------------------------------------------------
00270       class HandleIncMsgJob: public Job
00271       {
00272         public:
00273           HandleIncMsgJob( IncomingMsgHandler *handler ): pHandler( handler ) {};
00274           virtual ~HandleIncMsgJob() {};
00275           virtual void Run( void *arg )
00276           {
00277             Message *msg = (Message *)arg;
00278             pHandler->Process( msg );
00279             delete this;
00280           }
00281         private:
00282           IncomingMsgHandler *pHandler;
00283       };
00284 
00285       //------------------------------------------------------------------------
00287       //------------------------------------------------------------------------
00288       void OnFatalError( uint16_t           subStream,
00289                          Status             status,
00290                          XrdSysMutexHelper &lock );
00291 
00292       //------------------------------------------------------------------------
00294       //------------------------------------------------------------------------
00295       void MonitorDisconnection( Status status );
00296 
00297       typedef std::vector<SubStreamData*> SubStreamList;
00298 
00299       //------------------------------------------------------------------------
00300       // Data members
00301       //------------------------------------------------------------------------
00302       const URL                     *pUrl;
00303       uint16_t                       pStreamNum;
00304       std::string                    pStreamName;
00305       TransportHandler              *pTransport;
00306       Poller                        *pPoller;
00307       TaskManager                   *pTaskManager;
00308       JobManager                    *pJobManager;
00309       XrdSysRecMutex                 pMutex;
00310       InQueue                       *pIncomingQueue;
00311       AnyObject                     *pChannelData;
00312       uint16_t                       pLastStreamError;
00313       uint16_t                       pStreamErrorWindow;
00314       uint16_t                       pConnectionCount;
00315       uint16_t                       pConnectionRetry;
00316       time_t                         pConnectionInitTime;
00317       uint16_t                       pConnectionWindow;
00318       SubStreamList                  pSubStreams;
00319       std::vector<sockaddr_in>       pAddresses;
00320       ChannelHandlerList             pChannelEvHandlers;
00321       uint64_t                       pSessionId;
00322 
00323       //------------------------------------------------------------------------
00324       // Jobs
00325       //------------------------------------------------------------------------
00326       QueueIncMsgJob                *pQueueIncMsgJob;
00327 
00328       //------------------------------------------------------------------------
00329       // Monitoring info
00330       //------------------------------------------------------------------------
00331       timeval                        pConnectionStarted;
00332       timeval                        pConnectionDone;
00333       uint64_t                       pBytesSent;
00334       uint64_t                       pBytesReceived;
00335   };
00336 }
00337 
00338 #endif // __XRD_CL_STREAM_HH__