1 package ext.edu.ucsf.rbvi.strucviz2.port;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.HashMap;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
17 /***************************************************
19 **************************************************/
22 * Reply listener thread
24 public class ListenerThreads extends Thread {
25 private InputStream readChan = null;
26 private BufferedReader lineReader = null;
27 private Process chimera = null;
28 private Map<String, List<String>> replyLog = null;
29 private Logger logger;
30 private StructureManager structureManager = null;
33 * Create a new listener thread to read the responses from Chimera
36 * a handle to the Chimera Process
38 * a handle to a List to post the responses to
39 * @param chimeraObject
40 * a handle to the Chimera Object
42 public ListenerThreads(Process chimera, StructureManager structureManager) {
43 this.chimera = chimera;
44 this.structureManager = structureManager;
45 replyLog = new HashMap<String, List<String>>();
46 // Get a line-oriented reader
47 readChan = chimera.getInputStream();
48 lineReader = new BufferedReader(new InputStreamReader(readChan));
49 logger = LoggerFactory.getLogger(ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads.class);
53 * Start the thread running
56 // System.out.println("ReplyLogListener running");
60 } catch (IOException e) {
61 logger.warn("UCSF Chimera has exited: " + e.getMessage());
67 public List<String> getResponse(String command) {
69 // System.out.println("getResponse: "+command);
70 // TODO do we need a maximum wait time before aborting?
71 while (!replyLog.containsKey(command)) {
73 Thread.currentThread().sleep(100);
74 } catch (InterruptedException e) {
78 synchronized (replyLog) {
79 reply = replyLog.get(command);
80 // System.out.println("getResponse ("+command+") = "+reply);
81 replyLog.remove(command);
86 public void clearResponse(String command) {
88 Thread.currentThread().sleep(100);
89 } catch (InterruptedException e) {
91 if (replyLog.containsKey(command))
93 replyLog.remove(command);
99 * Read input from Chimera
101 * @return a List containing the replies from Chimera
103 private void chimeraRead() throws IOException {
110 while ((line = lineReader.readLine()) != null) {
111 // System.out.println("From Chimera-->" + line);
112 if (line.startsWith("CMD")) {
113 chimeraCommandRead(line.substring(4));
114 } else if (line.startsWith("ModelChanged: ")) {
115 (new ModelUpdater()).start();
116 } else if (line.startsWith("SelectionChanged: ")) {
117 (new SelectionUpdater()).start();
118 } else if (line.startsWith("Trajectory residue network info:")) {
119 (new NetworkUpdater(line)).start();
125 private void chimeraCommandRead(String command) throws IOException {
126 // Generally -- looking for:
130 // We return the text in between
131 List<String> reply = new ArrayList<String>();
132 boolean updateModels = false;
133 boolean updateSelection = false;
134 boolean importNetwork = false;
137 synchronized (replyLog) {
138 while ((line = lineReader.readLine()) != null) {
139 // System.out.println("From Chimera (" + command + ") -->" + line);
140 if (line.startsWith("CMD")) {
141 logger.warn("Got unexpected command from Chimera: " + line);
143 } else if (line.startsWith("END")) {
146 if (line.startsWith("ModelChanged: ")) {
148 } else if (line.startsWith("SelectionChanged: ")) {
149 updateSelection = true;
150 } else if (line.length() == 0) {
152 } else if (!line.startsWith("CMD")) {
154 } else if (line.startsWith("Trajectory residue network info:")) {
155 importNetwork = true;
158 replyLog.put(command, reply);
162 (new ModelUpdater()).start();
166 (new SelectionUpdater()).start();
169 (new NetworkUpdater(line)).start();
175 * Model updater thread
177 class ModelUpdater extends Thread {
179 public ModelUpdater() {
183 structureManager.updateModels();
184 structureManager.modelChanged();
189 * Selection updater thread
191 class SelectionUpdater extends Thread {
193 public SelectionUpdater() {
198 logger.info("Responding to chimera selection");
199 structureManager.chimeraSelectionChanged();
200 } catch (Exception e) {
201 logger.warn("Could not update selection", e);
207 * Selection updater thread
209 class NetworkUpdater extends Thread {
213 public NetworkUpdater(String line) {
219 // ((TaskManager<?, ?>) structureManager.getService(TaskManager.class))
220 // .execute(new ImportTrajectoryRINTaskFactory(structureManager, line)
221 // .createTaskIterator());
222 } catch (Exception e) {
223 logger.warn("Could not import trajectory network", e);