001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.errorhandler;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStreamReader;
047: import java.io.PrintWriter;
048: import java.net.ServerSocket;
049: import java.net.Socket;
050: import java.net.UnknownHostException;
051: import javax.swing.JTextArea;
052: import javax.swing.SwingUtilities;
053:
054: /*
055: * DebugClientThread.java
056: * Created on January 6, 2004, 1:05 PM
057: */
058:
059: /**
060: * @author Winston Prakash
061: */
062: public class DebugClientTestThread extends Thread {
063: BufferedReader in = null;
064: PrintWriter out = null;
065:
066: JTextArea textArea = null;
067:
068: StringBuffer message = new StringBuffer();
069:
070: Socket clientSocket = null;
071:
072: boolean connected = false;
073: boolean connectionFailed = false;
074:
075: public DebugClientTestThread(JTextArea textArea) {
076: this .textArea = textArea;
077: }
078:
079: public void run() {
080: String line;
081:
082: try {
083: clientSocket = new Socket("localhost", 4444);
084: displayText("Connected to server\n");
085: connected = true;
086: } catch (UnknownHostException exc) {
087: displayText("Could not connect. unknown host."
088: + exc.getLocalizedMessage());
089: connectionFailed = true;
090: } catch (IOException exc) {
091: displayText("Couldn't get I/O for the connection"
092: + exc.getLocalizedMessage());
093: connectionFailed = true;
094: }
095:
096: try {
097: out = new PrintWriter(clientSocket.getOutputStream(), true);
098: in = new BufferedReader(new InputStreamReader(clientSocket
099: .getInputStream()));
100: } catch (IOException exc) {
101: displayText("Could not get the socket I/O"
102: + exc.getLocalizedMessage());
103: }
104:
105: while (connected) {
106: try {
107: line = in.readLine();
108: if (line != null) {
109: displayText(line);
110: message.append(line + "\n");
111: } else {
112: connected = false;
113: displayText("Connection closed");
114: }
115: } catch (IOException exc) {
116: displayText(exc.getLocalizedMessage());
117: connected = false;
118: }
119: }
120:
121: }
122:
123: public boolean isConnected() {
124: while (!connected) {
125: if (connectionFailed)
126: return false;
127: }
128: return connected;
129: }
130:
131: public void disconnect() {
132: if (connected) {
133: try {
134: out.close();
135: in.close();
136: clientSocket.close();
137: } catch (IOException exc) {
138: displayText("Error occured while disconnecting socket"
139: + exc.getLocalizedMessage());
140: }
141: out = null;
142: in = null;
143: clientSocket = null;
144: connected = false;
145: }
146: }
147:
148: public void sendMessage(String text) {
149: if (out != null) {
150: out.println(text);
151: }
152: }
153:
154: public String getMessage() {
155: return message.toString();
156: }
157:
158: private void displayText(final String line) {
159: SwingUtilities.invokeLater(new Runnable() {
160: public void run() {
161: textArea.append(line);
162: }
163: });
164: }
165: }
|