Java – how to intercept and authorize user input to the server
Brief introduction:
I have a [physical] simulation framework, which used to be a single user desktop version The framework is used as a toolkit to implement, for example, teachers building various simulation settings without an in-depth understanding of Java programming and / or specific mathematics Finally, we want to apply a client server paradigm to the framework to allow multiple clients to collaborate when using the same simulation (= synchronize the simulation between all clients)
Some additional technical facts:
The framework / simulation is designed based on MVC pattern
If the client makes changes to the simulation - for example, by moving the slider or dragging the simulation element through the swing GUI - these changes must be authorized by the server to apply to the simulation (the server must be careful) to distribute the changes to all other clients, and these clients must also apply them
Authorization itself is very simple and basically only determines whether the timestamp should accept changes (to avoid problems caused by clients with different delays that cause changes to the same thing [almost] at the same time)
The questions are:
I now want to know what is the most elegant way to detect (and intercept) user input (pass it to the underlying messaging system responsible for client server communication)? Is there a pattern or best practice that is commonly used in the described scenario?
One of my main concerns is to avoid introducing new constraints, which must be considered by those who use the framework to build new content (= simulation) In this sense, I want to keep the existing simulation work with as few changes as possible
A solution that I think can be solved
I want to introduce a new interface, such as:
public interface Synchronizable { public boolean appliesChanges(); }
In this case, the constraint is any type of change listener. If you want to synchronize the change events they are listening for, you must implement this interface separately By doing so, the underlying framework can replace all objects that implement synchronizable with proxy objects, which are responsible for verifying the change [event] with the server (and successfully forwarding the event to the real change listener)
The idea behind the 'applychanges' method is that not all calls to the change listener will result in changes that need to be synchronized For example, swing jslider may generate an event when the knob is moved, but once the knob is released (i.e. 'the value is no longer adjusted'), changing the specific implementation of the listener may only apply the actual changes Changes that occur in between do not need to be sent to the server because they have no impact anyway This method is neither convenient nor particularly beautiful, but I can't think of any other possibilities to solve the problem in other ways?!
In addition to the problem that end users must explicitly consider which events they want to synchronize (and therefore declare and implement the interfaces mentioned for all specific listeners), the remaining problem is how to automatically find out which specific method is responsible for handling any type of events (to avoid having to implement a specific proxy for each possible change listener)
An example that outlines this problem:
public interface Synchronizedchangelistener extends changelistener,Synchronizable {} public interface SynchronizedPropertychangelistener extends Propertychangelistener,Synchronizable {} public static void main(String[] args) { Synchronizedchangelistener scl = new Synchronizedchangelistener() { public void stateChanged(ChangeEvent e) { System.out.println("Hello world - Synchronizedchangelistener"); } public boolean appliesChanges() { return true; } }; SynchronizedPropertychangelistener spcl = new SynchronizedPropertychangelistener() { public void propertyChange(PropertyChangeEvent evt) { System.out.println("Hello world - SynchronizedPropertychangelistener"); } public boolean appliesChanges() { return true; } }; }
How does the proxy listener know that it must call the 'propertychange' method for propertychangeevents and the 'statechanged' method for changeevents? Can reflection solve this problem?
Look forward to your input – I'll be happy with any ideas you may have or any literary re instructions on this subject
Solution
In my opinion, you have a user interface where users can provide some input You have another simulation UI that runs the physical simulation User interface input can only be provided to another user interface after server authorization
Good so far?
Now, your simulation UI should finally wait for events from the server I'm thinking about something like this:
>Prepare an XML containing the changes the user wants to make from the user UI. > Implement a simple servlet (or struts) on the Tomcat server to parse the XML and reply to users who use the authorization request The response can also be in XML format. > Submit this authorization back to the simulation UI through the queue The simulation UI should listen on this queue for any events placed on it. > This will take care of individual user scenarios For multiple users receiving events from the server, the simplest method is polling The timer is implemented at the level before simulating the UI, which polls the server The server can respond with the latest changes Now provide it to the queue that simulates the UI