package net.ihiroky.niotty;

import java.util.concurrent.TimeUnit;

/**
 * A implementation of {@link EventDispatcher}. Wait operation depends on {@code Object.wait()} and {@code Object.notify()}.
 */
public class DefaultEventDispatcher extends EventDispatcher {

    private boolean signaled_;
    private final Object lock_;

    /**
     * Create a new instance.
     */
    public DefaultEventDispatcher() {
        signaled_ = false;
        lock_ = new Object();
    }

    @Override
    protected void onOpen() {
    }

    @Override
    protected void onClose() {
    }

    @Override
    protected void poll(long timeout, TimeUnit timeUnit) throws InterruptedException {
        long start = System.nanoTime();
        synchronized (lock_) {
            while (!signaled_ && timeout > 0) {
                timeUnit.timedWait(lock_, timeout);
                long now = System.nanoTime();
                timeout -= timeUnit.convert(now - start, TimeUnit.NANOSECONDS);
                start = now;
            }
            signaled_ = false;
        }
    }

    @Override
    protected void wakeUp() {
        synchronized (lock_) {
            signaled_ = true;
            lock_.notify();
        }
    }
}

@@ -1128,32 +1128,66 @@
 nized (lock_) %7B%0A
+            if (!signaled_) %7B%0A    
             sign
@@ -1200,16 +1200,20 @@
 = true;%0A
+    
         
@@ -1228,24 +1228,38 @@
 _.notify();%0A
+            %7D%0A
         %7D%0A  

Call notify() if signaled