aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/consensus/Consensus.java
blob: ba3c8ed1604bc052f25c4bcd950e417be41efce6 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 This file is part of GNUnet.
 Copyright (C) 2013 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., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */

package org.gnunet.consensus;

import com.google.common.base.Preconditions;
import org.gnunet.consensus.messages.*;
import org.gnunet.mq.Envelope;
import org.gnunet.mq.NotifySentHandler;
import org.gnunet.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Multi-peer set reconciliation.
 */
public class Consensus {
    /**
     * Class logger.
     */
    private static final Logger logger = LoggerFactory
            .getLogger(Consensus.class);

    /**
     * Client connected to the consensus service.
     */
    private Client client;

    /**
     * Called when conclude has finished.
     */
    private ConsensusCallback consensusCallback;

    /**
     * Message dispatch for messages from the consensus service.
     */
    private class ConsensusMessageReceiver extends RunaboutMessageReceiver {
        public void visit(ConcludeDoneMessage m) {
            if (null == consensusCallback) {
                logger.error("unexpected conclude done message");
                return;
            }
            consensusCallback.onDone();
        }

        public void visit(NewElementMessage m) {
            ConsensusElement element = new ConsensusElement(m.elementData, m.elementType);
            element.elementType = m.elementType;
            element.data = m.elementData;
            consensusCallback.onElement(element);
        }

        @Override
        public void handleError() {
            System.out.println("Error receiving from consensus service.");
            consensusCallback.onElement(null);
        }
    }

    /**
     * Create a consensus session.  The set being reconciled is initially
     * empty.
     *
     * @param peers array of peers participating in this consensus session
     *              Inclusion of the local peer is optional.
     * @param sessionId session identifier
     *                   Allows a group of peers to have more than consensus session.
     * @param startTime when should the consensus start?
     * @param deadline when should we be done?
     */
    public Consensus(Configuration cfg, PeerIdentity[] peers, HashCode sessionId,
                     AbsoluteTime startTime, AbsoluteTime deadline) {
        client = new Client("consensus", cfg);
        client.installReceiver(new ConsensusMessageReceiver());
        String peersString = "";
        for (PeerIdentity pi : peers)
            peersString += pi + ", ";

        logger.info("starting consensus with {} peers given to consensus ({})", peers.length, peersString);
        JoinMessage m = new JoinMessage();
        m.numPeers = peers.length;
        m.peers = peers;
        m.sessionId = sessionId;
        m.startTime = startTime.asMessage();
        m.deadline = deadline.asMessage();
        client.send(m);
    }

    /**
     * Insert an element into the consensus set.
     *
     * @param element element to insert in the consensus
     */
    public void insertElement(ConsensusElement element) {
        insertElement(element, null);
    }

    /**
     * Insert an element into the consensus set.
     *
     * @param element element to insert in the consensus
     * @param idc called when the element has been sent to the service
     */
    public void insertElement(ConsensusElement element, final InsertDoneCallback idc) {
        InsertElementMessage m = new InsertElementMessage();
        m.elementData = element.data;
        m.elementType = element.elementType;
        Envelope ev = new Envelope(m);
        if (null != idc) {
            ev.notifySent(new NotifySentHandler() {
            @Override
            public void onSent() {
                idc.onInsertDone();
            }
        });
        }
        client.send(ev);
    }

    /**
     * We are done with inserting new elements into the consensus;
     * try to conclude the consensus within a given time window.
     * After conclude has been called, no further elements may be
     * inserted by the client.
     *
     * @param concludeCallback called when the consensus has concluded
     */
    public void conclude(ConsensusCallback concludeCallback) {
        Preconditions.checkNotNull(concludeCallback, "conclude with null callback");
        Preconditions.checkState(null == this.consensusCallback, "called conclude twice");
        this.consensusCallback = concludeCallback;
        ConcludeMessage m = new ConcludeMessage();
        client.send(m);
    }

    /**
     * Destroy a consensus handle.
     * Free all state associated with
     * it, no longer call any of the callbacks.
     */
    public void destroy() {
        client.disconnect();
        client = null;
    }
}