Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

IdentdServer.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 IdentdPlugin that owns this Server */
  39. private final IdentdPlugin myIdentdPlugin;
  40. /** The Thread in use for this server */
  41. private volatile Thread myThread = null;
  42. /** The current socket in use for this server */
  43. private ServerSocket serverSocket;
  44. /** Arraylist of all the clients we have */
  45. private final List<IdentClient> clientList = new ArrayList<IdentClient>();
  46. /**
  47. * Create the IdentdServer
  48. *
  49. * @param plugin The plugin that owns this Server
  50. */
  51. public IdentdServer(final IdentdPlugin plugin) {
  52. myIdentdPlugin = plugin;
  53. }
  54. /**
  55. * Run this IdentdServer
  56. */
  57. public void run() {
  58. Thread thisThread = Thread.currentThread();
  59. while (myThread == thisThread) {
  60. try {
  61. Socket clientSocket = serverSocket.accept();
  62. IdentClient client = new IdentClient(this, clientSocket);
  63. addClient(client);
  64. } catch (IOException e) {
  65. if (myThread == thisThread) {
  66. Logger.userError(ErrorLevel.HIGH ,"Accepting client failed: "+e.getMessage());
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Add an IdentClient to the clientList
  73. *
  74. * @param client Client to add
  75. */
  76. public void addClient(final IdentClient client) {
  77. synchronized (clientList) {
  78. clientList.add(client);
  79. }
  80. }
  81. /**
  82. * Remove an IdentClient from the clientList
  83. *
  84. * @param client Client to remove
  85. */
  86. public void delClient(final IdentClient client) {
  87. synchronized (clientList) {
  88. for (int i = 0; i < clientList.size() ; ++i) {
  89. if (clientList.get(i) == client) {
  90. clientList.remove(i);
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Check if the server is currently running
  98. *
  99. * @return True if the server is running
  100. */
  101. public boolean isRunning() {
  102. return (myThread != null);
  103. }
  104. /**
  105. * Start the ident server
  106. */
  107. public void startServer() {
  108. if (myThread == null) {
  109. try {
  110. final int identPort = IdentityManager.getGlobalConfig().getOptionInt(IdentdPlugin.getDomain(), "advanced.port", 113);
  111. serverSocket = new ServerSocket(identPort);
  112. myThread = new Thread(this);
  113. myThread.start();
  114. } catch (IOException e) {
  115. Logger.userError(ErrorLevel.MEDIUM ,"Unable to start identd server: "+e.getMessage());
  116. }
  117. }
  118. }
  119. /**
  120. * Stop the ident server
  121. */
  122. public void stopServer() {
  123. if (myThread != null) {
  124. Thread tmpThread = myThread;
  125. myThread = null;
  126. if (tmpThread != null) { tmpThread.interrupt(); }
  127. try { serverSocket.close(); } catch (IOException e) { }
  128. synchronized (clientList) {
  129. for (int i = 0; i < clientList.size() ; ++i) {
  130. clientList.get(i).close();
  131. }
  132. clientList.clear();
  133. }
  134. }
  135. }
  136. }