JAL-1725 first version of Jetty server / chimera selection listener
[jalview.git] / src / ext / edu / ucsf / rbvi / strucviz2 / ChimeraManager.java
1 package ext.edu.ucsf.rbvi.strucviz2;
2
3 import java.awt.Color;
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.apache.http.NameValuePair;
16 import org.apache.http.message.BasicNameValuePair;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
21 import ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads;
22
23 import jalview.ws.HttpClientUtils;
24
25 /**
26  * This object maintains the Chimera communication information.
27  */
28 public class ChimeraManager
29 {
30   private static final boolean debug = false;
31
32   private int chimeraRestPort;
33
34   private Process chimera;
35
36   private ListenerThreads chimeraListenerThread;
37
38   private Map<Integer, ChimeraModel> currentModelsMap;
39
40   private Logger logger = LoggerFactory
41           .getLogger(ext.edu.ucsf.rbvi.strucviz2.ChimeraManager.class);
42
43   private StructureManager structureManager;
44
45   public ChimeraManager(StructureManager structureManager)
46   {
47     this.structureManager = structureManager;
48     chimera = null;
49     chimeraListenerThread = null;
50     currentModelsMap = new HashMap<Integer, ChimeraModel>();
51
52   }
53
54   public List<ChimeraModel> getChimeraModels(String modelName)
55   {
56     List<ChimeraModel> models = getChimeraModels(modelName,
57             ModelType.PDB_MODEL);
58     models.addAll(getChimeraModels(modelName, ModelType.SMILES));
59     return models;
60   }
61
62   public List<ChimeraModel> getChimeraModels(String modelName,
63           ModelType modelType)
64   {
65     List<ChimeraModel> models = new ArrayList<ChimeraModel>();
66     for (ChimeraModel model : currentModelsMap.values())
67     {
68       if (modelName.equals(model.getModelName())
69               && modelType.equals(model.getModelType()))
70       {
71         models.add(model);
72       }
73     }
74     return models;
75   }
76
77   public Map<String, List<ChimeraModel>> getChimeraModelsMap()
78   {
79     Map<String, List<ChimeraModel>> models = new HashMap<String, List<ChimeraModel>>();
80     for (ChimeraModel model : currentModelsMap.values())
81     {
82       String modelName = model.getModelName();
83       if (!models.containsKey(modelName))
84       {
85         models.put(modelName, new ArrayList<ChimeraModel>());
86       }
87       if (!models.get(modelName).contains(model))
88       {
89         models.get(modelName).add(model);
90       }
91     }
92     return models;
93   }
94
95   public ChimeraModel getChimeraModel(Integer modelNumber,
96           Integer subModelNumber)
97   {
98     Integer key = ChimUtils.makeModelKey(modelNumber, subModelNumber);
99     if (currentModelsMap.containsKey(key))
100     {
101       return currentModelsMap.get(key);
102     }
103     return null;
104   }
105
106   public ChimeraModel getChimeraModel()
107   {
108     return currentModelsMap.values().iterator().next();
109   }
110
111   public Collection<ChimeraModel> getChimeraModels()
112   {
113     // this method is invoked by the model navigator dialog
114     return currentModelsMap.values();
115   }
116
117   public int getChimeraModelsCount(boolean smiles)
118   {
119     // this method is invokes by the model navigator dialog
120     int counter = currentModelsMap.size();
121     if (smiles)
122     {
123       return counter;
124     }
125
126     for (ChimeraModel model : currentModelsMap.values())
127     {
128       if (model.getModelType() == ModelType.SMILES)
129       {
130         counter--;
131       }
132     }
133     return counter;
134   }
135
136   public boolean hasChimeraModel(Integer modelNubmer)
137   {
138     return hasChimeraModel(modelNubmer, 0);
139   }
140
141   public boolean hasChimeraModel(Integer modelNubmer, Integer subModelNumber)
142   {
143     return currentModelsMap.containsKey(ChimUtils.makeModelKey(modelNubmer,
144             subModelNumber));
145   }
146
147   public void addChimeraModel(Integer modelNumber, Integer subModelNumber,
148           ChimeraModel model)
149   {
150     currentModelsMap.put(
151             ChimUtils.makeModelKey(modelNumber, subModelNumber), model);
152   }
153
154   public void removeChimeraModel(Integer modelNumber, Integer subModelNumber)
155   {
156     int modelKey = ChimUtils.makeModelKey(modelNumber, subModelNumber);
157     if (currentModelsMap.containsKey(modelKey))
158     {
159       currentModelsMap.remove(modelKey);
160     }
161   }
162
163   public List<ChimeraModel> openModel(String modelPath, ModelType type)
164   {
165     return openModel(modelPath, getFileNameFromPath(modelPath), type);
166   }
167
168   /**
169    * Overloaded method to allow Jalview to pass in a model name.
170    * 
171    * @param modelPath
172    * @param modelName
173    * @param type
174    * @return
175    */
176   public List<ChimeraModel> openModel(String modelPath, String modelName,
177           ModelType type)
178   {
179     logger.info("chimera open " + modelPath);
180     // stopListening();
181     List<String> response = null;
182     // TODO: [Optional] Handle modbase models
183     if (type == ModelType.MODBASE_MODEL)
184     {
185       response = sendChimeraCommand("open modbase:" + modelPath, true);
186       // } else if (type == ModelType.SMILES) {
187       // response = sendChimeraCommand("open smiles:" + modelName, true);
188       // modelName = "smiles:" + modelName;
189     }
190     else
191     {
192       response = sendChimeraCommand("open " + modelPath, true);
193     }
194     if (response == null)
195     {
196       // something went wrong
197       logger.warn("Could not open " + modelPath);
198       return null;
199     }
200     List<ChimeraModel> models = new ArrayList<ChimeraModel>();
201     int[] modelNumbers = null;
202     if (type == ModelType.PDB_MODEL)
203     {
204       for (String line : response)
205       {
206         if (line.startsWith("#"))
207         {
208           modelNumbers = ChimUtils.parseOpenedModelNumber(line);
209           if (modelNumbers != null)
210           {
211             int modelNumber = ChimUtils.makeModelKey(modelNumbers[0],
212                     modelNumbers[1]);
213             if (currentModelsMap.containsKey(modelNumber))
214             {
215               continue;
216             }
217             ChimeraModel newModel = new ChimeraModel(modelName, type,
218                     modelNumbers[0], modelNumbers[1]);
219             currentModelsMap.put(modelNumber, newModel);
220             models.add(newModel);
221
222             //
223             // patch for Jalview - set model name in Chimera
224             //
225             sendChimeraCommand("setattr M name " + modelName + " #"
226                     + modelNumbers[0], false);
227             // end patch for Jalview
228
229             modelNumbers = null;
230           }
231         }
232       }
233     }
234     else
235     {
236       // TODO: [Optional] Open smiles from file would fail. Do we need it?
237       // If parsing fails, iterate over all open models to get the right one
238       List<ChimeraModel> openModels = getModelList();
239       for (ChimeraModel openModel : openModels)
240       {
241         String openModelName = openModel.getModelName();
242         if (openModelName.endsWith("..."))
243         {
244           openModelName = openModelName.substring(0,
245                   openModelName.length() - 3);
246         }
247         if (modelPath.startsWith(openModelName))
248         {
249           openModel.setModelName(modelPath);
250           int modelNumber = ChimUtils
251                   .makeModelKey(openModel.getModelNumber(),
252                           openModel.getSubModelNumber());
253           if (!currentModelsMap.containsKey(modelNumber))
254           {
255             currentModelsMap.put(modelNumber, openModel);
256             models.add(openModel);
257           }
258         }
259       }
260     }
261
262     // assign color and residues to open models
263     for (ChimeraModel newModel : models)
264     {
265       // get model color
266       Color modelColor = getModelColor(newModel);
267       if (modelColor != null)
268       {
269         newModel.setModelColor(modelColor);
270       }
271
272       // Get our properties (default color scheme, etc.)
273       // Make the molecule look decent
274       // chimeraSend("repr stick "+newModel.toSpec());
275
276       // Create the information we need for the navigator
277       if (type != ModelType.SMILES)
278       {
279         addResidues(newModel);
280       }
281     }
282
283     sendChimeraCommand("focus", false);
284     // startListening(); // see ChimeraListener
285     return models;
286   }
287
288   /**
289    * Refactored method to extract the last (or only) element delimited by file
290    * path separator.
291    * 
292    * @param modelPath
293    * @return
294    */
295   private String getFileNameFromPath(String modelPath)
296   {
297     String modelName = modelPath;
298     if (modelPath == null)
299     {
300       return null;
301     }
302     // TODO: [Optional] Convert path to name in a better way
303     if (modelPath.lastIndexOf(File.separator) > 0)
304     {
305       modelName = modelPath.substring(modelPath
306               .lastIndexOf(File.separator) + 1);
307     }
308     else if (modelPath.lastIndexOf("/") > 0)
309     {
310       modelName = modelPath
311               .substring(modelPath.lastIndexOf("/") + 1);
312     }
313     return modelName;
314   }
315
316   public void closeModel(ChimeraModel model)
317   {
318     // int model = structure.modelNumber();
319     // int subModel = structure.subModelNumber();
320     // Integer modelKey = makeModelKey(model, subModel);
321     stopListening();
322     logger.info("chimera close model " + model.getModelName());
323     if (currentModelsMap.containsKey(ChimUtils.makeModelKey(
324             model.getModelNumber(), model.getSubModelNumber())))
325     {
326       sendChimeraCommand("close " + model.toSpec(), false);
327       // currentModelNamesMap.remove(model.getModelName());
328       currentModelsMap.remove(ChimUtils.makeModelKey(
329               model.getModelNumber(), model.getSubModelNumber()));
330       // selectionList.remove(chimeraModel);
331     }
332     else
333     {
334       logger.warn("Could not find model " + model.getModelName()
335               + " to close.");
336     }
337     startListening();
338   }
339
340   public void startListening()
341   {
342     sendChimeraCommand("listen start models; listen start selection", false);
343   }
344
345   public void stopListening()
346   {
347     sendChimeraCommand("listen stop models; listen stop selection", false);
348   }
349
350   /**
351    * Tell Chimera we are listening on the given URI
352    * 
353    * @param uri
354    */
355   public void startListening(String uri)
356   {
357     sendChimeraCommand("listen start models url " + uri, false);
358     sendChimeraCommand("listen start select prefix SelectionChanged url "
359             + uri, false);
360   }
361
362   /**
363    * Tell Chimera we have stopped listening on the given URI
364    * 
365    * @param uri
366    */
367   public void stopListening(String uri)
368   {
369     sendChimeraCommand("listen stop models url " + uri, false);
370     sendChimeraCommand("listen stop selection url " + uri, false);
371   }
372
373   /**
374    * Select something in Chimera
375    * 
376    * @param command
377    *          the selection command to pass to Chimera
378    */
379   public void select(String command)
380   {
381     sendChimeraCommand("listen stop selection; " + command
382             + "; listen start selection", false);
383   }
384
385   public void focus()
386   {
387     sendChimeraCommand("focus", false);
388   }
389
390   public void clearOnChimeraExit()
391   {
392     chimera = null;
393     currentModelsMap.clear();
394       this.chimeraRestPort = 0;
395     structureManager.clearOnChimeraExit();
396   }
397
398   public void exitChimera()
399   {
400     if (isChimeraLaunched() && chimera != null)
401     {
402       sendChimeraCommand("stop really", false);
403       try
404       {
405         chimera.destroy();
406       } catch (Exception ex)
407       {
408         // ignore
409       }
410     }
411     clearOnChimeraExit();
412   }
413
414   public Map<Integer, ChimeraModel> getSelectedModels()
415   {
416     Map<Integer, ChimeraModel> selectedModelsMap = new HashMap<Integer, ChimeraModel>();
417     List<String> chimeraReply = sendChimeraCommand(
418             "list selection level molecule", true);
419     if (chimeraReply != null)
420     {
421       for (String modelLine : chimeraReply)
422       {
423         ChimeraModel chimeraModel = new ChimeraModel(modelLine);
424         Integer modelKey = ChimUtils.makeModelKey(
425                 chimeraModel.getModelNumber(),
426                 chimeraModel.getSubModelNumber());
427         selectedModelsMap.put(modelKey, chimeraModel);
428       }
429     }
430     return selectedModelsMap;
431   }
432
433   /**
434    * Sends a 'list selection level residue' command to Chimera and returns the
435    * list of selected atomspecs
436    * 
437    * @return
438    */
439   public List<String> getSelectedResidueSpecs()
440   {
441     List<String> selectedResidues = new ArrayList<String>();
442     List<String> chimeraReply = sendChimeraCommand(
443             "list selection level residue", true);
444     if (chimeraReply != null)
445     {
446       for (String inputLine : chimeraReply)
447       {
448         String[] inputLineParts = inputLine.split("\\s+");
449         if (inputLineParts.length == 5)
450         {
451           selectedResidues.add(inputLineParts[2]);
452         }
453       }
454     }
455     return selectedResidues;
456   }
457
458   public void getSelectedResidues(
459           Map<Integer, ChimeraModel> selectedModelsMap)
460   {
461     List<String> chimeraReply = sendChimeraCommand(
462             "list selection level residue", true);
463     if (chimeraReply != null)
464     {
465       for (String inputLine : chimeraReply)
466       {
467         ChimeraResidue r = new ChimeraResidue(inputLine);
468         Integer modelKey = ChimUtils.makeModelKey(r.getModelNumber(),
469                 r.getSubModelNumber());
470         if (selectedModelsMap.containsKey(modelKey))
471         {
472           ChimeraModel model = selectedModelsMap.get(modelKey);
473           model.addResidue(r);
474         }
475       }
476     }
477   }
478
479   /**
480    * Return the list of ChimeraModels currently open. Warning: if smiles model
481    * name too long, only part of it with "..." is printed.
482    * 
483    * 
484    * @return List of ChimeraModel's
485    */
486   // TODO: [Optional] Handle smiles names in a better way in Chimera?
487   public List<ChimeraModel> getModelList()
488   {
489     List<ChimeraModel> modelList = new ArrayList<ChimeraModel>();
490     List<String> list = sendChimeraCommand("list models type molecule",
491             true);
492     if (list != null)
493     {
494       for (String modelLine : list)
495       {
496         ChimeraModel chimeraModel = new ChimeraModel(modelLine);
497         modelList.add(chimeraModel);
498       }
499     }
500     return modelList;
501   }
502
503   /**
504    * Return the list of depiction presets available from within Chimera. Chimera
505    * will return the list as a series of lines with the format: Preset type
506    * number "description"
507    * 
508    * @return list of presets
509    */
510   public List<String> getPresets()
511   {
512     ArrayList<String> presetList = new ArrayList<String>();
513     List<String> output = sendChimeraCommand("preset list", true);
514     if (output != null)
515     {
516       for (String preset : output)
517       {
518         preset = preset.substring(7); // Skip over the "Preset"
519         preset = preset.replaceFirst("\"", "(");
520         preset = preset.replaceFirst("\"", ")");
521         // string now looks like: type number (description)
522         presetList.add(preset);
523       }
524     }
525     return presetList;
526   }
527
528   public boolean isChimeraLaunched()
529   {
530     boolean launched = false;
531     if (chimera != null)
532     {
533       try
534       {
535         chimera.exitValue();
536         // if we get here, process has ended
537       } catch (IllegalThreadStateException e)
538       {
539         // ok - not yet terminated
540         launched = true;
541       }
542     }
543     return launched;
544   }
545
546   public boolean launchChimera(List<String> chimeraPaths)
547   {
548     // Do nothing if Chimera is already launched
549     if (isChimeraLaunched())
550     {
551       return true;
552     }
553
554     // Try to launch Chimera (eventually using one of the possible paths)
555     String error = "Error message: ";
556     String workingPath = "";
557     // iterate over possible paths for starting Chimera
558     for (String chimeraPath : chimeraPaths)
559     {
560       File path = new File(chimeraPath);
561       if (!path.canExecute())
562       {
563         error += "File '" + path + "' does not exist.\n";
564         continue;
565       }
566       try
567       {
568         List<String> args = new ArrayList<String>();
569         args.add(chimeraPath);
570         args.add("--start");
571         args.add("RESTServer");
572         ProcessBuilder pb = new ProcessBuilder(args);
573         chimera = pb.start();
574         error = "";
575         workingPath = chimeraPath;
576         break;
577       } catch (Exception e)
578       {
579         // Chimera could not be started
580         error += e.getMessage();
581       }
582     }
583     // If no error, then Chimera was launched successfully
584     if (error.length() == 0)
585     {
586       this.chimeraRestPort = getPortNumber();
587       System.out.println("Chimera REST API started on port "
588               + chimeraRestPort);
589       // structureManager.initChimTable();
590       structureManager.setChimeraPathProperty(workingPath);
591       // TODO: [Optional] Check Chimera version and show a warning if below 1.8
592       // Ask Chimera to give us updates
593       // startListening(); // later - see ChimeraListener
594       return true;
595     }
596
597     // Tell the user that Chimera could not be started because of an error
598     logger.warn(error);
599     return false;
600   }
601
602   /**
603    * Read and return the port number returned in the reply to --start RESTServer
604    */
605   private int getPortNumber()
606   {
607     int port = 0;
608     InputStream readChan = chimera.getInputStream();
609     BufferedReader lineReader = new BufferedReader(new InputStreamReader(
610             readChan));
611     String response = null;
612     try
613     {
614       // expect: REST server on host 127.0.0.1 port port_number
615       response = lineReader.readLine();
616       String [] tokens = response.split(" ");
617       if (tokens.length == 7 && "port".equals(tokens[5])) {
618         port = Integer.parseInt(tokens[6]);
619         logger.info("Chimera REST service listening on port "
620                 + chimeraRestPort);
621       }
622     } catch (Exception e)
623     {
624       logger.error("Failed to get REST port number from " + response + ": "
625               + e.getMessage());
626     } finally
627     {
628       try
629       {
630         lineReader.close();
631       } catch (IOException e2)
632       {
633       }
634     }
635     return port;
636   }
637
638   /**
639    * Determine the color that Chimera is using for this model.
640    * 
641    * @param model
642    *          the ChimeraModel we want to get the Color for
643    * @return the default model Color for this model in Chimera
644    */
645   public Color getModelColor(ChimeraModel model)
646   {
647     List<String> colorLines = sendChimeraCommand(
648             "list model spec " + model.toSpec() + " attribute color", true);
649     if (colorLines == null || colorLines.size() == 0)
650     {
651       return null;
652     }
653     return ChimUtils.parseModelColor(colorLines.get(0));
654   }
655
656   /**
657    * 
658    * Get information about the residues associated with a model. This uses the
659    * Chimera listr command. We don't return the resulting residues, but we add
660    * the residues to the model.
661    * 
662    * @param model
663    *          the ChimeraModel to get residue information for
664    * 
665    */
666   public void addResidues(ChimeraModel model)
667   {
668     int modelNumber = model.getModelNumber();
669     int subModelNumber = model.getSubModelNumber();
670     // Get the list -- it will be in the reply log
671     List<String> reply = sendChimeraCommand(
672             "list residues spec " + model.toSpec(), true);
673     if (reply == null)
674     {
675       return;
676     }
677     for (String inputLine : reply)
678     {
679       ChimeraResidue r = new ChimeraResidue(inputLine);
680       if (r.getModelNumber() == modelNumber
681               || r.getSubModelNumber() == subModelNumber)
682       {
683         model.addResidue(r);
684       }
685     }
686   }
687
688   public List<String> getAttrList()
689   {
690     List<String> attributes = new ArrayList<String>();
691     final List<String> reply = sendChimeraCommand("list resattr", true);
692     if (reply != null)
693     {
694       for (String inputLine : reply)
695       {
696         String[] lineParts = inputLine.split("\\s");
697         if (lineParts.length == 2 && lineParts[0].equals("resattr"))
698         {
699           attributes.add(lineParts[1]);
700         }
701       }
702     }
703     return attributes;
704   }
705
706   public Map<ChimeraResidue, Object> getAttrValues(String aCommand,
707           ChimeraModel model)
708   {
709     Map<ChimeraResidue, Object> values = new HashMap<ChimeraResidue, Object>();
710     final List<String> reply = sendChimeraCommand("list residue spec "
711             + model.toSpec() + " attribute " + aCommand, true);
712     if (reply != null)
713     {
714       for (String inputLine : reply)
715       {
716         String[] lineParts = inputLine.split("\\s");
717         if (lineParts.length == 5)
718         {
719           ChimeraResidue residue = ChimUtils
720                   .getResidue(lineParts[2], model);
721           String value = lineParts[4];
722           if (residue != null)
723           {
724             if (value.equals("None"))
725             {
726               continue;
727             }
728             if (value.equals("True") || value.equals("False"))
729             {
730               values.put(residue, Boolean.valueOf(value));
731               continue;
732             }
733             try
734             {
735               Double doubleValue = Double.valueOf(value);
736               values.put(residue, doubleValue);
737             } catch (NumberFormatException ex)
738             {
739               values.put(residue, value);
740             }
741           }
742         }
743       }
744     }
745     return values;
746   }
747
748   private volatile boolean busy = false;
749
750   /**
751    * Send a command to Chimera.
752    * 
753    * @param command
754    *          Command string to be send.
755    * @param reply
756    *          Flag indicating whether the method should return the reply from
757    *          Chimera or not.
758    * @return List of Strings corresponding to the lines in the Chimera reply or
759    *         <code>null</code>.
760    */
761   public List<String> sendChimeraCommand(String command, boolean reply)
762   {
763     if (!isChimeraLaunched() || command == null
764             || "".equals(command.trim()))
765     {
766       return null;
767     }
768     // TODO do we need a maximum wait time before aborting?
769     while (busy)
770     {
771       try
772       {
773         Thread.sleep(25);
774       } catch (InterruptedException q)
775       {
776       }
777     }
778     busy = true;
779     long startTime = System.currentTimeMillis();
780     try
781     {
782       return sendRestCommand(command);
783     } finally
784     {
785       /*
786        * Make sure busy flag is reset come what may!
787        */
788       busy = false;
789       if (debug)
790       {
791         System.out.println("Chimera command took "
792                 + (System.currentTimeMillis() - startTime) + "ms: "
793                 + command);
794       }
795
796     }
797   }
798
799   /**
800    * Sends the command to Chimera's REST API, and returns any response lines.
801    * 
802    * @param command
803    * @return
804    */
805   protected List<String> sendRestCommand(String command)
806   {
807     // System.out.println("Rest: " + command);
808     // TODO start a separate thread to do this so we don't block?
809     String restUrl = "http://127.0.0.1:" + this.chimeraRestPort + "/run";
810     List<NameValuePair> commands = new ArrayList<NameValuePair>(1);
811     commands.add(new BasicNameValuePair("command", command));
812
813     List<String> reply = new ArrayList<String>();
814     BufferedReader response = null;
815     try {
816       response = HttpClientUtils
817               .doHttpUrlPost(restUrl, commands, 100, 2000);
818       String line = "";
819       while ((line = response.readLine()) != null) {
820         reply.add(line);
821       }
822     } catch (Exception e)
823     {
824       logger.error("REST call " + command + " failed: " + e.getMessage());
825     } finally
826     {
827       if (response != null)
828       {
829         try
830         {
831           response.close();
832         } catch (IOException e)
833         {
834         }
835       }
836     }
837     return reply;
838   }
839
840   /**
841    * Send a command to stdin of Chimera process, and optionally read any
842    * responses.
843    * 
844    * @param command
845    * @param readReply
846    * @return
847    */
848   protected List<String> sendStdinCommand(String command, boolean readReply)
849   {
850     chimeraListenerThread.clearResponse(command);
851     String text = command.concat("\n");
852     try
853     {
854       // send the command
855       chimera.getOutputStream().write(text.getBytes());
856       chimera.getOutputStream().flush();
857     } catch (IOException e)
858     {
859       // logger.info("Unable to execute command: " + text);
860       // logger.info("Exiting...");
861       logger.warn("Unable to execute command: " + text);
862       logger.warn("Exiting...");
863       clearOnChimeraExit();
864       return null;
865     }
866     if (!readReply)
867     {
868       return null;
869     }
870     List<String> rsp = chimeraListenerThread.getResponse(command);
871     return rsp;
872   }
873
874   public StructureManager getStructureManager()
875   {
876     return structureManager;
877   }
878
879   public boolean isBusy()
880   {
881     return busy;
882   }
883 }