001: /*
002: * Server.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Server.java,v 1.2 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.InputStreamReader;
026: import java.io.OutputStream;
027: import java.net.ServerSocket;
028: import java.net.Socket;
029: import java.net.SocketException;
030: import java.util.Vector;
031: import javax.swing.SwingUtilities;
032:
033: public class Server implements Runnable {
034: private static Server server;
035:
036: private ServerSocket socket;
037: private Thread thread;
038:
039: public static void startServer() {
040: try {
041: server = new Server();
042: server.socket = new ServerSocket(0);
043: int port = server.socket.getLocalPort();
044: OutputStream out = Editor.portfile.getOutputStream();
045: out.write(String.valueOf(port).getBytes());
046: out.close();
047: server.thread = new Thread(server);
048: server.thread.setName("server");
049: server.thread.setPriority(Thread.MIN_PRIORITY);
050: server.thread.setDaemon(true);
051: server.thread.start();
052: } catch (Exception e) {
053: Log.error(e);
054: }
055: }
056:
057: public static void stopServer() {
058: Editor.portfile.delete();
059: }
060:
061: public void run() {
062: while (true) {
063: try {
064: Socket sock = socket.accept(); // Blocks.
065: // Process request.
066: BufferedReader in = new BufferedReader(
067: new InputStreamReader(sock.getInputStream()));
068: Vector v = null;
069: while (true) {
070: String s = in.readLine();
071: if (s == null)
072: break;
073: if (v == null)
074: v = new Vector();
075: v.add(s);
076: }
077: in.close();
078: sock.close();
079: SwingUtilities.invokeLater(new Messenger(v));
080: } catch (SocketException e) {
081: return;
082: } catch (Exception e) {
083: Log.error(e);
084: }
085: }
086: }
087:
088: class Messenger implements Runnable {
089: Vector v = null;
090:
091: // If this constructor is private, we run into jikes 1.15 bug #2256.
092: Messenger(Vector v) {
093: this .v = v;
094: }
095:
096: public void run() {
097: Editor editor = Editor.currentEditor();
098: if (v != null && v.size() > 0) {
099: Editor other = editor.getOtherEditor();
100: if (other != null && editor.getBuffer().isSecondary())
101: editor = other;
102: if (!editor.getBuffer().isPrimary())
103: Debug.bug();
104: Buffer toBeActivated = editor.openFiles(v);
105: if (toBeActivated != null) {
106: editor.makeNext(toBeActivated);
107: editor.switchToBuffer(toBeActivated);
108: if (!Editor.getEditorList().contains(editor))
109: Debug.bug();
110: editor.updateDisplay();
111: }
112: }
113: editor.getFrame().toFront();
114: editor.requestFocus();
115: }
116: }
117: }
|