ceph/branches/ebofs/mon/MonMap.h
sageweil dc48f25847 branch for ebofs changes
git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@2100 29311d96-e01e-0410-9327-a35deaab8ce9
2007-11-21 00:32:00 +00:00

113 lines
2.3 KiB
C++

// -*- 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 __MONMAP_H
#define __MONMAP_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "msg/Message.h"
#include "include/types.h"
class MonMap {
public:
epoch_t epoch; // what epoch/version of the monmap
ceph_fsid_t fsid;
vector<entity_inst_t> mon_inst;
int last_mon; // last mon i talked to
MonMap(int s=0) : epoch(0), mon_inst(s), last_mon(-1) {
generate_fsid();
}
unsigned size() {
return mon_inst.size();
}
void add_mon(entity_inst_t inst) {
mon_inst.push_back(inst);
}
// pick a mon.
// choice should be stable, unless we explicitly ask for a new one.
int pick_mon(bool newmon=false) {
if (newmon || (last_mon < 0)) {
last_mon = rand() % mon_inst.size();
}
return last_mon;
}
const entity_inst_t &get_inst(unsigned m) {
assert(m < mon_inst.size());
return mon_inst[m];
}
void encode(bufferlist& blist) {
::_encode(epoch, blist);
::_encode(fsid, blist);
::_encode(mon_inst, blist);
}
void decode(bufferlist& blist) {
int off = 0;
::_decode(epoch, blist, off);
::_decode(fsid, blist, off);
::_decode(mon_inst, blist, off);
}
void generate_fsid() {
fsid.major = ((uint64_t)rand() << 32) + rand();
fsid.minor = ((uint64_t)rand() << 32) + rand();
}
// read from/write to a file
int write(char *fn) {
// encode
bufferlist bl;
encode(bl);
// write
int fd = ::open(fn, O_RDWR|O_CREAT);
if (fd < 0) return fd;
::fchmod(fd, 0644);
::write(fd, (void*)bl.c_str(), bl.length());
::close(fd);
return 0;
}
int read(char *fn) {
// read
bufferlist bl;
int fd = ::open(fn, O_RDONLY);
if (fd < 0) return fd;
struct stat st;
::fstat(fd, &st);
bufferptr bp(st.st_size);
bl.append(bp);
::read(fd, (void*)bl.c_str(), bl.length());
::close(fd);
// decode
decode(bl);
return 0;
}
};
#endif