-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathApolloRoundtripManager.java
More file actions
133 lines (118 loc) · 4.37 KB
/
Copy pathApolloRoundtripManager.java
File metadata and controls
133 lines (118 loc) · 4.37 KB
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
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2026 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.roundtrip;
import com.lunarclient.apollo.async.future.UncertainFuture;
import com.lunarclient.apollo.roundtrip.pagination.ApolloPaginatedResponse;
import com.lunarclient.apollo.roundtrip.pagination.ApolloPaginationManager;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Manages Apollo round-trip messages.
*
* @since 1.0.0
*/
public final class ApolloRoundtripManager {
/**
* Represents a {@link Map} of {@link UUID} packet id as a key
* and {@link UncertainFuture} response as value.
*
* @since 1.0.0
*/
private final Map<UUID, UncertainFuture<ApolloResponse>> listeners;
/**
* The executor for packet timeouts.
*
* @since 1.0.0
*/
private final ScheduledThreadPoolExecutor timeoutExecutor;
/**
* The manager for paginated responses.
*
* @since 1.2.5
*/
private final ApolloPaginationManager paginationManager;
/**
* Constructs the {@link ApolloRoundtripManager}.
*
* @since 1.0.0
*/
public ApolloRoundtripManager() {
this.listeners = new ConcurrentHashMap<>();
this.timeoutExecutor = new ScheduledThreadPoolExecutor(1);
this.paginationManager = new ApolloPaginationManager(this);
}
/**
* Handles the given {@link ApolloResponse}.
*
* @param response the response
* @since 1.2.5
*/
public void handleResponse(ApolloResponse response) {
if (response instanceof ApolloPaginatedResponse<?>) {
this.paginationManager.handlePage((ApolloPaginatedResponse<?>) response);
return;
}
this.completeFuture(response);
}
/**
* Completes the {@link UncertainFuture} for a given {@link ApolloResponse}.
*
* @param response the response
* @since 1.2.5
*/
public void completeFuture(ApolloResponse response) {
UncertainFuture<ApolloResponse> future = this.listeners.remove(response.getPacketId());
if (future != null) {
future.handleSuccess(response);
}
}
/**
* Registers an {@link UncertainFuture} for an {@link ApolloRequest}.
*
* @param request the request
* @param future the future
* @param <T> the response type
* @since 1.0.0
*/
@SuppressWarnings("unchecked")
public <T extends ApolloResponse> void registerListener(ApolloRequest<T> request, UncertainFuture<T> future) {
UUID packetId = request.getRequestId();
this.timeoutExecutor.schedule(() -> {
try {
UncertainFuture<ApolloResponse> listener = this.listeners.remove(packetId);
this.paginationManager.handleTimeout(packetId);
if (listener != null) {
Throwable error = new Throwable("Timeout exceeded!");
future.handleFailure(error);
}
} catch (Exception e) {
e.printStackTrace();
}
}, ApolloRequest.TIMEOUT, TimeUnit.MILLISECONDS);
this.listeners.put(packetId, (UncertainFuture<ApolloResponse>) future);
}
}