Results 1 to 6 of 6
  1. #1
    dreadrocksean is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default Animation Synchronization

    Hi all.
    I have a server/client online app/applet development.
    To simplify my problem, the client get 2 commands from the server, one right after the other.
    The 1st one is dispatched to a Timer to do a particular animation. The next is handled on the EDT.

    Because the animation takes longer than the time difference between getting the 1st and 2nd commands, the 2nd command gets done way before the animation is over. However, I want the response from the 2nd command to begin ONLY when the animation is over.
    I tried synchronizing the Timer but of course it only atomizes each call, in between which, the EDT does stuff from the 1st command response anyway. (I could be wrong about this).
    I prefer not to use delays as it would differ between machines.
    I don't want to have the server wait till it gets an "endAnimation" signal from the client because EACH CLIENT MAY END AT A DIFFERENT TIME.
    So it must be handled within each client independently.

    Actually, there are many commands that the server sends right after the "doAnimation" one. By the time the animation is done, all the other command have already been done and were never seen, or they all appear at their end stages (the server uses delays to space the sending of the commands after the animation command to slow down the appearance of these onscreen objects on the client-side. This is nullified as they all happen in the background while the animation was doing its thing).
    I realize that this all sounds very complicated and that I may not be explaining myself properly.
    So in a nutshell:

    1. [SERVER]->doAnimation_________[CLIENT] startAnimation
    2. [SERVER]->showScreenWidget1___[CLIENT] still doing animation
    3. [SERVER]Pause_________________[CLIENT] still doing animation
    4. [SERVER]->showScreenWidget2___[CLIENT] still doing animation
    5. [SERVER]Pause_________________[CLIENT] still doing animation
    6. [SERVER]->showScreenWidget3___[CLIENT] still doing animation
      [CLIENT] done doing animation
      [CLIENT] show all screen widgets at once


    I realize also as I write this that whatever solution I use, the delays on the server side would become mute points so I'll have to find another way to pause between screen widgets. The priority, however, is to have the rest of the screen updates wait till the animation is over.
    Any help would be appreciated.
    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Have the client queue the show requests until the animation is done and then do them? Can the client detect that the animation is inprogress to know that it must queue the show request until completion?
    Can you show what you'd like to happen in the same format as your "in a nutshell:" form?
    Why doesn't the server send all the info to the client in one go and let the client do the displays? If there is no feedback from the client while it is showing the animation, why have the server send multiple responses to the client if all the responses can be determined for the first response.

  3. #3
    dreadrocksean is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Smile

    If I use the Swing Timer, then everything it does is on the EDT. What happens in between timer calls? Does the EDT do other stuff?
    How can I ensure that until the Timer is destroyed (marking the end of the animation), that nothing else gets done?
    I can't very well put a wait on the EDT as that's the very same thread I need for the animation. EVEN if I use the java.utils.Timer/TimerTask combo. I would still have to spin the GUI stuff off onto the EDT anyway.

    I understand the queue list concept but how would that work when its not just a single event but many as my animation is?
    I even thought of sending the pause command TO the client as a call to a pause method but that still wouldn't work because as soon as the animation command is sent and the 1st Timer call is done, while waiting for the Timer to fire off the next event, the pause routine would run prematurely. This means that it will pause the animation.

    Thanks for reply anyway

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Can you show what you'd like to happen in the same format as your "in a nutshell:" form?
    By using different threads, you can leave the EDT to do the animation and have other threads monitor the input, queue it, monitor the animation and start the next task after the animation is done. Its a scheduling job, keeping track of what to do when.

  5. #5
    dreadrocksean is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks Norm.
    I had a friend construct the remoting for me. That's the model I am using.

    This way I can call methods as if they were on the same machine. Acoording to you, I would have to alter that model.
    Part of the code is below. As you can see, it's already in queue mode.
    Maybe I would have to send methods that need to wait on an animation, to an intermediate method that monitors the animation and has its own queue list? The rest being sent normally?


    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.ArrayList;

    /**
    */
    public class RemoteEventListenerList<T extends RemoteEventListener> implements
    InvocationHandler {
    private List<T> list = new LinkedList<T>();
    private List<T> toAdd = new ArrayList<T>();
    private final Class<T> type;
    private T proxy;
    private List<T> toRemove = new ArrayList<T>();

    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    updateList();
    dispatchList(list, method, args);
    while (moreAdded()) {
    final List<T> temp = new ArrayList<T>(toAdd);
    updateList();
    dispatchList(temp, method, args);
    }
    return null;
    }

    private void updateList() {
    doRemove();
    doAdd();
    }

    private boolean moreAdded() {
    return !toAdd.isEmpty();
    }

    private void dispatchList(List<T> list, Method method, Object[] args)
    throws IllegalAccessException {
    for (Object t : list.toArray()) {
    dispatch(method, type.cast(t), args);
    }
    }

    private void doRemove() {
    list.removeAll(toRemove);
    toRemove.clear();
    }

    private void doAdd() {
    list.addAll(toAdd);
    toAdd.clear();
    }

    private void dispatch(Method method, T t, Object[] args)
    throws IllegalAccessException {
    try {
    method.invoke(t, args);
    } catch (InvocationTargetException e) {
    if (!(e.getTargetException() instanceof DisconnectedException)) {
    e.getTargetException().printStackTrace();
    remove(t);
    } else {
    e.printStackTrace();
    }
    } catch (RuntimeException e) {
    e.printStackTrace();
    }
    }

    public RemoteEventListenerList(Class<T> type) {
    this.type = type;
    }

    public T getProxy() {
    if (proxy == null) {
    proxy = type.cast(Proxy
    .newProxyInstance(type.getClassLoader(), new Class[]{type}, this)
    );
    }
    return proxy;
    }

    public void add(T t) {
    toAdd.add(t);
    }

    public void remove(T t) {
    toRemove.add(t);
    }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Why doesn't the server send all the info to the client in one go and let the client do the displays serially as it wants?

    The Proxy code above implies a different design then I was working towards. How much communication does the applet do with the server?
    The Proxy code would be to allow the applet to call methods on the server without having to worry about where the methods were located.
    That seems like a lot more complicated than you have described.
    Last edited by Norm; 08-08-2008 at 04:05 PM.

Similar Threads

  1. synchronization question
    By oguz in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2008, 08:56 AM
  2. Synchronization Doesn't seem to work
    By sherinpearl in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:30 PM
  3. Replies: 0
    Last Post: 11-10-2007, 04:47 PM
  4. Synchronization problems
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •