Skip to content

Commit

Permalink
Calculate a serial number for each simulator instance. This makes it
Browse files Browse the repository at this point in the history
possible to use the serial number to identify an instance of simulator
during and after restarted from a shutdown/crash.

A simulator instance serial number is calculated as follows (separated
by '-'.

SERIAL_PREFIX + ip + khomeHash + persistHome

where SERIAL_PREFIX is a constant,
ip is the ip of the simualtor,
khomeHash is the home (hash code) of the simulator instance,
persistHome is the persist home name of the instance set in the
SimulatorConfiguration.
  • Loading branch information
chiaming2000 committed Sep 11, 2014
1 parent 5928b5c commit 06f0103
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.seagate.kinetic.simulator.internal;

import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
Expand All @@ -29,7 +30,6 @@
import com.seagate.kinetic.heartbeat.message.ByteCounter;
import com.seagate.kinetic.heartbeat.message.OperationCounter;
import com.seagate.kinetic.proto.Kinetic.Command;

import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Capacity;
import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Configuration;
import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Limits;
Expand Down Expand Up @@ -93,7 +93,7 @@ public static boolean checkPermission(KineticMessage request,
return hasPermission;
}

public static void handleGetLog(SimulatorEngine engine, KineticMessage request, KineticMessage kmresp) throws UnknownHostException {
public static void handleGetLog(SimulatorEngine engine, KineticMessage request, KineticMessage kmresp) throws UnknownHostException, UnsupportedEncodingException {

Command.Builder respCommandBuilder = (Command.Builder) kmresp.getCommand();

Expand Down Expand Up @@ -126,7 +126,7 @@ public static void handleGetLog(SimulatorEngine engine, KineticMessage request,
}
break;
case CONFIGURATION:
Configuration configuration = ConfigurationUtil.getConfiguration(engine.getServiceConfiguration());
Configuration configuration = ConfigurationUtil.getConfiguration(engine);
getLog.setConfiguration(configuration);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,10 @@ public ConnectionInfo registerNewConnection(ChannelHandlerContext ctx) {

try {
Configuration configuration = ConfigurationUtil
.getConfiguration(this.config);
.getConfiguration(this);
cb.getBodyBuilder().getGetLogBuilder().getConfigurationBuilder()
.mergeFrom(configuration);
} catch (UnknownHostException e) {
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage(), e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
*/
package com.seagate.kinetic.simulator.utility;

import java.io.UnsupportedEncodingException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
Expand All @@ -37,26 +38,39 @@
import com.google.protobuf.ByteString;
import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Configuration;
import com.seagate.kinetic.proto.Kinetic.Command.GetLog.Configuration.Interface;
import com.seagate.kinetic.simulator.internal.SimulatorEngine;

public abstract class ConfigurationUtil {

private final static Logger logger = Logger
.getLogger(ConfigurationUtil.class.getName());
public final static String VENDER = "Seagate";
public final static String MODEL = "Simulator";
public final static byte[] SERIAL_NUMBER = "93C3DAFD-C894-3C88-A4B0-632A90D2A04B"
.getBytes(Charset.forName("UTF-8"));

public static final String SERIAL_PREFIX = "SIMULATOR-SN-";

//public final static byte[] SERIAL_NUMBER = "93C3DAFD-C894-3C88-A4B0-632A90D2A04B"
// .getBytes(Charset.forName("UTF-8"));

public final static String COMPILATION_DATE = new Date().toString();
public final static String PROTOCOL_COMPILATION_DATE = new Date()
.toString();

@SuppressWarnings("static-access")
public static Configuration getConfiguration(SimulatorConfiguration config)
throws UnknownHostException {
public static Configuration getConfiguration(SimulatorEngine engine)
throws UnknownHostException, UnsupportedEncodingException {

SimulatorConfiguration config = engine.getServiceConfiguration();

Configuration.Builder configuration = Configuration.newBuilder();
configuration.setVendor(VENDER);
configuration.setModel(MODEL);
configuration.setSerialNumber(ByteString.copyFrom(SERIAL_NUMBER));

// get serial no for this instance
String sn = getSerialNumber(engine);

configuration.setSerialNumber(ByteString.copyFrom(sn, "UTF8"));

configuration.setCompilationDate(COMPILATION_DATE);
configuration.setProtocolCompilationDate(PROTOCOL_COMPILATION_DATE);

Expand Down Expand Up @@ -145,4 +159,52 @@ private static String bytesToStringMac(byte[] mac) {

return sb.toString();
}

/**
* calculate serial no.
* <p>
* A simulator instance serial number is calculated as follows.
* <p>
* SN = SERIAL_PREFIX + ip + "-" + khomeHash + "-" + persistHome;
*
* @param engine simulator engine
*
* @return serial number for the specified instance of simulator
*/
private static String getSerialNumber (SimulatorEngine engine) {

SimulatorConfiguration config = engine.getServiceConfiguration();

int khomeHash = Math.abs(engine.getKineticHome().hashCode());

// get persist home name, use port# if not set
String persistHome = config.getProperty(SimulatorConfiguration.PERSIST_HOME, String.valueOf(config.getPort()));

//int phomeHash = Math.abs(persistHome.hashCode());

// default ip of this instance
String ip = "127.0.0.1";

try {
// get from Java API
ip = InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
;
}

// construct sn
String sn = SERIAL_PREFIX + ip + "-" + khomeHash + "-" + persistHome;

// replace '_' with '-'
sn = sn.replace('_', '-');

// replace '.' with '-'
sn = sn.replace('.', '-');

// convert to upper case
sn = sn.toUpperCase();

// return sn for this instance
return sn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class VirtualDrives {

public static final int MAX_SIMULATOR = 1000;
public static final int MAX_SIMULATOR = 100;

private int maxSimulator = MAX_SIMULATOR;

Expand Down

0 comments on commit 06f0103

Please sign in to comment.