summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/ha/Event.h
blob: f292499e7b1d2cfd794f77a8b4638343b2b9fb17 (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef QPID_HA_EVENT_H
#define QPID_HA_EVENT_H

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "types.h"
#include "qpid/broker/Message.h"
#include "qpid/framing/BufferTypes.h"

/**@file Defines event messages used to pass transaction information from
 * primary observers to backup replicators.
 */

namespace qpid {
namespace ha {

broker::Message makeMessage(
    const std::string& content,
    const std::string& destination,
    const std::string& routingKey);


/** Test if a string is an event key */
bool isEventKey(const std::string& key);

/** Base class for encodable events */
class Event {
  public:
    virtual ~Event() {}
    virtual void encode(framing::Buffer& buffer) const = 0;
    virtual void decode(framing::Buffer& buffer) = 0;
    virtual size_t encodedSize() const = 0;
    virtual std::string key() const = 0; // Routing key
    virtual void print(std::ostream& o) const = 0;
    broker::Message message(const std::string& destination=std::string()) const {
        return makeMessage(framing::encodeStr(*this), destination, key()); }
};


inline std::ostream& operator<<(std::ostream& o, const Event& e) {
    o << "<" << e.key() << ":";
    e.print(o);
    return o << ">";
}

/** Event base template */
template <class Derived> class EventBase : public Event {
  public:
    std::string key() const { return Derived::KEY; }
};

//////////////// Specific event type

//// QueueReplicator events

struct DequeueEvent : public EventBase<DequeueEvent> {
    static const std::string KEY;
    ReplicationIdSet ids;

    DequeueEvent(ReplicationIdSet ids_=ReplicationIdSet()) :  ids(ids_) {}
    void encode(framing::Buffer& b) const { b.put(ids); }
    void decode(framing::Buffer& b) { b.get(ids); }
    virtual size_t encodedSize() const { return ids.encodedSize(); }
    void print(std::ostream& o) const { o << ids; }
};

struct IdEvent : public EventBase<IdEvent> {
    static const std::string KEY;
    ReplicationId id;

    IdEvent(ReplicationId id_=0) : id(id_) {}
    void encode(framing::Buffer& b) const { b.put(id); }
    void decode(framing::Buffer& b) { b.get(id); }
    virtual size_t encodedSize() const { return id.encodedSize(); }
    void print(std::ostream& o) const { o << id; }
};

//// Transaction events

struct TxEnqueueEvent : public EventBase<TxEnqueueEvent> {
    static const std::string KEY;
    framing::LongString queue;
    ReplicationId id;

    TxEnqueueEvent(std::string q=std::string(), ReplicationId i=ReplicationId())
        : queue(q), id(i) {}
    void encode(framing::Buffer& b) const { b.put(queue); b.put(id); }
    void decode(framing::Buffer& b) { b.get(queue); b.get(id); }
    virtual size_t encodedSize() const { return queue.encodedSize()+id.encodedSize(); }
    void print(std::ostream& o) const { o << queue.value << " " << id; }
};

struct TxDequeueEvent : public EventBase<TxDequeueEvent> {
    static const std::string KEY;
    framing::LongString queue;
    ReplicationId id;

    TxDequeueEvent(std::string q=std::string(), ReplicationId r=0) :
        queue(q), id(r) {}
    void encode(framing::Buffer& b) const { b.put(queue);b.put(id); }
    void decode(framing::Buffer& b) { b.get(queue);b.get(id); }
    virtual size_t encodedSize() const { return queue.encodedSize()+id.encodedSize(); }
    void print(std::ostream& o) const { o << queue.value << " " << id; }
};

struct TxPrepareEvent : public EventBase<TxPrepareEvent> {
    static const std::string KEY;
    void encode(framing::Buffer&) const {}
    void decode(framing::Buffer&) {}
    virtual size_t encodedSize() const { return 0; }
    void print(std::ostream&) const {}
};

struct TxCommitEvent : public EventBase<TxCommitEvent> {
    static const std::string KEY;
    void encode(framing::Buffer&) const {}
    void decode(framing::Buffer&) {}
    virtual size_t encodedSize() const { return 0; }
    void print(std::ostream&) const {}
};

struct TxRollbackEvent : public EventBase<TxRollbackEvent> {
    static const std::string KEY;
    void encode(framing::Buffer&) const {}
    void decode(framing::Buffer&) {}
    virtual size_t encodedSize() const { return 0; }
    void print(std::ostream&) const {}
};

struct TxPrepareOkEvent : public EventBase<TxPrepareOkEvent> {
    static const std::string KEY;
    types::Uuid broker;
    TxPrepareOkEvent(const types::Uuid& b=types::Uuid()) : broker(b) {}

    void encode(framing::Buffer& b) const {
        b.putRawData(broker.data(), broker.size());
    }

    void decode(framing::Buffer& b) {
        std::string s;
        b.getRawData(s, broker.size());
        broker = types::Uuid(&s[0]);
    }
    virtual size_t encodedSize() const { return broker.size(); }
    void print(std::ostream& o) const { o << broker; }
};

struct TxPrepareFailEvent : public EventBase<TxPrepareFailEvent> {
    static const std::string KEY;
    types::Uuid broker;
    TxPrepareFailEvent(const types::Uuid& b=types::Uuid()) : broker(b) {}
    void encode(framing::Buffer& b) const { b.putRawData(broker.data(), broker.size()); }
    void decode(framing::Buffer& b) {
        std::string s;
        b.getRawData(s, broker.size());
        broker = types::Uuid(&s[0]);
    }
    virtual size_t encodedSize() const { return broker.size(); }
    void print(std::ostream& o) const { o << broker; }
};

struct TxMembersEvent : public EventBase<TxMembersEvent> {
    static const std::string KEY;
    UuidSet members;
    TxMembersEvent(const UuidSet& s=UuidSet()) : members(s) {}
    void encode(framing::Buffer& b) const { b.put(members); }
    void decode(framing::Buffer& b) { b.get(members); }
    size_t encodedSize() const { return members.encodedSize(); }
    void print(std::ostream& o) const { o << members; }
};

}} // namespace qpid::ha

#endif  /*!QPID_HA_EVENT_H*/