Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * TcpServerThread.java
  3.  *
  4.  * Created on den 25 augusti 2003, 22:50
  5.  */
  6. package Macbeth.Net;
  7.  
  8. import java.net.*;
  9. import java.io.*;
  10. import java.util.List;
  11.  
  12. /**
  13.  * Server thread for the TcpServer.
  14.  * Such a thread will be created for each connected
  15.  * client.
  16.  * @author Jimmy
  17.  */
  18. public class TcpServerThread extends Thread {
  19.     private Socket socket;
  20.     private ServerDataHandler dataHandler;
  21.     private BufferedReader stringInput = null;
  22.     private PrintWriter stringOutput = null;
  23.     boolean run;
  24.  
  25.     //reference to the list of server threads (this list is maintained in the TcpServer-class
  26.     private List serverThreads;
  27.  
  28.  
  29.     /**
  30.      * Creates a new instance of TcpServerThread.
  31.      * @param socket The socket associated with the client we're serving.
  32.      * @param datahandler The data handler that should be invoked when the client is sending us data.
  33.      * @param serverThreads The list of server threads maintained in TcpServer. The server thread needs
  34.      * to be able to delete itself from this list when it has finished running.
  35.      * @param TcpNoDelay Sets if socket use TcpNoDelay
  36.      **/
  37.     public TcpServerThread(Socket socket, ServerDataHandler datahandler, List serverThreads, boolean TcpNoDelay) {
  38.         super("TcpServerThread");
  39.         this.socket = socket;
  40.         this.dataHandler = datahandler;
  41.         this.serverThreads = serverThreads;
  42.         try {
  43.             this.socket.setTcpNoDelay(TcpNoDelay);
  44.         }
  45.         catch (SocketException e) {
  46.             System.out.println("Socket error: " + e);
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * Starts up the server thread.
  52.      */
  53.     public void startThread() {
  54.         try {
  55.             //get input stream from socket
  56.             stringInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  57.             //create output stream
  58.             stringOutput = new PrintWriter(socket.getOutputStream());
  59.             //timeout creates a pause in readline so that one can test if client is still connected
  60.             socket.setSoTimeout(35000);
  61.             //does this do anything
  62.             //socket.setKeepAlive(true);
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.             return;
  66.         }
  67.         //start new server thread
  68.         run = true;
  69.         super.start();
  70.     }
  71.  
  72.     /**
  73.      * Stops the server thread.
  74.      */
  75.     public void stopThread() {
  76.         if (run) {
  77.             run = false;
  78.             try {
  79.                 if (stringInput!=null) stringInput.close();
  80.                 socket.close();
  81.                 serverThreads.remove(this);
  82.             } catch (IOException e) {
  83.                 e.printStackTrace();
  84.             }
  85.         }
  86.     }
  87.     /**
  88.      * This will send data to this client.
  89.      * @param dataToSend String line to send to this client.
  90.      */
  91.     public void sendData(String dataToSend){
  92.         //borde inte denna generera exception då man skriver till en socket som inte är ansluten?
  93.         stringOutput.println(dataToSend);
  94.         stringOutput.flush();
  95.     }
  96.    
  97.     /**
  98.      * Allows application to ask for this clients address.
  99.      * @return The address of this client.
  100.      */
  101.     public String getAddress() {
  102.         return socket.getRemoteSocketAddress().toString();
  103.     }
  104.    
  105.     /**
  106.      * This will be called from the thread.
  107.      */
  108.     public void run() {
  109.         //tell the datahandler from whom we're receiving data
  110.         dataHandler.setRemoteAddress(socket.getRemoteSocketAddress().toString());
  111.        
  112.         try {
  113.             //do we have a string input stream?
  114.             if (stringInput!=null) {
  115.                 //read lines from this stream until connection is closed
  116.                 String line;
  117.                 while (run) {
  118.                     /*
  119.                     //if input buffer is not empty
  120.                     if (stringInput.ready()) {
  121.                         //read next line
  122.                         line=stringInput.readLine();
  123.                         //and let the data handler handle this new received line
  124.                         dataHandler.handleServerData(line);
  125.                     //else, if input buffer is empty
  126.                     } else {
  127.                         //get out of the loop
  128.                         break;
  129.                     }*/
  130.  
  131.                     //read next line
  132.                     try {
  133.                         line = stringInput.readLine();
  134.                         //gör om denna så den läser tecken för tecken tills ett förinställt termineringstecken dyker upp
  135.                    
  136.                     } catch (SocketTimeoutException e) {
  137.                         //don't care...fully normal since we have a timeout set
  138.                        
  139.                         //detta är socketens timeout
  140.                         //här borde man alltså kolla om det verkligen finns någon på andra sidan
  141.                         //System.out.println("Timeout: " + e);
  142.                         //e.printStackTrace();
  143.                         //continue;
  144.                         break;
  145.                     } catch (SocketException e) {
  146.                         //koppla ner socketen är väl det enda rätta?
  147.                         //System.out.println("Disconnect: " + e);
  148.                         break;
  149.                     }
  150.                     //line = null;
  151.                     if (line!=null) {
  152.                         dataHandler.handleServerData(line, socket.getRemoteSocketAddress().toString());
  153.                    }
  154.                     //until end of stream has been reached
  155.                     else {
  156.                         dataHandler.clientDropped(socket.getRemoteSocketAddress().toString());
  157.                         break;
  158.                     }
  159.                 }
  160.             }
  161.         } catch (IOException e) {
  162.             e.printStackTrace();
  163.         } finally {
  164.             //stop the thread and close the socket
  165.             stopThread();
  166.         }
  167.     }
  168. }