package net.hyperic.hq.product.fabric;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class FabricClient {

    private String host;
    private int port;
    private Socket socket;

    public FabricClient(String host, String port) {
        this.host = host;
        this.port = Integer.parseInt(port);
    }

    public void connect()
        throws UnknownHostException,
               IOException {

        this.socket = new Socket(host, this.port);
    }

    public void disconnect()
        throws IOException {

        this.socket.close();
    }

    public int getMetric(String component, String metricName)
        throws IOException {

        String cmd = metricName + "=" + component + "\n";
        byte[] buffer = new byte[24];

        this.socket.getOutputStream().write(cmd.getBytes());

        int bytes = this.socket.getInputStream().read(buffer, 0, 24);

        return Integer.parseInt(new String(buffer, 0, bytes));
    }
}
