Package: DbgpServer$DbgpSessionJob

DbgpServer$DbgpSessionJob

nameinstructionbranchcomplexitylinemethod
DbgpServer.DbgpSessionJob(Socket, IDbgpServerListener)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
run(IProgressMonitor)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
shouldSchedule()
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.core.dbgp;
23:
24: import java.io.IOException;
25: import java.net.ServerSocket;
26: import java.net.Socket;
27:
28: import org.eclipse.core.runtime.IProgressMonitor;
29: import org.eclipse.core.runtime.IStatus;
30: import org.eclipse.core.runtime.Status;
31: import org.eclipse.core.runtime.jobs.Job;
32: import org.overture.ide.debug.core.VdmDebugPlugin;
33: import org.overture.ide.debug.core.dbgp.internal.DbgpDebugingEngine;
34: import org.overture.ide.debug.core.dbgp.internal.DbgpSession;
35: import org.overture.ide.debug.core.dbgp.internal.DbgpWorkingThread;
36:
37: public class DbgpServer extends DbgpWorkingThread
38: {
39:         private final int port;
40:         private ServerSocket server;
41:         private final int clientTimeout;
42:
43:         public static int findAvailablePort(int fromPort, int toPort)
44:         {
45:                 if (fromPort > toPort)
46:                 {
47:                         throw new IllegalArgumentException("startPortShouldBeLessThanOrEqualToEndPort");
48:                 }
49:
50:                 int port = fromPort;
51:                 ServerSocket socket = null;
52:                 while (port <= toPort)
53:                 {
54:                         try
55:                         {
56:                                 socket = new ServerSocket(port);
57:                                 return port;
58:                         } catch (IOException e)
59:                         {
60:                                 ++port;
61:                         } finally
62:                         {
63:                                 if (socket != null)
64:                                 {
65:                                         try
66:                                         {
67:                                                 socket.close();
68:                                         } catch (IOException e)
69:                                         {
70:                                                 e.printStackTrace();
71:                                         }
72:                                 }
73:                         }
74:                 }
75:
76:                 return -1;
77:         }
78:
79:         private static final int STATE_NONE = 0;
80:         private static final int STATE_STARTED = 1;
81:         private static final int STATE_CLOSED = 2;
82:
83:         private final Object stateLock = new Object();
84:         private int state = STATE_NONE;
85:
86:         public boolean isStarted()
87:         {
88:                 synchronized (stateLock)
89:                 {
90:                         return state == STATE_STARTED;
91:                 }
92:         }
93:
94:         public boolean waitStarted()
95:         {
96:                 return waitStarted(15000);
97:         }
98:
99:         public boolean waitStarted(long timeout)
100:         {
101:                 synchronized (stateLock)
102:                 {
103:                         if (state == STATE_STARTED)
104:                         {
105:                                 return true;
106:                         } else if (state == STATE_CLOSED)
107:                         {
108:                                 return false;
109:                         }
110:                         try
111:                         {
112:                                 stateLock.wait(timeout);
113:                         } catch (InterruptedException e)
114:                         {
115:                                 // ignore
116:                         }
117:                         return state == STATE_STARTED;
118:                 }
119:         }
120:
121:         protected void workingCycle() throws Exception, IOException
122:         {
123:                 try
124:                 {
125:                         server = new ServerSocket(port);
126:                         synchronized (stateLock)
127:                         {
128:                                 state = STATE_STARTED;
129:                                 stateLock.notifyAll();
130:                         }
131:                         while (!server.isClosed())
132:                         {
133:                                 final Socket client = server.accept();
134:                                 client.setSoTimeout(clientTimeout);
135:                                 createSession(client);
136:                         }
137:                 } finally
138:                 {
139:                         if (server != null && !server.isClosed())
140:                         {
141:                                 server.close();
142:                         }
143:                         synchronized (stateLock)
144:                         {
145:                                 state = STATE_CLOSED;
146:                                 stateLock.notifyAll();
147:                         }
148:                 }
149:         }
150:
151:         private static final class DbgpSessionJob extends Job
152:         {
153:                 private final Socket client;
154:                 private final IDbgpServerListener listener;
155:
156:                 private DbgpSessionJob(Socket client, IDbgpServerListener listener)
157:                 {
158:                         super("acceptingDebuggingEngineConnection");
159:                         this.client = client;
160:                         this.listener = listener;
161:                         setSystem(true);
162:                 }
163:
164:                 public boolean shouldSchedule()
165:                 {
166:•                        return listener != null;
167:                 }
168:
169:                 protected IStatus run(IProgressMonitor monitor)
170:                 {
171:                         DbgpDebugingEngine engine = null;
172:                         try
173:                         {
174:                                 engine = new DbgpDebugingEngine(client);
175:                                 DbgpSession session = new DbgpSession(engine);
176:                                 listener.clientConnected(session);
177:                         } catch (Exception e)
178:                         {
179:                                 VdmDebugPlugin.log(e);
180:•                                if (engine != null)
181:                                 {
182:                                         engine.requestTermination();
183:                                 }
184:                         }
185:                         return Status.OK_STATUS;
186:                 }
187:         }
188:
189:         private void createSession(final Socket client)
190:         {
191:                 Job job = new DbgpSessionJob(client, listener);
192:                 job.schedule();
193:         }
194:
195:         public DbgpServer(int port, int clientTimeout)
196:         {
197:                 super("DbgpServer"); //$NON-NLS-1$
198:
199:                 this.port = port;
200:                 this.clientTimeout = clientTimeout;
201:         }
202:
203:         /**
204:          * @param port
205:          * @param serverTimeout
206:          * @param clientTimeout
207:          * @deprecated use {@link #DbgpServer(int, int)}
208:          */
209:         public DbgpServer(int port, int serverTimeout, int clientTimeout)
210:         {
211:                 this(port, clientTimeout);
212:         }
213:
214:         public void requestTermination()
215:         {
216:                 try
217:                 {
218:                         if (server != null)
219:                         {
220:                                 server.close();
221:                         }
222:                 } catch (IOException e)
223:                 {
224:                         VdmDebugPlugin.log(e);
225:                 }
226:                 super.requestTermination();
227:         }
228:
229:         private IDbgpServerListener listener;
230:
231:         public void setListener(IDbgpServerListener listener)
232:         {
233:                 this.listener = listener;
234:         }
235: }