summaryrefslogtreecommitdiff
path: root/src/main/java/com/orbekk/protobuf/TimeoutManager.java
blob: 48db8792ece17667563bb006d677a8700d2027c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.orbekk.protobuf;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.PriorityQueue;
import java.io.Closeable;
import java.io.IOException;

public class TimeoutManager extends Thread {
    private static final Logger logger =
            Logger.getLogger(TimeoutManager.class.getName());
    private Environment environment;
    private PriorityQueue<Entry> entries = new PriorityQueue<Entry>();

    private static class Entry implements Comparable<Entry> {
        // May not be null.
        public Long timeout;
        public Closeable closeable;

        public Entry(long timeout, Closeable closeable) {
            this.timeout = timeout;
            this.closeable = closeable;
        }

        public void close() throws IOException {
            closeable.close();
        }
        
        @Override public int compareTo(Entry other) {
            return timeout.compareTo(other.timeout);
        }
    }

    public interface Environment {
        long currentTimeMillis();
        void sleep(long millis) throws InterruptedException;
    }

    public static class DefaultEnvironment implements Environment {
        @Override public long currentTimeMillis() {
            return System.currentTimeMillis();
        }
        @Override public void sleep(long millis) throws InterruptedException {
            Thread.sleep(millis);
        }
    }

    TimeoutManager(Environment environment) {
        this.environment = environment;
    }

    public TimeoutManager() {
        this(new DefaultEnvironment());
    }

    public synchronized void performWork() {
        try {
            if (entries.isEmpty()) {
                wait();
            } else {
                long sleepTime = entries.peek().timeout -
                        environment.currentTimeMillis();
                if (sleepTime > 0) {
                    wait(sleepTime);
                }
            }
            closeExpiredEntries();
        } catch (InterruptedException e) {
            // Handled by outer loop.
        }
    }
    
    @Override public void run() {
        while (!Thread.interrupted()) {
            performWork();
        }
    }

    public synchronized void closeExpiredEntries() {
        long currentTime = environment.currentTimeMillis();
        while (!entries.isEmpty() && entries.peek().timeout <= currentTime) {
            try {
                entries.poll().close();
            } catch (IOException e) {
                logger.log(Level.INFO, "Could not close entry. ", e);
            }
        }
    }

    public synchronized void addEntry(long timeoutTime, Closeable closeable) {
        if (entries.isEmpty() || timeoutTime <= entries.peek().timeout) {
            notify();
        }
        entries.add(new Entry(timeoutTime, closeable));
    }
}