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
26 private BufferedReader lineReader = null;
28 private Process chimera = null;
30 private Map<String, List<String>> replyLog = null;
32 private Logger logger;
34 private StructureManager structureManager = null;
36 private boolean stopMe = false;
39 * Create a new listener thread to read the responses from Chimera
42 * a handle to the Chimera Process
43 * @param structureManager
44 * a handle to the Chimera structure manager
46 public ListenerThreads(Process chimera, StructureManager structureManager)
48 this.chimera = chimera;
49 this.structureManager = structureManager;
50 replyLog = new HashMap<String, List<String>>();
51 // Get a line-oriented reader
52 InputStream readChan = chimera.getInputStream();
53 lineReader = new BufferedReader(new InputStreamReader(readChan));
54 logger = LoggerFactory
55 .getLogger(ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads.class);
59 * Start the thread running
63 // System.out.println("ReplyLogListener running");
69 } catch (IOException e)
71 logger.warn("UCSF Chimera has exited: " + e.getMessage());
75 if (lineReader != null)
80 } catch (IOException e)
88 public List<String> getResponse(String command)
91 // System.out.println("getResponse: "+command);
92 // TODO do we need a maximum wait time before aborting?
93 while (!replyLog.containsKey(command))
97 Thread.currentThread().sleep(100);
98 } catch (InterruptedException e)
103 synchronized (replyLog)
105 reply = replyLog.get(command);
106 // System.out.println("getResponse ("+command+") = "+reply);
107 replyLog.remove(command);
112 public void clearResponse(String command)
116 Thread.currentThread().sleep(100);
117 } catch (InterruptedException e)
120 if (replyLog.containsKey(command))
122 replyLog.remove(command);
128 * Read input from Chimera
130 * @return a List containing the replies from Chimera
132 private void chimeraRead() throws IOException
140 while ((line = lineReader.readLine()) != null)
142 // System.out.println("From Chimera-->" + line);
143 if (line.startsWith("CMD"))
145 chimeraCommandRead(line.substring(4));
147 else if (line.startsWith("ModelChanged: "))
149 (new ModelUpdater()).start();
151 else if (line.startsWith("SelectionChanged: "))
153 (new SelectionUpdater()).start();
155 else if (line.startsWith("Trajectory residue network info:"))
157 (new NetworkUpdater(line)).start();
163 private void chimeraCommandRead(String command) throws IOException
165 // Generally -- looking for:
169 // We return the text in between
170 List<String> reply = new ArrayList<String>();
171 boolean updateModels = false;
172 boolean updateSelection = false;
173 boolean importNetwork = false;
176 synchronized (replyLog)
178 while ((line = lineReader.readLine()) != null)
180 // System.out.println("From Chimera (" + command + ") -->" + line);
181 if (line.startsWith("CMD"))
183 logger.warn("Got unexpected command from Chimera: " + line);
186 else if (line.startsWith("END"))
190 if (line.startsWith("ModelChanged: "))
194 else if (line.startsWith("SelectionChanged: "))
196 updateSelection = true;
198 else if (line.length() == 0)
202 else if (!line.startsWith("CMD"))
206 else if (line.startsWith("Trajectory residue network info:"))
208 importNetwork = true;
211 replyLog.put(command, reply);
215 (new ModelUpdater()).start();
219 (new SelectionUpdater()).start();
223 (new NetworkUpdater(line)).start();
229 * Model updater thread
231 class ModelUpdater extends Thread
234 public ModelUpdater()
240 structureManager.updateModels();
241 structureManager.modelChanged();
246 * Selection updater thread
248 class SelectionUpdater extends Thread
251 public SelectionUpdater()
259 logger.info("Responding to chimera selection");
260 structureManager.chimeraSelectionChanged();
261 } catch (Exception e)
263 logger.warn("Could not update selection", e);
269 * Selection updater thread
271 class NetworkUpdater extends Thread
276 public NetworkUpdater(String line)
285 // ((TaskManager<?, ?>) structureManager.getService(TaskManager.class))
286 // .execute(new ImportTrajectoryRINTaskFactory(structureManager, line)
287 // .createTaskIterator());
288 } catch (Exception e)
290 logger.warn("Could not import trajectory network", e);
296 * Set a flag that this thread should clean up and exit.
298 public void requestStop()