Subversion Repositories HomeAutomation

Rev

Rev 73 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * TcpServer.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.*;
  11.  
  12. /**
  13.  * Multithreaded multi-client TCP Server.
  14.  * @author Jimmy
  15.  */
  16. public class TcpServer extends Thread {
  17.     private ServerSocket serverSocket;
  18.     private boolean listening;
  19.     private int port;
  20.     private LinkedList serverThreads;
  21.     private ServerDataHandler dataHandler;
  22.     private boolean tcpNoDelay;
  23.  
  24.     /**
  25.      * Creates a new instance of TcpServer.
  26.      * @param port The port on which the server should listen for connections.
  27.      * @param datahandler The data handler that shall be invoked when incoming data needs to be handled.
  28.      */
  29.     public TcpServer(int port, ServerDataHandler datahandler) {
  30.         super("TcpServer");
  31.         serverSocket = null;
  32.         listening = false;
  33.         this.port = port;
  34.         dataHandler = datahandler;
  35.         serverThreads = new LinkedList();
  36.         tcpNoDelay = false;
  37.     }
  38.  
  39.     /**
  40.      * Starts listening for incoming connections.
  41.      */
  42.     public void startServer() {
  43.         listening = true;
  44.         start();
  45.     }
  46.  
  47.     /**
  48.      * Stops listening for incoming connections.
  49.      */
  50.     public void stopServer() {
  51.         listening = false;
  52.         //stop all server threads
  53.         while (serverThreads.size()>0) {
  54.             TcpServerThread t = (TcpServerThread)serverThreads.removeFirst();
  55.             t.stopThread();
  56.         }
  57.     }
  58.    
  59.     /**
  60.      * Allows application to turn off TcpNoDelay, http://www.dagblastit.com/java/sockets.html
  61.      * @param TcpNoDelay If TcpNoDelay should be turned on or off.
  62.      */
  63.     public void setTcpNoDelay(boolean TcpNoDelay) {
  64.         this.tcpNoDelay = TcpNoDelay;
  65.     }
  66.    
  67.     /**
  68.      * Sends dataToSend to all connected clients
  69.      * @param dataToSend The data to send.
  70.      */
  71.     public void sendToAll(String dataToSend) {
  72.         TcpServerThread t;
  73.         for (int i = 0; i < serverThreads.size(); i++) {
  74.             t = (TcpServerThread)serverThreads.get(i);
  75.             t.sendData(dataToSend);
  76.         }
  77.     }
  78.    
  79.     /**
  80.      * Sends dataToSend to all connected clients except those with a certain address
  81.      * @param dataToSend The data to send.
  82.      * @param address The address of the client to exclude from the send.
  83.      */
  84.     public void sendToAllExcept(String dataToSend, String address) {
  85.         TcpServerThread t;
  86.         for (int i = 0; i < serverThreads.size(); i++) {
  87.             t = (TcpServerThread)serverThreads.get(i);
  88.             if (!address.equals(t.getAddress())){
  89.                 t.sendData(dataToSend);
  90.             }
  91.         }
  92.     }
  93.    
  94.     /**
  95.      * Sends dataToSend to all connected clients except those with a certain address
  96.      * @param dataToSend The data to send.
  97.      * @param address The address of the client to send to.
  98.      */
  99.     public void sendToClient(String dataToSend, String address) {
  100.         TcpServerThread t;
  101.         for (int i = 0; i < serverThreads.size(); i++) {
  102.             t = (TcpServerThread)serverThreads.get(i);
  103.             if (address.equals(t.getAddress())) {
  104.                 t.sendData(dataToSend);
  105.             }
  106.         }
  107.     }
  108.    
  109.     /**
  110.      * This is called in a new thread when server starts listening.
  111.      */
  112.     public void run() {
  113.         try {
  114.             serverSocket = new ServerSocket(port);
  115.             serverSocket.setSoTimeout(2000);
  116.             //serverSocket.setReuseAddress(true);
  117.         } catch (IOException e) {
  118.             e.printStackTrace();
  119.             return;
  120.         }
  121.         TcpServerThread t;
  122.         Socket s;
  123.         //listen for incoming connections as long as flag is set
  124.         while (listening) {
  125.             try {
  126.                 s = serverSocket.accept();
  127.                 //start new server thread for each connection
  128.                 t = new TcpServerThread(s, dataHandler, serverThreads, tcpNoDelay);
  129.                 serverThreads.add(t);
  130.                 t.startThread();
  131.             } catch (SocketTimeoutException e) {
  132.                 //don't care...fully normal since we have a timeout set
  133.                 //detta är serversocketsens timeout, inte socketens
  134.                 //System.out.println("Timeout: " + e);
  135.                 //något skapar timeouts från start
  136.                 //det är MbInternetTransport
  137.                 continue;
  138.             } catch (Exception e) {
  139.                 e.printStackTrace();
  140.                 break;
  141.             }
  142.         }
  143.         try {
  144.             serverSocket.close();
  145.         } catch (IOException e) {
  146.             e.printStackTrace();
  147.         }
  148.     }
  149. }