blob: 940b6c722313b0c68b6d77cf601b40a7314d1332 [file] [log] [blame]
/**
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/
package org.eclipse.cdt.debug.edc.tests.tcfagent;
import java.io.IOException;
import org.eclipse.cdt.debug.edc.tcf.extension.AgentUtils;
import org.eclipse.cdt.debug.edc.tcf.extension.services.AbstractTCFService;
import org.eclipse.cdt.debug.edc.tcf.extension.services.ILogging;
import org.eclipse.tm.tcf.core.AbstractChannel;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IChannel.ICommandServer;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.protocol.Protocol;
/**
* {@link ILogging} service implementation in test agent.
*/
public class LoggingService extends AbstractTCFService {
public LoggingService(IChannel channel) {
super(channel);
}
public static final String CONSOLE_LOGGER_ID = "ProgramOutputConsoleLogger";
private static final String WRITELN_EVENT = "writeln";
private static final String WRITE_EVENT = "write";
private static final String DIALOG_EVENT = "dialog";
private int numProgramOuputListeners = 0;
public class CommandServer extends AbstractCommandServer {
public void addListener(IToken token, String name, Object[] args) throws IOException {
if (!AgentUtils.checkNumArgs(token, getChannel(), args.length, 1))
return;
String id = (String) args[0];
if (CONSOLE_LOGGER_ID.equals(id))
numProgramOuputListeners++;
getChannel().sendResult(token, JSON.toJSONSequence( new Object[] { null }));
}
public void removeListener(IToken token, String name, Object[] args) throws IOException {
if (!AgentUtils.checkNumArgs(token, getChannel(), args.length, 1))
return;
String id = (String) args[0];
if (CONSOLE_LOGGER_ID.equals(id)) {
numProgramOuputListeners--;
if (numProgramOuputListeners < 0)
numProgramOuputListeners = 0;
}
getChannel().sendResult(token, JSON.toJSONSequence( new Object[] { null }));
}
}
public void sendEvent(final String name, final Object[] args) {
Protocol.invokeLater(new Runnable() {
public void run() {
try {
((AbstractChannel) getChannel()).sendEvent(LoggingService.this, name,
JSON.toJSONSequence(args));
} catch (IOException e) {
getChannel().terminate(e);
}
}
});
}
public void write(String message) {
if (numProgramOuputListeners > 0) {
sendEvent(WRITE_EVENT, new Object[] { CONSOLE_LOGGER_ID, message });
}
}
public void writeln(String message){
if (numProgramOuputListeners > 0) {
sendEvent(WRITELN_EVENT, new Object[] { CONSOLE_LOGGER_ID, message });
}
}
public void dialog(int severity, String summary, String details) {
sendEvent(DIALOG_EVENT, new Object[] { CONSOLE_LOGGER_ID, severity, summary, details });
}
public String getName() {
return ILogging.NAME;
}
@Override
protected ICommandServer getCommandServer() {
return new CommandServer();
}
}