/
DataClient.java
189 lines (166 loc) · 4.82 KB
/
DataClient.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
package net.ME1312.SubData.Client;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Callback.ReturnCallback;
import net.ME1312.Galaxi.Library.Container.Pair;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Client.Library.DisconnectReason;
import net.ME1312.SubData.Client.Library.PingResponse;
import net.ME1312.SubData.Client.Protocol.MessageOut;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;
import java.util.UUID;
/**
* SubData Client API Class
*/
public abstract class DataClient implements DataSender {
public final Events on = new Events();
private UUID id;
/**
* SubData Client Event API Class
*/
public static class Events {
LinkedList<Callback<DataClient>> ready = new LinkedList<Callback<DataClient>>();
LinkedList<ReturnCallback<DataClient, Boolean>> close = new LinkedList<ReturnCallback<DataClient, Boolean>>();
LinkedList<Callback<Pair<DisconnectReason, DataClient>>> closed = new LinkedList<Callback<Pair<DisconnectReason, DataClient>>>();
private Events() {}
/**
* On Connection Ready Event
*
* @param callbacks Callback
*/
@SafeVarargs
public final void ready(Callback<DataClient>... callbacks) {
ready.addAll(Arrays.asList(callbacks));
}
/**
* On Connection Close Event
*
* @param callbacks Callback
*/
@SafeVarargs
public final void close(ReturnCallback<DataClient, Boolean>... callbacks) {
close.addAll(Arrays.asList(callbacks));
}
/**
* On Connection Closed Event
*
* @param callbacks Callback
*/
@SafeVarargs
public final void closed(Callback<Pair<DisconnectReason, DataClient>>... callbacks) {
closed.addAll(Arrays.asList(callbacks));
}
}
/**
* Grabs a Client from the Network
*
* @param id Client ID
* @return Client
*/
public abstract void getClient(UUID id, Callback<ObjectMap<String>> callback);
/**
* Grabs all the Clients on the Network
*
* @return Client Map
*/
public abstract void getClients(Callback<Map<UUID, ObjectMap<String>>> callback);
/**
* Ping the Server
*
* @param response Ping Response
*/
public abstract void ping(Callback<PingResponse> response);
/**
* Ping a remote Client
*
* @param id Client ID
* @param response Ping Response
*/
public abstract void ping(UUID id, Callback<PingResponse> response);
/**
* Send a message to the Server
*
* @param messages Messages to send
* @see net.ME1312.SubData.Client.Protocol.ForwardOnly Messages must <b><u>NOT</u></b> be tagged as Forward-Only
*/
public abstract void sendMessage(MessageOut... messages);
/**
* Forward a message to another Client
*
* @param id Client ID
* @param messages Messages to send
* @see net.ME1312.SubData.Client.Protocol.Forwardable Messages must be tagged as Forwardable
*/
public abstract void forwardMessage(UUID id, MessageOut... messages);
/**
* Get SubData's default Block Size
*
* @return Block Size
*/
public abstract int getBlockSize();
/**
* Set SubData's Block Size for the current message
*
* @param size Block Size (null for default)
*/
public abstract void tempBlockSize(Integer size);
/**
* Get the Client that connects the Server to us
*
* @deprecated The Client connection to the Server is this
* @return This Client
*/
@Deprecated
public DataClient getConnection() {
return this;
}
/**
* Get the Protocol for this Client
*
* @return Client Protocol
*/
public abstract DataProtocol getProtocol();
/**
* Get the ID of this Client
*
* @return Client ID
*/
public UUID getID() {
return id;
}
/**
* Get Remote Address
*
* @return Address
*/
public abstract InetSocketAddress getAddress();
/**
* Open an Async Data SubChannel
*
* @return New SubData Channel
*/
public abstract DataClient newChannel() throws IOException;
/**
* Closes the connection
*
* @throws IOException
*/
public abstract void close() throws IOException;
/**
* Wait for the connection to close
*
* @throws InterruptedException
*/
public void waitFor() throws InterruptedException {
while (!isClosed()) Thread.sleep(125);
}
/**
* Get if the connection has been closed
*
* @return Closed Status
*/
public abstract boolean isClosed();
}