1// @author Couchbase <info@couchbase.com>
2// @copyright 2014 Couchbase, Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16package message
17
18import (
19	"github.com/golang/protobuf/proto"
20	"strconv"
21	"bytes"
22	"fmt"
23)
24
25//
26// Proposal - implement Packet interface
27//
28func (req *Proposal) Name() string {
29	return "Proposal"
30}
31
32func (req *Proposal) Encode() (data []byte, err error) {
33	return proto.Marshal(req)
34}
35
36func (req *Proposal) Decode(data []byte) (err error) {
37	return proto.Unmarshal(data, req)
38}
39
40func (req *Proposal) DebugString() string {
41	var buf bytes.Buffer
42	buf.WriteString(fmt.Sprintf("Proposal Message:"))
43	buf.WriteString(fmt.Sprintf("	Txnid  : %d", req.GetTxnid()))
44	buf.WriteString(fmt.Sprintf("	Fid    : %s", req.GetFid()))
45	buf.WriteString(fmt.Sprintf("	ReqId  : %d", req.GetReqId()))
46	buf.WriteString(fmt.Sprintf("	OpCode : %d", req.GetOpCode()))
47	buf.WriteString(fmt.Sprintf("	Key    : %s", req.GetKey()))
48	return buf.String()
49}
50
51//
52// Accept - implement Packet interface
53//
54func (req *Accept) Name() string {
55	return "Accept"
56}
57
58func (req *Accept) Encode() (data []byte, err error) {
59	return proto.Marshal(req)
60}
61
62func (req *Accept) Decode(data []byte) (err error) {
63	return proto.Unmarshal(data, req)
64}
65
66func (req *Accept) DebugString() string {
67	var buf bytes.Buffer
68	buf.WriteString(fmt.Sprintf("Accept Message:"))
69	buf.WriteString(fmt.Sprintf("	Txnid : %d", req.GetTxnid()))
70	buf.WriteString(fmt.Sprintf("	Fid   : %s", req.GetFid()))
71	return buf.String()
72}
73
74//
75// Commit - implement Packet interface
76//
77func (req *Commit) Name() string {
78	return "Commit"
79}
80
81func (req *Commit) Encode() (data []byte, err error) {
82	return proto.Marshal(req)
83}
84
85func (req *Commit) Decode(data []byte) (err error) {
86	return proto.Unmarshal(data, req)
87}
88
89func (req *Commit) DebugString() string {
90	var buf bytes.Buffer
91	buf.WriteString(fmt.Sprintf("Commit Message:"))
92	buf.WriteString(fmt.Sprintf("	Txnid : %d", req.GetTxnid()))
93	return buf.String()
94}
95
96//
97// Abort - implement Packet interface
98//
99func (req *Abort) Name() string {
100	return "Abort"
101}
102
103func (req *Abort) Encode() (data []byte, err error) {
104	return proto.Marshal(req)
105}
106
107func (req *Abort) Decode(data []byte) (err error) {
108	return proto.Unmarshal(data, req)
109}
110
111func (req *Abort) DebugString() string {
112	var buf bytes.Buffer
113	buf.WriteString(fmt.Sprintf("Abort Message:"))
114	buf.WriteString(fmt.Sprintf("	Fid    : %s", req.GetFid()))
115	buf.WriteString(fmt.Sprintf("	ReqId  : %d", req.GetReqId()))
116	buf.WriteString(fmt.Sprintf("	Error : %s", req.GetError()))
117	return buf.String()
118}
119
120//
121// Response - implement Packet interface
122//
123func (req *Response) Name() string {
124	return "Response"
125}
126
127func (req *Response) Encode() (data []byte, err error) {
128	return proto.Marshal(req)
129}
130
131func (req *Response) Decode(data []byte) (err error) {
132	return proto.Unmarshal(data, req)
133}
134
135func (req *Response) DebugString() string {
136	var buf bytes.Buffer
137	buf.WriteString(fmt.Sprintf("Response Message:"))
138	buf.WriteString(fmt.Sprintf("	Fid         : %s", req.GetFid()))
139	buf.WriteString(fmt.Sprintf("	ReqId       : %d", req.GetReqId()))
140	buf.WriteString(fmt.Sprintf("	Error       : %s", req.GetError()))
141	buf.WriteString(fmt.Sprintf("	len(Content): %d", len(req.GetContent())))
142	return buf.String()
143}
144
145//
146// Vote - implement Packet interface
147//
148func (req *Vote) Name() string {
149	return "Vote"
150}
151
152func (req *Vote) Encode() (data []byte, err error) {
153	return proto.Marshal(req)
154}
155
156func (req *Vote) Decode(data []byte) (err error) {
157	return proto.Unmarshal(data, req)
158}
159
160func (req *Vote) DebugString() string {
161	var buf bytes.Buffer
162	buf.WriteString(fmt.Sprintf("Vote Message:"))
163	buf.WriteString(fmt.Sprintf("	Round           : %d", req.GetRound()))
164	buf.WriteString(fmt.Sprintf("	Status          : %d", req.GetStatus()))
165	buf.WriteString(fmt.Sprintf("	Epoch           : %d", req.GetEpoch()))
166	buf.WriteString(fmt.Sprintf("	Candidate Id    : %s", req.GetCndId()))
167	buf.WriteString(fmt.Sprintf("	Logged TxnId    : %d", req.GetCndLoggedTxnId()))
168	buf.WriteString(fmt.Sprintf("	Committed TxnId : %d", req.GetCndCommittedTxnId()))
169	buf.WriteString(fmt.Sprintf("	SolicitOnly     : %s", strconv.FormatBool(req.GetSolicit())))
170	return buf.String()
171}
172
173//
174// LogEntry - implement Packet interface
175//
176func (req *LogEntry) Name() string {
177	return "LogEntry"
178}
179
180func (req *LogEntry) Encode() (data []byte, err error) {
181	return proto.Marshal(req)
182}
183
184func (req *LogEntry) Decode(data []byte) (err error) {
185	return proto.Unmarshal(data, req)
186}
187
188func (req *LogEntry) DebugString() string {
189	var buf bytes.Buffer
190	buf.WriteString(fmt.Sprintf("LogEntry Message:"))
191	buf.WriteString(fmt.Sprintf("	Txnid  : %d", req.GetTxnid()))
192	buf.WriteString(fmt.Sprintf("	Key    : %s", req.GetKey()))
193	buf.WriteString(fmt.Sprintf("	OpCode : %d", req.GetOpCode()))
194	return buf.String()
195}
196
197//
198// FollowerInfo - implement Packet interface
199//
200func (req *FollowerInfo) Name() string {
201	return "FollowerInfo"
202}
203
204func (req *FollowerInfo) Encode() (data []byte, err error) {
205	return proto.Marshal(req)
206}
207
208func (req *FollowerInfo) Decode(data []byte) (err error) {
209	return proto.Unmarshal(data, req)
210}
211
212func (req *FollowerInfo) DebugString() string {
213	var buf bytes.Buffer
214	buf.WriteString(fmt.Sprintf("FollowerInfo Message:"))
215	buf.WriteString(fmt.Sprintf("	AcceptedEpoch : %d", req.GetAcceptedEpoch()))
216	buf.WriteString(fmt.Sprintf("	Voting        : %s", strconv.FormatBool(req.GetVoting())))
217	return buf.String()
218}
219
220//
221// EpochAck - implement Packet interface
222//
223func (req *EpochAck) Name() string {
224	return "EpochAck"
225}
226
227func (req *EpochAck) Encode() (data []byte, err error) {
228	return proto.Marshal(req)
229}
230
231func (req *EpochAck) Decode(data []byte) (err error) {
232	return proto.Unmarshal(data, req)
233}
234
235func (req *EpochAck) DebugString() string {
236	var buf bytes.Buffer
237	buf.WriteString(fmt.Sprintf("EpochAck Message:"))
238	buf.WriteString(fmt.Sprintf("	LastLoggedTxid : %d", req.GetLastLoggedTxid()))
239	buf.WriteString(fmt.Sprintf("	CurrentEpoch : %d", req.GetCurrentEpoch()))
240	return buf.String()
241}
242
243//
244// LeaderInfo - implement Packet interface
245//
246func (req *LeaderInfo) Name() string {
247	return "LeaderInfo"
248}
249
250func (req *LeaderInfo) Encode() (data []byte, err error) {
251	return proto.Marshal(req)
252}
253
254func (req *LeaderInfo) Decode(data []byte) (err error) {
255	return proto.Unmarshal(data, req)
256}
257
258func (req *LeaderInfo) DebugString() string {
259	var buf bytes.Buffer
260	buf.WriteString(fmt.Sprintf("LeaderInfo Message:"))
261	buf.WriteString(fmt.Sprintf("	AcceptedEpoch : %d", req.GetAcceptedEpoch()))
262	return buf.String()
263}
264
265//
266// NewLeader - implement Packet interface
267//
268func (req *NewLeader) Name() string {
269	return "NewLeader"
270}
271
272func (req *NewLeader) Encode() (data []byte, err error) {
273	return proto.Marshal(req)
274}
275
276func (req *NewLeader) Decode(data []byte) (err error) {
277	return proto.Unmarshal(data, req)
278}
279
280func (req *NewLeader) DebugString() string {
281	var buf bytes.Buffer
282	buf.WriteString(fmt.Sprintf("NewLeader Message:"))
283	buf.WriteString(fmt.Sprintf("	CurrentEpoch : %d", req.GetCurrentEpoch()))
284	return buf.String()
285}
286
287//
288// NewLeaderAck - implement Packet interface
289//
290func (req *NewLeaderAck) Name() string {
291	return "NewLeaderAck"
292}
293
294func (req *NewLeaderAck) Encode() (data []byte, err error) {
295	return proto.Marshal(req)
296}
297
298func (req *NewLeaderAck) Decode(data []byte) (err error) {
299	return proto.Unmarshal(data, req)
300}
301
302func (req *NewLeaderAck) DebugString() string {
303	var buf bytes.Buffer
304	buf.WriteString(fmt.Sprintf("NewLeaderAck Message: No field to print"))
305	return buf.String()
306}
307
308//
309// Request - implement Packet interface
310//
311func (req *Request) Name() string {
312	return "Request"
313}
314
315func (req *Request) Encode() (data []byte, err error) {
316	return proto.Marshal(req)
317}
318
319func (req *Request) Decode(data []byte) (err error) {
320	return proto.Unmarshal(data, req)
321}
322
323func (req *Request) DebugString() string {
324	var buf bytes.Buffer
325	buf.WriteString(fmt.Sprintf("Request Message:"))
326	buf.WriteString(fmt.Sprintf("	ReqId  : %d", req.GetReqId()))
327	buf.WriteString(fmt.Sprintf("	OpCode : %d", req.GetOpCode()))
328	buf.WriteString(fmt.Sprintf("	Key    : %s", req.GetKey()))
329	return buf.String()
330}
331