Statistics
| Branch: | Tag: | Revision:

one / include / AddressRangeInternal.h @ 1772a358

History | View | Annotate | Download (3.94 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* 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
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef ADDRESS_RANGE_INTERNAL_H_
18
#define ADDRESS_RANGE_INTERNAL_H_
19

    
20
#include <string>
21

    
22
#include "AddressRange.h"
23
#include "AddressRangePool.h"
24

    
25
class VectorAttribute;
26

    
27
class AddressRangeInternal : public AddressRange
28
{
29
public:
30
    AddressRangeInternal(unsigned int _id):AddressRange(_id), next(0){};
31

    
32
    virtual ~AddressRangeInternal(){};
33

    
34
    /* ---------------------------------------------------------------------- */
35
    /* AddressRange Interface                                                 */
36
    /* ---------------------------------------------------------------------- */
37
    /**
38
     *  Init an Address Range based on a vector attribute.
39
     *    @param attr the description of the AddressRange
40
     *    @param error_msg description if error
41
     *    @return 0 on success
42
     */
43
    int from_vattr(VectorAttribute * attr, std::string& error_msg)
44
    {
45
        return AddressRange::from_attr(attr, error_msg);
46
    }
47

    
48
    /**
49
     *  Sets the given range of addresses (by index) as used
50
     *    @param index the first address to set as used
51
     *    @param sz number of addresses to set
52
     *    @param msg describing the error if any
53
     *
54
     *    @return 0 if success
55
     */
56
    int allocate_addr(unsigned int index, unsigned int rsize, string& error_msg)
57
    {
58
        return 0;
59
    }
60

    
61
    /**
62
     *  Gets a range of free addresses
63
     *    @param index the first address in the range
64
     *    @param size number of addresses requested in the range
65
     *    @param msg describing the error if any
66
     *
67
     *    @return 0 if success
68
     */
69
    int get_addr(unsigned int& index, unsigned int rsize, string& error_msg)
70
    {
71
        if ( rsize == 1 )
72
        {
73
            return get_single_addr(index, error_msg);
74
        }
75

    
76
        return get_range_addr(index, rsize, error_msg);
77
    }
78

    
79
    /**
80
     *  Sets the given address (by index) as free
81
     *    @param index of the address
82
     *    @param msg describing the error if any
83
     *
84
     *    @return 0 if success
85
     */
86
    int free_addr(unsigned int index, string& msg)
87
    {
88
        return 0;
89
    };
90

    
91
private:
92
    /**
93
     *  Lookup index for the next free address lease
94
     */
95
    unsigned int next;
96

    
97
    /**
98
     *  Get a free lease
99
     *    @param index of the free lease, undefined if error
100
     *    @param msg with error description if any
101
     *    @return 0 on success -1 otherwise
102
     */
103
    int get_single_addr(unsigned int& index, std::string& msg);
104

    
105
    /**
106
     *  Get a free and continuous range of leases
107
     *    @param index of the first free lease in range, undefined if error
108
     *    @param sz of the range
109
     *    @param msg with error description if any
110
     *    @return 0 on success -1 otherwise
111
     */
112
    int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg);
113
};
114

    
115
#endif