Package eu.mosaic_cloud.benchmarks

Source Code of eu.mosaic_cloud.benchmarks.DebugErrorKv$LifeCycleHandler

//###
//# #%L
//# mosaic-java-benchmarks
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli
//#Authors: Massimiliano Rak, Giuseppe Aversano
//# %%
//# 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.
//# #L%
//###

package eu.mosaic_cloud.benchmarks;



import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnector;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.KvStoreCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.AmqpQueueConsumeCallbackArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnectorFactory;
import eu.mosaic_cloud.cloudlets.core.CallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.GenericCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.ICallback;
import eu.mosaic_cloud.cloudlets.core.ICloudletController;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultCloudletCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultKvStoreConnectorCallback;
import eu.mosaic_cloud.platform.core.configuration.ConfigUtils;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import java.util.Vector;
import java.io.*;

import com.google.gson.Gson;

public class DebugErrorKv
{
  public static final class AmqpConsumerCallback
      extends DefaultAmqpQueueConsumerConnectorCallback<DebugErrorKvContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> consume (final DebugErrorKvContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
    {
      final String data = arguments.getMessage ();
      this.logger.info ("DebugErrorKv received message for WGCloudlet", data);
      this.logger.info("\n\nMessage Ricevuto:"+data);
      this.logger.info("\n\n");
     
      context.object=context.gson.fromJson(data,JsonObjectfromString.class);
     
      context.NumIter=Integer.parseInt(context.object.getParameters(0));
      context.Oper1=Integer.parseInt(context.object.getParameters(2));
      context.Oper2=Integer.parseInt(context.object.getParameters(4));
     
      if ((context.object.getParameters(1).equals("set")) && (context.object.getParameters(3).equals("get"))) {
       
      for(int i=0;i<context.NumIter;i++)
      {
       
        for(int j=0;j<context.Oper1;j++)
        {
          //context.KeySet=Integer.toString((j+(Oper1*i)));
          context.kvStore.set(context.KeySet, context.KeySet);
          context.KeySet=Integer.toString((Integer.parseInt(context.KeySet))+1);
        }
       
        for(int k=0;k<context.Oper2;k++)
        {
         
          //context.KeyGet=Integer.toString((k+(Oper2*i)));
          context.kvStore.get(context.KeyGet);
          context.KeyGet=Integer.toString((Integer.parseInt(context.KeyGet))+1);
        }
       
      }
     
     
     
      } else {
       
        for(int i=0;i<context.NumIter;i++)
        {
         
          for(int k=0;k<context.Oper1;k++)
          {
           
          //  context.KeyGet=Integer.toString((k+(Oper1*i)));
            context.kvStore.get(context.KeyGet);
            context.KeyGet=Integer.toString((Integer.parseInt(context.KeyGet))+1);
          }
         
          for(int j=0;j<context.Oper2;j++)
          {
            //context.KeySet=Integer.toString((j+(Oper2*i)));
            context.kvStore.set(context.KeySet, context.KeySet);
            context.KeySet=Integer.toString((Integer.parseInt(context.KeySet))+1);
          }
         
         
        }

      }
           
      context.consumer.acknowledge (arguments.getToken ());
   
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("DebugErrorKv  destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("DebugErrorKv initialized successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> acknowledgeSucceeded (final DebugErrorKvContext context, final GenericCallbackCompletionArguments<Void> arguments)
    {
      //context.cloudlet.destroy ();
      return ICallback.SUCCESS;
    }
  }
 
 
  public static final class KeyValueCallback
      extends DefaultKvStoreConnectorCallback<DebugErrorKvContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> destroySucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("DebugErrorKv store destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
     
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("KeyValueResult initialized successfully");
        return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> setSucceeded(final DebugErrorKvContext context, final KvStoreCallbackCompletionArguments<String,Void> arguments) {
      this.logger.info("\n\nSet nel KV Store del DebugErrorKv Succeeded\n\n");
      String k=arguments.getKey();
      this.logger.info("\n\nKEY:"+k);
      this.logger.info("\n\n");
      String value=(String)arguments.getValue();
      this.logger.info("\n\nValue:"+value);
      this.logger.info("\n\n");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> deleteSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("DebugErrorKv store delete successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> getSucceeded(final DebugErrorKvContext context,final KvStoreCallbackCompletionArguments<String,Void> arguments) {
     
      this.logger.info("\n\nGet nel KV Store del DebugErrorKv Succeeded\n\n");
      String key =(String) arguments.getKey();
      this.logger.info("\n\nKEY:"+key);
      this.logger.info("\n\n");
      String value=(String) arguments.getValue();
      this.logger.info("\n\nValue:"+value);
      this.logger.info("\n\n");
      context.count++;
      if (value==null)
      {
        context.CountError=Integer.toString((Integer.parseInt(context.CountError))+1)
        this.logger.info("\n\nNumber Error:"+context.CountError);
      }
     
      if (context.object.getParameters(3).equals("get")) 
      {
       
      if (context.count==(context.Oper2*context.NumIter))
      {
        this.logger.info("\n\nCiclo get");
        this.logger.info("\n\nConteggio Cicli :"+context.count);
        context.count=0;
      //  context.KeyGet=context.KeySet;
        this.logger.info("\n\nTotal Number Error :"+context.CountError);
        context.TotalCountError=context.TotalCountError + context.CountError+" ";
       
        byte[] CountErrorbyte=null;
        try {
          CountErrorbyte = context.TotalCountError.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
       
        context.publisher.publish("OK", null);
        context.kvStorebyte.set(context.key, CountErrorbyte, null);
      }
     
      }else
     
      {
       
        if (context.count==(context.Oper1*context.NumIter))
        {
          this.logger.info("\n\nCiclo Set");
          this.logger.info("\n\nConteggio Cicli :"+context.count);
          context.count=0;
        //  context.KeyGet=context.KeySet;
          this.logger.info("\n\nTotal Number Error :"+context.CountError);
          context.TotalCountError=context.TotalCountError + context.CountError+" ";
         
          byte[] CountErrorbyte=null;
          try {
            CountErrorbyte = context.TotalCountError.getBytes("ISO-8859-1");
          } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          context.publisher.publish("OK", null);
          context.kvStorebyte.set(context.key, CountErrorbyte, null);
        }
       
       }

  return ICallback.SUCCESS;
 
  }
   
    public CallbackCompletion<Void> ungetSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
    {
      this.logger.info ("DebugErrorKv ungetSucceeded.");
      return ICallback.SUCCESS;
    }
   
  }
 
  public static final class AmqpPublisherCallback
  extends DefaultAmqpPublisherConnectorCallback<DebugErrorKvContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
{
  this.logger.info ("DebugErrorKvPublisher destroyed successfully.");
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> initializeSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
{
  this.logger.info ("DebugErrorKvPublisher initialized successfully.");
 

 
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> publishSucceeded (final DebugErrorKvContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
  //context.cloudlet.destroy ();
  this.logger.info ("DebugErrorKvPublisher publisher successfully.");
  return ICallback.SUCCESS;
}

}

  //KvStoreByte
 
    public static final class KeyValueCallbackbyte
    extends DefaultKvStoreConnectorCallback<DebugErrorKvContext, byte[], Void>
  {
  @Override
  public CallbackCompletion<Void> destroySucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueResultByte destroyed successfully.");
    return ICallback.SUCCESS;
  }

   

  @Override
  public CallbackCompletion<Void> initializeSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueResultByte initialized successfully");
      return ICallback.SUCCESS;
  }

  public CallbackCompletion<Void> setSucceeded(final DebugErrorKvContext context, final KvStoreCallbackCompletionArguments<byte[],Void> arguments) {
    this.logger.info("\n\nSet nel KV Store Contenente il Numero di Errori memorizzati in Byte Succeeded\n\n");
    String k=arguments.getKey();
    this.logger.info("\n\nKEY:"+k);
    this.logger.info("\n\n");
    byte[] valueb=(byte[])arguments.getValue();
    String value=new String(valueb);
    this.logger.info("\n\nResultati in byte:"+valueb);
    this.logger.info("\n\nResultati in String:"+value);
    this.logger.info("\n\n");
    context.CountError="0";
    return ICallback.SUCCESS;
  }

  public CallbackCompletion<Void> deleteSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueResultByte store delete successfully.");
    return ICallback.SUCCESS;
  }

  public CallbackCompletion<Void> getSucceeded(final DebugErrorKvContext context,final KvStoreCallbackCompletionArguments<byte[],Void> arguments) {
   
    this.logger.info("\n\nGet nel KV Store Contenente il Numero di Errori memorizzati in Byte Succeeded\n\n");
    String key =(String) arguments.getKey();
    byte[] value=(byte[])arguments.getValue();
   
   
  return ICallback.SUCCESS;
  }

  public CallbackCompletion<Void> ungetSucceeded (final DebugErrorKvContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueResult ungetSucceeded.");
    return ICallback.SUCCESS;
  }

  }   
 
 
  public static final class LifeCycleHandler
      extends DefaultCloudletCallback<DebugErrorKvContext>
  {
    @Override
    public CallbackCompletion<Void> destroy (final DebugErrorKvContext context, final CloudletCallbackArguments<DebugErrorKvContext> arguments)
    {
      this.logger.info ("DebugErrorKv destroying...");
      return CallbackCompletion.createAndChained (context.kvStore.destroy (), context.consumer.destroy ());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final DebugErrorKvContext context, final CloudletCallbackCompletionArguments<DebugErrorKvContext> arguments)
    {
      this.logger.info ("DebugErrorKv destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize (final DebugErrorKvContext context, final CloudletCallbackArguments<DebugErrorKvContext> arguments)
    {
      this.logger.info ("DebugErrorKv initializing...");
      context.cloudlet = arguments.getCloudlet ();
      final IConfiguration configuration = context.cloudlet.getConfiguration ();
      final IConfiguration kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstore"));
      context.kvStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, String.class, JsonDataEncoder.create (String.class), new KeyValueCallback (), context);
      final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queueresult.consumer"));
      context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallback (), context);
      final IConfiguration queueConfiguration2 =  configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuefine.publisher"));
      context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallback (), context);
      final IConfiguration kvConfigurationbyte = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstorebyte"));
      context.kvStorebyte = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfigurationbyte, byte[].class, NullDataEncoder.create (), new KeyValueCallbackbyte (), context);
      return CallbackCompletion.createAndChained (context.kvStore.initialize (), context.consumer.initialize (),context.publisher.initialize(),context.kvStorebyte.initialize());
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final DebugErrorKvContext context, final CloudletCallbackCompletionArguments<DebugErrorKvContext> arguments)
    {
     
      this.logger.info ("DebugErrorKv initialized successfully.");
      context.object=new JsonObjectfromString();
      context.gson=new Gson();
      return ICallback.SUCCESS;
    }
  }
 
  public static final class DebugErrorKvContext
  {
    ICloudletController<DebugErrorKvContext> cloudlet;
    IAmqpQueueConsumerConnector<String, Void> consumer;
    IKvStoreConnector<String, Void> kvStore;
    IAmqpQueuePublisherConnector<String, Void> publisher;
    IKvStoreConnector<byte[], Void> kvStorebyte;
    int NumIter=0;
    int Oper1=0;
    int Oper2=0;
    int count=0;
    String KeySet="0";
    String KeyGet="0";
    String CountError="0";
    String TotalCountError="The Number of Errors are: ";
    String key="TotalError";
    Gson gson;
    JsonObjectfromString object;
  }
}
TOP

Related Classes of eu.mosaic_cloud.benchmarks.DebugErrorKv$LifeCycleHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.