Unsupported library that attempts to punch holes through NAT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Candidate.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This file is part of JSTUN.
  3. *
  4. * Copyright (c) 2005 Thomas King <king@t-king.de> - All rights
  5. * reserved.
  6. *
  7. * This software is licensed under either the GNU Public License (GPL),
  8. * or the Apache 2.0 license. Copies of both license agreements are
  9. * included in this distribution.
  10. */
  11. package de.javawi.jstun.test.demo.ice;
  12. import java.net.DatagramSocket;
  13. import java.net.SocketException;
  14. import java.net.UnknownHostException;
  15. import de.javawi.jstun.util.Address;
  16. import de.javawi.jstun.util.UtilityException;
  17. public class Candidate implements Comparable {
  18. // The ieft-mmusic-ice-12 draft is not non-ambigious about the number of types.
  19. // Chapter 5.1 defines 3 and 4 types on page 16 and page 17, respectively.
  20. public enum CandidateType { Local, ServerReflexive, PeerReflexive, Relayed };
  21. private DatagramSocket socket;
  22. private CandidateType type;
  23. private short componentId;
  24. private int priority;
  25. private int foundationId;
  26. private Candidate base;
  27. private boolean isInUse;
  28. public Candidate(Address address, short componentId) throws SocketException, UnknownHostException, UtilityException {
  29. this.socket = new DatagramSocket(0, address.getInetAddress());
  30. this.type = CandidateType.Local;
  31. this.componentId = componentId;
  32. this.priority = 0;
  33. this.base = this;
  34. this.isInUse = false;
  35. }
  36. public Candidate(Address address, CandidateType type, short componentId, Candidate base) throws SocketException, UnknownHostException, UtilityException {
  37. this.socket = new DatagramSocket(0, address.getInetAddress());
  38. this.type = type;
  39. setComponentId(componentId);
  40. this.priority = 0;
  41. this.base = base;
  42. this.isInUse = false;
  43. }
  44. public void setBase(Candidate base) {
  45. this.base = base;
  46. }
  47. public Candidate getBase() {
  48. return base;
  49. }
  50. public CandidateType getCandidateType() {
  51. return type;
  52. }
  53. public void setComponentId(short componentId) {
  54. if ((componentId < 1) || (componentId > 256)) throw new IllegalArgumentException(componentId + " is not between 1 and 256 inclusive.");
  55. this.componentId = componentId;
  56. }
  57. public short getComponentId() {
  58. return componentId;
  59. }
  60. public void setFoundationId(int foundationId) {
  61. this.foundationId = foundationId;
  62. }
  63. public int getFoundationId() {
  64. return foundationId;
  65. }
  66. public void setPriority(int priority) {
  67. this.priority = priority;
  68. }
  69. public int getPriority() {
  70. return priority;
  71. }
  72. public Address getAddress() throws UtilityException {
  73. return new Address(socket.getLocalAddress().getAddress());
  74. }
  75. public int getPort() {
  76. return socket.getLocalPort();
  77. }
  78. public void setInUse(boolean isInUse) {
  79. this.isInUse = isInUse;
  80. }
  81. public boolean getInUse() {
  82. return isInUse;
  83. }
  84. public int compareTo(Object arg0) {
  85. Candidate cand = (Candidate) arg0;
  86. return cand.getPriority() - getPriority();
  87. }
  88. public boolean equals(Object o) {
  89. if (o == null) return false;
  90. if ((((Candidate) o).socket.equals(socket)) && (((Candidate) o).base.equals(base))) return true;
  91. return false;
  92. }
  93. }