001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava;
038:
039: import edu.rice.cs.drjava.ui.MainFrame;
040: import edu.rice.cs.util.FileOpenSelector;
041: import edu.rice.cs.util.OperationCanceledException;
042: import edu.rice.cs.drjava.config.OptionConstants;
043:
044: import java.io.*;
045: import java.net.*;
046: import java.util.*;
047:
048: /**
049: * This class contains a server that monitors incoming datagrams on port 4444
050: * (default; can be changed in OptionConstants.REMOTE_CONTROL_PORT).
051: * These datagrams can contain commands to open additional files.
052: *
053: * A client can query whether a server is running by sending QUERY_PREFIX.
054: * If a server is running, it will respond with RESPONSE_PREFIX.
055: *
056: * A client can tell a server to open a file by sending QUERY_PREFIX+" "+absoluteFileName.
057: * The server will respond with RESPONSE_PREFIX, or RESPONSE_PREFIX+" "+error if an error occurred.
058: *
059: * A client can tell a server to open a file and jump to a certain line number by sending
060: * QUERY_PREFIX+" "+absoluteFileName+File.pathSeparator+lineNumber.
061: * The server will respond with RESPONSE_PREFIX, or RESPONSE_PREFIX+" "+error if an error occurred.
062: */
063: public class RemoteControlServer {
064: /**
065: * Prefix of a legitimate query by a client.
066: */
067: public static final String QUERY_PREFIX = "DrJava Remote Control?";
068:
069: /**
070: * Prefix of a legitimate response by this server.
071: */
072: public static final String RESPONSE_PREFIX = "DrJava Remote Control ";
073:
074: /**
075: * Prefix of a legitimate response by this server, including the user name.
076: */
077: public static final String RESPONSE_PREFIX_WITH_USER = RESPONSE_PREFIX
078: + System.getProperty("user.name") + "!";
079:
080: /**
081: * Create a new remote control server, running in its own daemon thread.
082: * @param frame main frame
083: */
084: public RemoteControlServer(MainFrame frame) throws IOException {
085: RCServerThread rcsThread = new RCServerThread(frame);
086: rcsThread.setDaemon(true);
087: rcsThread.start();
088: }
089:
090: /**
091: * Thread class for the server.
092: */
093: public static class RCServerThread extends Thread {
094: /**
095: * Main frame access so the server can open files, etc.
096: */
097: protected MainFrame _frame;
098:
099: /**
100: * Socket used.
101: */
102: protected DatagramSocket socket = null;
103:
104: /**
105: * Create a new server thread.
106: * @param frame main frame
107: */
108: public RCServerThread(MainFrame frame) throws IOException {
109: this ("RCServerThread", frame);
110: }
111:
112: /**
113: * Create a new server thread with a specified name.
114: * @param name thread name
115: * @param frame main frame
116: */
117: public RCServerThread(String name, MainFrame frame)
118: throws IOException {
119: super (name);
120: _frame = frame;
121: socket = new DatagramSocket(DrJava.getConfig().getSetting(
122: OptionConstants.REMOTE_CONTROL_PORT));
123: }
124:
125: /**
126: * Main method of the thread. It loops indefinitely, waiting for queries.
127: * Since this is a daemon thread, it will get shut down at the end.
128: */
129: public void run() {
130: while (true) {
131: try {
132: byte[] buf = new byte[256];
133:
134: // receive request
135: DatagramPacket packet = new DatagramPacket(buf,
136: buf.length);
137: socket.receive(packet);
138:
139: String request = new String(packet.getData(), 0,
140: packet.getLength());
141:
142: // check if it was a legitimate query
143: if (request.startsWith(QUERY_PREFIX)) {
144: // construct response
145: String dString = RESPONSE_PREFIX_WITH_USER;
146: request = request.substring(QUERY_PREFIX
147: .length());
148:
149: // check if a file was specified
150: if ((request.length() > 0)
151: && (request.charAt(0) == ' ')) {
152: request = request.substring(1);
153:
154: // check if the request contained a line number
155: int lineNo = -1;
156: int pathSepIndex = request
157: .indexOf(File.pathSeparatorChar);
158: if (pathSepIndex >= 0) {
159: try {
160: lineNo = new Integer(
161: request
162: .substring(pathSepIndex + 1));
163: } catch (NumberFormatException nfe) {
164: lineNo = -1;
165: }
166: request = request.substring(0,
167: pathSepIndex);
168: }
169:
170: final File f = new File(request);
171:
172: if (f.exists()) {
173: FileOpenSelector openSelector = new FileOpenSelector() {
174: public File[] getFiles()
175: throws OperationCanceledException {
176: return new File[] { f };
177: }
178: };
179: if (_frame != null) {
180: _frame.open(openSelector);
181: if (lineNo >= 0) {
182: final int l = lineNo;
183: edu.rice.cs.util.swing.Utilities
184: .invokeLater(new Runnable() {
185: public void run() {
186: _frame
187: ._jumpToLine(l);
188: }
189: });
190: }
191: }
192: } else {
193: dString = dString
194: + " Cannot open file!";
195: }
196: }
197:
198: buf = dString.getBytes();
199:
200: // send the response to the client at "address" and "port"
201: InetAddress address = packet.getAddress();
202: int port = packet.getPort();
203: packet = new DatagramPacket(buf, buf.length,
204: address, port);
205:
206: socket.send(packet);
207: }
208: } catch (SocketTimeoutException e) {
209: // ignore
210: } catch (IOException e) {
211: e.printStackTrace();
212: }
213: }
214: }
215:
216: public void finalize() {
217: if (socket != null) {
218: socket.close();
219: }
220: }
221: }
222:
223: /**
224: * Main method for test purposes.
225: */
226: public static void main(String[] args) {
227: try {
228: (new RCServerThread(null)).start();
229: } catch (IOException ioe) {
230: System.err.println(ioe);
231: ioe.printStackTrace();
232: }
233: }
234: }
|