Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0b97c8e46e6982e338397045f2082f64c4ab244 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.launch;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.internal.debug.Activator;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.ILocator;
import org.eclipse.tcf.util.TCFTask;
import org.osgi.framework.Bundle;

/**
 * This class checks that TCF Agent is running on the local host,
 * and starts a new instance of the agent if it cannot be located.
 */
public class TCFLocalAgent {

    private static final String
        AGENT_HOST = "127.0.0.1",
        AGENT_PORT = "1534";

    private static Process agent;
    private static boolean destroed;

    private static String getAgentFileName() {
        String os = System.getProperty("os.name");
        String arch = System.getProperty("os.arch");
        String fnm = "agent";
        if (arch.equals("x86")) arch = "i386";
        if (arch.equals("i686")) arch = "i386";
        if (os.startsWith("Windows")) {
            os = "Windows";
            fnm = "agent.exe";
        }
        if (os.equals("Linux")) os = "GNU/Linux";
        return "agent/" + os + "/" + arch + "/" + fnm;
    }

    public static synchronized String runLocalAgent() throws CoreException {
        if (destroed) return null;
        String id = getLocalAgentID();
        if (id != null) return id;
        if (agent != null) {
            agent.destroy();
            agent = null;
        }
        Path fnm = new Path(getAgentFileName());
        try {
            Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
            URL url = FileLocator.find(bundle, fnm, null);
            if (url != null) {
                URLConnection ucn = url.openConnection();
                ucn.setRequestProperty("Method", "HEAD");
                ucn.connect();
                long mtime = ucn.getLastModified();
                File f = Activator.getDefault().getStateLocation().append(fnm).toFile();
                if (!f.exists() || mtime != f.lastModified()) {
                    f.getParentFile().mkdirs();
                    InputStream inp = url.openStream();
                    OutputStream out = new FileOutputStream(f);
                    byte[] buf = new byte[0x1000];
                    for (;;) {
                        int len = inp.read(buf);
                        if (len < 0) break;
                        out.write(buf, 0, len);
                    }
                    out.close();
                    inp.close();
                    if (!"exe".equals(fnm.getFileExtension())) {
                        String[] cmd = {
                                "chmod",
                                "a+x",
                                f.getAbsolutePath()
                        };
                        Runtime.getRuntime().exec(cmd).waitFor();
                    }
                    f.setLastModified(mtime);
                }
                String[] cmd = {
                        f.getAbsolutePath(),
                        "-s",
                        "TCP:" + AGENT_HOST + ":" + AGENT_PORT
                };
                final Process prs = agent = Runtime.getRuntime().exec(cmd);
                final TCFTask<String> waiting = waitAgentReady();
                Thread t = new Thread() {
                    public void run() {
                        try {
                            final int n = prs.waitFor();
                            if (n != 0) {
                                Protocol.invokeLater(new Runnable() {
                                    public void run() {
                                        if (waiting.isDone()) return;
                                        waiting.error(new IOException("TCF Agent exited with code " + n));
                                    }
                                });
                            }
                            synchronized (TCFLocalAgent.class) {
                                if (agent == prs) {
                                    if (n != 0 && !destroed) {
                                        Activator.log("TCF Agent exited with code " + n, null);
                                    }
                                    agent = null;
                                }
                            }
                        }
                        catch (InterruptedException x) {
                            Activator.log("TCF Agent Monitor interrupted", x);
                        }
                    }
                };
                t.setDaemon(true);
                t.setName("TCF Agent Monitor");
                t.start();
                return waiting.getIO();
            }
        }
        catch (Throwable x) {
            agent = null;
            throw new CoreException(new Status(IStatus.ERROR,
                    Activator.PLUGIN_ID, 0,
                    "Cannot start local TCF agent.",
                    x));
        }
        throw new CoreException(new Status(IStatus.ERROR,
                Activator.PLUGIN_ID, 0,
                "Cannot start local TCF agent: file not available:\n" + fnm,
                null));
    }

    private static boolean isLocalAgent(IPeer p) {
        String prot = p.getTransportName();
        if (prot.equals("PIPE")) return true;
        if (prot.equals("UNIX")) {
            String port = p.getAttributes().get(IPeer.ATTR_IP_PORT);
            return AGENT_PORT.equals(port);
        }
        String host = p.getAttributes().get(IPeer.ATTR_IP_HOST);
        String port = p.getAttributes().get(IPeer.ATTR_IP_PORT);
        return AGENT_HOST.equals(host) && AGENT_PORT.equals(port);
    }

    public static synchronized String getLocalAgentID() {
        return new TCFTask<String>() {
            public void run() {
                final ILocator locator = Protocol.getLocator();
                for (IPeer p : locator.getPeers().values()) {
                    if (isLocalAgent(p)) {
                        done(p.getID());
                        return;
                    }
                }
                done(null);
            }
        }.getE();
    }

    private static TCFTask<String> waitAgentReady() {
        return new TCFTask<String>() {
            public void run() {
                final ILocator locator = Protocol.getLocator();
                for (IPeer p : locator.getPeers().values()) {
                    if (isLocalAgent(p)) {
                        done(p.getID());
                        return;
                    }
                }
                final ILocator.LocatorListener listener = new ILocator.LocatorListener() {
                    public void peerAdded(IPeer p) {
                        if (!isDone() && isLocalAgent(p)) {
                            done(p.getID());
                            locator.removeListener(this);
                        }
                    }
                    public void peerChanged(IPeer peer) {
                    }
                    public void peerHeartBeat(String id) {
                    }
                    public void peerRemoved(String id) {
                    }
                };
                locator.addListener(listener);
                Protocol.invokeLater(30000, new Runnable() {
                    public void run() {
                        if (!isDone()) {
                            error(new Exception("Timeout waiting for TCF Agent to start"));
                            locator.removeListener(listener);
                        }
                    }
                });
            }
        };
    }

    public static synchronized void destroy() {
        if (agent != null) {
            destroed = true;
            agent.destroy();
        }
    }
}

Back to the top