1 /*
2 * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3 * Vita
4 */
5 package net.sourceforge.jsdp;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import net.sourceforge.jsdp.util.Address;
11 import net.sourceforge.jsdp.util.Resource;
12
13 /**
14 * A <tt>Connection</tt> represents an <b>c=<i><field value></i></b>
15 * field contained in a SDP message.
16 * <p>
17 * A connection field is used to identify a network address on which media can
18 * be received. For this purpose it also identifies the network type, the
19 * address type, the start of an address range, the time to live of the session
20 * and the number of addresses in the range, but the time to live and number of
21 * addresses are optional.
22 * </p>
23 *
24 * @since 0.1.0
25 *
26 * @version 1.0
27 *
28 * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
29 */
30 public class Connection implements Field {
31
32 /** The class Stream Unique Identifier, SUID */
33 private static final long serialVersionUID = 1555839237745744131L;
34
35 /** The IP4 address type */
36 public static final String IP4 = Address.IP4;
37
38 /** The IP6 address type */
39 public static final String IP6 = Address.IP6;
40
41 /** The pattern used to parse the field */
42 private static final Pattern fieldPattern = Pattern.compile("IN (IP4|IP6) ((.+))");
43
44 /** The network resource specified by the field */
45 protected Resource resource;
46
47 /**
48 * Creates a new <tt>Connection</tt>.
49 */
50 protected Connection() {
51
52 super();
53 }
54
55 /**
56 * Creates a new <tt>Connection</tt>.
57 *
58 * @param resource the network address on which media can be received. The
59 * address type associated with this connection depends by address
60 * value
61 *
62 * @throws SDPException if the network address is not valid
63 */
64 public Connection(final String resource) throws SDPException {
65
66 super();
67 this.resource = new Resource(resource);
68 }
69
70 /**
71 * Parse an input string and constructs the equivalent connection field.
72 *
73 * @param field the string to parse
74 *
75 * @return a new <tt>Connection</tt> instance
76 *
77 * @throws SDPParseException if an error occurs while parsing
78 */
79 public static Connection parse(final String field) throws SDPParseException {
80
81 if (!field.startsWith("c=")) {
82 throw new SDPParseException("The string \"" + field + "\" isn't a connection field");
83 }
84
85 Connection c = null;
86
87 /* Create the matcher */
88 Matcher matcher = fieldPattern.matcher(field.substring(2));
89
90 /* Test */
91 if (matcher.matches()) {
92 try {
93
94 /* Get the address type */
95 String type = matcher.group(1);
96
97 c = new Connection(matcher.group(2));
98
99 if (!c.getAddressType().equals(type)) {
100 throw new SDPParseException("The address " + c.getAddress() + " isn't an " + type + " address");
101 }
102 }
103 catch (SDPException parseException) {
104 throw new SDPParseException("The string \"" + field + "\" isn't a valid connection field", parseException);
105 }
106 }
107 else {
108 throw new SDPParseException("The string \"" + field + "\" isn't a valid connection field");
109 }
110
111 return c;
112 }
113
114 /**
115 * Returns a clone of this field.
116 *
117 * @return a clone of this field
118 */
119 public Object clone() {
120
121 Connection field = new Connection();
122 field.resource = (Resource) resource.clone();
123
124 return field;
125 }
126
127 /**
128 * Returns the network address.
129 *
130 * @return the network address
131 */
132 public String getAddress() {
133
134 return resource.getAddress();
135 }
136
137 /**
138 * Returns the type of the address.
139 *
140 * @return the type of the address: <b>IP4</b> or <b>IP6</b>
141 */
142 public String getAddressType() {
143
144 return resource.getAddressType();
145 }
146
147 /**
148 * Returns the network type.
149 *
150 * @return the network type. Because SDP was used in Internet, this method
151 * always returns <b>IN</b>
152 */
153 public String getNetType() {
154
155 return Address.IN;
156 }
157
158 /**
159 * Returns the type character for the field.
160 *
161 * @return the field type character: <b>c</b>
162 */
163 public char getType() {
164
165 return Field.CONNECTION_FIELD;
166 }
167
168 /**
169 * Sets the network address.
170 *
171 * @param address the network address
172 *
173 * @throws SDPException if the address isn't a valid IP4, IP6 or FQDN
174 * address
175 */
176 public void setAddress(final String address) throws SDPException {
177
178 resource.setAddress(address);
179 }
180
181 /**
182 * Sets the network address.
183 *
184 * @param address the network address
185 *
186 * @param ttl the time to live
187 *
188 * @throws SDPException if the address isn't a valid IP4, IP6 or FQDN
189 * address, or TTL is minor than 1 or greater than 255
190 */
191 public void setAddress(final String address, final int ttl) throws SDPException {
192
193 resource.setAddress(address);
194 resource.setTTL(ttl);
195 }
196
197 /**
198 * Sets the network address.
199 *
200 * @param address the network address
201 *
202 * @param ttl the time to live
203 *
204 * @param addresses the number of the addresses
205 *
206 * @throws SDPException if the address isn't a valid IP4, IP6 or FQDN
207 * address, or TTL is minor than 1 or greater than 255, or the
208 * number of addresses is negative
209 */
210 public void setAddress(final String address, final int ttl, final int addresses) throws SDPException {
211
212 resource.setAddress(address);
213 resource.setTTL(ttl);
214 resource.setAddresses(addresses);
215 }
216
217 /**
218 * Returns a string representation of the field. The representation has the
219 * form: <b>c=IN <i><type></i> <i><address></i></b>
220 *
221 * @return The string representation of the field
222 */
223 public String toString() {
224
225 return getType() + "=" + Address.IN + " " + resource.toString();
226 }
227 }