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 ext.edu.ucsf.rbvi.strucviz2.Logger;
46 //import org.slf4j.Logger;
47 //import org.slf4j.LoggerFactory;
49 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
51 /***************************************************
53 **************************************************/
56 * Reply listener thread
58 public class ListenerThreads extends Thread
60 private BufferedReader lineReader = null;
62 private Process chimera = null;
64 private Map<String, List<String>> replyLog = null;
66 private Logger logger = new Logger();
68 private StructureManager structureManager = null;
70 private boolean stopMe = false;
73 * Create a new listener thread to read the responses from Chimera
76 * a handle to the Chimera Process
77 * @param structureManager
78 * a handle to the Chimera structure manager
80 public ListenerThreads(Process chimera, StructureManager structureManager)
82 this.chimera = chimera;
83 this.structureManager = structureManager;
84 replyLog = new HashMap<String, List<String>>();
85 // Get a line-oriented reader
86 InputStream readChan = chimera.getInputStream();
87 lineReader = new BufferedReader(new InputStreamReader(readChan));
89 //LoggerFactory.getLogger(ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads.class);
93 * Start the thread running
97 // System.out.println("ReplyLogListener running");
103 } catch (IOException e)
105 logger.warn("UCSF Chimera has exited: " + e.getMessage());
109 if (lineReader != null)
114 } catch (IOException e)
122 public List<String> getResponse(String command)
125 // System.out.println("getResponse: "+command);
126 // TODO do we need a maximum wait time before aborting?
127 while (!replyLog.containsKey(command))
131 Thread.currentThread().sleep(100);
132 } catch (InterruptedException e)
137 synchronized (replyLog)
139 reply = replyLog.get(command);
140 // System.out.println("getResponse ("+command+") = "+reply);
141 replyLog.remove(command);
146 public void clearResponse(String command)
150 Thread.currentThread().sleep(100);
151 } catch (InterruptedException e)
154 if (replyLog.containsKey(command))
156 replyLog.remove(command);
162 * Read input from Chimera
164 * @return a List containing the replies from Chimera
166 private void chimeraRead() throws IOException
174 while ((line = lineReader.readLine()) != null)
176 // System.out.println("From Chimera-->" + line);
177 if (line.startsWith("CMD"))
179 chimeraCommandRead(line.substring(4));
181 else if (line.startsWith("ModelChanged: "))
183 (new ModelUpdater()).start();
185 else if (line.startsWith("SelectionChanged: "))
187 (new SelectionUpdater()).start();
189 else if (line.startsWith("Trajectory residue network info:"))
191 (new NetworkUpdater(line)).start();
197 private void chimeraCommandRead(String command) throws IOException
199 // Generally -- looking for:
203 // We return the text in between
204 List<String> reply = new ArrayList<String>();
205 boolean updateModels = false;
206 boolean updateSelection = false;
207 boolean importNetwork = false;
210 synchronized (replyLog)
212 while ((line = lineReader.readLine()) != null)
214 // System.out.println("From Chimera (" + command + ") -->" + line);
215 if (line.startsWith("CMD"))
217 logger.warn("Got unexpected command from Chimera: " + line);
220 else if (line.startsWith("END"))
224 if (line.startsWith("ModelChanged: "))
228 else if (line.startsWith("SelectionChanged: "))
230 updateSelection = true;
232 else if (line.length() == 0)
236 else if (!line.startsWith("CMD"))
240 else if (line.startsWith("Trajectory residue network info:"))
242 importNetwork = true;
245 replyLog.put(command, reply);
249 (new ModelUpdater()).start();
253 (new SelectionUpdater()).start();
257 (new NetworkUpdater(line)).start();
263 * Model updater thread
265 class ModelUpdater extends Thread
268 public ModelUpdater()
274 structureManager.updateModels();
275 structureManager.modelChanged();
280 * Selection updater thread
282 class SelectionUpdater extends Thread
285 public SelectionUpdater()
293 logger.info("Responding to chimera selection");
294 structureManager.chimeraSelectionChanged();
295 } catch (Exception e)
297 logger.warn("Could not update selection", e);
303 * Selection updater thread
305 class NetworkUpdater extends Thread
310 public NetworkUpdater(String line)
319 // ((TaskManager<?, ?>) structureManager.getService(TaskManager.class))
320 // .execute(new ImportTrajectoryRINTaskFactory(structureManager, line)
321 // .createTaskIterator());
322 } catch (Exception e)
324 logger.warn("Could not import trajectory network", e);
330 * Set a flag that this thread should clean up and exit.
332 public void requestStop()