Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IdentdServer.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.addons.identd;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.events.UserErrorEvent;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.plugins.PluginDomain;
  30. import java.io.IOException;
  31. import java.net.ServerSocket;
  32. import java.net.Socket;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import javax.inject.Inject;
  36. /**
  37. * The IdentdServer watches over the ident port when required
  38. */
  39. public final class IdentdServer implements Runnable {
  40. /** The event bus to post errors on. */
  41. private final DMDircMBassador eventBus;
  42. /** The Thread in use for this server */
  43. private volatile Thread myThread = null;
  44. /** The current socket in use for this server */
  45. private ServerSocket serverSocket;
  46. /** Arraylist of all the clients we have */
  47. private final List<IdentClient> clientList = new ArrayList<>();
  48. /** Server manager. */
  49. private final ServerManager serverManager;
  50. /** Have we failed to start this server previously? */
  51. private boolean failed;
  52. /** Global configuration to read plugin's from. */
  53. private final AggregateConfigProvider config;
  54. /** This plugin's config settings domain. */
  55. private final String domain;
  56. /**
  57. * Create the IdentdServer.
  58. *
  59. * @param eventBus The event bus to post errors on
  60. * @param serverManager Server manager to iterate over servers
  61. * @param config Global config
  62. * @param domain This plugin's setting domain
  63. */
  64. @Inject
  65. public IdentdServer(final DMDircMBassador eventBus,
  66. final ServerManager serverManager,
  67. @GlobalConfig final AggregateConfigProvider config,
  68. @PluginDomain(IdentdPlugin.class) final String domain) {
  69. this.eventBus = eventBus;
  70. this.serverManager = serverManager;
  71. this.config = config;
  72. this.domain = domain;
  73. }
  74. /**
  75. * Run this IdentdServer.
  76. */
  77. @Override
  78. public void run() {
  79. final Thread thisThread = Thread.currentThread();
  80. while (myThread == thisThread) {
  81. try {
  82. final Socket clientSocket = serverSocket.accept();
  83. final IdentClient client = new IdentClient(eventBus, this, clientSocket,
  84. serverManager, config, domain);
  85. client.start();
  86. addClient(client);
  87. } catch (IOException e) {
  88. if (myThread == thisThread) {
  89. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, e,
  90. "Accepting client failed: " + e.getMessage(), ""));
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Add an IdentClient to the clientList
  97. *
  98. * @param client Client to add
  99. */
  100. public void addClient(final IdentClient client) {
  101. synchronized (clientList) {
  102. clientList.add(client);
  103. }
  104. }
  105. /**
  106. * Remove an IdentClient from the clientList
  107. *
  108. * @param client Client to remove
  109. */
  110. public void delClient(final IdentClient client) {
  111. synchronized (clientList) {
  112. for (int i = 0; i < clientList.size(); ++i) {
  113. if (clientList.get(i) == client) {
  114. clientList.remove(i);
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Check if the server is currently running
  122. *
  123. * @return True if the server is running
  124. */
  125. public boolean isRunning() {
  126. return myThread != null;
  127. }
  128. /**
  129. * Start the ident server
  130. */
  131. public void startServer() {
  132. if (!failed && myThread == null) {
  133. try {
  134. final int identPort = config.getOptionInt(domain, "advanced.port");
  135. serverSocket = new ServerSocket(identPort);
  136. myThread = new Thread(this);
  137. myThread.start();
  138. } catch (IOException e) {
  139. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, e,
  140. "Unable to start identd server: " + e.getMessage(), ""));
  141. if ("Permission denied".equals(e.getMessage())) {
  142. failed = true;
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * Stop the ident server
  149. */
  150. public void stopServer() {
  151. if (myThread != null) {
  152. final Thread tmpThread = myThread;
  153. myThread = null;
  154. if (tmpThread != null) {
  155. tmpThread.interrupt();
  156. }
  157. try {
  158. serverSocket.close();
  159. } catch (IOException e) {
  160. }
  161. synchronized (clientList) {
  162. for (IdentClient aClientList : clientList) {
  163. aClientList.close();
  164. }
  165. clientList.clear();
  166. }
  167. }
  168. }
  169. }