2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ext.rbvi.chimera;
23 import jalview.httpserver.AbstractRequestHandler;
24 import jalview.structure.SelectionSource;
26 import java.net.BindException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
32 * This is a simple Http handler that can listen for selections in Chimera.
36 * <li>Start the Chimera process</li>
37 * <li>Start the REST service on Chimera, get the port number it is listening on
39 * <li>Start the ChimeraListener, get the URL it is listening on</li>
40 * <li>The first listener started will start the singleton HttpServer</li>
41 * <li>Send a 'listen' command to Chimera with the URL of the listener</li>
42 * <li>When Jalview's Chimera window is closed, shut down the ChimeraListener</li>
43 * <li>Multiple linked Chimera instances will each have a separate listener (but
44 * share one Http server)</li>
50 public class ChimeraListener extends AbstractRequestHandler implements
54 * Chimera notification parameter name
56 private static final String CHIMERA_NOTIFICATION = "chimeraNotification";
59 * Chimera model changed notifications start with this
61 private static final String MODEL_CHANGED = "ModelChanged: ";
64 * Chimera selection changed notification message
66 private static final String SELECTION_CHANGED = "SelectionChanged: selection changed\n";
69 * A static counter so each listener can be associated with a distinct context
70 * root (/chimera0,1,2,3...). This is needed so we can fetch selections from
71 * multiple Chimera instances without confusion.
73 private static int chimeraId = 0;
76 * Prefix for path below context root (myChimeraId is appended)
78 private static final String PATH_PREFIX = "chimera";
81 * Value of chimeraId (0, 1, 2...) for this instance
83 private int myChimeraId = 0;
86 * A reference to the object by which we can talk to Chimera
88 private JalviewChimeraBinding chimeraBinding;
91 * Constructor that registers this as an Http request handler on path
92 * /chimeraN, where N is incremented for each instance. Call getUri to get the
93 * resulting URI for this handler.
95 * @param chimeraBinding
96 * @throws BindException
97 * if no free port can be assigned
99 public ChimeraListener(JalviewChimeraBinding binding)
102 myChimeraId = chimeraId++;
103 this.chimeraBinding = binding;
104 setPath(PATH_PREFIX + myChimeraId);
109 * Process a message from Chimera
112 protected void processRequest(HttpServletRequest request,
113 HttpServletResponse response)
115 // dumpRequest(request);
116 String message = request.getParameter(CHIMERA_NOTIFICATION);
117 if (SELECTION_CHANGED.equals(message))
119 this.chimeraBinding.highlightChimeraSelection();
121 else if (message != null && message.startsWith(MODEL_CHANGED))
123 processModelChanged(message.substring(MODEL_CHANGED.length()));
127 System.err.println("Unexpected chimeraNotification: " + message);
132 * Handler a ModelChanged notification from Chimera
136 protected void processModelChanged(String message)
138 // System.out.println(message + " (not implemented in Jalview)");
142 * Returns a display name for this service
145 public String getName()
147 return "ChimeraListener";