Package: ConnectionThread

ConnectionThread

nameinstructionbranchcomplexitylinemethod
ConnectionThread(ThreadGroup, Socket, boolean, IClientMonitor)
M: 35 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: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getIdeId()
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%
process(byte[])
M: 53 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
processInit(XMLTagNode)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
processResponse(XMLTagNode)
M: 89 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
receive()
M: 162 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
run()
M: 54 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
setQuiet()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setTrace()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2009 Fujitsu Services Ltd. Author: Nick Battle This file is part of VDMJ. VDMJ is free software: you
3: * can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
4: * Software Foundation, either version 3 of the License, or (at your option) any later version. VDMJ is distributed in
5: * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a
7: * copy of the GNU General Public License along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
8: ******************************************************************************/
9:
10: package org.overture.combinatorialtesting.vdmj.server;
11:
12: import java.io.BufferedInputStream;
13: import java.io.BufferedOutputStream;
14: import java.io.IOException;
15: import java.net.Socket;
16: import java.net.SocketException;
17:
18: import org.overture.combinatorialtesting.vdmj.server.common.Utils;
19: import org.overture.combinatorialtesting.vdmj.server.xml.XMLNode;
20: import org.overture.combinatorialtesting.vdmj.server.xml.XMLParser;
21: import org.overture.combinatorialtesting.vdmj.server.xml.XMLTagNode;
22:
23: public class ConnectionThread extends Thread
24: {
25:         private final boolean principal;
26:         private final Socket socket;
27:         private final BufferedInputStream input;
28:         private final BufferedOutputStream output;
29:
30:         private String id = "";
31:         // private long xid = 0;
32:
33:         private boolean connected;
34:         private static boolean trace = false;
35:         private static boolean quiet = false;
36:         // private static ConnectionThread focus = null;
37:
38:         private final IClientMonitor monitor;
39:
40:         public ConnectionThread(ThreadGroup group, Socket conn, boolean principal,
41:                         IClientMonitor monitor) throws IOException
42:         {
43:                 super(group, null, "DBGp Connection");
44:
45:                 this.socket = conn;
46:                 this.input = new BufferedInputStream(conn.getInputStream());
47:                 this.output = new BufferedOutputStream(conn.getOutputStream());
48:                 this.principal = principal;
49:                 this.monitor = monitor;
50:
51:                 setDaemon(true);
52:         }
53:
54:         public String getIdeId()
55:         {
56:                 return id;
57:         }
58:
59:         public static synchronized boolean setTrace()
60:         {
61:•                trace = !trace;
62:                 return trace;
63:         }
64:
65:         public static synchronized boolean setQuiet()
66:         {
67:•                quiet = !quiet;
68:                 return quiet;
69:         }
70:
71:
72:         @Override
73:         public void run()
74:         {
75:                 connected = true;
76:                 try
77:                 {
78:                         if (!principal)
79:                         {
80:                                 // runme(); // Send run command to start new thread
81:                         }
82:
83:•                        while (connected)
84:                         {
85:•                                if(!receive()) // Blocking
86:                                 {
87:                                         return;
88:                                 }
89:                         }
90:                 } catch (SocketException e)
91:                 {e.printStackTrace();
92:                         monitor.traceError("Connection error: " + e.getMessage());
93:                         // Caused by die(), and VDMJ death
94:                 } catch (IOException e)
95:                 {
96:                         System.out.println("Connection exception: " + e.getMessage());
97:
98:                 } finally
99:                 {
100:                         die();
101:                 }
102:
103:•                if (!principal && !quiet)
104:                 {
105:                         System.out.println("Thread stopped: " + this);
106:                 }
107:         }
108:
109:         public synchronized void die()
110:         {
111:                 try
112:                 {
113:                         connected = false;
114:                         socket.close();
115:                 } catch (IOException e)
116:                 {
117:                         // ?
118:                 }
119:         }
120:
121:
122:         private boolean receive() throws IOException
123:         {
124:                 // <ascii length> \0 <XML data> \0
125:
126:                 int c = input.read();
127:                 int length = 0;
128:
129:•                while (c >= '0' && c <= '9')
130:                 {
131:                         length = length * 10 + c - '0';
132:                         c = input.read();
133:                 }
134:
135:•                if (c == -1)
136:                 {
137:                         connected = false; // End of thread
138:                         return false;
139:                 }
140:
141:•                if (c != 0)
142:                 {
143:                         throw new IOException("Malformed DBGp count on " + this);
144:                 }
145:
146:                 byte[] data = new byte[length];
147:                 int offset = 0;
148:                 int remaining = length;
149:                 int retries = 10;
150:                 int done = input.read(data, offset, remaining);
151:
152:•                while (done < remaining && --retries > 0)
153:                 {
154:                         Utils.milliPause(100);
155:                         remaining -= done;
156:                         offset += done;
157:                         done = input.read(data, offset, remaining);
158:                 }
159:
160:•                if (retries == 0)
161:                 {
162:                         throw new IOException("Timeout DBGp reply on thread " + this.id
163:                                         + ", got [" + new String(data) + "]");
164:                 }
165:
166:•                if (done != remaining)
167:                 {
168:                         throw new IOException("Short DBGp reply on thread " + this.id
169:                                         + ", got [" + new String(data) + "]");
170:                 }
171:
172:•                if (input.read() != 0)
173:                 {
174:                         throw new IOException("Malformed DBGp terminator on thread "
175:                                         + this.id + ", got [" + new String(data) + "]");
176:                 }
177:
178:         return        process(data);
179:         }
180:
181:         private boolean process(byte[] data) throws IOException
182:         {
183:                 // System.out.println(new String(data));
184:                 XMLParser parser = new XMLParser(data);
185:                 XMLNode node = parser.readNode();
186:
187:•                if (trace)
188:                 {
189:                         System.err.println("[" + id + "] " + node); // diags!
190:                 }
191:
192:                 try
193:                 {
194:                         XMLTagNode tagnode = (XMLTagNode) node;
195:
196:•                        if (tagnode.tag.equals("init"))
197:                         {
198:                                 processInit(tagnode);
199:                                 return true;
200:                         } else
201:                         {
202:                                 return processResponse(tagnode);
203:                         }
204:                 } catch (Exception e)
205:                 {
206:                         throw new IOException("Unexpected XML response: " + node);
207:                 }
208:         }
209:
210:         private boolean processResponse(XMLTagNode tagnode)
211:         {
212:                 String traceName = tagnode.getAttr("tracename");
213:                 String progress = tagnode.getAttr("progress");
214:                 String status = tagnode.getAttr("status");
215:
216:•                if (monitor == null)
217:                 {
218:                         System.out.println("PROGRESS: " + traceName + " " + progress);
219:•                } else if (status.equals("combinatorialtestingtart"))
220:                 {
221:                         monitor.traceStart(traceName);
222:•                } else if (status.equals("progress"))
223:                 {
224:                         Integer p = Integer.parseInt(progress);
225:                         monitor.progress(traceName, p);
226:•                } else if (status.equals("error"))
227:                 {
228:                         String errorMessage = tagnode.getAttr("message");
229:                         monitor.traceError(errorMessage);
230:•                } else if (status.equals("completed"))
231:                 {
232:                         try
233:                         {
234:                                 output.write("exit".getBytes());
235:                                 output.flush();
236:                         } catch (IOException e)
237:                         {
238:                         }
239:                         monitor.completed();
240:                         monitor.terminating();
241:                         return false;
242:
243:                 }
244:                 return true;
245:         }
246:
247:         private void processInit(XMLTagNode tagnode)
248:         {
249:                 String module = tagnode.getAttr("module");
250:•                if (monitor == null)
251:                 {
252:                         System.out.println("INIT: " + module);
253:                 } else
254:                 {
255:                         monitor.initialize(module);
256:                 }
257:         }
258:
259: }