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.*;
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 while (!replyLog.containsKey(command)) {
72 Thread.currentThread().sleep(100);
73 } catch (InterruptedException e) {
77 synchronized (replyLog) {
78 reply = replyLog.get(command);
79 // System.out.println("getResponse ("+command+") = "+reply);
80 replyLog.remove(command);
85 public void clearResponse(String command) {
87 Thread.currentThread().sleep(100);
88 } catch (InterruptedException e) {
90 if (replyLog.containsKey(command))
91 replyLog.remove(command);
96 * Read input from Chimera
98 * @return a List containing the replies from Chimera
100 private void chimeraRead() throws IOException {
105 while ((line = lineReader.readLine()) != null) {
106 // System.out.println("From Chimera-->" + line);
107 if (line.startsWith("CMD")) {
108 chimeraCommandRead(line.substring(4));
109 } else if (line.startsWith("ModelChanged: ")) {
110 (new ModelUpdater()).start();
111 } else if (line.startsWith("SelectionChanged: ")) {
112 (new SelectionUpdater()).start();
113 } else if (line.startsWith("Trajectory residue network info:")) {
114 (new NetworkUpdater(line)).start();
120 private void chimeraCommandRead(String command) throws IOException {
121 // Generally -- looking for:
125 // We return the text in between
126 List<String> reply = new ArrayList<String>();
127 boolean updateModels = false;
128 boolean updateSelection = false;
129 boolean importNetwork = false;
132 synchronized (replyLog) {
133 while ((line = lineReader.readLine()) != null) {
134 // System.out.println("From Chimera (" + command + ") -->" + line);
135 if (line.startsWith("CMD")) {
136 logger.warn("Got unexpected command from Chimera: " + line);
138 } else if (line.startsWith("END")) {
141 if (line.startsWith("ModelChanged: ")) {
143 } else if (line.startsWith("SelectionChanged: ")) {
144 updateSelection = true;
145 } else if (line.length() == 0) {
147 } else if (!line.startsWith("CMD")) {
149 } else if (line.startsWith("Trajectory residue network info:")) {
150 importNetwork = true;
153 replyLog.put(command, reply);
156 (new ModelUpdater()).start();
158 (new SelectionUpdater()).start();
160 (new NetworkUpdater(line)).start();
166 * Model updater thread
168 class ModelUpdater extends Thread {
170 public ModelUpdater() {
174 structureManager.updateModels();
175 structureManager.modelChanged();
180 * Selection updater thread
182 class SelectionUpdater extends Thread {
184 public SelectionUpdater() {
189 logger.info("Responding to chimera selection");
190 structureManager.chimeraSelectionChanged();
191 } catch (Exception e) {
192 logger.warn("Could not update selection", e);
198 * Selection updater thread
200 class NetworkUpdater extends Thread {
204 public NetworkUpdater(String line) {
210 // ((TaskManager<?, ?>) structureManager.getService(TaskManager.class))
211 // .execute(new ImportTrajectoryRINTaskFactory(structureManager, line)
212 // .createTaskIterator());
213 } catch (Exception e) {
214 logger.warn("Could not import trajectory network", e);