Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BaseParser.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 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. }
  69. @SuppressWarnings({
  70. "ThrowableResultOfMethodCallIgnored",
  71. "CallToPrintStackTrace",
  72. "UseOfSystemOutOrSystemErr"
  73. })
  74. protected void handleCallbackError(final PublicationError e) {
  75. if (Thread.holdsLock(errorHandlerLock)) {
  76. // ABORT ABORT ABORT - we're publishing an error on the same thread we just tried
  77. // to publish an error on. Something in the error reporting pipeline must be
  78. // breaking, so don't try adding any more errors.
  79. System.err.println("ERROR: Error when reporting error");
  80. e.getCause().printStackTrace();
  81. return;
  82. }
  83. synchronized (errorHandlerLock) {
  84. getCallbackManager().publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
  85. }
  86. }
  87. @Override
  88. public void quit(final String reason) {
  89. disconnect(reason);
  90. }
  91. @Override
  92. public URI getURI() {
  93. return uri;
  94. }
  95. @Override
  96. public URI getProxy() {
  97. if (proxy == null && ProxySelector.getDefault() != null) {
  98. final List<Proxy> proxies = ProxySelector.getDefault().select(getURI());
  99. if (!proxies.isEmpty()) {
  100. final SocketAddress sock = proxies.get(0).address();
  101. if (sock instanceof InetSocketAddress) {
  102. final InetSocketAddress isa = (InetSocketAddress)sock;
  103. try {
  104. return new URI("proxy://", "", isa.getAddress().getHostAddress(), isa.getPort(), "", "", "");
  105. } catch (final URISyntaxException use) { /* Oh well... */ }
  106. }
  107. }
  108. } else {
  109. return proxy;
  110. }
  111. return null;
  112. }
  113. @Override
  114. public void setProxy(final URI proxy) {
  115. this.proxy = proxy;
  116. }
  117. @Override
  118. public IgnoreList getIgnoreList() {
  119. return ignoreList;
  120. }
  121. @Override
  122. public void setIgnoreList(final IgnoreList ignoreList) {
  123. this.ignoreList = ignoreList;
  124. }
  125. @Override
  126. public long getPingTimerInterval() {
  127. return pingTimerInterval;
  128. }
  129. @Override
  130. public void setPingTimerInterval(final long newValue) {
  131. pingTimerInterval = newValue;
  132. }
  133. @Override
  134. public int getPingTimerFraction() {
  135. return pingTimerFraction;
  136. }
  137. @Override
  138. public void setPingTimerFraction(final int newValue) {
  139. pingTimerFraction = newValue;
  140. }
  141. @Override
  142. public CallbackManager getCallbackManager() {
  143. // If setCallbackManager hasn't been called, assume we want to use the default CallbackManager
  144. if (callbackManager == null) {
  145. setCallbackManager(new CallbackManager(this::handleCallbackError));
  146. }
  147. return callbackManager;
  148. }
  149. /**
  150. * Set the {@link CallbackManager} used by this parser.
  151. * This can only be called once
  152. *
  153. * @param manager CallbackManager to use
  154. */
  155. protected void setCallbackManager(final CallbackManager manager) {
  156. if (manager == null) {
  157. throw new UnsupportedOperationException("setCallbackManager can not be called with a null parameter.");
  158. } else if (callbackManager != null) {
  159. throw new UnsupportedOperationException("setCallbackManager can only be called once.");
  160. }
  161. callbackManager = manager;
  162. }
  163. @Override
  164. public void joinChannel(final String channel) {
  165. joinChannels(new ChannelJoinRequest(channel));
  166. }
  167. @Override
  168. public void joinChannel(final String channel, final String key) {
  169. joinChannels(new ChannelJoinRequest(channel, key));
  170. }
  171. @Override
  172. public String getServerName() {
  173. return serverName;
  174. }
  175. /**
  176. * Sets the name of this parser's server.
  177. *
  178. * @param serverName The new name for this parser's server
  179. */
  180. protected void setServerName(final String serverName) {
  181. this.serverName = serverName;
  182. }
  183. @Override
  184. public String getBindIP() {
  185. return bindIp;
  186. }
  187. @Override
  188. public void setBindIP(final String ip) {
  189. this.bindIp = ip;
  190. }
  191. @Override
  192. public String getBindIPv6() {
  193. return bindIpv6;
  194. }
  195. @Override
  196. public void setBindIPv6(final String ip) {
  197. this.bindIpv6 = ip;
  198. }
  199. }