From a22e084f960f197e842676c5f675fd62e9a6144f Mon Sep 17 00:00:00 2001 From: DaMachinator Date: Sat, 20 Jan 2018 15:10:05 -0500 Subject: [PATCH] Add commands to start and stop the bridge. --- .../bridge/IncomingMessageHandler.java | 3 +- .../bridge/OutgoingMessageHandler.java | 5 +- .../command/BridgeCommand.java | 60 +++++++++++++++++++ .../eventhandlers/FMLEventHandler.java | 2 + .../eventhandlers/UserActionHandler.java | 1 + 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/main/java/arcanitor/civilengineering/command/BridgeCommand.java diff --git a/src/main/java/arcanitor/civilengineering/bridge/IncomingMessageHandler.java b/src/main/java/arcanitor/civilengineering/bridge/IncomingMessageHandler.java index 6ab13dc..0d1f7b2 100644 --- a/src/main/java/arcanitor/civilengineering/bridge/IncomingMessageHandler.java +++ b/src/main/java/arcanitor/civilengineering/bridge/IncomingMessageHandler.java @@ -22,9 +22,10 @@ public class IncomingMessageHandler implements Runnable { new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = input.readLine( )) != null) { + if(CivilEngineering.incomingMessageThread.isInterrupted()) throw new InterruptedException(); + CivilEngineering.logger.debug(line); ServerChatWriter.messages.add(ApiMessage.decode(line)); - Thread.sleep(1); } } catch (Exception e) { diff --git a/src/main/java/arcanitor/civilengineering/bridge/OutgoingMessageHandler.java b/src/main/java/arcanitor/civilengineering/bridge/OutgoingMessageHandler.java index 889382f..a770d37 100644 --- a/src/main/java/arcanitor/civilengineering/bridge/OutgoingMessageHandler.java +++ b/src/main/java/arcanitor/civilengineering/bridge/OutgoingMessageHandler.java @@ -26,8 +26,8 @@ public class OutgoingMessageHandler implements Runnable { CivilEngineering.logger.error("Server returned error "+response); break; } - sleep(50); } + sleep(50); } } catch (Exception e) { if (e instanceof InterruptedException) { @@ -44,7 +44,7 @@ public class OutgoingMessageHandler implements Runnable { public int postMessage(ApiMessage message) throws IOException { //open a connection - URL url = new URL(Config.connectURL + "/api/messages"); + URL url = new URL(Config.connectURL + "/api/message"); URLConnection urlConnection = url.openConnection(); HttpURLConnection connection = (HttpURLConnection)urlConnection; @@ -68,7 +68,6 @@ public class OutgoingMessageHandler implements Runnable { post.close(); int response = connection.getResponseCode(); - CivilEngineering.logger.info(response); connection.disconnect(); diff --git a/src/main/java/arcanitor/civilengineering/command/BridgeCommand.java b/src/main/java/arcanitor/civilengineering/command/BridgeCommand.java new file mode 100644 index 0000000..5b5448b --- /dev/null +++ b/src/main/java/arcanitor/civilengineering/command/BridgeCommand.java @@ -0,0 +1,60 @@ +package arcanitor.civilengineering.command; + +import arcanitor.civilengineering.CivilEngineering; +import com.google.common.collect.Lists; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.command.WrongUsageException; +import net.minecraft.server.MinecraftServer; + +import javax.annotation.Nonnull; +import java.util.List; + +public class BridgeCommand extends CommandBase { + private final List aliases; + + public BridgeCommand(){ + aliases = Lists.newArrayList(CivilEngineering.MODID,"bridge","BRIDGE"); + } + + @Override + @Nonnull + public String getName() { + return "bridge"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "bridge "; + } + + @Override + @Nonnull + public List getAliases() { + return aliases; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) /*throws CommandException*/ { + if (args.length < 1) { + //throw new WrongUsageException("") + return; + } + String cmd = args[0]; + if (cmd.toLowerCase().equals("connect")) { + if(!CivilEngineering.incomingMessageThread.isAlive()) { + CivilEngineering.incomingMessageThread.start(); + } + if(!CivilEngineering.outgoingMessageThread.isAlive()){ + CivilEngineering.outgoingMessageThread.start(); + } + + } else if (cmd.toLowerCase().equals("disconnect")) { + CivilEngineering.incomingMessageThread.interrupt(); + CivilEngineering.outgoingMessageThread.interrupt(); + } + } + + +} diff --git a/src/main/java/arcanitor/civilengineering/eventhandlers/FMLEventHandler.java b/src/main/java/arcanitor/civilengineering/eventhandlers/FMLEventHandler.java index ca63bd7..b08ac6b 100644 --- a/src/main/java/arcanitor/civilengineering/eventhandlers/FMLEventHandler.java +++ b/src/main/java/arcanitor/civilengineering/eventhandlers/FMLEventHandler.java @@ -3,6 +3,7 @@ package arcanitor.civilengineering.eventhandlers; import arcanitor.civilengineering.CivilEngineering; import arcanitor.civilengineering.Config; +import arcanitor.civilengineering.command.BridgeCommand; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @@ -26,6 +27,7 @@ public class FMLEventHandler { } } public static void serverStarting(FMLServerStartingEvent event) { + event.registerServerCommand(new BridgeCommand()); CivilEngineering.incomingMessageThread.start(); CivilEngineering.outgoingMessageThread.start(); } diff --git a/src/main/java/arcanitor/civilengineering/eventhandlers/UserActionHandler.java b/src/main/java/arcanitor/civilengineering/eventhandlers/UserActionHandler.java index 4a278fa..11114fb 100644 --- a/src/main/java/arcanitor/civilengineering/eventhandlers/UserActionHandler.java +++ b/src/main/java/arcanitor/civilengineering/eventhandlers/UserActionHandler.java @@ -23,6 +23,7 @@ public class UserActionHandler { for(String word:args) { message = message + " " + word; } + message = message.trim(); OutgoingMessageHandler.queue.add(new ApiMessage(user,message,"user_action")); }