aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/requests/SimpleRequestIdentifier.java
blob: 57b1e672bc97eab8f8ba81c9cfd3b35f0e5da63f (plain) (blame)
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
97
98
99
100
101
102
103
104
105
106
/*
 This file is part of GNUnet.
 Copyright (C) 2014 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 3, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

package org.gnunet.requests;

import org.gnunet.mq.Envelope;
import org.gnunet.mq.MessageQueue;
import org.gnunet.mq.NotifySentHandler;
import org.gnunet.util.Cancelable;
import org.gnunet.util.RelativeTime;
import org.gnunet.util.Scheduler;

abstract class SimpleRequestIdentifier<T extends Request> implements RequestIdentifier<T> {
    private final T request;
    /**
     * Has the message been queued for sending?
     */
    boolean queued;
    /**
     * Cancel sending the message via the message queue.
     */
    private Cancelable sendCancel;
    /**
     * Has the request been irrevocably sent?
     */
    private boolean sent;
    private boolean canceled;
    private Scheduler.TaskIdentifier timeoutTask;

    public SimpleRequestIdentifier(T request) {
        this.request = request;
    }

    @Override
    public void retire() {
        if (null != timeoutTask) {
            timeoutTask.cancel();
            timeoutTask = null;
        }
    }

    @Override
    public void setTimeout(final RelativeTime timeout, final TimeoutHandler timeoutHandler) {
        if (null != timeoutTask)
            throw new AssertionError("timeout already set");
        timeoutTask = Scheduler.addDelayed(timeout, new Scheduler.Task() {
            @Override
            public void run(Scheduler.RunContext ctx) {
                if (ctx.reasons.contains(Scheduler.Reason.SHUTDOWN))
                    return;
                timeoutHandler.onTimeout();
            }
        });
    }

    @Override
    public T getRequest() {
        return request;
    }

    @Override
    public void cancel() {
        if (canceled) {
            throw new AssertionError("canceled twice");
        }
        canceled = true;
        if (sent) {
            request.cancel();
        } else if (null != sendCancel) {
            sendCancel.cancel();
            sendCancel = null;
        }
        retire();
    }

    public void send(MessageQueue mq) {
        queued = true;
        Envelope ev = request.assembleRequest();
        ev.notifySent(new NotifySentHandler() {
            @Override
            public void onSent() {
                sendCancel = null;
                sent = true;
            }
        });
        mq.send(ev);
        sendCancel = ev;
    }
}