Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern for request-response flow with inner classes

I have an application that consists of two processes, one client process with a (SWT-based) GUI and one server process. The client process is very lightweight, which means a lot of GUI operations will have to query the server process or request it to something, for example in response to the user clicking a button or choosing a menu item. This means that there will be a lot of event handlers that looks like this:

// Method invoked e.g. in response to the user choosing a menu item
void execute(Event event) {
    // This code is executed on the client, and now we need some info off the server:
    server.execute(new RemoteRequest() {
        public void run() {
            // This code is executed on the server, and we need to update the client 
            // GUI with current progress
            final Result result = doSomeProcessing();
            client.execute(new RemoteRequest() {
                public void run() {
                    // This code is again executed on the client
                    updateUi(result);
                }
            }
        }
    });
}

However, since the server.execute implies a serialization (it is executed on a remote machine), this pattern is not possible without making the whole class serializable (since the RemoteRequest inner classes are not static (just to be clear: it is not a requirement that the Request implementation can access the parent instance, for the sake of the application they could be static).

Of course, one solution is to create separate (possibly static inner) classes for the Request and Response, but this hurts readability and makes it harder to understand the execution flow.

I have tried to find any standard pattern for solving this problem, but I have not find anything that answers my concern about readability.

To be clear, there will be a lot of these operations, and the operations are often quite short. Note that Future objects are not entirely useful here, since in many cases one request to the server will need to do multiple things on the client (often varying), and it is also not always a result being returned.

Ideally, I would like to be able to write code like this: (obvious pseudo-code now, please disregard the obvious errors in details)

String personName = nameField.getText();
async exec on server {
    String personAddress = database.find(personName);
    async exec on client {
        addressField.setText(personAddress);
    }
    Order[] orders = database.searchOrderHistory(personName);
    async exec on client {
        orderListViewer.setInput(orders);
    }
}

Now I want to be clear, that the underlying architecture is in place and works well, the reason this solution is there is of course not the above example. The only thing I am looking for is a way to write code like the above, without having to define static classes for each process transition. I hope that I did not just complicate things by giving this example...

like image 525
Krumelur Avatar asked Nov 13 '22 09:11

Krumelur


1 Answers

My preference is to use Command Pattern and generic AsynchronousCallbacks. This kind of approach is used in GWT for communicating with the server, for example. Commands are Serializable, AsyncCallback is an interface.

Something along these lines:

    // from the client
    server.execute(new GetResultCommand(args), new AsyncCallback<Result>() 
            {
                public void onSuccess(Result result) {
                    updateUi(); // happens on the client
                }
            });

The server then needs to receive a command, process it and issue an appropriate response with Result.

like image 164
Andrejs Avatar answered Nov 16 '22 03:11

Andrejs