Package: ConnectionListener

ConnectionListener

nameinstructionbranchcomplexitylinemethod
ConnectionListener(int, IClientMonitor)
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
die()
M: 34 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
findConnection(String)
M: 34 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getConnections()
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getPort()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPrincipal()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
run()
M: 76 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2009 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.combinatorialtesting.vdmj.server;
25:
26: import java.io.IOException;
27: import java.net.ServerSocket;
28: import java.net.Socket;
29: import java.net.SocketException;
30: import java.net.SocketTimeoutException;
31:
32: import org.overture.ide.debug.core.VdmDebugPlugin;
33:
34: public class ConnectionListener extends Thread
35: {
36:         private ServerSocket socket;
37:         private ThreadGroup group;
38:         private boolean listening;
39:         private ConnectionThread principal = null;
40:         private IClientMonitor monitor;
41:
42:         public ConnectionListener(int port, IClientMonitor monitor)
43:                         throws IOException
44:         {
45:                 socket = new ServerSocket(port);
46:                 socket.setSoTimeout(VdmDebugPlugin.getDefault().getConnectionTimeout());
47:
48:                 this.monitor = monitor;
49:
50:                 group = new ThreadGroup("Connections");
51:                 setDaemon(true);
52:                 setName("Connection Listener");
53:         }
54:
55:         public int getPort()
56:         {
57:                 return socket.getLocalPort();
58:         }
59:
60:         public synchronized ConnectionThread getPrincipal()
61:         {
62:                 return principal;
63:         }
64:
65:         @Override
66:         public void run()
67:         {
68:                 listening = true;
69:
70:                 try
71:                 {
72:•                        while (listening)
73:                         {
74:                                 try
75:                                 {
76:                                         Socket conn = socket.accept();
77:
78:•                                        if (group.activeCount() >= 1)
79:                                         {
80:                                                 System.out.println("Too many DBGp connections");
81:                                                 conn.close();
82:                                                 continue;
83:                                         }
84:
85:•                                        ConnectionThread worker = new ConnectionThread(group, conn, principal == null, monitor);
86:
87:•                                        if (principal == null)
88:                                         {
89:                                                 principal = worker; // The main connection
90:                                         }
91:
92:                                         worker.start();
93:                                 } catch (SocketTimeoutException e)
94:                                 {
95:                                         // System.out.println("Listener timed out: " + e.getMessage());
96:                                 }
97:                         }
98:                 } catch (SocketException e)
99:                 {
100:                         // Killed by die() or VDMJ crash
101:                 } catch (SocketTimeoutException e)
102:                 {
103:                         System.out.println("Listener timed out: " + e.getMessage());
104:                 } catch (IOException e)
105:                 {
106:                         System.out.println("Listener exception: " + e.getMessage());
107:                 }
108:
109:                 die();
110:         }
111:
112:         public synchronized void die()
113:         {
114:                 try
115:                 {
116:                         listening = false;
117:                         socket.close();
118:
119:•                        for (ConnectionThread ct : getConnections())
120:                         {
121:                                 ct.die();
122:                         }
123:                 } catch (IOException e)
124:                 {
125:                         System.out.println("Cannot stop connection listener");
126:                 }
127:
128:                 monitor.terminating();
129:         }
130:
131:         public ConnectionThread findConnection(String id)
132:         {
133:•                if (id == null)
134:                 {
135:                         return principal;
136:                 }
137:
138:•                for (ConnectionThread ct : getConnections())
139:                 {
140:•                        if (ct.getIdeId() != null && ct.getIdeId().equals(id))
141:                         {
142:                                 return ct;
143:                         }
144:                 }
145:
146:                 return null;
147:         }
148:
149:         public ConnectionThread[] getConnections()
150:         {
151:                 ConnectionThread[] all = null;
152:
153:                 do
154:                 {
155:                         all = new ConnectionThread[group.activeCount()];
156:•                } while (group.enumerate(all) != all.length);
157:
158:                 return all;
159:         }
160: }