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.

BaseSocketAwareParser.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2013 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. /** {@inheritDoc} */
  46. @Override
  47. public int getLocalPort() {
  48. if (localPort == -1 && socket != null) {
  49. // Try to update the local port from the socket, as it may have
  50. // bound since the last time we tried
  51. localPort = socket.getLocalPort();
  52. }
  53. return localPort;
  54. }
  55. /**
  56. * Convenience method to record the local port of the specified socket.
  57. *
  58. * @param socket The socket whose port is being recorded
  59. * @return The socket reference passed in, for convenience
  60. */
  61. private Socket handleSocket(final Socket socket) {
  62. // Store the socket as it might not have been bound yet
  63. this.socket = socket;
  64. this.localPort = socket.getLocalPort();
  65. return socket;
  66. }
  67. /**
  68. * Creates a socket factory that can be used by this parser.
  69. *
  70. * @return An appropriately configured socket factory
  71. */
  72. protected SocketFactory getSocketFactory() {
  73. return new SocketFactory() {
  74. /** {@inheritDoc} */
  75. @Override
  76. public Socket createSocket(final String host, final int port) throws IOException {
  77. if (getBindIP() == null) {
  78. return handleSocket(new Socket(host, port));
  79. } else {
  80. return handleSocket(new Socket(host, port, InetAddress.getByName(getBindIP()), 0));
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public Socket createSocket(final InetAddress host, final int port) throws IOException {
  86. if (getBindIP() == null) {
  87. return handleSocket(new Socket(host, port));
  88. } else {
  89. return handleSocket(new Socket(host, port, InetAddress.getByName(getBindIP()), 0));
  90. }
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public Socket createSocket(final String host, final int port,
  95. final InetAddress localHost, final int localPort) throws IOException {
  96. return handleSocket(new Socket(host, port,
  97. getBindIP() == null ? localHost : InetAddress.getByName(getBindIP()), localPort));
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public Socket createSocket(final InetAddress address,
  102. final int port, final InetAddress localAddress,
  103. final int localPort) throws IOException {
  104. return handleSocket(new Socket(address, port,
  105. getBindIP() == null ? localAddress : InetAddress.getByName(getBindIP()), localPort));
  106. }
  107. };
  108. }
  109. }