001package org.xbib.elasticsearch.http;
002
003import org.elasticsearch.common.io.stream.StreamInput;
004import org.elasticsearch.common.io.stream.StreamOutput;
005import org.elasticsearch.common.io.stream.Streamable;
006import org.elasticsearch.common.xcontent.ToXContent;
007import org.elasticsearch.common.xcontent.XContentBuilder;
008import org.elasticsearch.common.xcontent.XContentBuilderString;
009
010import java.io.IOException;
011
012public class HttpStats implements Streamable, ToXContent {
013
014    private long serverOpen;
015    private long totalOpen;
016
017    HttpStats() {
018    }
019
020    public HttpStats(long serverOpen, long totalOpen) {
021        this.serverOpen = serverOpen;
022        this.totalOpen = totalOpen;
023    }
024
025    public long getServerOpen() {
026        return this.serverOpen;
027    }
028
029    public long getTotalOpen() {
030        return this.totalOpen;
031    }
032
033    public static HttpStats readHttpStats(StreamInput in) throws IOException {
034        HttpStats stats = new HttpStats();
035        stats.readFrom(in);
036        return stats;
037    }
038
039    @Override
040    public void readFrom(StreamInput in) throws IOException {
041        serverOpen = in.readVLong();
042        totalOpen = in.readVLong();
043    }
044
045    @Override
046    public void writeTo(StreamOutput out) throws IOException {
047        out.writeVLong(serverOpen);
048        out.writeVLong(totalOpen);
049    }
050
051    static final class Fields {
052        static final XContentBuilderString WEBSOCKET = new XContentBuilderString("websocket");
053        static final XContentBuilderString CURRENT_OPEN = new XContentBuilderString("current_open");
054        static final XContentBuilderString TOTAL_OPENED = new XContentBuilderString("total_opened");
055    }
056
057    @Override
058    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
059        builder.startObject(Fields.WEBSOCKET);
060        builder.field(Fields.CURRENT_OPEN, serverOpen);
061        builder.field(Fields.TOTAL_OPENED, totalOpen);
062        builder.endObject();
063        return builder;
064    }
065}