|
| 1 | +/* |
| 2 | + * This file is part of LuckPerms, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) lucko (Luck) <[email protected]> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +package net.luckperms.rest; |
| 27 | + |
| 28 | +import com.google.gson.Gson; |
| 29 | +import com.launchdarkly.eventsource.ConnectStrategy; |
| 30 | +import com.launchdarkly.eventsource.EventSource; |
| 31 | +import com.launchdarkly.eventsource.FaultEvent; |
| 32 | +import com.launchdarkly.eventsource.MessageEvent; |
| 33 | +import com.launchdarkly.eventsource.StreamEvent; |
| 34 | +import net.luckperms.rest.event.EventCall; |
| 35 | +import net.luckperms.rest.event.EventProducer; |
| 36 | +import okhttp3.HttpUrl; |
| 37 | +import okhttp3.OkHttpClient; |
| 38 | +import retrofit2.Call; |
| 39 | +import retrofit2.CallAdapter; |
| 40 | +import retrofit2.Retrofit; |
| 41 | + |
| 42 | +import java.lang.annotation.Annotation; |
| 43 | +import java.lang.reflect.ParameterizedType; |
| 44 | +import java.lang.reflect.Type; |
| 45 | +import java.util.List; |
| 46 | +import java.util.concurrent.CompletableFuture; |
| 47 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 48 | +import java.util.function.Consumer; |
| 49 | + |
| 50 | +class EventCallAdapterFactory extends CallAdapter.Factory { |
| 51 | + private final OkHttpClient client; |
| 52 | + |
| 53 | + EventCallAdapterFactory(OkHttpClient client) { |
| 54 | + this.client = client; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { |
| 59 | + if (getRawType(returnType) != EventCall.class) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + if (!(returnType instanceof ParameterizedType)) { |
| 64 | + throw new IllegalArgumentException("Return type must be parameterized as EventCall<Foo> or EventCall<? extends Foo>"); |
| 65 | + } |
| 66 | + |
| 67 | + Type responseType = getParameterUpperBound(0, (ParameterizedType) returnType); |
| 68 | + |
| 69 | + return new CallAdapter<Object, EventCall<?>>() { |
| 70 | + @Override |
| 71 | + public Type responseType() { |
| 72 | + return Object.class; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public EventCall<Object> adapt(Call<Object> call) { |
| 77 | + return new EventCallImpl<>(call.request().url(), responseType, EventCallAdapterFactory.this.client); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + private static final class EventCallImpl<E> implements EventCall<E> { |
| 83 | + private final HttpUrl url; |
| 84 | + private final Type responseType; |
| 85 | + private final OkHttpClient client; |
| 86 | + |
| 87 | + EventCallImpl(HttpUrl url, Type responseType, OkHttpClient client) { |
| 88 | + this.url = url; |
| 89 | + this.responseType = responseType; |
| 90 | + this.client = client; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public EventProducer<E> subscribe() throws Exception { |
| 95 | + EventSource eventSource = new EventSource.Builder(ConnectStrategy.http(this.url).httpClient(this.client)).build(); |
| 96 | + eventSource.start(); |
| 97 | + |
| 98 | + return new EventProducerImpl<>(eventSource, this.responseType); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static final class EventProducerImpl<E> implements EventProducer<E> { |
| 103 | + private static final Gson GSON = new Gson(); |
| 104 | + |
| 105 | + private final EventSource eventSource; |
| 106 | + private final Type responseType; |
| 107 | + |
| 108 | + private final List<Consumer<E>> handlers; |
| 109 | + private final List<Consumer<Exception>> errorHandlers; |
| 110 | + |
| 111 | + private EventProducerImpl(EventSource eventSource, Type responseType) { |
| 112 | + this.eventSource = eventSource; |
| 113 | + this.responseType = responseType; |
| 114 | + this.handlers = new CopyOnWriteArrayList<>(); |
| 115 | + this.errorHandlers = new CopyOnWriteArrayList<>(); |
| 116 | + |
| 117 | + startListening(); |
| 118 | + } |
| 119 | + |
| 120 | + private void startListening() { |
| 121 | + // TODO: should probably use a different pool |
| 122 | + CompletableFuture.runAsync(() -> { |
| 123 | + try { |
| 124 | + for (StreamEvent event : this.eventSource.anyEvents()) { |
| 125 | + if (event instanceof MessageEvent) { |
| 126 | + handleMessage((MessageEvent) event); |
| 127 | + } else if (event instanceof FaultEvent) { |
| 128 | + handleError((FaultEvent) event); |
| 129 | + } |
| 130 | + } |
| 131 | + } catch (Exception e) { |
| 132 | + e.printStackTrace(); |
| 133 | + } |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + private void handleMessage(MessageEvent e) { |
| 138 | + // TODO: fix logging in this method |
| 139 | + String eventName = e.getEventName(); |
| 140 | + if (!eventName.equals("message")) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + E parsedEvent; |
| 145 | + try { |
| 146 | + parsedEvent = GSON.fromJson(e.getData(), this.responseType); |
| 147 | + } catch (Exception ex) { |
| 148 | + ex.printStackTrace(); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + for (Consumer<E> handler : this.handlers) { |
| 153 | + try { |
| 154 | + handler.accept(parsedEvent); |
| 155 | + } catch (Exception ex) { |
| 156 | + ex.printStackTrace(); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void handleError(FaultEvent e) { |
| 162 | + for (Consumer<Exception> errorHandler : this.errorHandlers) { |
| 163 | + try { |
| 164 | + errorHandler.accept(e.getCause()); |
| 165 | + } catch (Exception ex) { |
| 166 | + ex.printStackTrace(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void subscribe(Consumer<E> consumer) { |
| 173 | + this.handlers.add(consumer); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void errorHandler(Consumer<Exception> errorHandler) { |
| 178 | + this.errorHandlers.add(errorHandler); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public void close() { |
| 183 | + this.eventSource.close(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments