source: java/main/src/main/java/com/framsticks/communication/Connection.java @ 90

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

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 5.3 KB
Line 
1package com.framsticks.communication;
2
3import com.framsticks.util.FramsticksException;
4import com.framsticks.util.io.Encoding;
5import com.framsticks.util.lang.Pair;
6import org.apache.log4j.Logger;
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.io.OutputStreamWriter;
11import java.io.PrintWriter;
12import java.net.Socket;
13import java.net.SocketTimeoutException;
14import java.util.regex.Matcher;
15import java.util.regex.Pattern;
16
17import com.framsticks.util.dispatching.AbstractJoinable;
18import com.framsticks.util.dispatching.Dispatching;
19import com.framsticks.util.dispatching.Joinable;
20import com.framsticks.util.dispatching.JoinableCollection;
21import com.framsticks.util.dispatching.JoinableParent;
22import com.framsticks.util.dispatching.JoinableState;
23import com.framsticks.util.dispatching.RunAt;
24import com.framsticks.util.dispatching.Thread;
25
26public abstract class Connection extends AbstractJoinable implements JoinableParent {
27
28        protected final static Logger log = Logger.getLogger(Connection.class);
29
30        protected PrintWriter output = null;
31        protected BufferedReader input = null;
32
33        protected Socket socket = null;
34
35        protected volatile boolean connected = false;
36
37        public boolean requestIdEnabled = false;
38
39        protected int protocolVersion = -1;
40
41        public String getAddress() {
42                return address;
43        }
44        protected final String address;
45
46        protected final Thread<Connection> senderThread = new Thread<>();
47        protected final Thread<Connection> receiverThread = new Thread<>();
48        protected final JoinableCollection<Thread<Connection>> threads = new JoinableCollection<>(true);
49
50        /**
51         *
52         */
53        public Connection(String address) {
54                this.address = address;
55                threads.add(senderThread);
56                threads.add(receiverThread);
57        }
58        public boolean isConnected() {
59                return connected;
60        }
61
62
63        protected static final String ARGUMENT_PATTERN_FRAGMENT = "((?:\\S+)|(?:\"[^\"]*\"))";
64        protected static final Pattern requestIdEnabledPattern = Pattern.compile("^\\s*([0-9]+)(?:\\s+" + ARGUMENT_PATTERN_FRAGMENT + ")?\\n$");
65        protected static final Pattern requestIDisabledPattern = Pattern.compile("^\\s*" + ARGUMENT_PATTERN_FRAGMENT + "?\\n$");
66        protected static final Pattern eventPattern = Pattern.compile("^\\s*(\\S+)\\s*(\\S+)\\n");
67
68
69        protected final Pair<Integer, String> parseRest(String rest) {
70                Matcher matcher = (requestIdEnabled ? requestIdEnabledPattern : requestIDisabledPattern).matcher(rest);
71                if (!matcher.matches()) {
72                        log.fatal("unmatched first line of input: " + rest);
73                        return null;
74                }
75                return new Pair<Integer, String>(requestIdEnabled ? Integer.parseInt(matcher.group(1)) : null, matcher.group(requestIdEnabled ? 2 : 1));
76        }
77
78        static final int BUFFER_LENGTH = 1024;
79
80        int readChars = 0;
81        int iterator = 0;
82        int bufferStart = 0;
83        char[] readBuffer = new char[BUFFER_LENGTH];
84
85        protected String getLine() {
86                StringBuilder lineBuffer = new StringBuilder();
87                try {
88                        while (!Thread.interrupted()) {
89                                while (iterator < readChars) {
90                                        if (readBuffer[iterator] != '\n') {
91                                                ++iterator;
92                                                continue;
93                                        }
94                                        lineBuffer.append(readBuffer, bufferStart, iterator - bufferStart + 1);
95                                        ++iterator;
96                                        bufferStart = iterator;
97                                        return lineBuffer.toString();
98                                }
99                                lineBuffer.append(readBuffer, bufferStart, readChars - bufferStart);
100
101                                readChars = 0;
102                                while (readChars == 0) {
103                                        try {
104                                                readChars = input.read(readBuffer);
105                                        } catch (SocketTimeoutException ignored) {
106                                                //timeout - continue
107                                        }
108                                }
109                                iterator = 0;
110                                bufferStart = 0;
111                        }
112                        throw new InterruptedException();
113                } catch (Exception e) {
114                        throw new FramsticksException().msg("failed to read line").cause(e);
115                }
116        }
117
118        protected abstract void receiverThreadRoutine();
119
120        protected void setUpThread(Thread<Connection> thread, String name) {
121                thread.setName("connection thread " + address + " " + name);
122        }
123
124        protected void runThreads() {
125                try {
126                        output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Encoding.getFramsticksCharset()), true);
127                        input = new BufferedReader(new InputStreamReader(socket.getInputStream(), Encoding.getFramsticksCharset()));
128                } catch (IOException e) {
129                        log.error("buffer creation failure");
130                        return;
131                }
132
133                setUpThread(senderThread, "sender");
134                setUpThread(receiverThread, "receiver");
135                Dispatching.use(threads, this);
136
137                receiverThread.dispatch(new RunAt<Connection>() {
138                        @Override
139                        public void run() {
140                                receiverThreadRoutine();
141                        }
142                });
143
144        }
145
146        /**
147         * Returns Query associated with query getId.
148         *
149         * @return Query associated with query getId.
150         */
151        public int getProtocolVersion() {
152                return protocolVersion;
153        }
154
155        @Override
156        protected void joinableInterrupt() {
157                protocolVersion = -1;
158
159                connected = false;
160                Dispatching.drop(threads, this);
161
162                // finish();
163        }
164
165        @Override
166        protected void joinableFinish() {
167                try {
168                        if (output != null) {
169                                output.close();
170                                output = null;
171                        }
172
173                        if (input != null) {
174                                input.close();
175                                input = null;
176                        }
177
178
179                        if (socket != null) {
180                                socket.close();
181                                socket = null;
182                        }
183                } catch (Exception e) {
184                        log.error("failed to stop connection: ", e);
185                }
186                log.debug("connection closed");
187        }
188
189        @Override
190        public void childChangedState(Joinable joinable, JoinableState state) {
191                proceedToState(state);
192        }
193
194        @Override
195        protected void joinableJoin() throws InterruptedException {
196                Dispatching.join(threads);
197
198        }
199
200}
Note: See TracBrowser for help on using the repository browser.