1
2
3
4
5 package net.sourceforge.jsdp.util;
6
7 import net.sourceforge.jsdp.SDPException;
8
9
10
11
12
13
14
15
16
17
18 public class Resource extends Address {
19
20
21 private static final long serialVersionUID = 1512624495750991087L;
22
23
24 protected int ttl;
25
26
27 protected int addresses;
28
29
30
31
32 protected Resource() {
33
34 super();
35 }
36
37
38
39
40
41
42
43
44
45 public Resource(final String address) throws SDPException {
46
47 if (address == null) {
48 throw new SDPException("Invalid network resource");
49 }
50
51 String[] values = address.split("/");
52 switch (values.length) {
53 case 1:
54 setAddress(values[0]);
55 this.ttl = 0;
56 this.addresses = 1;
57 break;
58 case 2:
59 setAddress(values[0]);
60 setTTL(Integer.parseInt(values[1]));
61 this.addresses = 1;
62 break;
63 case 3:
64 setAddress(values[0]);
65 setTTL(Integer.parseInt(values[1]));
66 setAddresses(Integer.parseInt(values[2]));
67 break;
68 default:
69 throw new SDPException("Invalid network resource");
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public Resource(final String address, final int ttl) throws SDPException {
84
85 super(address);
86 setTTL(ttl);
87 this.addresses = 1;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public Resource(final String address, final int ttl, final int addresses) throws SDPException {
103
104 super(address);
105 setTTL(ttl);
106 setAddresses(addresses);
107 }
108
109
110
111
112
113
114 public Object clone() {
115
116 Resource host = new Resource();
117
118 host.addressType = this.addressType;
119 host.address = new String(this.address);
120 host.ttl = this.ttl;
121 host.addresses = this.addresses;
122
123 return host;
124 }
125
126
127
128
129
130
131 public int getTTL() {
132
133 return ttl;
134 }
135
136
137
138
139
140
141
142 public boolean hasTTL() {
143
144 return ttl > 0;
145 }
146
147
148
149
150 public void removeTTL() {
151
152 this.ttl = 0;
153 this.addresses = 1;
154 }
155
156
157
158
159
160
161
162
163 public void setAddresses(final int addresses) throws SDPException {
164
165 if (addresses < 1) {
166 throw new SDPException();
167 }
168
169 this.addresses = addresses;
170 }
171
172
173
174
175
176
177
178
179 public void setTTL(final int ttl) throws SDPException {
180
181 if (ttl < 1 || ttl > 255) {
182 throw new SDPException("Invalid TTL");
183 }
184
185 this.ttl = ttl;
186 }
187
188
189
190
191 public String toString() {
192
193 StringBuffer result = new StringBuffer(super.toString());
194
195 if (hasTTL()) {
196 result.append("/" + ttl);
197 if (this.addresses > 1) {
198 result.append("/" + addresses);
199 }
200 }
201
202 return result.toString();
203 }
204 }