~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to src/processing/app/debug/MessageSiphon.java

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.*;
 
27
 
 
28
 
 
29
/**
 
30
 * Slurps up messages from compiler.
 
31
 */
 
32
class MessageSiphon implements Runnable {
 
33
  BufferedReader streamReader;
 
34
  Thread thread;
 
35
  MessageConsumer consumer;
 
36
 
 
37
 
 
38
  public MessageSiphon(InputStream stream, MessageConsumer consumer) {
 
39
    this.streamReader = new BufferedReader(new InputStreamReader(stream));
 
40
    this.consumer = consumer;
 
41
 
 
42
    thread = new Thread(this);
 
43
    // don't set priority too low, otherwise exceptions won't
 
44
    // bubble up in time (i.e. compile errors have a weird delay)
 
45
    //thread.setPriority(Thread.MIN_PRIORITY);
 
46
    thread.setPriority(Thread.MAX_PRIORITY-1);
 
47
    thread.start();
 
48
  }
 
49
 
 
50
 
 
51
  public void run() {
 
52
    try {
 
53
      // process data until we hit EOF; this will happily block
 
54
      // (effectively sleeping the thread) until new data comes in.
 
55
      // when the program is finally done, null will come through.
 
56
      //
 
57
      String currentLine;
 
58
      while ((currentLine = streamReader.readLine()) != null) {
 
59
        // \n is added again because readLine() strips it out
 
60
        //EditorConsole.systemOut.println("messaging in");
 
61
        consumer.message(currentLine + "\n");
 
62
        //EditorConsole.systemOut.println("messaging out");
 
63
      }
 
64
      //EditorConsole.systemOut.println("messaging thread done");
 
65
      thread = null;
 
66
 
 
67
    } catch (NullPointerException npe) {
 
68
      // Fairly common exception during shutdown
 
69
      thread = null;
 
70
 
 
71
    } catch (Exception e) {
 
72
      // On Linux and sometimes on Mac OS X, a "bad file descriptor"
 
73
      // message comes up when closing an applet that's run externally.
 
74
      // That message just gets supressed here..
 
75
      String mess = e.getMessage();
 
76
      if ((mess != null) &&
 
77
          (mess.indexOf("Bad file descriptor") != -1)) {
 
78
        //if (e.getMessage().indexOf("Bad file descriptor") == -1) {
 
79
        //System.err.println("MessageSiphon err " + e);
 
80
        //e.printStackTrace();
 
81
      } else {
 
82
        e.printStackTrace();
 
83
      }
 
84
      thread = null;
 
85
    }
 
86
  }
 
87
}