1 package jalview.ext.rbvi.chimera;
3 import java.net.BindException;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
8 import jalview.httpserver.AbstractRequestHandler;
9 import jalview.httpserver.HttpServer;
10 import jalview.structure.SelectionSource;
13 * This is a simple Http handler that can listen for selections in Chimera.
17 * <li>Start the Chimera process</li>
18 * <li>Start the REST service on Chimera, get the port number it is listening on
20 * <li>Start the ChimeraListener, get the port number it is listening on</li>
21 * <li>Send a 'listen' command to Chimera with the URL of the listener</li>
27 public class ChimeraListener extends AbstractRequestHandler implements
31 * A static counter so each listener can be associated with a distinct context
32 * root (/chimera0,1,2,3...). This is needed so we can fetch selections from
33 * multiple Chimera instances without confusion.
35 private static int chimeraId = 0;
38 * Path below context root that identifies this handler
40 private static final String LISTENER_PATH = "chimera";
43 * Value of chimeraId (0, 1, 2...) for this instance
45 private int myChimeraId = 0;
48 * A reference to the object by which we can talk to Chimera
50 private JalviewChimeraBinding chimeraBinding;
53 * The URI of this listener
58 * Constructor that also registers this as an Http request handler on path
59 * /chimeraN, where N is incremented for each instance. Call getUri to get the
60 * resulting URI for this handler.
62 * @param chimeraBinding
63 * @throws BindException
64 * if no free port can be assigned
66 public ChimeraListener(JalviewChimeraBinding binding)
69 myChimeraId = chimeraId++;
70 this.chimeraBinding = binding;
71 final String path = LISTENER_PATH + myChimeraId;
72 this.uri = HttpServer.getInstance().registerHandler(path, this);
76 * Returns the URI on which we are listening
80 public String getUri()
86 * Process a message from Chimera
89 protected void processRequest(HttpServletRequest request,
90 HttpServletResponse response)
92 // dumpRequest(request);
93 String message = request.getParameter("chimeraNotification");
94 if ("selection changed".equals(message))
96 this.chimeraBinding.highlightChimeraSelection();
100 System.err.println("Unexpected chimeraNotification: " + message);
101 // do it anyway for now!
102 this.chimeraBinding.highlightChimeraSelection();
107 * Deregister this listener and close it down
111 public void shutdown()
115 HttpServer.getInstance().removeHandler(this);
117 } catch (Exception e)
119 System.err.println("Error stopping chimera listener: "