Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. package com.vexsoftware.votifier.bungee.forwarding.proxy;
  2.  
  3. import com.vexsoftware.votifier.bungee.NuVotifier;
  4. import com.vexsoftware.votifier.bungee.forwarding.ForwardingVoteSource;
  5. import com.vexsoftware.votifier.bungee.forwarding.cache.VoteCache;
  6. import com.vexsoftware.votifier.bungee.forwarding.proxy.client.VotifierProtocol2Encoder;
  7. import com.vexsoftware.votifier.bungee.forwarding.proxy.client.VotifierProtocol2HandshakeHandler;
  8. import com.vexsoftware.votifier.bungee.forwarding.proxy.client.VotifierResponseHandler;
  9. import com.vexsoftware.votifier.model.Vote;
  10. import io.netty.bootstrap.Bootstrap;
  11. import io.netty.channel.ChannelFuture;
  12. import io.netty.channel.ChannelFutureListener;
  13. import io.netty.channel.ChannelInitializer;
  14. import io.netty.channel.nio.NioEventLoopGroup;
  15. import io.netty.channel.socket.nio.NioSocketChannel;
  16. import io.netty.handler.codec.DelimiterBasedFrameDecoder;
  17. import io.netty.handler.codec.Delimiters;
  18. import io.netty.handler.codec.string.StringDecoder;
  19. import io.netty.handler.timeout.ReadTimeoutHandler;
  20. import net.md_5.bungee.api.ChatColor;
  21. import net.md_5.bungee.api.ProxyServer;
  22. import net.md_5.bungee.api.chat.BaseComponent;
  23. import net.md_5.bungee.api.chat.TextComponent;
  24. import net.md_5.bungee.api.connection.ProxiedPlayer;
  25.  
  26. import java.net.InetSocketAddress;
  27. import java.nio.charset.StandardCharsets;
  28. import java.security.Key;
  29. import java.util.List;
  30. import java.util.concurrent.TimeUnit;
  31. import java.util.logging.Level;
  32.  
  33. public class ProxyForwardingVoteSource implements ForwardingVoteSource {
  34. private static final int MAX_RETRIES = 5;
  35. private final NuVotifier plugin;
  36. private final NioEventLoopGroup eventLoopGroup;
  37. private final List<BackendServer> backendServers;
  38. private final VoteCache voteCache;
  39.  
  40. private static final StringDecoder STRING_DECODER = new StringDecoder(StandardCharsets.US_ASCII);
  41.  
  42. public ProxyForwardingVoteSource(NuVotifier plugin, NioEventLoopGroup eventLoopGroup, List<BackendServer> backendServers, VoteCache voteCache) {
  43. this.plugin = plugin;
  44. this.eventLoopGroup = eventLoopGroup;
  45. this.backendServers = backendServers;
  46. this.voteCache = voteCache;
  47. }
  48.  
  49. @Override
  50. public void forward(Vote v) {
  51. for (final BackendServer server : backendServers) {
  52. forwardVote(server, v, 0);
  53. }
  54. }
  55.  
  56. private int fib(int t) {
  57. if (t <= 1) return 1;
  58. return fib(t - 2) + fib(t - 1);
  59. }
  60.  
  61. private void forwardVote(final BackendServer server, final Vote v, final int tries) {
  62. new Bootstrap()
  63. .channel(NioSocketChannel.class)
  64. .group(eventLoopGroup)
  65. .handler(new ChannelInitializer<NioSocketChannel>() {
  66. @Override
  67. protected void initChannel(NioSocketChannel channel) throws Exception {
  68. channel.pipeline().addLast(new DelimiterBasedFrameDecoder(256, true, Delimiters.lineDelimiter()));
  69. channel.pipeline().addLast(new ReadTimeoutHandler(8, TimeUnit.SECONDS));
  70. channel.pipeline().addLast(STRING_DECODER);
  71. channel.pipeline().addLast(new VotifierProtocol2Encoder(server.key));
  72. channel.pipeline().addLast(new VotifierProtocol2HandshakeHandler(v, new VotifierResponseHandler() {
  73. @Override
  74. public void onSuccess() {
  75. if (plugin.isDebug()) {
  76. plugin.getLogger().info("Successfully forwarded vote " + v + " to " + server.address + ".");
  77. }
  78. switch (plugin.getMessaging()) {
  79. case "broadcast":
  80. TextComponent message = new TextComponent ("Teszt broadcast");
  81. message.setColor(ChatColor.LIGHT_PURPLE);
  82. ProxyServer.getInstance().broadcast(message);
  83. break;
  84. case "message":
  85. ProxiedPlayer player = ProxyServer.getInstance().getPlayer(v.getUsername());
  86. if(player != null) {
  87. player.sendMessage(new TextComponent("Teszt message"));
  88. }
  89. default:
  90. break;
  91. }
  92. }
  93.  
  94. @Override
  95. public void onFailure(Throwable error) {
  96. handleFailure(server, v, error, tries);
  97. }
  98. }, plugin));
  99. }
  100. })
  101. .connect(server.address)
  102. .addListener(new ChannelFutureListener() {
  103. @Override
  104. public void operationComplete(ChannelFuture future) throws Exception {
  105. if (!future.isSuccess()) {
  106. handleFailure(server, v, future.cause(), tries);
  107. }
  108. }
  109. });
  110. }
  111.  
  112. private void handleFailure(final BackendServer server, final Vote v, Throwable cause, final int tries) {
  113. int nextDelay = fib(tries + 1);
  114. boolean willRetry = tries < MAX_RETRIES;
  115.  
  116. String msg = "Unable to send vote to " + server.address + ".";
  117. if (willRetry) {
  118. msg += " Will retry sending in " + nextDelay + " second(s).";
  119. } else {
  120. if (voteCache == null) {
  121. msg += " This vote will be lost!";
  122. } else {
  123. voteCache.addToCache(v, server.name);
  124. }
  125. }
  126.  
  127. if (plugin.isDebug()) {
  128. plugin.getLogger().log(Level.SEVERE, msg, cause);
  129. } else {
  130. plugin.getLogger().log(Level.SEVERE, msg);
  131. }
  132.  
  133. if (willRetry) {
  134. plugin.getProxy().getScheduler().schedule(plugin, new Runnable() {
  135. @Override
  136. public void run() {
  137. forwardVote(server, v, tries + 1);
  138. }
  139. }, nextDelay, TimeUnit.SECONDS);
  140. }
  141. }
  142.  
  143. @Override
  144. public void halt() {
  145.  
  146. }
  147.  
  148. public static class BackendServer {
  149. private final String name;
  150. private final InetSocketAddress address;
  151. private final Key key;
  152.  
  153. public BackendServer(String name, InetSocketAddress address, Key key) {
  154. this.name = name;
  155. this.address = address;
  156. this.key = key;
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement