1 package jalview.ext.rbvi.chimera;
3 import jalview.httpserver.AbstractRequestHandler;
4 import jalview.structure.SelectionSource;
6 import java.net.BindException;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
12 * This is a simple Http handler that can listen for selections in Chimera.
16 * <li>Start the Chimera process</li>
17 * <li>Start the REST service on Chimera, get the port number it is listening on
19 * <li>Start the ChimeraListener, get the URL it is listening on</li>
20 * <li>The first listener started will start the singleton HttpServer</li>
21 * <li>Send a 'listen' command to Chimera with the URL of the listener</li>
22 * <li>When Jalview's Chimera window is closed, shut down the ChimeraListener</li>
23 * <li>Multiple linked Chimera instances will each have a separate listener (but
24 * share one Http server)</li>
30 public class ChimeraListener extends AbstractRequestHandler implements
34 * Chimera notification parameter name
36 private static final String CHIMERA_NOTIFICATION = "chimeraNotification";
39 * Chimera model changed notifications start with this
41 private static final String MODEL_CHANGED = "ModelChanged: ";
44 * Chimera selection changed notification message
46 private static final String SELECTION_CHANGED = "SelectionChanged: selection changed\n";
49 * A static counter so each listener can be associated with a distinct context
50 * root (/chimera0,1,2,3...). This is needed so we can fetch selections from
51 * multiple Chimera instances without confusion.
53 private static int chimeraId = 0;
56 * Prefix for path below context root (myChimeraId is appended)
58 private static final String PATH_PREFIX = "chimera";
61 * Value of chimeraId (0, 1, 2...) for this instance
63 private int myChimeraId = 0;
66 * A reference to the object by which we can talk to Chimera
68 private JalviewChimeraBinding chimeraBinding;
71 * Constructor that registers this as an Http request handler on path
72 * /chimeraN, where N is incremented for each instance. Call getUri to get the
73 * resulting URI for this handler.
75 * @param chimeraBinding
76 * @throws BindException
77 * if no free port can be assigned
79 public ChimeraListener(JalviewChimeraBinding binding)
82 myChimeraId = chimeraId++;
83 this.chimeraBinding = binding;
84 setPath(PATH_PREFIX + myChimeraId);
89 * Process a message from Chimera
92 protected void processRequest(HttpServletRequest request,
93 HttpServletResponse response)
95 // dumpRequest(request);
96 String message = request.getParameter(CHIMERA_NOTIFICATION);
97 if (SELECTION_CHANGED.equals(message))
99 this.chimeraBinding.highlightChimeraSelection();
101 else if (message != null && message.startsWith(MODEL_CHANGED))
103 processModelChanged(message.substring(MODEL_CHANGED.length()));
107 System.err.println("Unexpected chimeraNotification: " + message);
112 * Handler a ModelChanged notification from Chimera
116 protected void processModelChanged(String message)
118 // System.out.println(message + " (not implemented in Jalview)");
122 * Returns a display name for this service
125 public String getName()
127 return "ChimeraListener";