blob: 110d1a74af5df2d481b6edf5cc12a4c1b3fb6f81 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 Nokia 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:
* Nokia - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.edc.tests.tcfagent;
import java.io.IOException;
import java.net.InetAddress;
import java.util.LinkedList;
import org.eclipse.cdt.debug.edc.tcf.extension.AgentUtils;
import org.eclipse.cdt.debug.edc.tcf.extension.ServerTCP;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IEventQueue;
import org.eclipse.tm.tcf.protocol.IService;
import org.eclipse.tm.tcf.protocol.IServiceProvider;
import org.eclipse.tm.tcf.protocol.Protocol;
/**
* A TCF agent in Java used for unit test.
*
*/
public class EDCTestAgent {
private static final String NAME = "TCFTestAgentForEDC";
// Make it a singleton class.
private static EDCTestAgent sInstance = null;
private ServerTCP fServer = null;
private IServiceProvider serviceProvider = new IServiceProvider() {
/*
* Tell framework that we are offering this service.
*/
public IService[] getLocalService(IChannel channel) {
return getProvidedServices(channel);
}
public IService getServiceProxy(IChannel channel, String serviceName) {
return null;
}
};
protected static class EventQueue extends Thread implements IEventQueue {
private final LinkedList<Runnable> queue = new LinkedList<Runnable>();
public EventQueue() {
setName("TCF Event Dispatch");
start();
}
@Override
public void run() {
try {
while (true) {
Runnable r = null;
synchronized (this) {
while (queue.isEmpty())
wait();
r = queue.removeFirst();
}
try {
r.run();
} catch (Throwable x) {
System.err.println("Error dispatching TCF event:");
x.printStackTrace();
}
}
} catch (Throwable x) {
x.printStackTrace();
System.exit(1);
}
}
public synchronized int getCongestion() {
int n = queue.size() - 100;
if (n > 100)
n = 100;
return n;
}
public synchronized void invokeLater(Runnable runnable) {
queue.add(runnable);
notify();
}
public boolean isDispatchThread() {
return Thread.currentThread() == this;
}
}
/**
* Run the agent as a standalone Java application.
*
* @param args
*/
public static void main(String[] args) {
try {
Protocol.setEventQueue(new EventQueue());
getInstance().start();
} catch (IOException e) {
System.out.println("Fail to start the agent. IOException: " + e.getMessage());
return;
}
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Define the services the agent provides.
*
* @param channel
* @return
*/
protected IService[] getProvidedServices(IChannel channel) {
return new IService[] {
new UnitTestDriverService(channel),
new LoggingService(channel),
new SettingsService(channel),
};
}
protected EDCTestAgent() {
}
public static EDCTestAgent getInstance() {
if (sInstance == null) {
sInstance = new EDCTestAgent();
}
return sInstance;
}
public void start() throws IOException {
if (fServer == null) {
Protocol.addServiceProvider(serviceProvider);
final IOException exc[] = { null };
Protocol.invokeAndWait(new Runnable() {
public void run() {
try {
fServer = new ServerTCP(getAgentName(), AgentUtils.findFreePort()) {
@Override
protected boolean shouldCreatePeerOnAddress(
InetAddress localAddr) {
// Make this agent only visible on host machine.
if (localAddr.isLoopbackAddress())
return true;
return false;
}
};
} catch (IOException e) {
exc[0] = e;
}
}
});
if (exc[0] != null)
throw exc[0];
}
}
public String getAgentName() {
return NAME;
}
public void stop() throws IOException {
if (fServer != null) {
fServer.close();
Protocol.removeServiceProvider(serviceProvider);
}
fServer = null;
}
public boolean isRunning() {
return fServer != null;
}
}