-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputStreamThread.java
276 lines (263 loc) · 13.3 KB
/
InputStreamThread.java
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
/*
*
* * Copyright (c) 2021. Zyonic Software - Niklas Griese
* * This File, its contents and by extention the corresponding project is property of Zyonic Software and may not be used without explicit permission to do so.
* *
* * contact(at)zyonicsoftware.com
*
*/
package com.zyonicsoftware.minereaper.signal.incoming;
import com.zyonicsoftware.minereaper.runnable.RedEugeneSchedulerRunnable;
import com.zyonicsoftware.minereaper.signal.buffer.ReadingByteBuffer;
import com.zyonicsoftware.minereaper.signal.cache.Cache;
import com.zyonicsoftware.minereaper.signal.caller.SignalCallRegistry;
import com.zyonicsoftware.minereaper.signal.client.Client;
import com.zyonicsoftware.minereaper.signal.exception.SignalException;
import com.zyonicsoftware.minereaper.signal.packet.Packet;
import com.zyonicsoftware.minereaper.signal.packet.PacketRegistry;
import com.zyonicsoftware.minereaper.signal.packet.ahead.ClientDisconnectPacket;
import com.zyonicsoftware.minereaper.signal.packet.ahead.KeepAlivePacket;
import com.zyonicsoftware.minereaper.signal.packet.ahead.UpdateUUIDPacket;
import com.zyonicsoftware.minereaper.signal.scheduler.RedEugeneScheduler;
import com.zyonicsoftware.minereaper.signal.signal.SignalProvider;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @author Niklas Griese
* @see com.zyonicsoftware.minereaper.runnable.RedEugeneSchedulerRunnable
* @see java.lang.Runnable
* @see Client
* @see Socket
* @see InputStream
* @see ReadingByteBuffer
* @see Packet
* @see SignalProvider
*/
public class InputStreamThread extends RedEugeneSchedulerRunnable {
private final Client client;
private final Socket socket;
private final InputStream finalInputStream;
private long cachedTime = 0;
public InputStreamThread(
@NotNull final String eugeneJobName,
@NotNull final TimeUnit timeUnit,
final long period,
final Client client) {
super(eugeneJobName, timeUnit, period);
this.client = client;
this.socket = this.client.getSocket();
try {
this.finalInputStream = this.socket.getInputStream();
} catch (final IOException exception) {
throw new SignalException(
SignalProvider.getSignalProvider().getOutputStreamThrowsAnException(), exception);
}
}
@Override
public void run() {
super.run();
// initialise inputStream
try {
if (this.socket.isClosed()) {
// interrupt thread
this.interrupt();
return;
}
// check if finalInputStream is null
if (this.finalInputStream != null) {
if (this.finalInputStream.available() > 0) {
final int b = this.finalInputStream.read();
if (b != -1) {
// check if byte array length smaller then 255 bytes
if (b < 255) {
// this.bytes.set(new byte[b]);
// receive bytes
// this.finalInputStream.read(this.bytes.get(), 0, b);
final ReadingByteBuffer readingByteBuffer =
new ReadingByteBuffer(this.finalInputStream.readNBytes(b));
// read packetId
final int packetId = readingByteBuffer.readInt();
// check if packet is UpdateUUIDPacket
switch (packetId) {
case -2: {
// read connectionUUID
final UUID connectionUUID = readingByteBuffer.readUUID();
// set updated connectionUUID
this.client.getConnectionUUID().set(connectionUUID);
this.resetCalculation();
this.receiveIncomingPacketMessage(
UpdateUUIDPacket.class.getName(), connectionUUID.toString());
break;
}
case -3: {
// read connectionUUID
final UUID connectionUUID = readingByteBuffer.readUUID();
// set updated connectionUUID
this.resetCalculation();
this.receiveIncomingPacketMessage(
KeepAlivePacket.class.getName(), connectionUUID.toString());
break;
}
case -4: {
final UUID connectionUUID = readingByteBuffer.readUUID();
this.resetCalculation();
this.receiveIncomingPacketMessage(
ClientDisconnectPacket.class.getName(), connectionUUID.toString());
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.clientFromClientServerSideDisconnection(connectionUUID);
break;
}
default:
// get packet
final Class<? extends Packet> packet = PacketRegistry.get(packetId);
// check if received packet not null
if (packet != null) {
// read connectionUUID
final UUID connectionUUID = readingByteBuffer.readUUID();
// initialise packet
packet
.getDeclaredConstructor(UUID.class)
.newInstance(connectionUUID)
.receive(readingByteBuffer);
Cache.setIncomingPackets(Cache.getIncomingPackets() + 1);
this.client.setIncomingPackets(this.client.getIncomingPackets() + 1);
// set cached time to 0;
this.resetCalculation();
// SignalProvider.getSignalProvider().setIncomingPackets(SignalProvider.getSignalProvider().getIncomingPackets() + 1);
this.receiveIncomingPacketMessage(packet.getName(), connectionUUID.toString());
} else {
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.receivePacketIsNullMessage(
SignalProvider.getSignalProvider().getIncomingPacketIsNull());
}
break;
}
} else {
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.receiveLengthToLargeMessage(
SignalProvider.getSignalProvider().getIncomingLengthToLarge());
}
} else {
// close socket
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.receiveSocketCloseMessage(
SignalProvider.getSignalProvider()
.getIncomingSocketCloseMessage()
.replaceAll("%ip%", this.socket.getInetAddress().getHostAddress()));
this.socket.close();
}
} else {
this.calculateClientTimeoutTime();
}
}
} catch (final InstantiationException
| IOException
| IllegalAccessException
| NoSuchMethodException
| InvocationTargetException exception) {
throw new SignalException(
SignalProvider.getSignalProvider().getIncomingInputThrowsAnException(), exception);
}
}
/**
* @param packetName intern call
* @param connectionUUID intern call
* @throws NoSuchMethodException when instance can not be called
* @throws InvocationTargetException when instance can not be called
* @throws InstantiationException when instance can not be called
* @throws IllegalAccessException when instance can not be called
*/
private void receiveIncomingPacketMessage(final String packetName, final String connectionUUID)
throws NoSuchMethodException, InvocationTargetException, InstantiationException,
IllegalAccessException {
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.receivePacketMessage(
SignalProvider.getSignalProvider()
.getIncomingPacketMessage()
.replace("%packet%", packetName)
.replace("%client%", connectionUUID));
}
/**
* rested the calculation at the client time outed
*/
private void resetCalculation() {
if (this.cachedTime > 0) {
this.cachedTime = 0;
}
}
/**
* calculate the client timeout
*/
private void calculateClientTimeoutTime() {
if (this.cachedTime == 0) {
this.cachedTime = System.currentTimeMillis();
} else {
final long estimatedTime = System.currentTimeMillis() - this.cachedTime;
if (estimatedTime >= this.client.getTimeout()) {
try {
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.clientTimeoutMessage(
SignalProvider.getSignalProvider()
.getTimeoutClient()
.replace("%client%", this.client.getConnectionUUID().get().toString()));
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.clientTimeout(this.client.getConnectionUUID().get());
} catch (final InstantiationException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException exception) {
throw new SignalException(
SignalProvider.getSignalProvider().getIncomingInputThrowsAnException(), exception);
}
}
}
}
/**
* destroy the runner at the client disconnect
*/
public void interrupt() {
try {
this.finalInputStream.close();
final String jobName = this.getEugeneJob().getName();
if (RedEugeneScheduler.getRedEugeneIntroduction().forceCancelEugeneJob(jobName)) {
try {
SignalCallRegistry.getReferenceCaller()
.getDeclaredConstructor(String.class)
.newInstance(this.toString())
.canceledJobMessage(
SignalProvider.getSignalProvider()
.getCanceledJobMessage()
.replaceAll("%job%", jobName));
} catch (final InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException exception) {
throw new SignalException(
SignalProvider.getSignalProvider().getCanceledJobThrowsAnException(), exception);
}
}
} catch (final IOException exception) {
throw new SignalException(
SignalProvider.getSignalProvider().getIncomingInputThrowsAnException(), exception);
}
}
}