Open Lighting Architecture
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 
32 using std::ostream;
33 using std::string;
34 
40 class URLEntry {
41  public:
42  URLEntry() {}
43 
49  URLEntry(const string &url, uint16_t lifetime)
50  : m_url(url),
51  m_lifetime(lifetime) {
52  }
53 
54  ~URLEntry() {}
55 
56  string url() const { return m_url; }
57  uint16_t lifetime() const { return m_lifetime; }
58  void set_lifetime(uint16_t lifetime) { m_lifetime = lifetime; }
59 
60  /*
61  * Age this URL by the given number of seconds
62  * @returns true if this url has now expired, false otherwise.
63  */
64  bool AgeLifetime(uint16_t seconds) {
65  if (m_lifetime <= seconds) {
66  m_lifetime = 0;
67  return true;
68  }
69  m_lifetime-= seconds;
70  return false;
71  }
72 
73  // Return the total size of this URL entry as it appears on the wire
74  unsigned int PackedSize() const { return 6 + m_url.size(); }
75 
76  // Write this ServiceEntry to an IOQueue
77  void Write(ola::io::BigEndianOutputStreamInterface *output) const;
78 
79  // equality is based on the url only
80  bool operator==(const URLEntry &other) const {
81  return m_url == other.m_url;
82  }
83 
84  bool operator!=(const URLEntry &other) const {
85  return m_url != other.m_url;
86  }
87 
88  URLEntry& operator=(const URLEntry &other) {
89  if (this != &other) {
90  m_url = other.m_url;
91  m_lifetime = other.m_lifetime;
92  }
93  return *this;
94  }
95 
96  void ToStream(ostream *out) const {
97  *out << m_url << "(" << m_lifetime << ")";
98  }
99 
100  string ToString() const {
101  std::ostringstream str;
102  ToStream(&str);
103  return str.str();
104  }
105 
106  friend ostream& operator<<(ostream &out, const URLEntry &entry) {
107  entry.ToStream(&out);
108  return out;
109  }
110 
111  protected:
112  string m_url;
113  uint16_t m_lifetime;
114  // TODO(simon): add auth blocks here
115 };
116 
117 // typedef for convenience
118 typedef std::vector<URLEntry> URLEntries;
119 } // namespace slp
120 } // namespace ola
121 #endif // INCLUDE_OLA_SLP_URLENTRY_H_