Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BaseSocketAwareParser.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import java.io.IOException;
  24. import java.net.InetAddress;
  25. import java.net.Socket;
  26. import java.net.URI;
  27. import javax.net.SocketFactory;
  28. /**
  29. * A base parser which can construct a SocketFactory given socket-related
  30. * options.
  31. */
  32. public abstract class BaseSocketAwareParser extends BaseParser {
  33. /** The socket that was most recently created by this parser. */
  34. private Socket socket;
  35. /** The local port that this parser's *most recently created* socket bound to. */
  36. private int localPort = -1;
  37. /**
  38. * Creates a new base parser for the specified URI.
  39. *
  40. * @param uri The URI this parser will connect to.
  41. */
  42. public BaseSocketAwareParser(final URI uri) {
  43. super(uri);
  44. }
  45. @Override
  46. public int getLocalPort() {
  47. if (localPort == -1 && socket != null) {
  48. // Try to update the local port from the socket, as it may have
  49. // bound since the last time we tried
  50. localPort = socket.getLocalPort();
  51. }
  52. return localPort;
  53. }
  54. /**
  55. * Convenience method to record the local port of the specified socket.
  56. *
  57. * @param socket The socket whose port is being recorded
  58. * @return The socket reference passed in, for convenience
  59. */
  60. private Socket handleSocket(final Socket socket) {
  61. // Store the socket as it might not have been bound yet
  62. this.socket = socket;
  63. this.localPort = socket.getLocalPort();
  64. return socket;
  65. }
  66. /**
  67. * Creates a socket factory that can be used by this parser.
  68. *
  69. * @return An appropriately configured socket factory
  70. */
  71. protected SocketFactory getSocketFactory() {
  72. return new SocketFactory() {
  73. @Override
  74. public Socket createSocket(final String host, final int port) throws IOException {
  75. if (getBindIP() == null) {
  76. return handleSocket(new Socket(host, port));
  77. } else {
  78. return handleSocket(new Socket(host, port, InetAddress.getByName(getBindIP()), 0));
  79. }
  80. }
  81. @Override
  82. public Socket createSocket(final InetAddress host, final int port) throws IOException {
  83. if (getBindIP() == null) {
  84. return handleSocket(new Socket(host, port));
  85. } else {
  86. return handleSocket(new Socket(host, port, InetAddress.getByName(getBindIP()), 0));
  87. }
  88. }
  89. @Override
  90. public Socket createSocket(final String host, final int port,
  91. final InetAddress localHost, final int localPort) throws IOException {
  92. return handleSocket(new Socket(host, port,
  93. getBindIP() == null ? localHost : InetAddress.getByName(getBindIP()), localPort));
  94. }
  95. @Override
  96. public Socket createSocket(final InetAddress address,
  97. final int port, final InetAddress localAddress,
  98. final int localPort) throws IOException {
  99. return handleSocket(new Socket(address, port,
  100. getBindIP() == null ? localAddress : InetAddress.getByName(getBindIP()), localPort));
  101. }
  102. };
  103. }
  104. }