/
DataClient.java
146 lines (128 loc) · 3.7 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
package net.ME1312.SubData.Client;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Callback.ReturnCallback;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.NamedContainer;
import net.ME1312.SubData.Client.Library.DisconnectReason;
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 {
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<NamedContainer<DisconnectReason, DataClient>>> closed = new LinkedList<Callback<NamedContainer<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<NamedContainer<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, ? extends ObjectMap<String>>> callback);
/**
* Send a message to the Server
*
* @param message Message to send
*/
public abstract void sendMessage(MessageOut message);
/**
* Forward a message to another Client
*
* @param id Client ID
* @param message Message to send
*/
public abstract void forwardMessage(UUID id, MessageOut message);
/**
* 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 stream using the same connection info
*
* @return New SubData Channel
*/
public abstract DataClient reopen() 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();
}