View Javadoc

1   /*
2    * Copyright 2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.eviware.soapui.impl.wsdl.monitor;
18  
19  /***
20   * class to simulate slow connections by slowing down the system
21   */
22  public class SlowLinkSimulator
23  {
24  
25  	/***
26  	 * Field delayBytes
27  	 */
28  	private int delayBytes;
29  
30  	/***
31  	 * Field delayTime
32  	 */
33  	private int delayTime;
34  
35  	/***
36  	 * Field currentBytes
37  	 */
38  	private int currentBytes;
39  
40  	/***
41  	 * Field totalBytes
42  	 */
43  	private int totalBytes;
44  
45  	/***
46  	 * construct
47  	 * 
48  	 * @param delayBytes
49  	 *           bytes per delay; set to 0 for no delay
50  	 * @param delayTime
51  	 *           delay time per delay in milliseconds
52  	 */
53  	public SlowLinkSimulator( int delayBytes, int delayTime )
54  	{
55  		this.delayBytes = delayBytes;
56  		this.delayTime = delayTime;
57  	}
58  
59  	/***
60  	 * construct by copying delay bytes and time, but not current count of bytes
61  	 * 
62  	 * @param that
63  	 *           source of data
64  	 */
65  	public SlowLinkSimulator( SlowLinkSimulator that )
66  	{
67  		this.delayBytes = that.delayBytes;
68  		this.delayTime = that.delayTime;
69  	}
70  
71  	/***
72  	 * how many bytes have gone past?
73  	 * 
74  	 * @return integer
75  	 */
76  	public int getTotalBytes()
77  	{
78  		return totalBytes;
79  	}
80  
81  	/***
82  	 * log #of bytes pumped. Will pause when necessary. This method is not
83  	 * synchronized
84  	 * 
85  	 * @param bytes
86  	 */
87  	public void pump( int bytes )
88  	{
89  		totalBytes += bytes;
90  		if( delayBytes == 0 )
91  		{
92  
93  			// when not delaying, we are just a byte counter
94  			return;
95  		}
96  		currentBytes += bytes;
97  		if( currentBytes > delayBytes )
98  		{
99  
100 			// we have overshot. lets find out how far
101 			int delaysize = currentBytes / delayBytes;
102 			long delay = delaysize * ( long )delayTime;
103 
104 			// move byte counter down to the remainder of bytes
105 			currentBytes = currentBytes % delayBytes;
106 
107 			// now wait
108 			try
109 			{
110 				Thread.sleep( delay );
111 			}
112 			catch( InterruptedException e )
113 			{
114 				// ignore the exception
115 			}
116 		}
117 	}
118 
119 	/***
120 	 * get the current byte count
121 	 * 
122 	 * @return integer
123 	 */
124 	public int getCurrentBytes()
125 	{
126 		return currentBytes;
127 	}
128 
129 	/***
130 	 * set the current byte count
131 	 * 
132 	 * @param currentBytes
133 	 */
134 	public void setCurrentBytes( int currentBytes )
135 	{
136 		this.currentBytes = currentBytes;
137 	}
138 }