Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
URLEntry.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * URLEntry.h
17  * The object which holds a url and lifetime.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_SLP_URLENTRY_H_
22 #define INCLUDE_OLA_SLP_URLENTRY_H_
23 
24 #include <ola/io/BigEndianStream.h>
25 #include <ostream>
26 #include <string>
27 #include <vector>
28 
29 namespace ola {
30 namespace slp {
31 
37 class URLEntry {
38  public:
39  URLEntry() {}
40 
46  URLEntry(const std::string &url, uint16_t lifetime)
47  : m_url(url),
48  m_lifetime(lifetime) {
49  }
50 
51  ~URLEntry() {}
52 
53  std::string url() const { return m_url; }
54  uint16_t lifetime() const { return m_lifetime; }
55  void set_lifetime(uint16_t lifetime) { m_lifetime = lifetime; }
56 
57  /*
58  * Age this URL by the given number of seconds
59  * @returns true if this url has now expired, false otherwise.
60  */
61  bool AgeLifetime(uint16_t seconds) {
62  if (m_lifetime <= seconds) {
63  m_lifetime = 0;
64  return true;
65  }
66  m_lifetime-= seconds;
67  return false;
68  }
69 
70  // Return the total size of this URL entry as it appears on the wire
71  unsigned int PackedSize() const { return 6 + m_url.size(); }
72 
73  // Write this ServiceEntry to an IOQueue
74  void Write(ola::io::BigEndianOutputStreamInterface *output) const;
75 
76  // equality is based on the url only
77  bool operator==(const URLEntry &other) const {
78  return m_url == other.m_url;
79  }
80 
81  bool operator!=(const URLEntry &other) const {
82  return m_url != other.m_url;
83  }
84 
85  URLEntry& operator=(const URLEntry &other) {
86  if (this != &other) {
87  m_url = other.m_url;
88  m_lifetime = other.m_lifetime;
89  }
90  return *this;
91  }
92 
93  void ToStream(std::ostream *out) const {
94  *out << m_url << "(" << m_lifetime << ")";
95  }
96 
97  std::string ToString() const {
98  std::ostringstream str;
99  ToStream(&str);
100  return str.str();
101  }
102 
103  friend std::ostream& operator<<(std::ostream &out, const URLEntry &entry) {
104  entry.ToStream(&out);
105  return out;
106  }
107 
108  protected:
109  std::string m_url;
110  uint16_t m_lifetime;
111  // TODO(simon): add auth blocks here
112 };
113 
114 // typedef for convenience
115 typedef std::vector<URLEntry> URLEntries;
116 } // namespace slp
117 } // namespace ola
118 #endif // INCLUDE_OLA_SLP_URLENTRY_H_