aboutsummaryrefslogtreecommitdiff
path: root/dmucs_hosts_file.h
blob: 9b9416666ff8fd8a606f32483ddf7708e1bed583 (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
#ifndef _DMUCS_HOST_FILE_H_
#define _DMUCS_HOST_FILE_H_ 1

/*
 * dmucs_hosts_file.h: code to read the hosts-info configuration file.
 *
 * Copyright (C) 2005, 2006  Victor T. Norman
 *
 * This program 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 2 of the License, or (at your
 * option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <map>
#include <string>

#ifdef PKGDATADIR
const std::string HOSTS_INFO_FILE = std::string(PKGDATADIR) + \
	std::string("/") + std::string("hosts-info");
#else
const std::string HOSTS_INFO_FILE = "hosts-info";
#endif


class DmucsHostsFile
{
public:

    static DmucsHostsFile *getInstance(const std::string &hostsInfoFile);
    void getDataForHost(const struct in_addr &ipAddr, int *numCpus,
			int *powerIndex) const;

    /*
     * This class implements the singleton pattern, as only one instance of
     * it needs to exist, and it should take care to re-read the hostsfile,
     * etc., when necessary.
     */

private:
    // this is private so that it cannot be used (this is a Singleton).
    DmucsHostsFile(const std::string &hostsInfoFile);	
    ~DmucsHostsFile();

    struct info_t {
	int numCpus_;
	int powerIndex_;
	info_t(int ncpus, int pindex) :
	    numCpus_(ncpus), powerIndex_(pindex) {};
    };

    static DmucsHostsFile *instance_;
    std::string hostsInfoFile_;

    /*
     * This maps an IP address (in 32-bit format) to the pair of values:
     * numCpus and powerIndex.
     */
    typedef std::map<unsigned int, info_t> host_info_db_t;
    typedef host_info_db_t::iterator host_info_db_iter_t;

    mutable host_info_db_t db_;
    mutable time_t lastFileChangeTime_;

    void readFileIntoDb() const;

    /* If the file modification time has changed since the last time this
       was called, then return true AND update lastFileChangeTime_ to the
       new modification time. */
    bool hasFileChanged() const;

};

#endif