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.

BaseParser.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2017 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 com.dmdirc.parser.events.ParserErrorEvent;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  25. import com.dmdirc.parser.interfaces.ClientInfo;
  26. import java.net.InetSocketAddress;
  27. import java.net.Proxy;
  28. import java.net.ProxySelector;
  29. import java.net.SocketAddress;
  30. import java.net.URI;
  31. import java.net.URISyntaxException;
  32. import java.time.LocalDateTime;
  33. import java.util.List;
  34. import net.engio.mbassy.bus.error.PublicationError;
  35. /**
  36. * Implements common base functionality for parsers.
  37. * <p>
  38. * Implementations of this class must be annotated with {@link ChildImplementations} to define
  39. * the implementations to use for instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
  40. */
  41. public abstract class BaseParser extends ThreadedParser {
  42. private final Object errorHandlerLock = new Object();
  43. /** The URI that this parser was constructed for. */
  44. private final URI uri;
  45. /** The ignore list for this parser. */
  46. private IgnoreList ignoreList;
  47. /** The ping timer interval for this parser. */
  48. private long pingTimerInterval;
  49. /** The ping timer fraction for this parser. */
  50. private int pingTimerFraction;
  51. /** The cached name of the server this parser is connected to. */
  52. private String serverName;
  53. /** The IP that this parser should bind to. */
  54. private String bindIp;
  55. /** The IPv6 IP that this parser should bind to. */
  56. private String bindIpv6;
  57. /** The URI of the proxy to use when connecting, if any. */
  58. private URI proxy;
  59. /** The callback manager to use for this parser. */
  60. private final CallbackManager callbackManager;
  61. /**
  62. * Creates a new base parser for the specified URI.
  63. *
  64. * @param uri The URI this parser will connect to.
  65. */
  66. public BaseParser(final URI uri) {
  67. this.uri = uri;
  68. callbackManager = new CallbackManager(this::handleCallbackError);
  69. }
  70. @SuppressWarnings({
  71. "ThrowableResultOfMethodCallIgnored",
  72. "CallToPrintStackTrace",
  73. "UseOfSystemOutOrSystemErr"
  74. })
  75. private void handleCallbackError(final PublicationError e) {
  76. if (Thread.holdsLock(errorHandlerLock)) {
  77. // ABORT ABORT ABORT - we're publishing an error on the same thread we just tried
  78. // to publish an error on. Something in the error reporting pipeline must be
  79. // breaking, so don't try adding any more errors.
  80. System.err.println("ERROR: Error when reporting error");
  81. e.getCause().printStackTrace();
  82. return;
  83. }
  84. synchronized (errorHandlerLock) {
  85. callbackManager.publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
  86. }
  87. }
  88. @Override
  89. public void quit(final String reason) {
  90. disconnect(reason);
  91. }
  92. @Override
  93. public URI getURI() {
  94. return uri;
  95. }
  96. @Override
  97. public URI getProxy() {
  98. if (proxy == null && ProxySelector.getDefault() != null) {
  99. final List<Proxy> proxies = ProxySelector.getDefault().select(getURI());
  100. if (!proxies.isEmpty()) {
  101. final SocketAddress sock = proxies.get(0).address();
  102. if (sock instanceof InetSocketAddress) {
  103. final InetSocketAddress isa = (InetSocketAddress)sock;
  104. try {
  105. return new URI("proxy://", "", isa.getAddress().getHostAddress(), isa.getPort(), "", "", "");
  106. } catch (final URISyntaxException use) { /* Oh well... */ }
  107. }
  108. }
  109. } else {
  110. return proxy;
  111. }
  112. return null;
  113. }
  114. @Override
  115. public void setProxy(final URI proxy) {
  116. this.proxy = proxy;
  117. }
  118. @Override
  119. public IgnoreList getIgnoreList() {
  120. return ignoreList;
  121. }
  122. @Override
  123. public void setIgnoreList(final IgnoreList ignoreList) {
  124. this.ignoreList = ignoreList;
  125. }
  126. @Override
  127. public long getPingTimerInterval() {
  128. return pingTimerInterval;
  129. }
  130. @Override
  131. public void setPingTimerInterval(final long newValue) {
  132. pingTimerInterval = newValue;
  133. }
  134. @Override
  135. public int getPingTimerFraction() {
  136. return pingTimerFraction;
  137. }
  138. @Override
  139. public void setPingTimerFraction(final int newValue) {
  140. pingTimerFraction = newValue;
  141. }
  142. @Override
  143. public CallbackManager getCallbackManager() {
  144. return callbackManager;
  145. }
  146. @Override
  147. public void joinChannel(final String channel) {
  148. joinChannels(new ChannelJoinRequest(channel));
  149. }
  150. @Override
  151. public void joinChannel(final String channel, final String key) {
  152. joinChannels(new ChannelJoinRequest(channel, key));
  153. }
  154. @Override
  155. public String getServerName() {
  156. return serverName;
  157. }
  158. /**
  159. * Sets the name of this parser's server.
  160. *
  161. * @param serverName The new name for this parser's server
  162. */
  163. protected void setServerName(final String serverName) {
  164. this.serverName = serverName;
  165. }
  166. @Override
  167. public String getBindIP() {
  168. return bindIp;
  169. }
  170. @Override
  171. public void setBindIP(final String ip) {
  172. this.bindIp = ip;
  173. }
  174. @Override
  175. public String getBindIPv6() {
  176. return bindIpv6;
  177. }
  178. @Override
  179. public void setBindIPv6(final String ip) {
  180. this.bindIpv6 = ip;
  181. }
  182. }