File Source: IdentdServer.java

         /* 
    P/P   *  Method: com.dmdirc.addons.identd.IdentdServer__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 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  
    23  package com.dmdirc.addons.identd;
    24  
    25  import com.dmdirc.config.IdentityManager;
    26  import com.dmdirc.logger.Logger;
    27  import com.dmdirc.logger.ErrorLevel;
    28  import com.dmdirc.plugins.PluginInfo;
    29  import com.dmdirc.plugins.PluginManager;
    30  
    31  import java.net.Socket;
    32  import java.net.ServerSocket;
    33  import java.io.IOException;
    34  import java.util.ArrayList;
    35  import java.util.List;
    36  
    37  /**
    38   * The IdentdServer watches over the ident port when required
    39   *
    40   * @author Shane "Dataforce" Mc Cormack
    41   */
    42  public final class IdentdServer implements Runnable {
    43  
    44  	/** The Thread in use for this server */
    45  	private volatile Thread myThread = null;
    46  	/** The current socket in use for this server */
    47  	private ServerSocket serverSocket;
    48  	/** Arraylist of all the clients we have */
    49  	private final List<IdentClient> clientList = new ArrayList<IdentClient>();
    50  	/** The plugin that owns us. */
    51  	private final IdentdPlugin myPlugin;
    52  	
    53  	/**
    54  	 * Create the IdentdServer.
    55  	 */
    56  	public IdentdServer(final IdentdPlugin plugin) {
        		 /* 
    P/P 		  *  Method: void com.dmdirc.addons.identd.IdentdServer(IdentdPlugin)
        		  * 
        		  *  Postconditions:
        		  *    this.clientList == &amp;new ArrayList(IdentdServer#1)
        		  *    this.myPlugin == plugin
        		  *    init'ed(this.myPlugin)
        		  *    this.myThread == null
        		  *    new ArrayList(IdentdServer#1) num objects == 1
        		  */
    57  		super();
    58  		myPlugin = plugin;
    59  	}
    60  	
    61  	/**
    62  	 * Run this IdentdServer.
    63  	 */
    64      @Override
    65  	public void run() {
        		 /* 
    P/P 		  *  Method: void run()
        		  * 
        		  *  Preconditions:
        		  *    (soft) this.clientList != null
        		  *    (soft) init'ed(this.myThread)
        		  *    (soft) this.serverSocket != null
        		  * 
        		  *  Presumptions:
        		  *    init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
        		  *    this.serverSocket@69 != null
        		  */
    66  		final Thread thisThread = Thread.currentThread();
    67  		while (myThread == thisThread) {
    68  			try {
    69  				final Socket clientSocket = serverSocket.accept();
    70  				final IdentClient client = new IdentClient(this, clientSocket, myPlugin);
    71  				addClient(client);
    72  			} catch (IOException e) {
    73  				if (myThread == thisThread) {
    74  					Logger.userError(ErrorLevel.HIGH ,"Accepting client failed: "+e.getMessage());
    75  				}
    76  			}
    77  		}
    78  	}
    79  	
    80  	/**
    81  	 * Add an IdentClient to the clientList
    82  	 *
    83  	 * @param client Client to add
    84  	 */
    85  	public void addClient(final IdentClient client) {
        		 /* 
    P/P 		  *  Method: void addClient(IdentClient)
        		  * 
        		  *  Preconditions:
        		  *    this.clientList != null
        		  */
    86  		synchronized (clientList) {
    87  			clientList.add(client);
    88  		}
    89  	}
    90  	
    91  	/**
    92  	 * Remove an IdentClient from the clientList
    93  	 *
    94  	 * @param client Client to remove
    95  	 */
    96  	public void delClient(final IdentClient client) {
        		 /* 
    P/P 		  *  Method: void delClient(IdentClient)
        		  * 
        		  *  Preconditions:
        		  *    this.clientList != null
        		  */
    97  		synchronized (clientList) {
    98  			for (int i = 0; i < clientList.size() ; ++i) {
    99  				if (clientList.get(i) == client) {
   100  					clientList.remove(i);
   101  					break;
   102  				}
   103  			}
   104  		}
   105  	}
   106  	
   107  	/**
   108  	 * Check if the server is currently running
   109  	 *
   110  	 * @return True if the server is running
   111  	 */
   112  	public boolean isRunning() {
        		 /* 
    P/P 		  *  Method: bool isRunning()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.myThread)
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   113  		return (myThread != null);
   114  	}
   115  	
   116  	/**
   117  	 * Start the ident server
   118  	 */
   119  	public void startServer() {
        		 /* 
    P/P 		  *  Method: void startServer()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.myThread)
        		  *    (soft) this.myPlugin != null
        		  * 
        		  *  Presumptions:
        		  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@122 != null
        		  *    init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
        		  *    com.dmdirc.plugins.PluginManager:getPluginManager(...)@129 != null
        		  *    com.dmdirc.plugins.PluginManager:getPluginManager(...)@131 != null
        		  *    com.dmdirc.plugins.PluginManager:getPluginManager(...)@132 != null
        		  *    ...
        		  * 
        		  *  Postconditions:
        		  *    this.myThread == One-of{old this.myThread, &amp;new Thread(startServer#2)}
        		  *    init'ed(this.myThread)
        		  *    this.serverSocket == One-of{old this.serverSocket, &amp;new ServerSocket(startServer#1)}
        		  *    new ServerSocket(startServer#1) num objects <= 1
        		  *    new Thread(startServer#2) num objects <= 1
        		  * 
        		  *  Test Vectors:
        		  *    this.myThread: Inverse{null}, Addr_Set{null}
        		  *    com.dmdirc.plugins.PluginManager:getPluginInfoByName(...)@129: Addr_Set{null}, Inverse{null}
        		  *    java.lang.String:equals(...)@128: {0}, {1}
        		  */
   120  		if (myThread == null) {
   121  			try {
   122  				final int identPort = IdentityManager.getGlobalConfig().getOptionInt(myPlugin.getDomain(), "advanced.port");
   123  				serverSocket = new ServerSocket(identPort);
   124  				myThread = new Thread(this);
   125  				myThread.start();
   126  			} catch (IOException e) {
   127  				Logger.userError(ErrorLevel.MEDIUM ,"Unable to start identd server: "+e.getMessage());
   128  				if (e.getMessage().equals("Permission denied")) {
   129  					final PluginInfo plugin = PluginManager.getPluginManager().getPluginInfoByName("identd");
   130  					if (plugin != null) {
   131  						if (PluginManager.getPluginManager().delPlugin(plugin.getRelativeFilename())) {
   132  							PluginManager.getPluginManager().updateAutoLoad(plugin);
   133  						}
   134  					}
   135  				}
   136  			}
   137  		}
   138  	}
   139  	
   140  	/**
   141  	 * Stop the ident server
   142  	 */
   143  	public void stopServer() {
        		 /* 
    P/P 		  *  Method: void stopServer()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.myThread)
        		  *    (soft) this.clientList != null
        		  *    (soft) this.serverSocket != null
        		  * 
        		  *  Presumptions:
        		  *    java.util.List:get(...).mySocket@152 != null
        		  *    java.util.List:get(...)@152 != null
        		  * 
        		  *  Postconditions:
        		  *    this.myThread == null
        		  * 
        		  *  Test Vectors:
        		  *    this.myThread: Addr_Set{null}, Inverse{null}
        		  */
   144  		if (myThread != null) {
   145  			final Thread tmpThread = myThread;
   146  			myThread = null;
   147  			if (tmpThread != null) { tmpThread.interrupt(); }
   148  			try { serverSocket.close(); } catch (IOException e) { }
   149  			
   150  			synchronized (clientList) {
   151  				for (int i = 0; i < clientList.size() ; ++i) {
   152  					clientList.get(i).close();
   153  				}
   154  				clientList.clear();
   155  			}
   156  		}
   157  	}
   158  	
   159  }
   160  








SofCheck Inspector Build Version : 2.17854
IdentdServer.java 2009-Jun-25 01:54:24
IdentdServer.class 2009-Sep-02 17:04:15