KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > DbgServerHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import org.netbeans.api.debugger.Breakpoint;
34 import org.netbeans.api.debugger.DebuggerManager;
35 import org.netbeans.modules.scripting.php.dbginterface.api.DbgSourceMap;
36 import org.netbeans.modules.scripting.php.dbginterface.breakpoints.PhpBreakpoint;
37 import org.netbeans.modules.scripting.php.dbginterface.models.DebugError;
38 import org.netbeans.modules.scripting.php.dbginterface.models.DebugFrame;
39 import org.netbeans.modules.scripting.php.dbginterface.models.Variable;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.text.Line;
43 import org.openide.util.Utilities;
44
45 /**
46  *
47  * @author marcow
48  */

49 public class DbgServerHandler extends DbgDebuggerImpl.Context implements Runnable JavaDoc {
50     private Socket JavaDoc socket;
51     private InputStream JavaDoc in;
52     private int flags;
53     private boolean stop;
54     private List JavaDoc<DbgPacket> packetsToSend = new ArrayList JavaDoc<DbgPacket>();
55     private Thread JavaDoc handlerThread;
56     private String JavaDoc sessionId;
57     private DbgSourceMap sourceMap;
58     private DebuggerAnnotation currentLineAnnotation;
59     private Line currentLine;
60     private Set JavaDoc<DebuggerAnnotation> errorAnnotations = new HashSet JavaDoc<DebuggerAnnotation>();
61     private boolean suspended;
62     private List JavaDoc<DebugFrame> callStack;
63     // modNo -> FileName
64
private Map JavaDoc<Integer JavaDoc, String JavaDoc> sourceFileMap = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc>();
65     
66     public DbgServerHandler(DbgDebuggerImpl impl, Socket JavaDoc socket) {
67         super(impl);
68         sourceMap = impl.getSourceMap();
69         this.socket = socket;
70         stop = false;
71         
72         try {
73             in = new BufferedInputStream JavaDoc(socket.getInputStream());
74         }
75         catch (IOException JavaDoc ioe) {
76             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
77         }
78     }
79     
80     public void sendPacket(DbgPacket packet) {
81         if (handlerThread == null) {
82             return;
83         }
84         
85         synchronized (packetsToSend) {
86             packetsToSend.add(packet);
87         }
88         
89         handlerThread.interrupt();
90     }
91     
92     public void stop() {
93         Thread.dumpStack();
94         synchronized (this) {
95             stop = true;
96         
97             if (handlerThread != null) {
98                 handlerThread.interrupt();
99             }
100         }
101     }
102     
103     public void run() {
104         System.err.println("mw DbgServerHandler.run() started!");
105     
106         synchronized (this) {
107             handlerThread = Thread.currentThread();
108         }
109         
110         while (!stop && !socket.isClosed()) {
111             try {
112                 List JavaDoc<DbgPacket> toSend = null;
113                 
114                 synchronized (packetsToSend) {
115                     if (packetsToSend != null && packetsToSend.size() > 0) {
116                         toSend = new ArrayList JavaDoc<DbgPacket>(packetsToSend);
117                         packetsToSend.clear();
118                     }
119                 }
120                 
121                 if (toSend != null) {
122                     for (DbgPacket p : toSend) {
123                         if (p.isWaitAck()) {
124                             // Are there packets from the debuggee to handle first?
125
handleIncomingPackets(false);
126                         }
127                         
128                         p.send(socket.getOutputStream());
129                         
130                         if (p.isWaitAck()) {
131                             // Wait for the acknowledgement packet
132
handleIncomingPackets(true);
133                         }
134                     }
135                 }
136
137                 handleIncomingPackets(false);
138             }
139             catch (IOException JavaDoc ioe) {
140                 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
141             }
142             
143             try {
144                 // Wake up every 100 milliseconds and see if the debuggee has something to say.
145
// The IDE side can interrupt the sleep to send new packets to the
146
// debuggee.
147
Thread.sleep(100);
148             }
149             catch (InterruptedException JavaDoc ie) {
150                 // OK, run the look again.
151
}
152         }
153         
154         if (!socket.isClosed()) {
155             try {
156                 socket.close();
157             } catch (IOException JavaDoc ioe) {
158                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
159             }
160         }
161         
162         synchronized (this) {
163             handlerThread = null;
164         }
165         
166         callStack = null;
167         getServer().getCallStackModel().refresh(true);
168         getServer().getVariablesModel().clearModel();
169         hideCurrentLine();
170         
171         for (DebuggerAnnotation a : errorAnnotations) {
172             a.detach();
173         }
174     }
175     
176     public boolean isSuspended() {
177         return suspended;
178     }
179
180     public void setSuspended(boolean s) {
181         suspended = s;
182         
183         getServer().setEnableContActions(s);
184     }
185     
186     public void setCurrentLine(Line line) {
187         currentLine =line;
188     }
189     
190     public void finish() {
191         getServer().closeScriptContext(this);
192     }
193     
194     public DbgSourceMap getSourceMap() {
195         return sourceMap;
196     }
197     
198     public void addError(DebugError e) {
199         String JavaDoc aType = null;
200         if (e.isError()) {
201             aType = DebuggerAnnotation.ERROR_ANNOTATION_TYPE;
202         }
203         else if (e.isNotice()) {
204             aType = DebuggerAnnotation.NOTICE_ANNOTATION_TYPE;
205         }
206         else if (e.isWarning()) {
207             aType = DebuggerAnnotation.WARNING_ANNOTATION_TYPE;
208         }
209         
210         errorAnnotations.add(new DebuggerAnnotation(aType, e.getLine(), e.getString()));
211     }
212     
213     public void hideCurrentLine() {
214         if (currentLineAnnotation != null) {
215             currentLineAnnotation.detach();
216             currentLineAnnotation = null;
217         }
218     }
219     
220     public void handleCurrentLine() {
221         if (currentLineAnnotation != null) {
222             currentLineAnnotation.detach();
223         }
224
225         if (currentLine != null) {
226             final Line line = currentLine;
227
228             currentLineAnnotation = new DebuggerAnnotation(DebuggerAnnotation.CURRENT_LINE_ANNOTATION_TYPE,
229                     line);
230
231             SwingUtilities.invokeLater(new Runnable JavaDoc() {
232                 public void run() {
233                     line.show(Line.SHOW_GOTO);
234                 }
235             });
236         }
237     }
238     
239     public void setCallStack(List JavaDoc<DebugFrame> list) {
240         callStack = list;
241         getServer().getCallStackModel().setNeedsRefresh();
242         getServer().getVariablesModel().setStackFrame(callStack.get(0));
243         getServer().getWatchesModel().setStackFrame(callStack.get(0));
244     }
245     
246     public List JavaDoc<DebugFrame> getCallStack() {
247         return callStack;
248     }
249
250     public void addSourceFileInfo(int modNo, String JavaDoc filename) {
251         sourceFileMap.put(new Integer JavaDoc(modNo), filename);
252     }
253     
254     public String JavaDoc getSourceFileInfo(int modNo) {
255         return sourceFileMap.get(new Integer JavaDoc(modNo));
256     }
257     
258     public Variable getScopeVariables(DebugFrame frame) {
259         // Initialize the $this if the scope has one.
260
// That's done by asking for it?!
261
DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
262                     
263         packet.setFlags(DbgConstants.DBGF_WAITACK);
264         DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, "$this");
265         
266         packet.addFrame(f);
267         packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope()));
268         
269         try {
270             packet.send(socket.getOutputStream());
271                 // Wait for the response
272
packet = DbgPacket.receive(in);
273             packet.handle(this);
274         }
275         catch (IOException JavaDoc ioe) {
276             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
277         }
278         
279         // OK, now get the scope variables;-)
280
packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
281                     
282         packet.setFlags(DbgConstants.DBGF_WAITACK);
283         packet.addFrame(new DbgPacket.FrameEval(0, frame.getScope()));
284         
285         try {
286             packet.send(socket.getOutputStream());
287                 // Wait for the response
288
packet = DbgPacket.receive(in);
289             packet.handle(this);
290         }
291         catch (IOException JavaDoc ioe) {
292             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
293         }
294         
295         List JavaDoc<Variable> vars = packet.getEvalValue();
296         Variable ret = null;
297         
298         if (vars != null) {
299             ret = vars.get(0);
300         }
301         return ret;
302     }
303
304     public void setVariableValue(DebugFrame frame, Variable v, Object JavaDoc value) {
305         DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
306                     
307         packet.setFlags(DbgConstants.DBGF_WAITACK);
308         DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, v.getDisplayName() +
309                 "=" + value + ";");
310         
311         packet.addFrame(f);
312         packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope()));
313         
314         try {
315             packet.send(socket.getOutputStream());
316                 // Wait for the response
317
packet = DbgPacket.receive(in);
318             packet.handle(this);
319         }
320         catch (IOException JavaDoc ioe) {
321             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
322         }
323
324     }
325     
326     public Variable evaluateExpr(DebugFrame frame, String JavaDoc expr) {
327         if (expr == null) {
328             return null;
329         }
330         
331         DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
332                     
333         packet.setFlags(DbgConstants.DBGF_WAITACK);
334         DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, expr);
335         
336         packet.addFrame(f);
337         packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), frame.getScope()));
338         
339         try {
340             packet.send(socket.getOutputStream());
341                 // Wait for the response
342
packet = DbgPacket.receive(in);
343             packet.handle(this);
344         }
345         catch (IOException JavaDoc ioe) {
346             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
347         }
348         
349         List JavaDoc<Variable> vars = packet.getEvalValue();
350         Variable ret = null;
351         
352         if (vars != null) {
353             ret = vars.get(0);
354         }
355         return ret;
356     }
357
358     public void setSessionId(String JavaDoc sId) {
359         sessionId = sId;
360     }
361     
362     public void setBreakpoints() {
363         Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager().getBreakpoints();
364         
365         for (int i = 0; i < breakpoints.length; i++) {
366             System.err.println("mw DbgServerHandler.setBreakpoints() breakpoints[" + i + "]= " + breakpoints[i]);
367             
368             if (breakpoints[i] instanceof PhpBreakpoint) {
369                 PhpBreakpoint b = (PhpBreakpoint)breakpoints[i];
370                 
371                 Line line = b.getLine();
372                 FileObject sourceFile = line.getLookup().lookup(FileObject.class);
373                 String JavaDoc targetFile = sourceMap.mapToServerPath(sourceFile);
374                 
375                 if (targetFile != null) {
376
377                     System.err.println("mw addBP! line= " + sourceFile + ":" + (line.getLineNumber() + 1));
378                     System.err.println("mw targetFile= " + targetFile);
379                     
380                     DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
381                     
382                     packet.setFlags(DbgConstants.DBGF_WAITACK);
383             
384                     DbgPacket.FrameRawdata mod_name = new DbgPacket.FrameRawdata(packet, targetFile);
385                     packet.addFrame(mod_name);
386                     DbgPacket.FrameRawdata condition = new DbgPacket.FrameRawdata(packet, b.getCondition());
387                     packet.addFrame(condition);
388                     
389                     packet.addFrame(new DbgPacket.FrameBps(0, line.getLineNumber() + 1, 0, mod_name.getRawId(),
390                             condition.getRawId(), b.isTemp(), 0,
391                             (b.isEnabled() ? DbgConstants.BPS_ENABLED : DbgConstants.BPS_DISABLED)));
392                     
393                     try {
394                         packet.send(socket.getOutputStream());
395                             // Wait for the response
396
packet = DbgPacket.receive(in);
397                         DbgPacket.FrameBpl f = (DbgPacket.FrameBpl)packet.getFirstFrame();
398                         b.setBpNo(f.getBpNo());
399                     }
400                     catch (IOException JavaDoc ioe) {
401                         ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
402                     }
403                 }
404            }
405         }
406     }
407     
408     public void updateSourceTree() {
409         DbgPacket packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
410                     
411         packet.setFlags(DbgConstants.DBGF_WAITACK);
412             
413         DbgPacket.FrameSrcTree f = new DbgPacket.FrameSrcTree();
414         packet.addFrame(f);
415         
416         try {
417             packet.send(socket.getOutputStream());
418                 // Wait for the response
419
packet = DbgPacket.receive(in);
420             
421             packet.handle(this);
422         }
423         catch (IOException JavaDoc ioe) {
424             ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
425         }
426     }
427     
428     public void resume() {
429         callStack = null;
430         getServer().getCallStackModel().setNeedsRefresh();
431         getServer().getVariablesModel().clearModel();
432         
433         cont(DbgConstants.DBGA_CONTINUE);
434     }
435
436     public void stepInto() {
437         cont(DbgConstants.DBGA_STEPINTO);
438     }
439     
440     public void stepOut() {
441         cont(DbgConstants.DBGA_STEPOUT);
442     }
443     
444     public void stepOver() {
445         cont(DbgConstants.DBGA_STEPOVER);
446     }
447     
448     private void cont(int step) {
449         currentLine = null;
450         hideCurrentLine();
451
452         System.err.println("mw DbgServerHandler.cont(" + step + ")");
453         
454         sendPacket(new DbgPacket(step));
455         setSuspended(false);
456     }
457     
458     private void handleIncomingPackets(boolean block) throws IOException JavaDoc {
459         while (block || in.available() > 0) {
460             DbgPacket packet = DbgPacket.receive(in);
461         
462             block = false;
463             packet.handle(this);
464         }
465     }
466     
467     public DebuggerAnnotation getCurrentLineAnnotation() {
468         return currentLineAnnotation;
469     }
470     
471     public void setCurrentLineAnnotation(DebuggerAnnotation annotation) {
472         this.currentLineAnnotation = annotation;
473     }
474 /*
475                 packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
476             packet.addFrame(new DbgPacket.FrameVer());
477             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
478             System.err.println("mw packet to send= " + packet);
479             
480             packet.send(socket.getOutputStream());
481
482             packet = DbgPacket.receive(in);
483             
484             System.err.println("mw packet returned= " + packet);
485
486             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
487             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
488             packet.addFrame(new DbgPacket.FrameSrcTree());
489            
490             System.err.println("mw packet to send= " + packet);
491             packet.send(socket.getOutputStream());
492
493             packet = DbgPacket.receive(in);
494             
495             System.err.println("mw packet returned= " + packet);
496             
497             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
498             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
499             packet.addFrame(new DbgPacket.FrameSrcLinesInfo(1));
500            
501             System.err.println("mw packet to send= " + packet);
502             packet.send(socket.getOutputStream());
503
504             packet = DbgPacket.receive(in);
505             
506             System.err.println("mw packet returned= " + packet);
507
508             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
509             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
510             packet.addFrame(new DbgPacket.FrameSrcCtxInfo(1));
511            
512             System.err.println("mw packet to send= " + packet);
513             packet.send(socket.getOutputStream());
514
515             packet = DbgPacket.receive(in);
516             
517             System.err.println("mw packet returned= " + packet);
518             
519             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
520             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
521             packet.addFrame(new DbgPacket.FrameSource(1, 10));
522            
523             System.err.println("mw packet to send= " + packet);
524             packet.send(socket.getOutputStream());
525
526             packet = DbgPacket.receive(in);
527             
528             System.err.println("mw packet returned= " + packet);
529
530             packet = new DbgPacket(DbgConstants.DBGA_CONTINUE);
531             System.err.println("mw packet to send= " + packet);
532             packet.send(socket.getOutputStream());
533             
534             try {
535                 Thread.sleep(1000);
536             }
537             catch (InterruptedException ie) {
538                 
539             }
540             
541             while (in.available() > 0) {
542                 packet = DbgPacket.receive(in);
543             
544                 System.err.println("mw packet returned= " + packet);
545             }
546             
547             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
548             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
549             packet.addFrame(new DbgPacket.FrameStack());
550             
551             System.err.println("mw packet to send= " + packet);
552             packet.send(socket.getOutputStream());
553
554             packet = DbgPacket.receive(in);
555             
556             System.err.println("mw packet returned= " + packet);
557
558             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
559             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
560             DbgPacket.FrameRawdata f = new DbgPacket.FrameRawdata(packet, "$i");
561             packet.addFrame(f);
562             packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), 1));
563            
564             System.err.println("mw packet to send= " + packet);
565             packet.send(socket.getOutputStream());
566
567             packet = DbgPacket.receive(in);
568             
569             System.err.println("mw packet returned= " + packet);
570             
571             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
572             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
573             f = new DbgPacket.FrameRawdata(packet, "$GLOBALS");
574             packet.addFrame(f);
575             packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), 1));
576            
577             System.err.println("mw packet to send= " + packet);
578             packet.send(socket.getOutputStream());
579
580             packet = DbgPacket.receive(in);
581             
582             System.err.println("mw packet returned= " + packet);
583             
584             packet = new DbgPacket(DbgConstants.DBGA_REQUEST);
585             packet.setFlags(flags | DbgConstants.DBGF_WAITACK);
586             f = new DbgPacket.FrameRawdata(packet, "");
587             packet.addFrame(f);
588             packet.addFrame(new DbgPacket.FrameEval(f.getRawId(), 1));
589            
590             System.err.println("mw packet to send= " + packet);
591             packet.send(socket.getOutputStream());
592
593             packet = DbgPacket.receive(in);
594             
595             System.err.println("mw packet returned= " + packet);
596  */

597 }
598
Popular Tags