/spark/src/plugins/sip/src/java/net/java/sipmack/media/TransportCandidate.java

https://github.com/joechen2010/IM · Java · 519 lines · 270 code · 62 blank · 187 comment · 85 complexity · a3f32ca7c98c102563aeae56ecd42496 MD5 · raw file

  1. /**
  2. * $RCSfile: ,v $
  3. * $Revision: $
  4. * $Date: $
  5. *
  6. * Copyright (C) 2004-2011 Jive Software. All rights reserved.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package net.java.sipmack.media;
  21. import java.net.SocketException;
  22. import java.net.UnknownHostException;
  23. import org.jivesoftware.smack.XMPPConnection;
  24. /**
  25. * Transport candidate.
  26. * <p/>
  27. * A candidate represents the possible transport for data interchange between
  28. * the two endpoints.
  29. *
  30. * @author Thiago Camargo
  31. */
  32. public abstract class TransportCandidate {
  33. private String name;
  34. private String ip; // IP address
  35. private int port; // Port to use, or 0 for any port
  36. private String localIp;
  37. private int generation;
  38. protected String password;
  39. private String sessionId;
  40. private XMPPConnection connection;
  41. private TransportCandidate symmetric;
  42. public void addCandidateEcho() throws SocketException, UnknownHostException {
  43. }
  44. public String getIp() {
  45. return ip;
  46. }
  47. /**
  48. * Set the IP address.
  49. *
  50. * @param ip the IP address
  51. */
  52. public void setIp(String ip) {
  53. this.ip = ip;
  54. }
  55. /**
  56. * Get local IP to bind to this candidate
  57. *
  58. * @return
  59. */
  60. public String getLocalIp() {
  61. return localIp == null ? ip : localIp;
  62. }
  63. /**
  64. * Set local IP to bind to this candidate
  65. *
  66. * @param localIp
  67. */
  68. public void setLocalIp(String localIp) {
  69. this.localIp = localIp;
  70. }
  71. /**
  72. * Get the symetric candidate for this candidate if it exists.
  73. *
  74. * @return
  75. */
  76. public TransportCandidate getSymmetric() {
  77. return symmetric;
  78. }
  79. /**
  80. * Set the symetric candidate for this candidate.
  81. *
  82. * @param symetric
  83. */
  84. public void setSymmetric(TransportCandidate symetric) {
  85. this.symmetric = symetric;
  86. }
  87. /**
  88. * Get the password used by ICE or relayed candidate
  89. *
  90. * @return a password
  91. */
  92. public String getPassword() {
  93. return password;
  94. }
  95. /**
  96. * Set the password used by ICE or relayed candidate
  97. *
  98. * @param password a password
  99. */
  100. public void setPassword(String password) {
  101. this.password = password;
  102. }
  103. /**
  104. * Get the XMPPConnection use to send or receive this candidate
  105. *
  106. * @return
  107. */
  108. public XMPPConnection getConnection() {
  109. return connection;
  110. }
  111. /**
  112. * Set the XMPPConnection use to send or receive this candidate
  113. *
  114. * @param connection
  115. */
  116. public void setConnection(XMPPConnection connection) {
  117. this.connection = connection;
  118. }
  119. /**
  120. * Get the jingle�s sessionId that is using this candidate
  121. *
  122. * @return
  123. */
  124. public String getSessionId() {
  125. return sessionId;
  126. }
  127. /**
  128. * Set the jingle�s sessionId that is using this candidate
  129. *
  130. * @param sessionId
  131. */
  132. public void setSessionId(String sessionId) {
  133. this.sessionId = sessionId;
  134. }
  135. /**
  136. * Empty constructor
  137. */
  138. public TransportCandidate() {
  139. this(null, 0, 0);
  140. }
  141. /**
  142. * Constructor with IP address and port
  143. *
  144. * @param ip The IP address.
  145. * @param port The port number.
  146. */
  147. public TransportCandidate(String ip, int port) {
  148. this(ip, port, 0);
  149. }
  150. /**
  151. * Constructor with IP address and port
  152. *
  153. * @param ip The IP address.
  154. * @param port The port number.
  155. * @param generation The generation
  156. */
  157. public TransportCandidate(String ip, int port, int generation) {
  158. this.ip = ip;
  159. this.port = port;
  160. this.generation = generation;
  161. }
  162. /**
  163. * Return true if the candidate is not valid.
  164. *
  165. * @return true if the candidate is null.
  166. */
  167. public boolean isNull() {
  168. if (ip == null) {
  169. return true;
  170. }
  171. else if (ip.length() == 0) {
  172. return true;
  173. }
  174. else if (port < 0) {
  175. return true;
  176. }
  177. else {
  178. return false;
  179. }
  180. }
  181. /**
  182. * Get the port, or 0 for any port.
  183. *
  184. * @return the port or 0
  185. */
  186. public int getPort() {
  187. return port;
  188. }
  189. /**
  190. * Set the port, using 0 for any port
  191. *
  192. * @param port the port
  193. */
  194. public void setPort(int port) {
  195. this.port = port;
  196. }
  197. /**
  198. * Get the generation for a transportElement definition
  199. *
  200. * @return the generation
  201. */
  202. public int getGeneration() {
  203. return generation;
  204. }
  205. /**
  206. * Set the generation for a transportElement definition.
  207. *
  208. * @param generation the generation number
  209. */
  210. public void setGeneration(int generation) {
  211. this.generation = generation;
  212. }
  213. /**
  214. * Get the name used for identifying this transportElement method (optional)
  215. *
  216. * @return a name used for identifying this transportElement (ie,
  217. * "myrtpvoice1")
  218. */
  219. public String getName() {
  220. return name;
  221. }
  222. /**
  223. * Set a name for identifying this transportElement.
  224. *
  225. * @param name the name used for the transportElement
  226. */
  227. public void setName(String name) {
  228. this.name = name;
  229. }
  230. /*
  231. * (non-Javadoc)
  232. *
  233. * @see java.lang.Object#equals(java.lang.Object)
  234. */
  235. public boolean equals(Object obj) {
  236. if (this == obj) {
  237. return true;
  238. }
  239. if (obj == null) {
  240. return false;
  241. }
  242. if (getClass() != obj.getClass()) {
  243. return false;
  244. }
  245. final TransportCandidate other = (TransportCandidate) obj;
  246. if (generation != other.generation) {
  247. return false;
  248. }
  249. if (getIp() == null) {
  250. if (other.getIp() != null) {
  251. return false;
  252. }
  253. }
  254. else if (!getIp().equals(other.getIp())) {
  255. return false;
  256. }
  257. if (getName() == null) {
  258. if (other.getName() != null) {
  259. return false;
  260. }
  261. }
  262. else if (!getName().equals(other.getName())) {
  263. return false;
  264. }
  265. if (getPort() != other.getPort()) {
  266. return false;
  267. }
  268. return true;
  269. }
  270. /**
  271. * Fixed transport candidate
  272. */
  273. public static class Fixed extends TransportCandidate {
  274. public Fixed() {
  275. super();
  276. }
  277. /**
  278. * Constructor with IP address and port
  279. *
  280. * @param ip The IP address.
  281. * @param port The port number.
  282. */
  283. public Fixed(String ip, int port) {
  284. super(ip, port);
  285. }
  286. /**
  287. * Constructor with IP address and port
  288. *
  289. * @param ip The IP address.
  290. * @param port The port number.
  291. * @param generation The generation
  292. */
  293. public Fixed(String ip, int port, int generation) {
  294. super(ip, port, generation);
  295. }
  296. }
  297. /**
  298. * Type-safe enum for the transportElement protocol
  299. */
  300. public static class Protocol {
  301. public static final Protocol UDP = new Protocol("udp");
  302. public static final Protocol TCP = new Protocol("tcp");
  303. public static final Protocol TCPACT = new Protocol("tcp-act");
  304. public static final Protocol TCPPASS = new Protocol("tcp-pass");
  305. public static final Protocol SSLTCP = new Protocol("ssltcp");
  306. private String value;
  307. public Protocol(String value) {
  308. this.value = value;
  309. }
  310. public String toString() {
  311. return value;
  312. }
  313. /**
  314. * Returns the Protocol constant associated with the String value.
  315. */
  316. public static Protocol fromString(String value) {
  317. if (value == null) {
  318. return UDP;
  319. }
  320. value = value.toLowerCase();
  321. if (value.equals("udp")) {
  322. return UDP;
  323. }
  324. else if (value.equals("tcp")) {
  325. return TCP;
  326. }
  327. else if (value.equals("tcp-act")) {
  328. return TCPACT;
  329. }
  330. else if (value.equals("tcp-pass")) {
  331. return TCPPASS;
  332. }
  333. else if (value.equals("ssltcp")) {
  334. return SSLTCP;
  335. }
  336. else {
  337. return UDP;
  338. }
  339. }
  340. /*
  341. * (non-Javadoc)
  342. *
  343. * @see java.lang.Object#equals(java.lang.Object)
  344. */
  345. public boolean equals(Object obj) {
  346. if (this == obj) {
  347. return true;
  348. }
  349. if (obj == null) {
  350. return false;
  351. }
  352. if (getClass() != obj.getClass()) {
  353. return false;
  354. }
  355. final Protocol other = (Protocol) obj;
  356. if (value == null) {
  357. if (other.value != null) {
  358. return false;
  359. }
  360. }
  361. else if (!value.equals(other.value)) {
  362. return false;
  363. }
  364. return true;
  365. }
  366. /**
  367. * Return true if the protocol is not valid.
  368. *
  369. * @return true if the protocol is null
  370. */
  371. public boolean isNull() {
  372. if (value == null) {
  373. return true;
  374. }
  375. else if (value.length() == 0) {
  376. return true;
  377. }
  378. else {
  379. return false;
  380. }
  381. }
  382. }
  383. /**
  384. * Type-safe enum for the transportElement channel
  385. */
  386. public static class Channel {
  387. public static final Channel MYRTPVOICE = new Channel("myrtpvoice");
  388. public static final Channel MYRTCPVOICE = new Channel("myrtcpvoice");
  389. private String value;
  390. public Channel(String value) {
  391. this.value = value;
  392. }
  393. public String toString() {
  394. return value;
  395. }
  396. /**
  397. * Returns the MediaChannel constant associated with the String value.
  398. */
  399. public static Channel fromString(String value) {
  400. if (value == null) {
  401. return MYRTPVOICE;
  402. }
  403. value = value.toLowerCase();
  404. if (value.equals("myrtpvoice")) {
  405. return MYRTPVOICE;
  406. }
  407. else if (value.equals("tcp")) {
  408. return MYRTCPVOICE;
  409. }
  410. else {
  411. return MYRTPVOICE;
  412. }
  413. }
  414. /*
  415. * (non-Javadoc)
  416. *
  417. * @see java.lang.Object#equals(java.lang.Object)
  418. */
  419. public boolean equals(Object obj) {
  420. if (this == obj) {
  421. return true;
  422. }
  423. if (obj == null) {
  424. return false;
  425. }
  426. if (getClass() != obj.getClass()) {
  427. return false;
  428. }
  429. final Channel other = (Channel) obj;
  430. if (value == null) {
  431. if (other.value != null) {
  432. return false;
  433. }
  434. }
  435. else if (!value.equals(other.value)) {
  436. return false;
  437. }
  438. return true;
  439. }
  440. /**
  441. * Return true if the channel is not valid.
  442. *
  443. * @return true if the channel is null
  444. */
  445. public boolean isNull() {
  446. if (value == null) {
  447. return true;
  448. }
  449. else if (value.length() == 0) {
  450. return true;
  451. }
  452. else {
  453. return false;
  454. }
  455. }
  456. }
  457. }