-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiverIntBuffer.java
More file actions
123 lines (99 loc) · 2.65 KB
/
Copy pathReceiverIntBuffer.java
File metadata and controls
123 lines (99 loc) · 2.65 KB
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
import java.util.concurrent.atomic.*;
/*
Invariants:
- buf[loc(wp)] = 1st open to write to
- unless wp = rp + size at which point buf is full
- buf[loc(rp)] = 1st unread
- unless wp == rp at which point buf is empty and there is nothign to read
*/
public class ReceiverIntBuffer extends Buffer {
int[] buffer;
int rp;
AtomicInteger wp;
int size;
// need 0 < wp - rp < size
public ReceiverIntBuffer(int size) {
buffer = new int[size];
rp = 0;
wp = new AtomicInteger();
this.size = size;
}
public int getWrite() {
// returns ABSOLUTE write pointer index
return wp.intValue();
}
public int getSize() {
// returns size of data in buffer
return wp.intValue() - rp;
}
public int getRead() {
// returns ABSOLUTE read pointer
return rp;
}
public int getUnsent() {
return -1;
}
public int getUnAcked() {
return -1;
}
public boolean canWrite() {
// boolean of whether therre's space (flow control)
return wp.intValue() < rp + this.size && wp.intValue() >= rp;
}
public boolean canRead() {
// whether there's data to read
return rp < wp.intValue();
}
public int loc(int ptr) {
// modulo to convert ABSOLUTE to indexes
return ptr % size;
}
public int getAvail() {
// return available size
return this.size - (wp.intValue() - rp);
}
public int write(int[] srcBuf, int pos, int len) {
// in from srcBuf
// Write INTO buffer
int wrote = 0;
while (pos < len && this.canWrite()) {
// if (wp == size){
buffer[loc(wp.intValue())] = srcBuf[pos];
// + wp);
// }
wrote++;
wp.incrementAndGet();
pos++;
}
return wrote;
}
public int read(int[] destBuf, int pos, int len) {
// out to destBuf
// read OUT
int read = 0;
while (pos < len && this.canRead()) {
// parentSock.logOutput("BUF READING: " + buffer[loc(rp)]);
destBuf[pos] = buffer[loc(rp)];
// parentSock.logOutput("BUF READING: " + buffer[loc(rp)] + " to destbuf pos " +
// pos + " val: " + destBuf[pos]);
rp++;
pos++;
read++;
}
return read;
}
public int reset() {
// ignore
return -1;
}
public int acknowledge(int len) {
// ignore
return -1;
}
public int getSendBase() {
return -1;
}
public int getSendMax() {
return -1;
}
}