KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > tcp > sampler > TCPClientImpl


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java,v 1.6.2.2 2004/06/10 15:10:25 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 /*
20  * Basic TCP Sampler Client class
21  *
22  * Can be used to test the TCP Sampler against an HTTP server
23  *
24  * The protocol handler class name is defined by the property tcp.handler
25  *
26  */

27 package org.apache.jmeter.protocol.tcp.sampler;
28
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InterruptedIOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 import org.apache.jmeter.util.JMeterUtils;
36 import org.apache.jorphan.logging.LoggingManager;
37 import org.apache.log.Logger;
38
39 /**
40  *
41  * @version $Revision: 1.6.2.2 $ $Date: 2004/06/10 15:10:25 $
42  *
43  */

44 public class TCPClientImpl implements TCPClient
45 {
46     private static Logger log = LoggingManager.getLoggerForClass();
47     private byte eolByte = (byte) JMeterUtils.getPropDefault("tcp.eolByte",0);
48     
49     public TCPClientImpl()
50     {
51         super();
52         log.info("Using eolByte="+eolByte);
53     }
54
55
56     /* (non-Javadoc)
57      * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#setupTest()
58      */

59     public void setupTest()
60     {
61         log.info("setuptest");
62     }
63
64     /* (non-Javadoc)
65      * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#teardownTest()
66      */

67     public void teardownTest()
68     {
69         log.info("teardowntest");
70         
71     }
72
73     /* (non-Javadoc)
74      * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#write(java.io.OutputStream, java.lang.String)
75      */

76     public void write(OutputStream JavaDoc os, String JavaDoc s) {
77         try
78         {
79             os.write(s.getBytes());
80             os.flush();
81         }
82         catch (IOException JavaDoc e)
83         {
84             log.debug("Write error",e);
85         }
86         log.debug("Wrote: "+s);
87         return;
88     }
89
90
91     /* (non-Javadoc)
92      * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#read(java.io.InputStream)
93      */

94     public String JavaDoc read(InputStream JavaDoc is)
95     {
96         byte [] buffer = new byte[4096];
97         ByteArrayOutputStream JavaDoc w = new ByteArrayOutputStream JavaDoc();
98         int x = 0;
99         try {
100             while ((x = is.read(buffer)) > -1)
101             {
102                 w.write(buffer, 0, x);
103                 if ((eolByte != 0) && (buffer[x-1] == eolByte))
104                     break;
105             }
106         /*
107          * Timeout is reported as follows:
108          * JDK1.3: InterruptedIOException
109          * JDK1.4: SocketTimeoutException, which extends InterruptedIOException
110          *
111          * So to make the code work on both, just check for InterruptedIOException
112          *
113          * If 1.3 support is dropped, can change to using SocketTimeoutException
114          *
115          * For more accurate detection of timeouts under 1.3,
116          * one could perhaps examine the Exception message text...
117          *
118          */

119         } catch (InterruptedIOException JavaDoc e) {
120             // drop out to handle buffer
121
} catch (IOException JavaDoc e) {
122             log.warn("Read error:"+e);
123             return "";
124         }
125         
126         // do we need to close byte array (or flush it?)
127
log.debug("Read: "+w.size()+ "\n"+w.toString());
128         return w.toString();
129     }
130
131
132     /* (non-Javadoc)
133      * @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#write(java.io.OutputStream, java.io.InputStream)
134      */

135     public void write(OutputStream JavaDoc os, InputStream JavaDoc is) {
136         // TODO Auto-generated method stub
137
return;
138     }
139
140     /**
141      * @return Returns the eolByte.
142      */

143     public byte getEolByte() {
144         return eolByte;
145     }
146     /**
147      * @param eolByte The eolByte to set.
148      */

149     public void setEolByte(byte eolByte) {
150         this.eolByte = eolByte;
151     }
152 }
153
Popular Tags