001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.anarres.qemu.manager;
006
007import java.io.IOException;
008import java.net.InetSocketAddress;
009import java.util.Map;
010import java.util.UUID;
011import java.util.concurrent.ConcurrentHashMap;
012import java.util.concurrent.ConcurrentMap;
013import javax.annotation.CheckForNull;
014import javax.annotation.Nonnull;
015import org.anarres.qemu.exec.QEmuCommandLine;
016import org.anarres.qemu.exec.util.QEmuCommandLineUtils;
017import org.anarres.qemu.qapi.common.QApiException;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021/**
022 *
023 * @author shevek
024 */
025public class QEmuManager {
026
027    private static final Logger LOG = LoggerFactory.getLogger(QEmuManager.class);
028    private final ConcurrentMap<UUID, QEmuProcess> processes = new ConcurrentHashMap<UUID, QEmuProcess>();
029
030    @Nonnull
031    public QEmuProcess execute(@Nonnull QEmuCommandLine commandLine) throws IOException, InterruptedException, QApiException {
032        UUID uuid = QEmuCommandLineUtils.getUuid(commandLine);
033        if (LOG.isDebugEnabled())
034            LOG.debug("Executing (uuid=" + uuid + ") " + commandLine);
035        InetSocketAddress qmpAddress = QEmuCommandLineUtils.getMonitorAddress(commandLine);
036
037        Process process = commandLine.exec();
038        // LOG.debug("Executing (process=" + process + ") " + commandLine);
039        QEmuProcess qEmuProcess = new QEmuProcess(process, qmpAddress);
040        // QApiConnection connection = qEmuProcess.getConnection(10, TimeUnit.SECONDS);
041        // LOG.debug("Executing (connection=" + connection + ") " + commandLine);
042        // UUID uuid = connection.call(new QueryUuidCommand());
043        if (uuid != null)
044            processes.put(uuid, qEmuProcess);
045        return qEmuProcess;
046    }
047
048    @Nonnull
049    public Map<? extends UUID, ? extends QEmuProcess> getProcesses() {
050        return processes;
051    }
052
053    @CheckForNull
054    public QEmuProcess getProcess(UUID id) {
055        return processes.get(id);
056    }
057}