Debian Codegraph @

arduino-core/src/processing/app/debug/MessageSiphon.java

raw symbols SCIP
1 /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3 /*
4 Part of the Processing project - http://processing.org
5
6 Copyright (c) 2004-06 Ben Fry and Casey Reas
7 Copyright (c) 2001-04 Massachusetts Institute of Technology
8
9 This program 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 2 of the License, or
12 (at your option) any later version.
13
14 This program 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 this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 package processing.app.debug;
25
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29 import java.net.SocketException;
30
31 /**
32 * Slurps up messages from compiler.
33 */
34 public class MessageSiphon implements Runnable {
35
36 private final Reader streamReader;
37 private final MessageConsumer consumer;
38
39 private Thread thread;
40 private boolean canRun;
41 // Data is processed line-by-line if possible, but if this is non-zero
42 // then a partial line is also processed if no line end is received
43 // within this many milliseconds.
44 private int lineTimeout;
45
46 public MessageSiphon(InputStream stream, MessageConsumer consumer) {
47 this(stream, consumer, 0);
48 }
49
50 public MessageSiphon(InputStream stream, MessageConsumer consumer, int lineTimeout) {
51 this.streamReader = new InputStreamReader(stream);
52 this.consumer = consumer;
53 this.canRun = true;
54 this.lineTimeout = lineTimeout;
55
56 thread = new Thread(this);
57 thread.setName("MessageSiphon");
58 // don't set priority too low, otherwise exceptions won't
59 // bubble up in time (i.e. compile errors have a weird delay)
60 //thread.setPriority(Thread.MIN_PRIORITY);
61 thread.setPriority(Thread.MAX_PRIORITY - 1);
62 thread.start();
63 }
64
65
66 @Override
67 public void run() {
68 try {
69 // process data until we hit EOF; this will happily block
70 // (effectively sleeping the thread) until new data comes in.
71 // when the program is finally done, null will come through.
72 //
73 StringBuilder currentLine = new StringBuilder();
74 long lineStartTime = 0;
75 while (canRun) {
76 // First, try to read as many characters as possible. Take care
77 // not to block when:
78 // 1. lineTimeout is nonzero, and
79 // 2. we have some characters buffered already
80 while (lineTimeout == 0 || currentLine.length() == 0 || streamReader.ready()) {
81 int c = streamReader.read();
82 if (c == -1)
83 return; // EOF
84 if (!canRun)
85 return;
86
87 // Keep track of the line start time
88 if (currentLine.length() == 0)
89 lineStartTime = System.nanoTime();
90
91 // Store the character line
92 currentLine.append((char)c);
93
94 if (c == '\n') {
95 // We read a full line, pass it on
96 consumer.message(currentLine.toString());
97 currentLine.setLength(0);
98 }
99 }
100
101 // No more characters available. Wait until lineTimeout
102 // milliseconds have passed since the start of the line and then
103 // try reading again. If the time has already passed, then just
104 // pass on the characters read so far.
105 long passed = (System.nanoTime() - lineStartTime) / 1000;
106 if (passed < this.lineTimeout) {
107 Thread.sleep(this.lineTimeout - passed);
108 continue;
109 }
110
111 consumer.message(currentLine.toString());
112 currentLine.setLength(0);
113 }
114 //EditorConsole.systemOut.println("messaging thread done");
115 } catch (NullPointerException npe) {
116 // Fairly common exception during shutdown
117 } catch (SocketException e) {
118 // socket has been close while we were wainting for data. nothing to see here, move along
119 } catch (Exception e) {
120 // On Linux and sometimes on Mac OS X, a "bad file descriptor"
121 // message comes up when closing an applet that's run externally.
122 // That message just gets supressed here..
123 String mess = e.getMessage();
124 if ((mess != null) &&
125 (mess.indexOf("Bad file descriptor") != -1)) {
126 //if (e.getMessage().indexOf("Bad file descriptor") == -1) {
127 //System.err.println("MessageSiphon err " + e);
128 //e.printStackTrace();
129 } else {
130 e.printStackTrace();
131 }
132 } finally {
133 thread = null;
134 }
135 }
136
137 // Wait until the MessageSiphon thread is complete.
138 public void join() throws java.lang.InterruptedException {
139 // Grab a temp copy in case another thread nulls the "thread"
140 // member variable
141 Thread t = thread;
142 if (t != null) t.join();
143 }
144
145 public void stop() {
146 this.canRun = false;
147 }
148
149 }