Skip to content

Commit 592b5c8

Browse files
committed
implemented DynamicByteBuffer.java and PacketRegistry.java
1 parent 9968f93 commit 592b5c8

File tree

8 files changed

+267
-20
lines changed

8 files changed

+267
-20
lines changed

src/de/javasocketapi/core/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void disconnect() throws IOException {
5656
}
5757
}
5858

59-
public void send(byte... bytes) {
60-
this.outputStreamThread.send(bytes);
59+
public void send(Packet packet) {
60+
this.outputStreamThread.send(packet);
6161
}
6262
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package de.javasocketapi.core;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
6+
public class DynamicByteBuffer implements Comparable<ByteBuffer> {
7+
private ByteBuffer byteBuffer;
8+
private float expandFactor;
9+
private int writeIndex;
10+
private int readIndex;
11+
12+
DynamicByteBuffer(int initialCapacity, float expandFactor) {
13+
if (expandFactor < 2) {
14+
throw new IllegalArgumentException(
15+
"The expand factor must be greater or equal to 2!");
16+
}
17+
this.byteBuffer = ByteBuffer.allocate(initialCapacity);
18+
this.expandFactor = expandFactor;
19+
this.writeIndex = 0;
20+
this.readIndex = 0;
21+
}
22+
23+
DynamicByteBuffer(int initialCapacity) {
24+
this(initialCapacity, 2);
25+
}
26+
27+
DynamicByteBuffer() {
28+
this(16);
29+
}
30+
31+
DynamicByteBuffer(int initialCapacity, float expandFactor, byte... bytes) {
32+
this(initialCapacity, expandFactor);
33+
put(bytes);
34+
}
35+
36+
DynamicByteBuffer(int initialCapacity, byte... bytes) {
37+
this(initialCapacity, 2, bytes);
38+
}
39+
40+
DynamicByteBuffer(byte... bytes) {
41+
this(16, 2, bytes);
42+
}
43+
44+
public byte[] array() {
45+
return byteBuffer.array();
46+
}
47+
48+
public int compareTo(ByteBuffer that) {
49+
return byteBuffer.compareTo(that);
50+
}
51+
52+
public int remaining() {
53+
return byteBuffer.remaining();
54+
}
55+
56+
public boolean equals(Object ob) {
57+
return byteBuffer.equals(ob);
58+
}
59+
60+
public byte get() {
61+
int index = this.readIndex;
62+
this.readIndex += 1;
63+
return byteBuffer.get(index);
64+
}
65+
66+
public char getChar() {
67+
int index = this.readIndex;
68+
this.readIndex += 2;
69+
return byteBuffer.getChar(index);
70+
}
71+
72+
public double getDouble() {
73+
int index = this.readIndex;
74+
this.readIndex += 8;
75+
return byteBuffer.getDouble(index);
76+
}
77+
78+
public float getFloat() {
79+
int index = this.readIndex;
80+
this.readIndex += 4;
81+
return byteBuffer.getFloat(index);
82+
}
83+
84+
public int getInt() {
85+
int index = this.readIndex;
86+
this.readIndex += 4;
87+
return byteBuffer.getInt(index);
88+
}
89+
90+
public long getLong() {
91+
int index = this.readIndex;
92+
this.readIndex += 8;
93+
return byteBuffer.getLong(index);
94+
}
95+
96+
public short getShort() {
97+
int index = this.readIndex;
98+
this.readIndex += 2;
99+
return byteBuffer.getShort(index);
100+
}
101+
102+
public ByteOrder order() {
103+
return byteBuffer.order();
104+
}
105+
106+
public ByteBuffer order(ByteOrder bo) {
107+
return byteBuffer.order(bo);
108+
}
109+
110+
public ByteBuffer put(byte b) {
111+
ensureSpace(1 + 1);
112+
int index = this.writeIndex;
113+
this.writeIndex += 1;
114+
return byteBuffer.put(index, b);
115+
}
116+
117+
public ByteBuffer put(byte[] src) {
118+
ensureSpace(src.length);
119+
return byteBuffer.put(src);
120+
}
121+
122+
public ByteBuffer put(ByteBuffer src) {
123+
ensureSpace(src.remaining());
124+
return byteBuffer.put(src);
125+
}
126+
127+
public ByteBuffer putChar(char value) {
128+
ensureSpace(2 + 1);
129+
int index = this.writeIndex;
130+
this.writeIndex += 2;
131+
return byteBuffer.putChar(index, value);
132+
}
133+
134+
public ByteBuffer putDouble(double value) {
135+
ensureSpace(8 + 1);
136+
int index = this.writeIndex;
137+
this.writeIndex += 8;
138+
return byteBuffer.putDouble(index, value);
139+
}
140+
141+
public ByteBuffer putFloat(float value) {
142+
ensureSpace(4 + 1);
143+
int index = this.writeIndex;
144+
this.writeIndex += 4;
145+
return byteBuffer.putFloat(index, value);
146+
}
147+
148+
public ByteBuffer putInt(int value) {
149+
ensureSpace(4 + 1);
150+
int index = this.writeIndex;
151+
this.writeIndex += 4;
152+
return byteBuffer.putInt(index, value);
153+
}
154+
155+
public ByteBuffer putLong(long value) {
156+
ensureSpace(8 + 1);
157+
int index = this.writeIndex;
158+
this.writeIndex += 8;
159+
return byteBuffer.putLong(index, value);
160+
}
161+
162+
public ByteBuffer putShort(short value) {
163+
ensureSpace(2 + 1);
164+
int index = this.writeIndex;
165+
this.writeIndex += 2;
166+
return byteBuffer.putShort(index, value);
167+
}
168+
169+
@Override
170+
public int hashCode() {
171+
return byteBuffer.hashCode();
172+
}
173+
174+
@Override
175+
public String toString() {
176+
return byteBuffer.toString();
177+
}
178+
179+
public ByteBuffer getByteBuffer() {
180+
return byteBuffer;
181+
}
182+
183+
public float getExpandFactor() {
184+
return expandFactor;
185+
}
186+
187+
public int getWriteIndex() {
188+
return writeIndex;
189+
}
190+
191+
public int getReadIndex() {
192+
return readIndex;
193+
}
194+
195+
private void ensureSpace(int needed) {
196+
if (remaining() >= needed) {
197+
return;
198+
}
199+
int newCapacity = (int) (byteBuffer.capacity() * expandFactor);
200+
while (newCapacity < (byteBuffer.capacity() + needed)) {
201+
newCapacity *= expandFactor;
202+
}
203+
ByteBuffer expanded = ByteBuffer.allocate(newCapacity);
204+
expanded.order(byteBuffer.order());
205+
expanded.put(byteBuffer.array());
206+
byteBuffer = expanded;
207+
}
208+
}

src/de/javasocketapi/core/InputStreamThread.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.net.Socket;
6-
import java.util.Arrays;
76

87
class InputStreamThread extends Thread {
98

@@ -33,10 +32,12 @@ public void run() {
3332
}
3433
bytes = new byte[b];
3534
inputStream.read(bytes, 0, b);
36-
System.out.println(Arrays.toString(bytes)); //TODO change to packet
37-
System.out.println(new String(bytes));
35+
DynamicByteBuffer dynamicByteBuffer = new DynamicByteBuffer(bytes);
36+
int packetId = dynamicByteBuffer.getInt();
37+
Class<? extends Packet> packet = PacketRegistry.get(packetId);
38+
packet.newInstance().recieve(dynamicByteBuffer);
3839
}
39-
} catch (IOException e) {
40+
} catch (IOException | InstantiationException | IllegalAccessException e) {
4041
e.printStackTrace();
4142
}
4243
}

src/de/javasocketapi/core/OutputStreamThread.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
class OutputStreamThread extends Thread {
1010

1111
private Socket socket;
12-
private List<byte[]> bytes;
12+
private List<Packet> packets;
1313

1414
{
15-
this.bytes = new LinkedList<>();
15+
this.packets = new LinkedList<>();
1616
}
1717

1818
public OutputStreamThread(Socket socket) {
@@ -31,24 +31,28 @@ public void run() {
3131
interrupt();
3232
break;
3333
}
34-
if (this.bytes.isEmpty()) {
34+
if (this.packets.isEmpty()) {
3535
continue;
3636
}
37-
byte[] bytes = this.bytes.get(0);
38-
if (bytes != null) {
39-
this.bytes.remove(0);
37+
Packet packet = this.packets.get(0);
38+
if (packet != null) {
39+
this.packets.remove(0);
40+
DynamicByteBuffer dynamicByteBuffer = new DynamicByteBuffer();
41+
int packetId = PacketRegistry.indexOf(packet.getClass());
42+
dynamicByteBuffer.putInt(packetId);
43+
packet.send(dynamicByteBuffer);
44+
byte[] bytes = dynamicByteBuffer.array();
4045
outputStream.write(bytes.length);
4146
outputStream.write(bytes);
4247
outputStream.flush();
4348
}
44-
Thread.sleep(0, 1);
4549
}
46-
} catch (IOException | InterruptedException e) {
50+
} catch (IOException e) {
4751
e.printStackTrace();
4852
}
4953
}
5054

51-
public void send(byte... bytes) {
52-
this.bytes.add(bytes);
55+
public void send(Packet packet) {
56+
this.packets.add(packet);
5357
}
5458
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.javasocketapi.core;
2+
3+
public interface Packet {
4+
5+
void recieve(DynamicByteBuffer dynamicByteBuffer);
6+
7+
void send(DynamicByteBuffer dynamicByteBuffer);
8+
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.javasocketapi.core;
2+
3+
import org.reflections.Reflections;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
class PacketRegistry {
9+
10+
private static List<Class<? extends Packet>> registerdPackets;
11+
12+
public static int indexOf(Class<? extends Packet> packetClass) {
13+
return registerdPackets.indexOf(packetClass);
14+
}
15+
16+
public static Class<? extends Packet> get(int index) {
17+
return registerdPackets.get(index);
18+
}
19+
20+
static {
21+
Reflections reflections = new Reflections();
22+
PacketRegistry.registerdPackets = new ArrayList<>(reflections.getSubTypesOf(Packet.class));
23+
}
24+
25+
}

src/de/javasocketapi/core/Server.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public void disconnect() throws IOException {
4444
}
4545
}
4646

47-
public void sendToAllClients(byte... bytes) {
47+
public void sendToAllClients(Packet packet) {
4848
//send to all clients
49-
this.serverSocketAcceptingThread.sendToAllClients(bytes);
49+
this.serverSocketAcceptingThread.sendToAllClients(packet);
5050
}
5151

5252
public void disconnectAllClients() throws IOException {

src/de/javasocketapi/core/ServerSocketAcceptingThread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public void run() {
4040
}
4141
}
4242

43-
public void sendToAllClients(byte... bytes) {
43+
public void sendToAllClients(Packet packet) {
4444
//send to all clients
4545
for (Client client : this.clients) {
46-
client.send(bytes);
46+
client.send(packet);
4747
}
4848
}
4949

0 commit comments

Comments
 (0)