edu.umass.cs.msocket.proxy.forwarder.ProxyLoadStatistics.java Source code

Java tutorial

Introduction

Here is the source code for edu.umass.cs.msocket.proxy.forwarder.ProxyLoadStatistics.java

Source

/*******************************************************************************
 *
 * Mobility First - mSocket library
 * Copyright (C) 2013, 2014 - University of Massachusetts Amherst
 * Contact: arun@cs.umass.edu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *
 * Initial developer(s): Arun Venkataramani, Aditya Yadav, Emmanuel Cecchet.
 * Contributor(s): ______________________.
 *
 *******************************************************************************/

package edu.umass.cs.msocket.proxy.forwarder;

import org.json.JSONArray;
import org.json.JSONException;

/**
 * This is a static class, which keeps various proxy load statistics
 * 
 * @author <a href="mailto:cecchet@cs.umass.edu">Emmanuel Cecchet</a>
 * @version 1.0
 */
public class ProxyLoadStatistics {

    /*
     * is the number of splices that proxy does
     */
    private static int numTCPConnectionsAtProxy = 0;

    /*
     * number of bytes transferred per sec through the splicers
     */
    // private static int proxyThoughput=0;

    /* curr value */
    private static int currThroughput = 0;

    /*
     * last second value, subtraction gives throughput
     */
    private static int numBytesSplicedLastSec = 0;

    /**
     * Get the number of open TCP connections at the proxy
     * 
     * @return number of connections
     */
    public synchronized static int getOpenTcpConn() {
        return numTCPConnectionsAtProxy;
    }

    /**
     * Sets the number of TCP connections open at the proxy
     * 
     * @param value the new throughput value
     */
    public synchronized static void setOpenTcpConn(int value) {
        numTCPConnectionsAtProxy = value;
    }

    /**
     * Add the given number of TCP connections to the total of open connections at
     * the proxy
     * 
     * @param value the number of open connections to add
     */
    public synchronized static void addOpenTcpConn(int value) {
        numTCPConnectionsAtProxy += value;
    }

    /**
     * Get the current throughput at the proxy
     * 
     * @return current throughput in bytes/sec?
     */
    public synchronized static int getCurrentThroughput() {
        return currThroughput;
    }

    /**
     * Sets the current throughput at the proxy
     * 
     * @param value the new number of open connections
     */
    public synchronized static void setCurrentThroughput(int value) {
        currThroughput = value;
    }

    /**
     * Add the given number of TCP connections to the total of open connections at
     * the proxy
     * 
     * @param value the number of open connections to add
     */
    public synchronized static void updateCurrentThroughput(int value) {
        currThroughput = value - numBytesSplicedLastSec;
        numBytesSplicedLastSec = value;
    }

    /**
     * Return the current load information serialized into a String
     * 
     * @return a String with the load information
     */
    public synchronized static JSONArray serializeLoadInformation() {
        return new JSONArray().put(numTCPConnectionsAtProxy).put(currThroughput);
    }

    /**
     * Return the number of open TCP connections from the serialized form of stats
     * generated by {@link #serializeLoadInformation()}
     * 
     * @param load
     * @return
     * @throws JSONException
     */
    public static int getOpenTcpConnFromSerializedInfo(JSONArray load) throws JSONException {
        // TCP conn is the 1st element
        return load.getInt(0);
    }

    /**
     * Return the throughput from the serialized form of stats generated by
     * {@link #serializeLoadInformation()}
     * 
     * @param load
     * @return
     * @throws JSONException
     */
    public static int getThroughputFromSerializedInfo(JSONArray load) throws JSONException {
        // Throughput is the 2nd element
        return load.getInt(1);
    }

}