|
| 1 | +package com.bookmap.api.rpc.server.addon; |
| 2 | + |
| 3 | +import com.bookmap.addons.broadcasting.api.view.BroadcasterConsumer; |
| 4 | +import com.bookmap.addons.broadcasting.api.view.Event; |
| 5 | +import com.bookmap.addons.broadcasting.api.view.GeneratorInfo; |
| 6 | + |
| 7 | +import com.bookmap.api.rpc.server.EventLoop; |
| 8 | +import com.bookmap.api.rpc.server.addon.listeners.ConnectionListener; |
| 9 | +import com.bookmap.api.rpc.server.addon.listeners.EventListener; |
| 10 | +import com.bookmap.api.rpc.server.addon.listeners.LiveConnectionListener; |
| 11 | +import com.bookmap.api.rpc.server.log.RpcLogger; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Optional; |
| 16 | +import java.util.concurrent.*; |
| 17 | + |
| 18 | +public class Connector { |
| 19 | + private final BroadcasterConsumer broadcasterConsumer; |
| 20 | + private final ConnectionListener connectionListener; |
| 21 | + private final Map<String, LiveConnectionListener> liveSubscriptionListenersByAlias = new ConcurrentHashMap<>(); |
| 22 | + private final ExecutorService service = Executors.newSingleThreadExecutor(); |
| 23 | + |
| 24 | + public Connector(BroadcasterConsumer broadcasterConsumer) { |
| 25 | + this.broadcasterConsumer = broadcasterConsumer; |
| 26 | + connectionListener = new ConnectionListener(); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + public Future<Boolean> connect(String providerName) { |
| 31 | + RpcLogger.info("Connecting to " + providerName); |
| 32 | + broadcasterConsumer.connectToProvider(providerName, connectionListener); |
| 33 | + return service.submit(() -> { |
| 34 | + int numberOfTries = 30; |
| 35 | + for (int i = 1; i <= numberOfTries; i++) { |
| 36 | + if (isConnected()) { |
| 37 | + RpcLogger.info("Successfully connected on " + i + " try"); |
| 38 | + return true; |
| 39 | + } |
| 40 | + try { |
| 41 | + if (i % 10 == 0) { |
| 42 | + RpcLogger.info(String.format("Tried to connect %d times", i)); |
| 43 | + } |
| 44 | + Thread.sleep(300); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + RpcLogger.error("Subscribing to live data interrupted ", e); |
| 47 | + } |
| 48 | + } |
| 49 | + return false; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean isConnected(){ |
| 54 | + return connectionListener.isConnected(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public void subscribeToLiveData(String alias, EventLoop eventLoop, String providerName) { |
| 59 | + if (isConnected()) { |
| 60 | + Optional<GeneratorInfo> generatorInfoOptional = getGeneratorInfo(alias, providerName); |
| 61 | + generatorInfoOptional.ifPresent(generatorInfo -> { |
| 62 | + LiveConnectionListener subscriptionListener = |
| 63 | + liveSubscriptionListenersByAlias.computeIfAbsent(alias, listener -> |
| 64 | + new LiveConnectionListener()); |
| 65 | + |
| 66 | + // Creating a listener for the events themselves. |
| 67 | + EventListener eventListener = new EventListener(eventLoop, alias); |
| 68 | + |
| 69 | + // Trying to subscribe for live events. |
| 70 | + // Broadcasting will notify us of a successful subscription through the LiveConnectionListener. |
| 71 | + broadcasterConsumer.subscribeToLiveData(providerName, generatorInfo.getGeneratorName(), |
| 72 | + Event.class, eventListener, subscriptionListener); |
| 73 | + broadcasterConsumer.setListenersForGenerator(providerName, alias, null, null); |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private Optional<GeneratorInfo> getGeneratorInfo(String alias, String providerName) { |
| 79 | + GeneratorInfo generatorInfo = null; |
| 80 | + List<GeneratorInfo> generatorsInfo = broadcasterConsumer.getGeneratorsInfo(providerName); |
| 81 | + for (GeneratorInfo generator : generatorsInfo) { |
| 82 | + // In AbsorptionIndicator, generators have the names of the aliases they work with. |
| 83 | + // Therefore, according to the alias, we will get a suitable generator for us. |
| 84 | + if (generator.getGeneratorName().equals(alias)) { |
| 85 | + generatorInfo = generator; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + return Optional.ofNullable(generatorInfo); |
| 90 | + } |
| 91 | + |
| 92 | + public void finish() { |
| 93 | + service.shutdownNow(); |
| 94 | + } |
| 95 | + |
| 96 | + public boolean isSubscribeToLive(String alias){ |
| 97 | + LiveConnectionListener listener = liveSubscriptionListenersByAlias.get(alias); |
| 98 | + return listener != null && listener.isConnect(); |
| 99 | + } |
| 100 | +} |
0 commit comments