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.

IdentdServer.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config.IdentityManager;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import java.net.Socket;
  27. import java.net.ServerSocket;
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * The IdentdServer watches over the ident port when required
  33. *
  34. * @author Shane "Dataforce" Mc Cormack
  35. * @version $Id: IdentdServer.java 969 2007-04-30 18:38:20Z ShaneMcC $
  36. */
  37. public final class IdentdServer implements Runnable {
  38. /** The Thread in use for this server */
  39. private volatile Thread myThread = null;
  40. /** The current socket in use for this server */
  41. private ServerSocket serverSocket;
  42. /** Arraylist of all the clients we have */
  43. private final List<IdentClient> clientList = new ArrayList<IdentClient>();
  44. /**
  45. * Create the IdentdServer.
  46. */
  47. public IdentdServer() {
  48. super();
  49. }
  50. /**
  51. * Run this IdentdServer.
  52. */
  53. public void run() {
  54. final Thread thisThread = Thread.currentThread();
  55. while (myThread == thisThread) {
  56. try {
  57. final Socket clientSocket = serverSocket.accept();
  58. final IdentClient client = new IdentClient(this, clientSocket);
  59. addClient(client);
  60. } catch (IOException e) {
  61. if (myThread == thisThread) {
  62. Logger.userError(ErrorLevel.HIGH ,"Accepting client failed: "+e.getMessage());
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Add an IdentClient to the clientList
  69. *
  70. * @param client Client to add
  71. */
  72. public void addClient(final IdentClient client) {
  73. synchronized (clientList) {
  74. clientList.add(client);
  75. }
  76. }
  77. /**
  78. * Remove an IdentClient from the clientList
  79. *
  80. * @param client Client to remove
  81. */
  82. public void delClient(final IdentClient client) {
  83. synchronized (clientList) {
  84. for (int i = 0; i < clientList.size() ; ++i) {
  85. if (clientList.get(i) == client) {
  86. clientList.remove(i);
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * Check if the server is currently running
  94. *
  95. * @return True if the server is running
  96. */
  97. public boolean isRunning() {
  98. return (myThread != null);
  99. }
  100. /**
  101. * Start the ident server
  102. */
  103. public void startServer() {
  104. if (myThread == null) {
  105. try {
  106. final int identPort = IdentityManager.getGlobalConfig().getOptionInt(IdentdPlugin.getDomain(), "advanced.port", 113);
  107. serverSocket = new ServerSocket(identPort);
  108. myThread = new Thread(this);
  109. myThread.start();
  110. } catch (IOException e) {
  111. Logger.userError(ErrorLevel.MEDIUM ,"Unable to start identd server: "+e.getMessage());
  112. }
  113. }
  114. }
  115. /**
  116. * Stop the ident server
  117. */
  118. public void stopServer() {
  119. if (myThread != null) {
  120. final Thread tmpThread = myThread;
  121. myThread = null;
  122. if (tmpThread != null) { tmpThread.interrupt(); }
  123. try { serverSocket.close(); } catch (IOException e) { }
  124. synchronized (clientList) {
  125. for (int i = 0; i < clientList.size() ; ++i) {
  126. clientList.get(i).close();
  127. }
  128. clientList.clear();
  129. }
  130. }
  131. }
  132. }