1 | package com.framsticks.communication; |
---|
2 | |
---|
3 | import java.util.regex.Matcher; |
---|
4 | import java.util.regex.Pattern; |
---|
5 | |
---|
6 | import com.framsticks.communication.queries.*; |
---|
7 | import com.framsticks.util.FramsticksException; |
---|
8 | import com.framsticks.util.lang.Pair; |
---|
9 | import com.framsticks.util.lang.Strings; |
---|
10 | |
---|
11 | /** |
---|
12 | * Class stores information about query sent to manager. |
---|
13 | */ |
---|
14 | public abstract class Request { |
---|
15 | |
---|
16 | public abstract String getCommand(); |
---|
17 | |
---|
18 | protected abstract StringBuilder construct(StringBuilder buffer); |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | public static void quoteValue(StringBuilder builder, String value) { |
---|
24 | String quote = ((value.indexOf(' ') > 0) || (value.length() == 0) ? "\"" : ""); |
---|
25 | builder.append(quote).append(value).append(quote); |
---|
26 | } |
---|
27 | |
---|
28 | public static Request parse(CharSequence type, CharSequence rest) { |
---|
29 | final Request request = Request.createRequestByTypeString(type.toString()); |
---|
30 | request.parseRest(rest); |
---|
31 | return request; |
---|
32 | } |
---|
33 | |
---|
34 | public static Request createRequestByTypeString(String type) { |
---|
35 | switch (type) { |
---|
36 | case "get": return new GetRequest(); |
---|
37 | case "set": return new SetRequest(); |
---|
38 | case "info": return new InfoRequest(); |
---|
39 | case "call": return new CallRequest(); |
---|
40 | case "reg": return new RegisterRequest(); |
---|
41 | case "use": return new UseRequest(); |
---|
42 | case "version": return new VersionRequest(); |
---|
43 | } |
---|
44 | throw new FramsticksException().msg("unknown request type").arg("type", type); |
---|
45 | } |
---|
46 | |
---|
47 | public abstract CharSequence parseRest(CharSequence rest); |
---|
48 | |
---|
49 | @Override |
---|
50 | public String toString() { |
---|
51 | // return getCommand(); |
---|
52 | return "request " + stringRepresentation(); |
---|
53 | } |
---|
54 | |
---|
55 | public String stringRepresentation() { |
---|
56 | return construct(new StringBuilder().append(getCommand()).append(" ")).toString(); |
---|
57 | } |
---|
58 | |
---|
59 | public static final Pattern EVENT_PATTERN = Pattern.compile("^\\s*(\\S+)\\s*(\\S+)"); |
---|
60 | |
---|
61 | public static final Pattern STRING_BREAKER_PATTERN = Pattern.compile("^\\s*(?:(?:\\\"([^\"]*)\\\")|([^ ]+))\\s*(.*)$"); |
---|
62 | public static final Pattern IDENTIFIER_BREAKER_PATTERN = Pattern.compile("^\\s*([^ ]+)\\s*(.*)$"); |
---|
63 | |
---|
64 | // public static Matcher match(Pattern pattern, CharSequence line) { |
---|
65 | // Matcher matcher = pattern.matcher(line); |
---|
66 | // if (!matcher.matches()) { |
---|
67 | // return null; |
---|
68 | // // throw new FramsticksException().msg("failed to take").arg("pattern", pattern).arg("line", line); |
---|
69 | // } |
---|
70 | // return matcher; |
---|
71 | // } |
---|
72 | |
---|
73 | public static Pair<CharSequence, CharSequence> takeIdentifier(CharSequence line) { |
---|
74 | Matcher matcher = IDENTIFIER_BREAKER_PATTERN.matcher(line); |
---|
75 | if (!matcher.matches()) { |
---|
76 | return null; |
---|
77 | } |
---|
78 | return new Pair<CharSequence, CharSequence>(line.subSequence(matcher.start(1), matcher.end(1)), line.subSequence(matcher.start(2), matcher.end(2))); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | public static Pair<CharSequence, CharSequence> takeString(CharSequence line) { |
---|
83 | Matcher matcher = STRING_BREAKER_PATTERN.matcher(line); |
---|
84 | if (!matcher.matches()) { |
---|
85 | return null; |
---|
86 | } |
---|
87 | assert ((matcher.start(1) == -1) != (matcher.start(2) == -1)); |
---|
88 | return new Pair<CharSequence, CharSequence>(Strings.takeGroup(line, matcher, (matcher.start(1) != -1 ? 1 : 2)), Strings.takeGroup(line, matcher, 3)); |
---|
89 | } |
---|
90 | |
---|
91 | protected static final Pattern REQUEST_ID_BREAKER_PATTERN = Pattern.compile("^\\s*(-?[0-9]+)\\s*(.*)$"); |
---|
92 | |
---|
93 | protected final static Pair<Integer, CharSequence> takeRequestId(boolean withId, CharSequence line) { |
---|
94 | if (withId) { |
---|
95 | Matcher matcher = REQUEST_ID_BREAKER_PATTERN.matcher(line); |
---|
96 | if (!matcher.matches()) { |
---|
97 | return null; |
---|
98 | } |
---|
99 | return new Pair<Integer, CharSequence>(Integer.valueOf(Strings.takeGroup(line, matcher, 1).toString()), Strings.takeGroup(line, matcher, 2)); |
---|
100 | } |
---|
101 | return new Pair<Integer, CharSequence>(null, line); |
---|
102 | } |
---|
103 | |
---|
104 | public static StringBuilder quoteArgumentIfNeeded(StringBuilder builder, Object argument) { |
---|
105 | String r = argument.toString(); |
---|
106 | if (r.indexOf(' ') != -1) { |
---|
107 | builder.append('"').append(r).append('"'); |
---|
108 | } else { |
---|
109 | builder.append(r); |
---|
110 | } |
---|
111 | return builder; |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|