3 * Copyright (c) 2006 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions, and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * 3. Redistributions must acknowledge that this software was
16 * originally developed by the UCSF Computer Graphics Laboratory
17 * under support by the NIH National Center for Research Resources,
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package ext.edu.ucsf.rbvi.strucviz2.port;
35 import java.io.BufferedReader;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
49 /***************************************************
51 **************************************************/
54 * Reply listener thread
56 public class ListenerThreads extends Thread
58 private BufferedReader lineReader = null;
60 private Process chimera = null;
62 private Map<String, List<String>> replyLog = null;
64 private Logger logger;
66 private StructureManager structureManager = null;
68 private boolean stopMe = false;
71 * Create a new listener thread to read the responses from Chimera
74 * a handle to the Chimera Process
75 * @param structureManager
76 * a handle to the Chimera structure manager
78 public ListenerThreads(Process chimera, StructureManager structureManager)
80 this.chimera = chimera;
81 this.structureManager = structureManager;
82 replyLog = new HashMap<String, List<String>>();
83 // Get a line-oriented reader
84 InputStream readChan = chimera.getInputStream();
85 lineReader = new BufferedReader(new InputStreamReader(readChan));
86 logger = LoggerFactory
87 .getLogger(ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads.class);
91 * Start the thread running
95 // System.out.println("ReplyLogListener running");
101 } catch (IOException e)
103 logger.warn("UCSF Chimera has exited: " + e.getMessage());
107 if (lineReader != null)
112 } catch (IOException e)
120 public List<String> getResponse(String command)
123 // System.out.println("getResponse: "+command);
124 // TODO do we need a maximum wait time before aborting?
125 while (!replyLog.containsKey(command))
129 Thread.currentThread().sleep(100);
130 } catch (InterruptedException e)
135 synchronized (replyLog)
137 reply = replyLog.get(command);
138 // System.out.println("getResponse ("+command+") = "+reply);
139 replyLog.remove(command);
144 public void clearResponse(String command)
148 Thread.currentThread().sleep(100);
149 } catch (InterruptedException e)
152 if (replyLog.containsKey(command))
154 replyLog.remove(command);
160 * Read input from Chimera
162 * @return a List containing the replies from Chimera
164 private void chimeraRead() throws IOException
172 while ((line = lineReader.readLine()) != null)
174 // System.out.println("From Chimera-->" + line);
175 if (line.startsWith("CMD"))
177 chimeraCommandRead(line.substring(4));
179 else if (line.startsWith("ModelChanged: "))
181 (new ModelUpdater()).start();
183 else if (line.startsWith("SelectionChanged: "))
185 (new SelectionUpdater()).start();
187 else if (line.startsWith("Trajectory residue network info:"))
189 (new NetworkUpdater(line)).start();
195 private void chimeraCommandRead(String command) throws IOException
197 // Generally -- looking for:
201 // We return the text in between
202 List<String> reply = new ArrayList<String>();
203 boolean updateModels = false;
204 boolean updateSelection = false;
205 boolean importNetwork = false;
208 synchronized (replyLog)
210 while ((line = lineReader.readLine()) != null)
212 // System.out.println("From Chimera (" + command + ") -->" + line);
213 if (line.startsWith("CMD"))
215 logger.warn("Got unexpected command from Chimera: " + line);
218 else if (line.startsWith("END"))
222 if (line.startsWith("ModelChanged: "))
226 else if (line.startsWith("SelectionChanged: "))
228 updateSelection = true;
230 else if (line.length() == 0)
234 else if (!line.startsWith("CMD"))
238 else if (line.startsWith("Trajectory residue network info:"))
240 importNetwork = true;
243 replyLog.put(command, reply);
247 (new ModelUpdater()).start();
251 (new SelectionUpdater()).start();
255 (new NetworkUpdater(line)).start();
261 * Model updater thread
263 class ModelUpdater extends Thread
266 public ModelUpdater()
272 structureManager.updateModels();
273 structureManager.modelChanged();
278 * Selection updater thread
280 class SelectionUpdater extends Thread
283 public SelectionUpdater()
291 logger.info("Responding to chimera selection");
292 structureManager.chimeraSelectionChanged();
293 } catch (Exception e)
295 logger.warn("Could not update selection", e);
301 * Selection updater thread
303 class NetworkUpdater extends Thread
308 public NetworkUpdater(String line)
317 // ((TaskManager<?, ?>) structureManager.getService(TaskManager.class))
318 // .execute(new ImportTrajectoryRINTaskFactory(structureManager, line)
319 // .createTaskIterator());
320 } catch (Exception e)
322 logger.warn("Could not import trajectory network", e);
328 * Set a flag that this thread should clean up and exit.
330 public void requestStop()