How to send arbitrary download request to proper handler - c++

I'm building a Qt application and using the QNetworkAccessManager to manager my http requests. Due to the asyncronous nature of QNetworkAccessManager you have to bind a slot to recieve the QNetworkReply when it's done.
I'm new to multithreaded design so I'm not sure how to handle this. I will have 3 seperate types of network replies which need to be parsed in the bound slot and then passed to the correct handler. One will extract a link, the 2nd will extract a picture, and the third will parse a post request's reply to verify that it was successful. All of this takes place within a QWidget subclass.
So, I'm wondering how this is normally handled. As I see it, based purely on intuition as I've done little reading on this specific subject, I would think there are two ways to handle this. One would be to rebind the network manager's finished() signal depending on the call and the other would be to use some sort of state flags and check those to see what the reply is expected to be. What is the preferred method, not necessarily between these two, that's just all I could think of myself, someone more experienced may well have a better solution.
Now, I'm also fairly new to c++, so if the later is the better way what's the best way to handle flags in this case? Would I use a bitset, an enum/flag setting function, or something else? Thanks guys!

If you know the type of reply you're going to get as a result of doing specific request you can connect void QNetworkReply::finished () signal to suitable handler.

Related

Simulating network failures with QNetworkAccessManager & QNetworkReply

I'm using Qt4.8 and I want to inject network faults into pre-existing code that uses QNetworkAccessManager. However, this class - and particularly the related QNetworkReply seem to have some strange semantics. I created a subclass of QNetworkAccessManager as well as one of QNetworkReply. My QTestNetworkAccessManager returns either an object created by the base QNetworkAccessManager, or the next test reply in the list:
QNetworkReply *QTestNetworkAccessManager
::createRequest( Operation op,
const QNetworkRequest &req,
QIODevice * outgoingData )
{
// QList<QTestNetworkReply*> m_replies;
if (m_replies.isEmpty())
{
return QNetworkAccessManager::
createRequest( op, req, outgoingData );
}
QTestNetworkReply* pReply = m_replies.takeFirst();
pReply->setUrl(req.url()); // lie about URLs
// this sets a timer that fires finished, etc.
pReply->queueFinished();
return pReply;
}
In the simple, trivial case of a single request, my test code seems to work, but two problems show up when I put it in a bigger app doing lots of network traffic.
First is QNetworkAccessManager seems to have private knowledge of the QNetworkReply objects it's creating. (based on inspecting the Qt source). For example it creates various flavors of QHttpReplyImpl and various other Impl objects and hooks them up in various ways, so my test objects don't seem to be good citizens in that environment. This seems to cause problems like error() signals not propagating correctly and my memory space is experiencing stack corruption.
(I'm not posting the entire QTextNetworkReply object because it's pretty simple and my question isn't really to troubleshoot my specific code.)
My question is: Has anyone done this kind of fault injection with QNetworkAccessManager and been able to simulate various HTTP faults.
One thought was to use the HttpProxy hooks to create a proxy server that actually injects data at the socket level so that it traverses the entire QHttp* object parser, but that seems like a lot of trouble.
Is there really no easier way to inject faults into QNetworkAccessManager so it acts like it is talking to a live server?
Thanks!
P.S. I am familiar with https://blogs.kde.org/2010/08/28/implementing-reusable-custom-qnetworkreply which is why I went down the road I'm on of subclassing QNAM, but it doesn't seem to handle the error semantics correctly and the gitorious link to the code is broken.

Managing asynchronous communication: how to examine response received in another thread

I have a dispatcher thread and a listener thread. When I dispatch a command, I want to wait for response before I send follow up command. Moreover I need to examine the respond before I can proceed with 2nd command, the least of which is to confirm the response is received and everything is okay. My pseudo code is below:
void MainWindow::downloadData()
{
dispatcher->getInfo(); // sends command
// QString response = receiver->response() // idealy I would like to check response but since its async, i can't really do that!
dipatcher->askData(); // the 2nd command and so forth
}
Is there any elegant way to solve this issue? The only way I can think of is if I use the same thread and all calls are blocking but that's not necessarily a good solution.
In Qt, I could use signals and slots to connect them in cascading manner so when the first signal is triggered it initiates the whole sequence of operation (each slot emitting a new signal) but seems rather dirty as well.
One of the most robust ways to handle asynchronous events and process a chains/graphs of actions upon these events are FSMs. Qt provides a basis for implementing FSMs with its Qt-State machine framework. I'd suggest to go this way. Unfortunately all the examples provided by Qt for FSM are dealing with GUIs and animations.
The advantage of FSM approach is, FSMs can be represented both as graphs and as tables. The first option is great for understanding, the second for validation, that there are no endless loops and "dead" ends.
I've built on basis of Qt-FSM framework own framework for defining FSMs in a domain specific language. I use it for controlling a complex machine having couple of sensors actors all working asynchronously. Using DSL helps me to implement in higher abstraction - in the abstraction level of FSM-graphs.

How to stop QNetworkAccessManager from getting a reply C++

Hi I have a QNetworkAccessManager which I use to send request to get image data from server. This call is asynchronous. I do multiple calls with it. Each call is done by opening a new instance of QNetworkAccessManager So when a specific condition occurs I want to stop the QNetworkAccessManager from receiving the replies from its network requests. Is there any way to do it? Thanks.
Don't use a new QNetworkAccessManager for each request but share the manager. It's usually fine to have just one. Of course one can have multiple if the application design suggests it - but e.g. managing multiple of them in a single controlling object is usually unnecessary. Just have one manager with the same lifetime as the object controlling the network requests.
To cancel running operations, keep the QNetworkReply* pointers QNetworkAccessManager::get/put/post return and call abort() when your condition occurs.
Connect to the finished() signal to remove them from the bookkeeping (as otherwise you would end up with dangling pointers).
If that becomes too complicated, think about using the command pattern. In this answer I describe why I find it particularly useful in this context.

Making an Qt HTTP request and receiving the response in a single function call

I'm attempting to create a library whose API will be used in the following way:
WebService *service = new WebService( username, password );
User *user = service->getAuthenticatedUser();
UserAssets *assets = user->assets();
// And so on
Neither the authenticated user, nor their assets, will be downloaded when the WebServer instance is created, rather they will only be retrieved if explicitly requested.
Whenever I've had to retrieve data from the network before using Qt, I've followed the standard pattern of connection the finished() signal of the QNetworkReply to the appropriate slot and using that slot to handle the returned data.
My problem here is that pattern does not seem to accommodate my desired use-case. I would like the users of my library (other developers) to be able to use a single line to request and receive the data they desire, but the 'Qt way' seems, at least from my point of view, to require them to initiate the request on one line, and then connect some signal to some other slot to get the data back, which is not the experience I would like them to have.
I'm new to network programming, both in general and with Qt, but I've used libraries written in Python, communicating with the exact same service, that are able to achieve this, so it does seem to be possible.
Is it possible to perform the full lifecycle of a HTTP request with Qt with a single function call?
Your best bet is probably to use a QEventLoop. This would allow you to 1) initiate the HTTP connection and, from your caller's perspective, 2) effectively block until you get a response.
For example:
QNetworkReply wait for finished
As already other have mentioned you could use QEventLoop to wait for finished() or error() signals, and the quitting event loop. This solution while working, have some serious disadvantages.
If it takes longer to download given address, then you might be stuck in your event loop for quite a while. The event loop is processing events nicely, so your app doesn't frezze, but there are some quirks connected to it anyway. Imagine that user is waiting for load, and then presses another button, to load something else. Then you will have multiple loop-in-loop, and first file will have to wait for the second to finish downloading.
Doing things in single call suggest to many programmers, that this will happen at one instant. But your function is processing events internally, so this might not hold. Imagine a code like
// some pointer accessible to many functions/methods (eg. member, global)
MyData* myData=0;
Then a code calling your function:
if (myData){
QNetworkReply* reply = getMyWobsite(whatever);
myData->modify(reply);
}
Seems fine, but what if some other slot happens to call
myData=0;
If this slot will be executed while waiting for request, application will crash. If you decide to use QEventLoop in your function, be sure to mention it in function documentation, so programmers using it will be able to avoid such problems.
If you are not using qt for anything else, you might even consider some alternative libraries (eg. libcurl) that might have what you need already implemented.

XMLRPCPP asynchronously handling multiple calls?

I have a remote server which handles various different commands, one of which is an event fetching method.
The event fetch returns right away if there is 1 or more events listed in the queue ready for processing. If the event queue is empty, this method does not return until a timeout of a few seconds. This way I don't run into any HTTP/socket timeouts. The moment an event becomes available, the method returns right away. This way the client only ever makes connections to the server, and the server does not have to make any connections to the client.
This event mechanism works nicely. I'm using the boost library to handle queues, event notifications, etc.
Here's the problem. While the server is holding back on returning from the event fetch method, during that time, I can't issue any other commands.
In the source code, XmlRpcDispatch.cpp, I'm seeing in the "work" method, a simple loop that uses a blocking call to "select".
Seems like while the handling of a method is busy, no other requests are processed.
Question: am I not seeing something and can XmlRpcpp (xmlrpc++) handle multiple requests asynchronously? Does anyone know of a better xmlrpc library for C++? I don't suppose the Boost library has a component that lets me issue remote commands?
I actually don't care about the XML or over-HTTP feature. I simply need to issue (asynchronous) commands over TCP in any shape or form?
I look forward to any input anyone might offer.
I had some problems with XMLRPC also, and investigated many solutions like GSoap and XMLRPC++, but in the end I gave up and wrote the whole HTTP+XMLRPC from scratch using Boost.ASIO and TinyXML++ (later I swaped TinyXML to expat). It wasn't really that much work; I did it myself in about a week, starting from scratch and ending up with many RPC calls fully implemented.
Boost.ASIO gave great results. It is, as its name says, totally async, and with excellent performance with little overhead, which to me was very important because it was running in an embedded environment (MIPS).
Later, and this might be your case, I changed XML to Google's Protocol-buffers, and was even happier. Its API, as well as its message containers, are all type safe (i.e. you send an int and a float, and it never gets converted to string and back, as is the case with XML), and once you get the hang of it, which doesn't take very long, its very productive solution.
My recomendation: if you can ditch XML, go with Boost.ASIO + ProtobufIf you need XML: Boost.ASIO + Expat
Doing this stuff from scratch is really worth it.