All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.collectd.protocol.PacketWriter Maven / Gradle / Ivy

Go to download

The jcollectd package implements the collectd protocol in Java, making it possible for Java applications to push data into collectd over the wire.

The newest version!
/*
 * jcollectd
 * Copyright (C) 2009 Hyperic, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; only version 2 of the License is applicable.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

package org.collectd.protocol;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;

import org.collectd.api.DataSource;
import org.collectd.api.PluginData;
import org.collectd.api.ValueList;

/**
 * collectd/src/network.c:network_write 
 */
public class PacketWriter {

    private ByteArrayOutputStream _bos;
    private DataOutputStream _os;
    private final TypesDB _types = TypesDB.getInstance();

    public PacketWriter() {
        this(new ByteArrayOutputStream(Network.BUFFER_SIZE));
    }

    public PacketWriter(ByteArrayOutputStream bos) {
        _bos = bos;
        _os = new DataOutputStream(_bos);
    }

    public int getSize() {
        return _bos.size();
    }

    public byte[] getBytes() {
        return _bos.toByteArray();
    }

    public boolean isFull() {
        return getSize() >= Network.BUFFER_SIZE;
    }

    public void reset() {
        _bos.reset();
    }

    public void write(PluginData data)
        throws IOException {

        String type = data.getType();

        writeString(Network.TYPE_HOST, data.getHost());
        writeNumber(Network.TYPE_TIME, data.getTime()/1000);
        writeString(Network.TYPE_PLUGIN, data.getPlugin());
        writeString(Network.TYPE_PLUGIN_INSTANCE, data.getPluginInstance());
        writeString(Network.TYPE_TYPE, type);
        writeString(Network.TYPE_TYPE_INSTANCE, data.getTypeInstance());
        
        if (data instanceof ValueList) {
            ValueList vl = (ValueList)data;
            List ds = _types.getType(type);
            List values = vl.getValues();

            if ((ds != null) && (ds.size() != values.size())) {
                String msg =
                    type + " datasource mismatch, expecting " +
                    ds.size() + ", given " + values.size();
                throw new IOException(msg);
            }

            writeNumber(Network.TYPE_INTERVAL, vl.getInterval());
            writeValues(ds, values);
        }
        else {
            //XXX Notification
        }
    }

    private void writeHeader(int type, int len)
        throws IOException {
        _os.writeShort(type);
        _os.writeShort(len);
    }

    private void writeValues(List ds, List values)
        throws IOException {

        int num = values.size();
        int len =
            Network.HEADER_LEN +
            Network.UINT16_LEN +
            (num * Network.UINT8_LEN) +
            (num * Network.UINT64_LEN);

        byte[] types = new byte[num];
        int ds_len;
        if (ds == null) {
            ds_len = 0;
        }
        else {
            ds_len = ds.size();
        }

        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy