00001
00002
00003
00004
00005
00006
00007 #ifndef BALL_SYSTEM_TCPTRANSFER
00008 #define BALL_SYSTEM_TCPTRANSFER
00009
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013
00014 #ifndef BALL_DATATYPE_STRING_H
00015 # include <BALL/DATATYPE/string.h>
00016 #endif
00017
00018 #include <fstream>
00019
00020 #define BUFFER_SIZE 1024
00021
00022 namespace BALL
00023 {
00033 class BALL_EXPORT TCPTransfer
00034 {
00035 public:
00036
00038 typedef int Socket;
00039
00040 static const int TIMEOUT;
00041
00046 enum Status
00047 {
00048 OK = 0,
00049 GETHOSTBYNAME__ERROR ,
00050 SOCKET__ERROR ,
00051 CONNECT__ERROR ,
00052 RECV__ERROR ,
00053 OUTOFMEMORY__ERROR ,
00054 BODY__ERROR ,
00055 UNKNOWN__ERROR ,
00056 ADDRESS__ERROR ,
00057 UNINITIALIZED__ERROR ,
00058 TRANSFER__ERROR ,
00059 SEND__ERROR ,
00060 PORT__ERROR ,
00061 UNKNOWN_PROTOCOL__ERROR ,
00062 LOGON__ERROR ,
00063 PROXY__ERROR ,
00064 FILENOTFOUND__ERROR = 404
00065 };
00066
00069 enum Protocol
00070 {
00072 UNKNOWN_PROTOCOL = 0,
00073
00075 HTTP_PROTOCOL = 1,
00076
00078 FTP_PROTOCOL = 2
00079 };
00080
00085 class BALL_EXPORT TransferFailed
00086 : public Exception::GeneralException
00087 {
00088 public:
00089
00090 TransferFailed(const char* file, int line, Index error_code);
00091 };
00092
00096 TCPTransfer();
00097
00105 TCPTransfer(std::ostream& file, const String& address)
00106 throw(TransferFailed);
00107
00109 virtual ~TCPTransfer();
00110
00115 void clear();
00116
00121 bool set(::std::ostream& file, const String& address);
00122
00123
00127 void set(::std::ostream& file,
00128 Protocol protocol,
00129 const String& host_address,
00130 const String& file_address,
00131 const String& login,
00132 const String& password,
00133 Position port = 80);
00134
00137 const String& getHostAddress() const
00138 {
00139 return host_address_;
00140 }
00141
00143 const String& getFileAddress() const
00144 {
00145 return file_address_;
00146 }
00147
00149 Position getPort() const
00150 {
00151 return port_;
00152 }
00153
00157 Status getStatusCode() const
00158 {
00159 return status_;
00160 }
00161
00163 String getErrorCode() const;
00164
00166 Position getReceivedBytes() const
00167 {
00168 return received_bytes_;
00169 }
00170
00174 Protocol getProtocol() const
00175 {
00176 return protocol_;
00177 }
00178
00180 const String& getLogin() const
00181 {
00182 return login_;
00183 }
00184
00186 const String& getPassword() const
00187 {
00188 return password_;
00189 }
00190
00192 const ::std::ostream* getStream() const
00193 {
00194 return fstream_;
00195 }
00196
00202 const char* getBuffer() const
00203 {
00204 return &(buffer_[0]);
00205 }
00206
00213 Status transfer();
00214
00216 void setProxy(const String proxy_address, Position port);
00217
00219 bool usingProxy() const;
00220
00222 void abort() { abort_ = true;}
00223
00228 void dump(std::ostream& s = std::cout, Size depth = 0) const;
00229
00230 protected:
00231
00232 String host_address_;
00233 String file_address_;
00234 Position port_;
00235 String login_;
00236 String password_;
00237 Status status_;
00238 int received_bytes_;
00239 Protocol protocol_;
00240 char buffer_[BUFFER_SIZE + 1];
00241 Socket socket_;
00242 std::ostream* fstream_;
00243 String proxy_address_;
00244 Position proxy_port_;
00245 bool abort_;
00246
00247
00248
00249 Status sendData_(const String& query, Socket socket);
00250
00251
00252
00253
00254 Status logon_(const String& query);
00255
00256
00257 Status getFTP_();
00258
00259
00260 Status getHTTP_();
00261
00262
00263
00264
00265
00266 bool getFTPMessage_(Index status);
00267
00268
00269 Status getFTPStatus_();
00270
00271
00272 Status getHTTPStatus_();
00273
00274
00275 Status setBlock_(Socket socket, bool block = true);
00276
00277
00278 bool waitForOutput_(const String& key, Size seconds);
00279
00280
00281 int getReceivedBytes_(Socket& socket);
00282
00285 int readInputFromSocket_(Socket& socket);
00286
00287 private:
00288
00289 void operator = (TCPTransfer);
00290 };
00291
00292 }
00293
00294 #endif // BALL_SYSTEM_TCPTRANSFER_H