/
DTNScheme.java
333 lines (254 loc) · 8.44 KB
/
DTNScheme.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* This file is part of the Bytewalla Project
* More information can be found at "http://www.tslab.ssvl.kth.se/csd/projects/092106/".
*
* Copyright 2009 Telecommunication Systems Laboratory (TSLab), Royal Institute of Technology, Sweden.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package se.kth.ssvl.tslab.bytewalla.androiddtn.servlib.naming;
import java.io.Serializable;
import java.net.URI;
import java.util.regex.Pattern;
import android.util.Log;
/**
* This is the default scheme for the DTN network.
* The pattern for the scheme is "dtn://{host}/{application_tag}"
* The NULL EID for this scheme is "dtn:none"
*
* @author Rerngvit Yanggratoke (rerngvit@kth.se)
*/
public class DTNScheme extends Scheme implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2671779767857883030L;
/**
* The TAG name for Android Logging mechanism
*/
private String TAG = "DTNScheme";
/**
* schemeText used internally
*/
private static String schemeText = "dtn";
/**
* Part of the Singleton implementation
*/
private static DTNScheme instance_ = null;
/**
* NULL EID Text Representation
*/
private static final String NullEIDText = "dtn:none";
/**
* Wildcard EID Text Representation
*/
private static final String WildcardEIDText = "dtn://*";
/**
* Variable to hold singleton NULL EndpointID
*/
private static EndpointID null_eid;
/**
* Variable to hold singleton Wildcard EndpointID
*/
private static EndpointIDPattern wildcard_eid;
/**
* Private constructor
*/
private DTNScheme() {
// Exists only to defeat instantiation
}
static {
// Right now only support DTN Scheme
null_eid = new EndpointID(NullEIDText);
wildcard_eid = new EndpointIDPattern(WildcardEIDText);
}
/**
* Singleton implementation of DTNScheme
* @return the instance of DTNScheme
*/
public static DTNScheme getInstance() {
if (instance_ == null) {
instance_ = new DTNScheme();
}
return instance_;
}
/**
* Getter function for the NULL Endpoint ID of this DTNScheme
* @return The NULL EndpointID
*/
public static EndpointID NULL_EID()
{
return null_eid;
}
/**
* Return the special wildcard Endpoint ID. This functionality is
* not in the bundle spec, but is used internally to this
* implementation.
*/
public final static EndpointIDPattern WILDCARD_EID()
{
return wildcard_eid;
}
// End Singleton Implementation of the DTNScheme
/**
* Validate that the SSP in the given URI is legitimate for this scheme. If
* the 'is_pattern' paraemeter is true, then the ssp is being validated as
* an EndpointIDPattern.
*
* @return true if valid
*/
@Override
public boolean validate(URI uri, boolean is_pattern) {
if (uri.toString().equals(NullEIDText) || uri.toString().equals(WildcardEIDText))
{
return true;
}
if ( is_pattern ) return true;
if (uri.getHost() == null ) return false;
if (!uri.getScheme().equals(schemeText) ) return false;
// There shouldn't be nested path in this Scheme.
// For example, this is fine: dtn://endpoint.com/tag
// But, this is not valid: dtn://endpoint.com/tag/nested
if (!EndpointID.is_able_to_create_URI(uri.toString()))
{
Log.d(TAG, "DTNScheme::validate: invalid URI");
return false;
}
// a valid dtn scheme uri must have a host component in the
// authority unless it's the special "dtn:none" uri
if (!uri.getSchemeSpecificPart().equals("none") && uri.getHost().length() == 0 ) {
return false;
}
return true;
}
/**
* Match the pattern to the endpoint id in a scheme-specific manner.
*/
@Override
public boolean match(final EndpointIDPattern pattern, final EndpointID eid) {
// sanity check
assert (pattern.scheme() == this): "Sanity check in match method fail";
// we only match endpoint ids of the same scheme
if (!eid.known_scheme() || (!(eid.scheme() instanceof DTNScheme))) {
return false;
}
// if the ssp of either string is "none", then nothing should
// match it (ever)
if (pattern.ssp().equals("none") || eid.ssp().equals("none")) {
return false;
}
// check for a wildcard host specifier e.g dtn://*
if (pattern.equals(DTNScheme.WILDCARD_EID()))
{
return true;
}
// return Instantly when the eid and Pattern is exactly the same
if (pattern.equals(eid))
{
return true;
}
try
{
String regexPattern = pattern.uri().toString().replaceAll("\\*", "[a-z[0-9]\\/\\.]*");
//regexPattern = regexPattern.replaceAll("\\.", ".");
boolean matchResult = Pattern.matches(regexPattern, eid.uri().toString());
return matchResult;
}
catch (Exception e)
{
Log.d(TAG, "DTNScheme::match: Pattern Exception");
return false;
}
}
/**
* Check the URI, it will return the InvalidURIException otherwise, it will do nothing
*/
private void check_URI(URI uri) throws InvalidURIException
{
if (uri == null) throw new InvalidURIException();
if (!uri.getScheme().equals(schemeText)) throw new InvalidURIException();
if (!EndpointID.is_able_to_create_URI(uri.toString())) throw new InvalidURIException();
}
/**
* Append the given service tag to the uri in a scheme-specific manner. By
* default, the scheme is not capable of this.
*
* @return true if this scheme is capable of service tags and the tag is a
* legal one, false otherwise.
* @throws InvalidURIException
*/
@Override
public URI append_service_tag(URI uri, final String tag) throws InvalidURIException{
check_URI(uri);
URI uri_result;
if (tag.charAt(0) != '/')
{
uri_result = URI.create(uri.getScheme() + "://" + uri.getHost() + "/" + tag);
}
else
{
uri_result = URI.create(uri.getScheme() + "://" + uri.getHost() + tag);
}
return uri_result;
}
/**
* Append wildcard to the uri in a scheme-specific manner. The default
* scheme is not capable of this.
*
* @return true if this scheme is capable of wildcards and the wildcard is
* successfully appended, else false.
* @throws NamingException
*/
@Override
public URI append_service_wildcard(URI uri) throws InvalidURIException {
if (uri.toString().equals(NullEIDText))throw new InvalidURIException();
if (!uri.getPath().equals("")) throw new InvalidURIException();
check_URI(uri);
URI result_uri = URI.create(uri.getScheme() + "://" + uri.getHost() + "/*");
return result_uri;
}
/**
* Reduce URI to node ID in a scheme specific manner. The default scheme is
* not capable of this.
*
* @return true if this scheme is capable of this reduction and the
* reduction is successful, else false.
*/
@Override
public URI remove_service_tag(URI uri) throws InvalidURIException {
if (uri == null) throw new InvalidURIException();
URI result_uri = URI.create(uri.getScheme() + "://" + uri.getHost());
return result_uri;
}
/**
* Check if the given URI is a singleton endpoint id.
*/
@Override
public EndpointID.singleton_info_t is_singleton(final URI uri) {
if (uri.toString().contains("*"))
{
Log.d(TAG, "URI host contains a wildcard, so is MULTINODE");
return EndpointID.singleton_info_t.MULTINODE;
}
Log.d(TAG, "URI host contains a wildcard, so is SINGLETON");
return EndpointID.singleton_info_t.SINGLETON;
}
/**
* Return scheme specific String
*/
@Override
public String scheme_str() {
return schemeText;
}
}