OpendTect  6.3
odhttp.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6 (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7 Author: Salil Agarwal
8 Date: Oct 2012
9 ________________________________________________________________________
10 
11 -*/
12 
13 
14 #include "networkmod.h"
15 
16 #include "notify.h"
17 #include "iopar.h"
18 #include "limits.h"
19 #include "manobjectset.h"
20 #include "ptrman.h"
21 #include "uistring.h"
22 
23 class QEventLoop;
24 class QNetworkReply;
25 class QNetworkReplyConn;
26 class QNetworkRequest;
27 class QNetworkAccessManager;
28 class DataBuffer;
29 class QByteArray;
30 class QSslError;
31 template <class T> class QList;
32 
33 
34 namespace Network
35 {
36 class HttpRequestProcess;
37 class HttpRequestManager;
38 
41 mExpClass(Network) HttpRequest : public RefCount::Referenced
42 {
43 public:
44  enum AccessType { Get, Put, Delete, Post, Head };
45  HttpRequest(const char* url,
46  AccessType);
47  HttpRequest(const HttpRequest&);
48 
49  HttpRequest& payloadData(const DataBuffer&);
50  HttpRequest& contentType(const BufferString&);
51  HttpRequest& rawHeader(const char* key,
52  const char* val);
53 
54  void setPayloadData(const DataBuffer&);
56  void setContentType(const BufferString&);
57  void setRawHeader(const char* key,
58  const char* val);
59 protected:
60  ~HttpRequest();
61 private:
62 
63  friend class HttpRequestManager;
64 
65  void fillRequest(QNetworkRequest&) const;
66  QByteArray* payload_;
71 };
72 
73 
82 {
83 public:
84  static HttpRequestManager& instance();
85 
88 
90  RefMan<HttpRequestProcess> get(const char* url);
91  RefMan<HttpRequestProcess> head(const char* url);
92 
93 private:
94  static void CloseInstance();
95  void shutDownThreading();
96 
97  void threadFuncCB(CallBacker*);
98 
100  QNetworkAccessManager* qnam_;
101  QEventLoop* eventloop_;
103  //Temporary, only used in constructor
104 
105  void doRequestCB(CallBacker*);
106  //Only called in thread_.
107  //CB must be HttpRequestProcess
108 
111 };
112 
113 
116 mExpClass(Network) HttpRequestProcess : public RefCount::Referenced,
117  public CallBacker
118 {
119 public:
120  //General purpose callbacks
123 
124  bool isRunning() const;
125  bool isFinished() const;
126  bool isError() const;
127 
128  void waitForFinish(int timeout_in_ms=-1);
129  //<Waits for error or Finish
130 
131  //Download access
133  bool waitForDownloadData(int timeout_ms=-1);
134  //Returns false if timeout was hit
135  od_int64 downloadBytesAvailable() const;
136  od_int64 read(char*,od_int64 bufsize);
138  BufferString readAll();
139 
140  od_int64 getContentLengthHeader() const;
141 
142  //Upload access
144  od_int64 getBytesUploaded() const;
145  od_int64 getTotalBytesToUpload() const;
146 
147  uiString errMsg() const;
148 
149 private:
150 
151  //Interface from QNetworkReplyConn
152  friend class ::QNetworkReplyConn;
153 
154  void reportDownloadProgress(od_int64 nrdone,
155  od_int64 totalnr);
156  void reportError();
157  void reportSSLErrors(const QList<QSslError>&);
158  void reportFinished();
159  void reportUploadProgress(od_int64 bytes,
160  od_int64 totalbytes);
161  void reportMetaDataChanged();
162  void reportReadyRead();
163 
164  //Interface from NetworkAccessManager
165  friend class HttpRequestManager;
166  void setQNetworkReply(QNetworkReply*);
170  bool waitForRequestStart();
177 
178  enum Status { NotStarted, //Before setQNetworkReply
179  Running, Error, Finished };
180 
183 
185 
191 
193  QNetworkReply* qnetworkreply_;
194 
195  QByteArray* receiveddata_;
197 
199 };
200 
201 }; //namespace Network
#define mExpClass(module)
Definition: commondefs.h:157
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
Definition: odhttp.h:81
Inherit from this class to be able to send and/or receive CallBacks.
Definition: callback.h:182
Definition: odhttp.h:116
Notifier< HttpRequestProcess > uploadProgress
Definition: odhttp.h:143
Definition: jobcommunic.h:22
Threads::Atomic< od_int64 > totalbytestodownload_
Definition: odhttp.h:189
BufferString url_
Definition: odhttp.h:67
#define od_int64
Definition: plftypes.h:34
Status
Definition: odhttp.h:178
QEventLoop * eventloop_
Definition: odhttp.h:101
Status status_
Definition: odhttp.h:181
QNetworkReplyConn * qnetworkreplyconn_
Definition: odhttp.h:192
const AccessType accesstype_
Definition: odhttp.h:70
Definition: uistring.h:88
Threads::ConditionVar * eventlooplock_
Definition: odhttp.h:102
Class to help setup a callback handling.
Definition: notify.h:121
Definition: odhttp.h:44
Definition: i_odhttpconn.h:27
Notifier< HttpRequestProcess > finished
Definition: odhttp.h:121
Is the base class for all threads. Start it by creating it and give it the function or CallBack to ex...
Definition: thread.h:290
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:53
Is an alternative to Mutex. It is a lock which causes a thread trying to acquire it to simply wait in...
Definition: atomic.h:130
Threads::Atomic< od_int64 > contentlengthheader_
Definition: odhttp.h:190
QByteArray * receiveddata_
Definition: odhttp.h:195
ConstRefMan< HttpRequest > request_
Definition: odhttp.h:184
QNetworkReply * qnetworkreply_
Definition: odhttp.h:193
uiString errmsg_
Definition: odhttp.h:198
Resizable buffer of elements.
Definition: databuf.h:19
IOPar rawheaders_
Definition: odhttp.h:69
RefObjectSet< HttpRequestProcess > activeevents_
Definition: odhttp.h:109
Threads::Atomic< od_int64 > bytesdownloaded_
Definition: odhttp.h:188
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:38
Threads::Thread * thread_
Definition: odhttp.h:99
Definition: manobjectset.h:78
Definition: oduicommon.h:65
QNetworkAccessManager * qnam_
Definition: odhttp.h:100
Threads::ConditionVar statuslock_
Definition: odhttp.h:182
BufferString contenttype_
Definition: odhttp.h:68
const char * errMsg() const
Definition: horizontracker.h:59
Definition: odhttp.h:179
QByteArray * payload_
Definition: odhttp.h:66
Definition: odhttp.h:41
bool isFinished(const uiRetVal &)
Threads::SpinLock activeeventslock_
Definition: odhttp.h:110
Notifier< HttpRequestProcess > downloadDataAvailable
Definition: odhttp.h:132
Threads::Atomic< od_int64 > bytesuploaded_
Definition: odhttp.h:186
AccessType
Definition: odhttp.h:44
Definition: ptrman.h:227
Threads::ConditionVar receiveddatalock_
Definition: odhttp.h:196
Notifier< HttpRequestProcess > error
Definition: odhttp.h:122
Threads::Atomic< od_int64 > totalbytestoupload_
Definition: odhttp.h:187
Definition: ptrman.h:200
Definition: odhttp.h:31

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017