Subversion Repositories HomeAutomation

Rev

Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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