summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_javalibrary/evel_javalib2/src/evel_javalibrary/att/com/RingBuffer.java
blob: 28b309a35cb03fb7a318e9d241a9ea40b2571882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package evel_javalibrary.att.com;
/**************************************************************************//**
 * @file
 * RingBuffer class
 *
  * This file implements internal Ringbuffer for storing and
  *  forwarding events to Collector.
 *
 * License
 * -------
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

import java.util.concurrent.Semaphore;
/*
 * Ringbuffer to store and Forward http(s) POST requests
 */
public class RingBuffer {

	    // message count semaphore
	    public static Semaphore countsem;
	    // space semaphore
	    public static Semaphore spacesem;
	    // lock semaphore
	    public static Semaphore lock;

	    public Object[] elements = null;

	    public int capacity  = 0;
	    public int writePos  = 0;
	    public int available = 0;

	    /*
	     * Constructs Ringbuffer of specified capacity
	     */
	    public RingBuffer(int capacity) {
	        this.capacity = capacity;
	        this.elements = new Object[capacity];
	        countsem = new Semaphore(1);
	        spacesem = new Semaphore(capacity);
	        lock = new Semaphore(1);
	    }

	    //resets the positions
	    public void reset() {
	        this.writePos = 0;
	        this.available = 0;
	    }

	    //returns available capacity
	    public int remainingCapacity() {
	        return this.capacity - this.available;
	    }



	    //Puts Java object into ringbuffer
	    public boolean put(Object element){
	    	
	    	boolean ret = false;
	    	//acquire locks
	    	try {
				spacesem.acquire();
				lock.acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	

	    	//store object
	        if(available < capacity){
	            if(writePos >= capacity){
	                writePos = 0;
	            }
	            elements[writePos] = element;
	            writePos++;
	            available++;
	            ret = true;
	        }
	        
	    	//release Locks
		    lock.release();
			countsem.release();


	        return ret;
	    }

	    public int put(Object[] newElements){
	        return put(newElements, newElements.length);
	    }

	    public int put(Object[] newElements, int length){
	    	//Acquire locks
	    	try {
				spacesem.acquire();
				lock.acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	        int readPos = 0;
	        if(this.writePos > this.available){
	            //space above writePos is all empty

	            if(length <= this.capacity - this.writePos){
	                //space above writePos is sufficient to insert batch

	                for(;  readPos < length; readPos++){
	                    this.elements[this.writePos++] = newElements[readPos];
	                }
	                this.available += readPos;
	                //release
	    		    lock.release();
	    			countsem.release();
	                return length;

	            } else {
	                //both space above writePos and below writePos is necessary to use
	                //to insert batch.

	                int lastEmptyPos = writePos - available;

	                for(; this.writePos < this.capacity; this.writePos++){
	                    this.elements[this.writePos] = newElements[readPos++];
	                }

	                //fill into bottom of array too.
	                this.writePos = 0;

	                int endPos = Math.min(length - readPos, capacity - available - readPos);
	                for(;this.writePos < endPos; this.writePos++){
	                    this.elements[this.writePos] = newElements[readPos++];
	                }
	                this.available += readPos;
	                //release
	    		    lock.release();
	    			countsem.release();
	                return readPos;
	            }
	        } else {
	            int endPos = this.capacity - this.available + this.writePos;

	            for(; this.writePos < endPos; this.writePos++){
	                this.elements[this.writePos] = newElements[readPos++];
	            }
	            this.available += readPos;
	            //release
			    lock.release();
				countsem.release();

	            return readPos;
	        }

	    }

	    /*
	     * Takes a stored object in Ringbuffer and releases the space
	     */

	    public Object take() {
	    	
	    	Object nextObj;
	    	//acquire lock
	    	try {
				countsem.acquire();
				lock.acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	        if(available == 0){
	            nextObj = null;
	        }
	        else {
	          int nextSlot = writePos - available;
	          if(nextSlot < 0){
	            nextSlot += capacity;
	          }
	          nextObj = elements[nextSlot];
	          available--;
	        }
	        //releases object
		    lock.release();
			spacesem.release();
	        
	        return nextObj;
	    }


	    public int take(Object[] into){
	        return take(into, into.length);
	    }


	    public int take(Object[] into, int length){
	        int intoPos = 0;
	        
	    	//acquire lock
	    	try {
				countsem.acquire();
				lock.acquire();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	        if(available <= writePos){
	            int nextPos= writePos - available;
	            int endPos   = nextPos + Math.min(available, length);

	            for(;nextPos < endPos; nextPos++){
	                into[intoPos++] = this.elements[nextPos];
	            }
	            this.available -= intoPos;
	            
	            //release
			    lock.release();
				countsem.release();
				
	            return intoPos;
	        } else {
	            int nextPos = writePos - available + capacity;

	            int leftInTop = capacity - nextPos;
	            if(length <= leftInTop){
	                //copy directly
	                for(; intoPos < length; intoPos++){
	                    into[intoPos] = this.elements[nextPos++];
	                }
	                this.available -= length;
		            //release
				    lock.release();
					countsem.release();
	                return length;

	            } else {
	                //copy top
	                for(; nextPos < capacity; nextPos++){
	                    into[intoPos++] = this.elements[nextPos];
	                }

	                //copy bottom - from 0 to writePos
	                nextPos = 0;
	                int leftToCopy = length - intoPos;
	                int endPos = Math.min(writePos, leftToCopy);

	                for(;nextPos < endPos; nextPos++){
	                    into[intoPos++] = this.elements[nextPos];
	                }

	                this.available -= intoPos;
	                
		            //release
				    lock.release();
					countsem.release();

	                return intoPos;
	            }
	        }
	    }
	
}