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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2011 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 IP address or hostname that this parser's sockets should bind to. */
  34. private String bindIp = null;
  35. /** The socket that was most recently created by this parser. */
  36. private Socket socket;
  37. /** The local port that this parser's *most recently created* socket bound to. */
  38. private int localPort = -1;
  39. /**
  40. * Creates a new base parser for the specified URI.
  41. *
  42. * @param uri The URI this parser will connect to.
  43. */
  44. public BaseSocketAwareParser(final URI uri) {
  45. super(uri);
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public String getBindIP() {
  50. return bindIp;
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public void setBindIP(final String ip) {
  55. bindIp = ip;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public int getLocalPort() {
  60. if (localPort == -1 && socket != null) {
  61. // Try to update the local port from the socket, as it may have
  62. // bound since the last time we tried
  63. localPort = socket.getLocalPort();
  64. }
  65. return localPort;
  66. }
  67. /**
  68. * Convenience method to record the local port of the specified socket.
  69. *
  70. * @param socket The socket whose port is being recorded
  71. * @return The socket reference passed in, for convenience
  72. */
  73. private Socket handleSocket(final Socket socket) {
  74. // Store the socket as it might not have been bound yet
  75. this.socket = socket;
  76. this.localPort = socket.getLocalPort();
  77. return socket;
  78. }
  79. /**
  80. * Creates a socket factory that can be used by this parser.
  81. *
  82. * @return An appropriately configured socket factory
  83. */
  84. protected SocketFactory getSocketFactory() {
  85. return new SocketFactory() {
  86. /** {@inheritDoc} */
  87. @Override
  88. public Socket createSocket(final String host, final int port) throws IOException {
  89. if (bindIp == null) {
  90. return handleSocket(new Socket(host, port));
  91. } else {
  92. return handleSocket(new Socket(host, port, InetAddress.getByName(bindIp), 0));
  93. }
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public Socket createSocket(final InetAddress host, final int port) throws IOException {
  98. if (bindIp == null) {
  99. return handleSocket(new Socket(host, port));
  100. } else {
  101. return handleSocket(new Socket(host, port, InetAddress.getByName(bindIp), 0));
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public Socket createSocket(final String host, final int port,
  107. final InetAddress localHost, final int localPort) throws IOException {
  108. return handleSocket(new Socket(host, port,
  109. bindIp == null ? localHost : InetAddress.getByName(bindIp), localPort));
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public Socket createSocket(final InetAddress address,
  114. final int port, final InetAddress localAddress,
  115. final int localPort) throws IOException {
  116. return handleSocket(new Socket(address, port,
  117. bindIp == null ? localAddress : InetAddress.getByName(bindIp), localPort));
  118. }
  119. };
  120. }
  121. }