Skip to content

Commit 3f8c99e

Browse files
committed
added ReadingByteBuffer.java, WritingByteBuffer.java and UpdateUUIDPacket.java
1 parent e2ec9cc commit 3f8c99e

12 files changed

+339
-42
lines changed

src/de/javasocketapi/core/Client.java

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

6-
public class Client implements Connection {
6+
public class Client extends Connection {
77

88
private String hostname;
99
private int port;
@@ -19,6 +19,10 @@ public int getPort() {
1919
return port;
2020
}
2121

22+
public Socket getSocket() {
23+
return socket;
24+
}
25+
2226
public Client(String hostname, int port) {
2327
this.hostname = hostname;
2428
this.port = port;
@@ -37,9 +41,9 @@ public void connect() throws IOException {
3741
this.socket = new Socket(this.hostname, this.port);
3842
}
3943
//start reading and writing
40-
this.inputStreamThread = new InputStreamThread(this.socket);
44+
this.inputStreamThread = new InputStreamThread(this);
4145
this.inputStreamThread.start();
42-
this.outputStreamThread = new OutputStreamThread(this.socket);
46+
this.outputStreamThread = new OutputStreamThread(this);
4347
this.outputStreamThread.start();
4448
}
4549

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package de.javasocketapi.core;
22

3+
import javafx.beans.property.SimpleObjectProperty;
4+
35
import java.io.IOException;
6+
import java.util.UUID;
7+
8+
abstract class Connection {
9+
10+
private volatile SimpleObjectProperty<UUID> connectionUUID;
11+
12+
public SimpleObjectProperty<UUID> getConnectionUUID() {
13+
return connectionUUID;
14+
}
15+
16+
{
17+
this.connectionUUID = new SimpleObjectProperty<>(UUID.randomUUID());
18+
}
19+
20+
public abstract void connect() throws IOException;
421

5-
interface Connection {
6-
void connect() throws IOException;
22+
public abstract void disconnect() throws IOException;
723

8-
void disconnect() throws IOException;
924
}

src/de/javasocketapi/core/DynamicByteBuffer.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ public short getShort() {
9999
return byteBuffer.getShort(index);
100100
}
101101

102+
public String getString() {
103+
int lenght = getInt();
104+
byte[] bytes = new byte[lenght];
105+
for (int i = 0; i < lenght; i++) {
106+
bytes[i] = get();
107+
}
108+
return new String(bytes);
109+
}
110+
102111
public ByteOrder order() {
103112
return byteBuffer.order();
104113
}
@@ -107,63 +116,98 @@ public ByteBuffer order(ByteOrder bo) {
107116
return byteBuffer.order(bo);
108117
}
109118

110-
public ByteBuffer put(byte b) {
119+
public void put(byte b) {
111120
ensureSpace(1 + 1);
112121
int index = this.writeIndex;
113122
this.writeIndex += 1;
114-
return byteBuffer.put(index, b);
123+
byteBuffer.put(index, b);
115124
}
116125

117-
public ByteBuffer put(byte[] src) {
126+
public void put(byte[] src) {
118127
ensureSpace(src.length);
119-
return byteBuffer.put(src);
128+
byteBuffer.put(src);
120129
}
121130

122-
public ByteBuffer put(ByteBuffer src) {
131+
public void put(ByteBuffer src) {
123132
ensureSpace(src.remaining());
124-
return byteBuffer.put(src);
133+
byteBuffer.put(src);
125134
}
126135

127-
public ByteBuffer putChar(char value) {
136+
public void putChar(char value) {
128137
ensureSpace(2 + 1);
129138
int index = this.writeIndex;
130139
this.writeIndex += 2;
131-
return byteBuffer.putChar(index, value);
140+
byteBuffer.putChar(index, value);
132141
}
133142

134-
public ByteBuffer putDouble(double value) {
143+
public void putDouble(double value) {
135144
ensureSpace(8 + 1);
136145
int index = this.writeIndex;
137146
this.writeIndex += 8;
138-
return byteBuffer.putDouble(index, value);
147+
byteBuffer.putDouble(index, value);
139148
}
140149

141-
public ByteBuffer putFloat(float value) {
150+
public void putFloat(float value) {
142151
ensureSpace(4 + 1);
143152
int index = this.writeIndex;
144153
this.writeIndex += 4;
145-
return byteBuffer.putFloat(index, value);
154+
byteBuffer.putFloat(index, value);
146155
}
147156

148-
public ByteBuffer putInt(int value) {
157+
public void putInt(int value) {
149158
ensureSpace(4 + 1);
150159
int index = this.writeIndex;
151160
this.writeIndex += 4;
152-
return byteBuffer.putInt(index, value);
161+
byteBuffer.putInt(index, value);
153162
}
154163

155-
public ByteBuffer putLong(long value) {
164+
public void putLong(long value) {
156165
ensureSpace(8 + 1);
157166
int index = this.writeIndex;
158167
this.writeIndex += 8;
159-
return byteBuffer.putLong(index, value);
168+
byteBuffer.putLong(index, value);
160169
}
161170

162-
public ByteBuffer putShort(short value) {
171+
public void putShort(short value) {
163172
ensureSpace(2 + 1);
164173
int index = this.writeIndex;
165174
this.writeIndex += 2;
166-
return byteBuffer.putShort(index, value);
175+
byteBuffer.putShort(index, value);
176+
}
177+
178+
public void putString(String value) {
179+
putInt(value.length());
180+
for (byte b : value.getBytes()) {
181+
put(b);
182+
}
183+
}
184+
185+
public static void main(String[] args) {
186+
DynamicByteBuffer dynamicByteBuffer = new DynamicByteBuffer();
187+
/*dynamicByteBuffer.putString("Hello World!");
188+
dynamicByteBuffer.putString("Bye World!");
189+
System.out.println(dynamicByteBuffer.getString());
190+
System.out.println(dynamicByteBuffer.getString());*/
191+
dynamicByteBuffer.putInt(0);
192+
dynamicByteBuffer.putInt(1);
193+
dynamicByteBuffer.putInt(2);
194+
dynamicByteBuffer.putInt(3);
195+
dynamicByteBuffer.putInt(4);
196+
dynamicByteBuffer.putInt(5);
197+
dynamicByteBuffer.putInt(6);
198+
dynamicByteBuffer.putInt(7);
199+
dynamicByteBuffer.putInt(8);
200+
dynamicByteBuffer.putInt(9);
201+
System.out.println(dynamicByteBuffer.getInt());
202+
System.out.println(dynamicByteBuffer.getInt());
203+
System.out.println(dynamicByteBuffer.getInt());
204+
System.out.println(dynamicByteBuffer.getInt());
205+
System.out.println(dynamicByteBuffer.getInt());
206+
System.out.println(dynamicByteBuffer.getInt());
207+
System.out.println(dynamicByteBuffer.getInt());
208+
System.out.println(dynamicByteBuffer.getInt());
209+
System.out.println(dynamicByteBuffer.getInt());
210+
System.out.println(dynamicByteBuffer.getInt());
167211
}
168212

169213
@Override

src/de/javasocketapi/core/InputStreamThread.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.lang.reflect.InvocationTargetException;
56
import java.net.Socket;
7+
import java.util.UUID;
68

79
class InputStreamThread extends Thread {
810

11+
private Client client;
912
private Socket socket;
1013

11-
public InputStreamThread(Socket socket) {
12-
this.socket = socket;
14+
public InputStreamThread(Client client) {
15+
this.client = client;
16+
this.socket = this.client.getSocket();
1317
}
1418

1519
@Override
@@ -22,22 +26,38 @@ public void run() {
2226
byte[] bytes;
2327
while (true) {
2428
if (this.socket.isClosed()) {
29+
//interrupt thread
2530
interrupt();
2631
break;
2732
}
2833
int b = inputStream.read();
2934
if (b == -1) {
35+
//close socket
3036
this.socket.close();
3137
continue;
3238
}
3339
bytes = new byte[b];
40+
//receive bytes
3441
inputStream.read(bytes, 0, b);
35-
DynamicByteBuffer dynamicByteBuffer = new DynamicByteBuffer(bytes);
36-
int packetId = dynamicByteBuffer.getInt();
37-
Class<? extends Packet> packet = PacketRegistry.get(packetId);
38-
packet.newInstance().recieve(dynamicByteBuffer);
42+
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes);
43+
//read packetId
44+
int packetId = readingByteBuffer.readInt();
45+
//check if packet is UpdateUUIDPacket
46+
if (packetId == -2) {
47+
//read connectionUUID
48+
UUID connectionUUID = readingByteBuffer.readUUID();
49+
//set updated connectionUUID
50+
this.client.getConnectionUUID().set(connectionUUID);
51+
} else {
52+
//get packet
53+
Class<? extends Packet> packet = PacketRegistry.get(packetId);
54+
//read connectionUUID
55+
UUID connectionUUID = readingByteBuffer.readUUID();
56+
//initialise packet
57+
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
58+
}
3959
}
40-
} catch (IOException | InstantiationException | IllegalAccessException e) {
60+
} catch (IOException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
4161
e.printStackTrace();
4262
}
4363
}

src/de/javasocketapi/core/OutputStreamThread.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
class OutputStreamThread extends Thread {
1010

11+
private Client client;
1112
private Socket socket;
1213
private List<Packet> packets;
1314

1415
{
1516
this.packets = new LinkedList<>();
1617
}
1718

18-
public OutputStreamThread(Socket socket) {
19-
this.socket = socket;
19+
public OutputStreamThread(Client client) {
20+
this.client = client;
21+
this.socket = this.client.getSocket();
2022
}
2123

2224
@Override
@@ -28,22 +30,42 @@ public void run() {
2830
//send byte arrays
2931
while (true) {
3032
if (this.socket.isClosed()) {
33+
//interrupt thread
3134
interrupt();
3235
break;
3336
}
3437
if (this.packets.isEmpty()) {
38+
//skip when no packets available to send
3539
continue;
3640
}
41+
//get next packet available to send
3742
Packet packet = this.packets.get(0);
43+
//check if packet is valid
3844
if (packet != null) {
45+
//remove packet
3946
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();
47+
WritingByteBuffer writingByteBuffer = new WritingByteBuffer();
48+
//check if packet is UpdateUUIDPacket
49+
if (packet.getClass().equals(UpdateUUIDPacket.class)) {
50+
writingByteBuffer.writeInt(-2);
51+
writingByteBuffer.writeUUID(packet.getConnectionUUID());
52+
} else {
53+
//get packetId
54+
int packetId = PacketRegistry.indexOf(packet.getClass());
55+
//write packetId
56+
writingByteBuffer.writeInt(packetId);
57+
//write connectionUuid
58+
writingByteBuffer.writeUUID(this.client.getConnectionUUID().get());
59+
//initialise packet
60+
packet.send(writingByteBuffer);
61+
}
62+
//receive bytes
63+
byte[] bytes = writingByteBuffer.toBytes();
64+
//write bytes length
4565
outputStream.write(bytes.length);
66+
//write bytes
4667
outputStream.write(bytes);
68+
//flush outputStream
4769
outputStream.flush();
4870
}
4971
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
package de.javasocketapi.core;
22

3-
public interface Packet {
3+
import java.util.UUID;
44

5-
void recieve(DynamicByteBuffer dynamicByteBuffer);
5+
public abstract class Packet {
66

7-
void send(DynamicByteBuffer dynamicByteBuffer);
7+
private UUID connectionUUID;
8+
9+
public UUID getConnectionUUID() {
10+
return connectionUUID;
11+
}
12+
13+
public Packet(UUID connectionUUID) {
14+
this.connectionUUID = connectionUUID;
15+
}
16+
17+
public abstract void send(WritingByteBuffer writingByteBuffer);
18+
19+
public abstract void recieve(ReadingByteBuffer readingByteBuffer);
820

921
}

src/de/javasocketapi/core/PacketRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ static Class<? extends Packet> get(int index) {
1616
}
1717

1818
public static void registerPacket(Class<? extends Packet> packetClass) {
19+
//register packet
1920
PacketRegistry.registerdPackets.add(packetClass);
2021
}
2122

2223
public static void registerPackets(List<Class<? extends Packet>> packetClasses) {
24+
//register packets
2325
PacketRegistry.registerdPackets.addAll(packetClasses);
2426
}
2527

0 commit comments

Comments
 (0)