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

com.gemstone.gemfire.distributed.internal.DistributedState Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.distributed.internal;

import com.gemstone.gemfire.DataSerializable;
import com.gemstone.gemfire.DataSerializer;
import com.gemstone.gemfire.internal.HeapDataOutputStream;
import com.gemstone.gemfire.internal.shared.Version;
//import com.gemstone.gemfire.InternalGemFireException;
import java.io.*;

/**
 * Contains state that is distributed among distribution managers
 * using the JGroups {@link
 * com.gemstone.org.jgroups.Channel#getState "state"} mechanism.
 * It contains information that distribution managers need when
 * starting up.
 *
 * @author David Whitlock
 *
 *
 * @since 2.1
 */
class DistributedState implements DataSerializable {
  private static final long serialVersionUID = -4776743091985815549L;

  /** The version of GemFire being used */
  private String version;

  /** The current "cache time" */
  private long cacheTime;

  /////////////////////  Static Methods  /////////////////////

  /**
   * Returns a DistributedState created from the given
   * byte array.
   *
   * @throws IOException
   *         Something went wrong while deserializing
   *         bytes 
   */
  public static DistributedState fromBytes(byte[] bytes) 
    throws IOException {

    DistributedState state = new DistributedState();

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    DataInputStream dis = new DataInputStream(bais);

    state.fromData(dis);
    return state;
  }

  /**
   * Returns a byte representation of the given
   * DistributedState. 
   *
   * @throws IOException
   *         Something went wrong while serializing state
   */
  public static byte[] toBytes(DistributedState state)
    throws IOException {

    HeapDataOutputStream hdos = new HeapDataOutputStream(256, Version.CURRENT);
    state.toData(hdos);
    return hdos.toByteArray();
  }

  //////////////////////  Constructors  //////////////////////

  /**
   * Creates a new DistributedState.  This method is
   * invoked when a member (usually the "coordinator") of the
   * JGroups group receives a {@link
   * com.gemstone.org.jgroups.GetStateEvent}.
   */
  public DistributedState() {

  }

  /////////////////////  Instance Methods  /////////////////////

  /**
   * Sets the version of GemFire being used
   */
  public void setGemFireVersion(String version) {
    this.version = version;
  }

  /**
   * Returns the version of GemFire being used (by the sender of this
   * state). 
   */
  public String getGemFireVersion() {
    return this.version;
  }

  /**
   * Sets the current "cache time"
   */
  public void setCacheTime(long cacheTime) {
    this.cacheTime = cacheTime;
  }

  /**
   * Returns the "cache time" of the sender of this state
   */
  public long getCacheTime() {
    return this.cacheTime;
  }

  /**
   * Serializes this DistributedState as data
   */
  public void toData(DataOutput out) throws IOException {
    DataSerializer.writeString(this.version, out);
    out.writeLong(this.cacheTime);
  }

  /**
   * Popuplates the DistributedState from the given
   * data. 
   */
  public void fromData(DataInput in) throws IOException {
    this.version = DataSerializer.readString(in);
    this.cacheTime = in.readLong();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy