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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.tests.jfluid.wireio;
042:
043: import org.netbeans.lib.profiler.tests.jfluid.CommonProfilerTestCase;
044: import org.netbeans.lib.profiler.wireprotocol.Command;
045: import org.netbeans.lib.profiler.wireprotocol.WireIO;
046: import java.io.IOException;
047: import java.io.ObjectInputStream;
048: import java.io.ObjectOutputStream;
049: import java.net.ServerSocket;
050: import java.net.Socket;
051:
052: /**
053: *
054: * @author ehucka
055: */
056: public class CommonWireIOTestCase extends CommonProfilerTestCase {
057: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
058:
059: protected class LoggingThread extends Thread {
060: //~ Instance fields ------------------------------------------------------------------------------------------------------
061:
062: boolean prepared = false;
063: private boolean running = true;
064:
065: //~ Methods --------------------------------------------------------------------------------------------------------------
066:
067: public boolean isPrepared() {
068: return prepared;
069: }
070:
071: public void setRunning(boolean running) {
072: this .running = running;
073: }
074:
075: public boolean isRunning() {
076: return running;
077: }
078:
079: public void run() {
080: try {
081: ServerSocket serverSocket = new ServerSocket(PORT);
082: ref("Server start to listen on port "
083: + String.valueOf(PORT));
084: prepared = true;
085:
086: Socket clientSocket = serverSocket.accept();
087:
088: WireIO wireIO = createWireIO(clientSocket);
089:
090: while (running) {
091: running &= simpleLogCommands(wireIO);
092: }
093:
094: serverSocket.close();
095: } catch (IOException ex) {
096: ex.printStackTrace();
097: }
098: }
099: }
100:
101: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
102:
103: protected static int PORT = 5140;
104:
105: //~ Constructors -------------------------------------------------------------------------------------------------------------
106:
107: /** Creates a new instance of CommonWireIOTestCase */
108: public CommonWireIOTestCase(String name) {
109: super (name);
110: }
111:
112: //~ Methods ------------------------------------------------------------------------------------------------------------------
113:
114: public WireIO createWireIO(Socket clientSocket) {
115: try {
116: clientSocket.setTcpNoDelay(true); // Necessary at least on Solaris to avoid delays in e.g. readInt() etc.
117:
118: ObjectInputStream socketIn = new ObjectInputStream(
119: clientSocket.getInputStream());
120: ObjectOutputStream socketOut = new ObjectOutputStream(
121: clientSocket.getOutputStream());
122: WireIO wireIO = new WireIO(socketOut, socketIn);
123:
124: return wireIO;
125: } catch (IOException ex) {
126: ex.printStackTrace();
127: }
128:
129: return null;
130: }
131:
132: public WireIO createWireIOClient(Socket clientSocket) {
133: try {
134: clientSocket.setSoTimeout(0);
135: clientSocket.setTcpNoDelay(true); // Necessary at least on Solaris to avoid delays in e.g. readInt() etc.
136:
137: ObjectOutputStream socketOut = new ObjectOutputStream(
138: clientSocket.getOutputStream());
139: ObjectInputStream socketIn = new ObjectInputStream(
140: clientSocket.getInputStream());
141: WireIO wireIO = new WireIO(socketOut, socketIn);
142:
143: return wireIO;
144: } catch (IOException ex) {
145: ex.printStackTrace();
146: }
147:
148: return null;
149: }
150:
151: public boolean simpleLogCommands(WireIO wireIO) {
152: try {
153: Object o = wireIO.receiveCommandOrResponse();
154:
155: if (o == null) {
156: ref("Connection interrupted.");
157:
158: return false;
159: } else {
160: if (o instanceof Command) {
161: ref(" received command " + o.toString());
162: } else {
163: ref(" received object " + o.getClass().getName()
164: + " " + o.toString());
165: }
166: }
167: } catch (IOException ex) {
168: return false;
169: }
170:
171: return true;
172: }
173: }
|