diff options
Diffstat (limited to 'juick-console/src/main/java/com/juick/console/ConsoleApp.java')
-rw-r--r-- | juick-console/src/main/java/com/juick/console/ConsoleApp.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/juick-console/src/main/java/com/juick/console/ConsoleApp.java b/juick-console/src/main/java/com/juick/console/ConsoleApp.java new file mode 100644 index 00000000..9a061bb6 --- /dev/null +++ b/juick-console/src/main/java/com/juick/console/ConsoleApp.java @@ -0,0 +1,56 @@ +package com.juick.console; + +import com.juick.User; +import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolReply; +import com.juick.service.UserService; +import jline.console.ConsoleReader; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; + +import javax.inject.Inject; +import java.io.IOException; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; + +/** + * Created by vitalyster on 19.12.2016. + */ +public class ConsoleApp { + @Inject + JuickProtocol juickProtocol; + @Inject + Environment env; + @Inject + UserService userService; + + public ConsoleApp(ApplicationContext context) { + juickProtocol = context.getBean(JuickProtocol.class); + } + public void start() { + //jline.TerminalFactory.registerFlavor(jline.TerminalFactory.Flavor.WINDOWS, UnsupportedTerminal.class); + try (ConsoleReader reader = new ConsoleReader()) { + PrintWriter out = new PrintWriter(reader.getOutput()); + String uname = env.getProperty("uname", "ugnich"); + User user = userService.getUserByName(uname); + if (user != null) { + reader.setPrompt(String.format("%s>", user.getName())); + out.println(String.format("logged in as @%s", user.getName())); + out.flush(); + String input; + while ((input = reader.readLine()) != null) { + ProtocolReply reply = juickProtocol.getReply(user, input); + out.println(reply.getResult()); + out.flush(); + } + } + out.println("uname missing or invalid"); + } catch (IOException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} |