summaryrefslogtreecommitdiff
path: root/src/messages/MMonSubscribe.h
blob: 6ec157921fbfcf2589d3c1d189c9c183a781ebe4 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#ifndef CEPH_MMONSUBSCRIBE_H
#define CEPH_MMONSUBSCRIBE_H

#include "msg/Message.h"

/*
 * compatibility with old crap
 */
struct ceph_mon_subscribe_item_old {
	__le64 unused;
	__le64 have;
	__u8 onetime;
} __attribute__ ((packed));
WRITE_RAW_ENCODER(ceph_mon_subscribe_item_old)


struct MMonSubscribe : public Message {
  map<string, ceph_mon_subscribe_item> what;
  
  MMonSubscribe() : Message(CEPH_MSG_MON_SUBSCRIBE) {}
private:
  ~MMonSubscribe() {}

public:  
  void sub_want(const char *w, version_t start, unsigned flags) {
    what[w].start = start;
    what[w].flags = flags;
  }

  const char *get_type_name() { return "mon_subscribe"; }
  void print(ostream& o) {
    o << "mon_subscribe(" << what << ")";
  }

  void decode_payload(CephContext *cct) {
    bufferlist::iterator p = payload.begin();
    if (header.version < 2) {
      map<string, ceph_mon_subscribe_item_old> oldwhat;
      ::decode(oldwhat, p);
      what.clear();
      for (map<string, ceph_mon_subscribe_item_old>::iterator q = oldwhat.begin();
	   q != oldwhat.end();
	   q++) {
	if (q->second.have)
	  what[q->first].start = q->second.have + 1;
	else
	  what[q->first].start = 0;
	what[q->first].flags = 0;
	if (q->second.onetime)
	  what[q->first].flags |= CEPH_SUBSCRIBE_ONETIME;
      }
    } else {
      ::decode(what, p);
    }
  }
  void encode_payload(CephContext *cct) {
    if (get_connection()->has_feature(CEPH_FEATURE_SUBSCRIBE2)) {
      header.version = 2;
      ::encode(what, payload);
    } else {
      map<string, ceph_mon_subscribe_item_old> oldwhat;
      for (map<string, ceph_mon_subscribe_item>::iterator q = what.begin();
	   q != what.end();
	   q++) {
	if (q->second.start)
	  // warning: start=1 -> have=0, which was ambiguous
	  oldwhat[q->first].have = q->second.start - 1;
	else
	  oldwhat[q->first].have = 0;
	oldwhat[q->first].onetime = q->second.flags & CEPH_SUBSCRIBE_ONETIME;
      }
      ::encode(oldwhat, payload);
    }
  }
};

#endif