source: java/main/src/main/java/com/framsticks/running/ExternalProcess.java @ 101

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 5.5 KB
Line 
1package com.framsticks.running;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.io.OutputStreamWriter;
8import java.io.PrintWriter;
9import java.util.ArrayList;
10import java.util.List;
11
12
13import org.apache.logging.log4j.Logger;
14import org.apache.logging.log4j.LogManager;
15
16import com.framsticks.core.ValueChange;
17import com.framsticks.params.EventListener;
18import com.framsticks.params.EventListeners;
19import com.framsticks.params.ParamFlags;
20import com.framsticks.params.annotations.AutoAppendAnnotation;
21import com.framsticks.params.annotations.FramsClassAnnotation;
22import com.framsticks.params.annotations.ParamAnnotation;
23import com.framsticks.util.FramsticksException;
24import com.framsticks.util.Misc;
25import com.framsticks.util.dispatching.AbstractJoinable;
26import com.framsticks.util.dispatching.Dispatching;
27import com.framsticks.util.dispatching.Joinable;
28import com.framsticks.util.dispatching.JoinableParent;
29import com.framsticks.util.dispatching.JoinableState;
30import com.framsticks.util.dispatching.RunAt;
31import com.framsticks.util.dispatching.Thread;
32import com.framsticks.util.dispatching.ThrowExceptionHandler;
33import com.framsticks.util.io.Encoding;
34
35@FramsClassAnnotation
36public class ExternalProcess extends AbstractJoinable implements JoinableParent {
37        private static final Logger log = LogManager.getLogger(ExternalProcess.class);
38
39        protected List<String> arguments = new ArrayList<>();
40        protected Process process;
41        protected final ProcessBuilder builder = new ProcessBuilder();
42        protected Thread<ExternalProcess> readerThread = new Thread<ExternalProcess>();
43
44        protected PrintWriter input;
45        protected BufferedReader output;
46        protected Integer exitCode;
47        protected String echoInput;
48
49        protected final EventListeners<ValueChange> listeners = new EventListeners<>();
50
51        @AutoAppendAnnotation
52        @ParamAnnotation(id = "line_output")
53        public void addOutputListener(EventListener<ValueChange> listener) {
54                synchronized (listeners) {
55                        listeners.add(listener);
56                }
57        }
58
59        @ParamAnnotation(id = "line_output")
60        public void removeOutputListener(EventListener<ValueChange> listener) {
61                synchronized (listeners) {
62                        listeners.remove(listener);
63                }
64        }
65
66        /**
67         *
68         */
69        public ExternalProcess() {
70                super();
71                setName("process");
72                arguments.add(null);
73                builder.redirectErrorStream(true);
74        }
75
76        /**
77         * @return the command
78         */
79        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
80        public String getCommand() {
81                return arguments.get(0);
82        }
83
84        /**
85         * @param command the command to set
86         */
87        @ParamAnnotation
88        public void setCommand(String command) {
89                arguments.set(0, command);
90        }
91
92        protected void readerTask() {
93
94                log.debug("reading output from " + this);
95                String line;
96                try {
97                        try {
98                                while ((line = output.readLine()) != null) {
99                                        log.trace("read line: {}", line);
100                                        synchronized (listeners) {
101                                                listeners.actionForAll(new ValueChange(line));
102                                        }
103                                }
104                        } catch (IOException e) {
105                                throw new FramsticksException().msg("failed to read line from output of process").cause(e);
106                        }
107                        try {
108                                exitCode = process.waitFor();
109                        } catch (InterruptedException e) {
110                                throw new FramsticksException().msg("failed to wait for process").cause(e);
111                        }
112                        log.debug("process ended {}", this);
113                        // process = null;
114                } catch (FramsticksException e) {
115                        log.error("exception caught in process {}", this, e);
116                }
117                interruptJoinable();
118                // finish();
119        }
120
121        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
122        public void setDirectory(String directory) {
123                builder.directory(new File(directory));
124        }
125
126        @ParamAnnotation
127        public String getDirectory() {
128                return builder.directory() != null ? builder.directory().getName() : ".";
129        }
130
131        @Override
132        protected void joinableStart() {
133                log.debug("running process with arguments: {}", arguments);
134                builder.command(arguments);
135                try {
136                        process = builder.start();
137                        input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset()));
138                        output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset()));
139
140                } catch (IOException e) {
141                        throw new FramsticksException().msg("failed to start process").cause(e);
142                }
143
144                readerThread.dispatch(new RunAt<ExternalProcess>(ThrowExceptionHandler.getInstance()) {
145
146                        @Override
147                        protected void runAt() {
148                                readerTask();
149                        }
150
151                });
152                Dispatching.use(readerThread, this);
153
154                if (echoInput != null) {
155                        input.println(echoInput);
156                        input.flush();
157                }
158        }
159
160        @Override
161        public String toString() {
162                return getName() + "[" + Misc.returnNotNull(getCommand(), "?") + "]";
163        }
164
165        /**
166         * @return the input
167         */
168        public PrintWriter getInput() {
169                return input;
170        }
171
172        /**
173         * @return the echoInput
174         */
175        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
176        public String getEchoInput() {
177                return echoInput;
178        }
179
180        /**
181         * @param echoInput the echoInput to set
182         */
183        @ParamAnnotation
184        public void setEchoInput(String echoInput) {
185                this.echoInput = echoInput;
186        }
187
188        @Override
189        protected void joinableInterrupt() {
190                process.destroy();
191                Dispatching.drop(readerThread, this);
192                // finish();
193        }
194
195        @Override
196        @ParamAnnotation(flags = ParamFlags.USERREADONLY)
197        public String getName() {
198                return readerThread.getName();
199        }
200
201        /**
202         * @param name the name to set
203         */
204        @ParamAnnotation
205        public void setName(String name) {
206                readerThread.setName(name);
207        }
208
209        @Override
210        protected void joinableFinish() {
211
212        }
213
214        @Override
215        protected void joinableJoin() throws InterruptedException {
216                Dispatching.join(readerThread);
217        }
218
219        @Override
220        public void childChangedState(Joinable joinable, JoinableState state) {
221                proceedToState(state);
222        }
223
224
225
226}
Note: See TracBrowser for help on using the repository browser.