source: java/main/src/main/java/com/framsticks/communication/Request.java @ 96

Last change on this file since 96 was 96, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

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