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.

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