|
1 /* |
|
2 * Copyright 2007 Baylor University |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain 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 _PROPHET_DICTIONARY_H_ |
|
18 #define _PROPHET_DICTIONARY_H_ |
|
19 |
|
20 #include <string> |
|
21 #include <map> |
|
22 #include "Util.h" |
|
23 #include "Bundle.h" |
|
24 #include "BundleCore.h" |
|
25 |
|
26 namespace prophet |
|
27 { |
|
28 |
|
29 /** |
|
30 * Utility class to facilitate converting to and from routing string |
|
31 * (endpoint ID) and 16-bit string IDs |
|
32 */ |
|
33 class Dictionary |
|
34 { |
|
35 public: |
|
36 typedef std::map<u_int16_t,std::string> rribd; |
|
37 typedef std::map<u_int16_t,std::string>::const_iterator |
|
38 const_iterator; |
|
39 |
|
40 /** |
|
41 * Reserve 0xffff for in-band error signal |
|
42 */ |
|
43 static const u_int16_t INVALID_SID; |
|
44 |
|
45 /** |
|
46 * Used for in-band error signal |
|
47 */ |
|
48 static const std::string NULL_STR; |
|
49 |
|
50 /** |
|
51 * Default constructor |
|
52 */ |
|
53 Dictionary(const std::string& sender = "", |
|
54 const std::string& receiver = ""); |
|
55 |
|
56 /** |
|
57 * Copy constructor |
|
58 */ |
|
59 Dictionary(const Dictionary& d); |
|
60 |
|
61 /** |
|
62 * Destructor |
|
63 */ |
|
64 ~Dictionary() {} |
|
65 |
|
66 /** |
|
67 * If dest_id is valid, returns sid; else returns INVALID_SID |
|
68 */ |
|
69 u_int16_t find(const std::string& dest_id) const; |
|
70 |
|
71 /** |
|
72 * If id is valid, returns dest_id; else returns empty string |
|
73 */ |
|
74 const std::string& find(u_int16_t sid) const; |
|
75 |
|
76 /** |
|
77 * Convenience function; sender is defined as sid == 0 |
|
78 */ |
|
79 const std::string& sender() const { return sender_; } |
|
80 |
|
81 /** |
|
82 * Convenience function; receiver is defined as sid == 1 |
|
83 */ |
|
84 const std::string& receiver() const { return receiver_; } |
|
85 |
|
86 /** |
|
87 * Insert dest_id and return valid id upon success; else |
|
88 * returns INVALID_SID |
|
89 */ |
|
90 u_int16_t insert(const std::string& dest_id); |
|
91 |
|
92 /** |
|
93 * Convenience wrapper that accepts const Bundle* and |
|
94 * passes through a call to insert(const std::string&) |
|
95 */ |
|
96 u_int16_t insert(const Bundle* b); |
|
97 |
|
98 /** |
|
99 * Assign dest_id to arbitrary id (used by TLV deserialization); |
|
100 * return true upon success, false upon collision |
|
101 */ |
|
102 bool assign(const std::string& dest_id, u_int16_t sid); |
|
103 |
|
104 ///@{ Iterators |
|
105 const_iterator begin() const { return rribd_.begin(); } |
|
106 const_iterator end() const { return rribd_.end(); } |
|
107 ///@} |
|
108 |
|
109 /** |
|
110 * Return the number of elements in the dictionary |
|
111 */ |
|
112 size_t size() const { return ribd_.size(); } |
|
113 |
|
114 /** |
|
115 * Helper function for serializing, where RASsz is the |
|
116 * sizeof(RoutingAddressString), the overhead per RIBD entry |
|
117 */ |
|
118 size_t guess_ribd_size(size_t RASsz) const; |
|
119 |
|
120 /** |
|
121 * Wipe out dictionary and prepare to deserialize new one |
|
122 */ |
|
123 void clear(); |
|
124 |
|
125 /** |
|
126 * Assignment operator |
|
127 */ |
|
128 Dictionary& operator= (const Dictionary& d) |
|
129 { |
|
130 sender_.assign(d.sender_); |
|
131 receiver_.assign(d.receiver_); |
|
132 ribd_ = d.ribd_; |
|
133 rribd_ = d.rribd_; |
|
134 return *this; |
|
135 } |
|
136 |
|
137 /** |
|
138 * Debug method for printing out internal state to log |
|
139 */ |
|
140 void dump(BundleCore* core,const char* file,u_int line) const; |
|
141 |
|
142 protected: |
|
143 typedef std::map<std::string,u_int16_t,less_string> ribd; |
|
144 |
|
145 std::string sender_; ///< destination id of peering initiator |
|
146 std::string receiver_; ///< destination id of passive peer |
|
147 ribd ribd_; ///< forward lookup, dest_id to string id |
|
148 rribd rribd_; ///< reverse lookup from string id to dest_id |
|
149 |
|
150 }; // Dictionary |
|
151 |
|
152 }; // namespace prophet |
|
153 #endif // _PROPHET_DICTIONARY_H_ |