aboutsummaryrefslogtreecommitdiff
path: root/juick-console/src/main/java/com/juick/console/ConsoleApp.java
blob: bfa3eae38397ba2d7ff1316c493e774f6f3c76dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {

    private static final Logger logger = LoggerFactory.getLogger(ConsoleApp.class);

    @Inject
    JuickProtocol juickProtocol;
    @Inject
    Environment env;
    @Inject
    UserService userService;

    public ConsoleApp(ApplicationContext context) {
        juickProtocol = context.getBean(JuickProtocol.class);
    }
    public void start() throws IOException {
        Terminal terminal = TerminalBuilder.builder()
                .system(true)
                .nativeSignals(true)
                .jna(true)
                .build();
        LineReader reader = LineReaderBuilder.builder()
                .terminal(terminal)
                .build();
        PrintWriter out = terminal.writer();
        String uname = env.getProperty("uname", "ugnich");
        User user = userService.getUserByName(uname);
        if (user != null) {
            String prompt = String.format("@%s>", user.getName());
            while (true) {
                try {
                    String input = reader.readLine(prompt, null, null);
                    if (input == null) {
                        logger.error("invalid terminal");
                        break;
                    }
                    ProtocolReply reply = juickProtocol.getReply(user, input);
                    out.println(reply.getResult());
                    out.flush();
                } catch (UserInterruptException e) {
                    logger.debug("user interrupt");
                } catch (EndOfFileException e) {
                    logger.debug("app exit");
                    break;
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    logger.warn("protocol exception", e);
                }
            }
        } else {
            out.println("uname missing or invalid");
        }
    }
}