Package eu.mosaic_cloud.sunutilities

Source Code of eu.mosaic_cloud.sunutilities.ConsumerByteBenchmarkWithTimeInizio$LifeCycleHandler

//###
//# #%L
//# mosaic-java-sun-utilities
//# %%
//# 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.sunutilities;

import java.util.Vector;

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.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import eu.mosaic_cloud.platform.core.utils.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;


public class ConsumerByteBenchmarkWithTimeInizio
{
  public static final class AmqpPublisherCallback
      extends DefaultAmqpPublisherConnectorCallback<ConsumerByteBenchmarkWithTimeInizioContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ConsumerBytePublisher destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ConsumerBytePublisher initialized successfully.");
     
   
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> publishSucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final GenericCallbackCompletionArguments<Void> arguments)
    {
      //context.cloudlet.destroy ();
      this.logger.info ("ConsumerByte publisher successfully.");
     
      if(context.countinizio==1167){
       
        context.inizio=0;
        ListaTempi value_inizio= new ListaTempi(context.tempi_iniziali);
        context.kvStoretempi.set(context.keyinizio, value_inizio, null);
      }
     
      return ICallback.SUCCESS;
    }
  }
 
 
  public static final class AmqpConsumerCallback
  extends DefaultAmqpQueueConsumerConnectorCallback<ConsumerByteBenchmarkWithTimeInizioContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> consume (final ConsumerByteBenchmarkWithTimeInizioContext context, final AmqpQueueConsumeCallbackArguments<byte[]> arguments)
{
  final byte[] val = arguments.getMessage ();
 
  this.logger.info ("\n\nConsumerByte received  message Byte:"+val);
 
  String data = new String(val);
 
  this.logger.info ("\n\nConsumerByte received  message String:"+data);
 
  context.countinizio++;
  this.logger.info("Count Inizio:"+context.countinizio);
  context.inizio = System.currentTimeMillis();
  context.tempi_iniziali.add(context.countinizio,context.inizio);
 
  context.publisher.publish(data, null);
 
 
  context.consumer.acknowledge (arguments.getToken ());
 
 
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> destroySucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CallbackArguments arguments)
{
  this.logger.info ("ConsumerByteConsumer  destroyed successfully.");
  return ICallback.SUCCESS;
}

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

public CallbackCompletion<Void> acknowledgeSucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//  context.cloudlet.destroy ();
  return ICallback.SUCCESS;
}
}
 
 
 
 
  //KV Store Tempi
 
      public static final class KeyValueCallbackTempi
      extends DefaultKvStoreConnectorCallback<ConsumerByteBenchmarkWithTimeInizioContext, ListaTempi, Void>
    {
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CallbackArguments arguments)
    {
      this.logger.info ("KeyValueTempi destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CallbackArguments arguments)
    {
      this.logger.info ("KeyValueTempi initialized successfully");
        return ICallback.SUCCESS;
    }

    public CallbackCompletion<Void> setSucceeded(final ConsumerByteBenchmarkWithTimeInizioContext context, final KvStoreCallbackCompletionArguments<ListaTempi,Void> arguments) {
      this.logger.info("\n\nSet nel KV Store Contenente i Tempi memorizzati Succeeded\n\n");
      return ICallback.SUCCESS;
    }

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

    public CallbackCompletion<Void> getSucceeded(final ConsumerByteBenchmarkWithTimeInizioContext context,final KvStoreCallbackCompletionArguments<ListaTempi,Void> arguments) {
     
      this.logger.info("\n\nGet nel KV Store Contenente i Tempi memorizzati Succeeded\n\n")
      return ICallback.SUCCESS;
    }

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

    }
 
 
  public static final class LifeCycleHandler
      extends DefaultCloudletCallback<ConsumerByteBenchmarkWithTimeInizioContext>
  {
    @Override
    public CallbackCompletion<Void> destroy (final ConsumerByteBenchmarkWithTimeInizioContext context, final CloudletCallbackArguments<ConsumerByteBenchmarkWithTimeInizioContext> arguments)
    {
      this.logger.info ("ConsumerByte is being destroyed...");
      return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CloudletCallbackCompletionArguments<ConsumerByteBenchmarkWithTimeInizioContext> arguments)
    {
      this.logger.info ("ConsumerByte was destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize (final ConsumerByteBenchmarkWithTimeInizioContext context, final CloudletCallbackArguments<ConsumerByteBenchmarkWithTimeInizioContext> arguments)
    {
      this.logger.info ("ConsumerByte initializing...");
      context.cloudlet = arguments.getCloudlet ();
      final IConfiguration configuration = context.cloudlet.getConfiguration ();
      final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuemhttpgw.consumer"));
      final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queueclient.publisher"));
      context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallback (), context);
      context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration, byte[].class, NullDataEncoder.create(), new AmqpConsumerCallback (), context);
      final IConfiguration kvConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("timestore"));
      context.kvStoretempi = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration2, ListaTempi.class, JsonDataEncoder.create(ListaTempi.class) , new KeyValueCallbackTempi (), context);
      return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize (),context.kvStoretempi.initialize());
    }
   
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ConsumerByteBenchmarkWithTimeInizioContext context, final CloudletCallbackCompletionArguments<ConsumerByteBenchmarkWithTimeInizioContext> arguments)
    {
      this.logger.info ("ConsumerByte initialized successfully.");
      context.tempi_iniziali.setSize(1170);
     
      return ICallback.SUCCESS;
    }
  }
 
  public static final class ConsumerByteBenchmarkWithTimeInizioContext
  {
    ICloudletController<ConsumerByteBenchmarkWithTimeInizioContext> cloudlet;
    IAmqpQueuePublisherConnector<String, Void> publisher;
    IAmqpQueueConsumerConnector<byte[], Void> consumer;
    IKvStoreConnector<ListaTempi, Void> kvStoretempi;
    int countinizio=0;
    long inizio=0;
    Vector<Long> tempi_iniziali=new Vector<Long>();
    String keyinizio="inizio";
   
  }
}
TOP

Related Classes of eu.mosaic_cloud.sunutilities.ConsumerByteBenchmarkWithTimeInizio$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.