Question

The applications in my project were until now communicating over qtdbus using synchronous calls. However I now need to convert a few of these calls to asynchronous. For that I chose to use this API available in qtdbus QDBusAbstractInterface::callWithCallback But the problem is that the current implementation has these qtdbus sync calls scattered in a lot of places in the code and the code snippets which follow these sync calls assume that the control only reaches them when the preceding call has been successfully serviced and a reply is obtained. This will no longer be the case when the calls change to async. Moreover the calls are made in different contexts, so I will be required to maintain the state of the system before each qtdbus call, so that I know what to do when I receive the reply. Is there any chance really to somehow convert the calls to async without rupturing the fabric of the current code in a big way? One approach I can think of is to use the FSM pattern. Any hints or design suggestions will be much appreciated. Thanks!

Was it helpful?

Solution

The way I am understanding is that you will need to call the same method and then process the return value differently based on the state at the time of the call. Such as

void function()
{
    //do stuff
    value = SynchronousCall();
    if (state == 1)
    {
        doSomething(value);
    }
    else
    {
        doSomethingElse(value);
    }
}

I would recommend instead of a full implementation of the Finite State Machine pattern which can make a mess with the number of classes that it adds, add separate methods for each state

void function()
{
    //do stuff
    if (state == 1)
    {
        callback = *doSomething(ValueType);
    }
    else
    {
        callback = *doSomethingElse(ValueType);
    }
    callWithCallback(method,args, receiver,callback,error);

}

Then in each method you can assume the state and process the return value accordingly.

Another slightly (very) hacky way would be to simply have a spin wait after all the asynchronous calls and use a QThread:: yield() in the loop while you wait for the value to return. That way it is still technically an asynchronous call but it acts synchronous.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top