File Source: DCCChat.java

         /* 
    P/P   *  Method: com.dmdirc.addons.dcc.DCCChat__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.dcc;
    24  
    25  import java.io.BufferedReader;
    26  import java.io.IOException;
    27  import java.io.InputStreamReader;
    28  import java.io.PrintWriter;
    29  
    30  /**
    31   * This class handles a DCC Chat
    32   *
    33   * @author Shane 'Dataforce' McCormack
    34   */
    35  public class DCCChat extends DCC {
    36  	/** The handler for this DCCChat. */
    37  	private DCCChatInterface handler = null;
    38  	/** Used to send data out the socket. */
    39  	private PrintWriter out;
    40  	/** Used to read data from the socket. */
    41  	private BufferedReader in;
    42  	
    43  	
    44  	/**
    45  	 * Creates a new instance of DCCChat.
    46  	 */
    47  	public DCCChat() {
        		 /* 
    P/P 		  *  Method: void com.dmdirc.addons.dcc.DCCChat()
        		  * 
        		  *  Postconditions:
        		  *    this.address == 0
        		  *    this.listen == 0
        		  *    this.port == 0
        		  *    this.running == 0
        		  *    this.handler == null
        		  *    this.serverListeningSem == &new Semaphore(DCC#2)
        		  *    this.serverSocketSem == &new Semaphore(DCC#1)
        		  *    new Semaphore(DCC#1) num objects == 1
        		  *    new Semaphore(DCC#2) num objects == 1
        		  */
    48  		super();
    49  	}
    50  	
    51  	/**
    52  	 * Change the handler for this DCC Chat.
    53  	 *
    54  	 * @param handler A class implementing DCCChatInterface
    55  	 */
    56  	public void setHandler(final DCCChatInterface handler) {
        		 /* 
    P/P 		  *  Method: void setHandler(DCCChatInterface)
        		  * 
        		  *  Postconditions:
        		  *    this.handler == handler
        		  *    init'ed(this.handler)
        		  */
    57  		this.handler = handler;
    58  	}
    59  	
    60  	/**
    61  	 * Called when the socket is first opened, before any data is handled.
    62  	 */
    63  	@Override
    64  	protected void socketOpened() {
    65  		try {
        			 /* 
    P/P 			  *  Method: void socketOpened()
        			  * 
        			  *  Preconditions:
        			  *    init'ed(this.handler)
        			  *    (soft) this.socket != null
        			  * 
        			  *  Postconditions:
        			  *    this.in in Addr_Set{null,&new BufferedReader(socketOpened#2)}
        			  *    this.out in Addr_Set{null,&new PrintWriter(socketOpened#1)}
        			  *    new BufferedReader(socketOpened#2) num objects <= 1
        			  *    new PrintWriter(socketOpened#1) num objects <= 1
        			  * 
        			  *  Test Vectors:
        			  *    this.handler: Addr_Set{null}, Inverse{null}
        			  */
    66  			out = new PrintWriter(socket.getOutputStream(), true);
    67  			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    68  			if (handler != null) {
    69  				handler.socketOpened(this);
    70  			}
    71  		} catch (IOException ioe) {
    72              socketClosed();
    73          }
    74  	}
    75  	
    76  	/**
    77  	 * Called when the socket is closed, before the thread terminates.
    78  	 */
    79  	@Override
    80  	protected void socketClosed() {
        		 /* 
    P/P 		  *  Method: void socketClosed()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.handler)
        		  * 
        		  *  Postconditions:
        		  *    this.in == null
        		  *    this.out == null
        		  * 
        		  *  Test Vectors:
        		  *    this.handler: Addr_Set{null}, Inverse{null}
        		  */
    81  		out = null;
    82  		in = null;
    83  		if (handler != null) {
    84  			handler.socketClosed(this);
    85  		}
    86  	}
    87  	
    88  	/**
    89  	 * Handle the socket.
    90  	 *
    91  	 * @return false when socket is closed, true will cause the method to be
    92  	 *         called again.
    93  	 */
    94  	@Override
    95  	protected boolean handleSocket() {
        		 /* 
    P/P 		  *  Method: bool handleSocket()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.out)
        		  *    (soft) init'ed(this.handler)
        		  *    (soft) init'ed(this.in)
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    this.handler: Addr_Set{null}, Inverse{null}
        		  *    this.in: Inverse{null}, Addr_Set{null}
        		  *    this.out: Addr_Set{null}, Inverse{null}
        		  *    java.io.BufferedReader:readLine(...)@99: Inverse{null}, Addr_Set{null}
        		  */
    96  		if (out == null || in == null) { return false; }
    97  		final String inLine;
    98  		try {
    99  			inLine = in.readLine();
   100  			if (inLine == null) {
   101  				return false;
   102  			} else {
   103  				if (handler != null) {
   104  					handler.handleChatMessage(this, inLine);
   105  				}
   106  				return true;
   107  			}
   108  		} catch (IOException e) {
   109  			return false;
   110  		}
   111  	}
   112  	
   113  	/**
   114  	 * Check if this socket can be written to.
   115  	 */
   116  	@Override
   117  	public boolean isWriteable() {
        		 /* 
    P/P 		  *  Method: bool isWriteable()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.out)
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   118  		return out != null;
   119  	}
   120  	
   121  	/**
   122  	 * Send a line out the socket.
   123  	 *
   124  	 * @param line The line to be sent
   125  	 */
   126  	public void sendLine(final String line) {
        		 /* 
    P/P 		  *  Method: void sendLine(String)
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.out)
        		  * 
        		  *  Test Vectors:
        		  *    this.out: Addr_Set{null}, Inverse{null}
        		  */
   127  		if (out != null) {
   128  			out.printf("%s\r\n", line);
   129  		}
   130  	}
   131  }








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