Package eu.mosaic_cloud.benchmarks

Source Code of eu.mosaic_cloud.benchmarks.WGExtractionResultMessageCloudlet$AmqpConsumerCallbackControl

//###
//# #%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.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
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 eu.mosaic_cloud.platform.core.configuration.ConfigUtils;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import java.util.Vector;
import com.google.gson.Gson;

public class WGExtractionResultMessageCloudlet

{
 
  // Consumer verso risorsa da testare
  public static final class AmqpConsumerCallback
      extends DefaultAmqpQueueConsumerConnectorCallback<WGExtractionResultMessageCloudletContext, ExtractionResultMessage, Void>
  {
    @Override
    public CallbackCompletion<Void> consumeWGExtractionResultMessageCloudletContext context,  AmqpQueueConsumeCallbackArguments<ExtractionResultMessage> arguments )
    {
      ExtractionResultMessage data = arguments.getMessage ();
      this.logger.info ("ConsumerCloudlet received  FileMessage:");
     
      //this.logger.info("\nDato ricevuto:"+data+"\n");
     
      context.verifica= context.wgb.WGCheck(context);
     
     
      if(context.verifica){   
      context.fine = System.currentTimeMillis();
      this.logger.info("\nTempo fine:"+context.fine+"\n");
      this.logger.info("\nTempoTotale:"+(context.fine-context.inizio));
     
      context.publisherev.publish("{ 'type':'Stopping','id':'2','result':'"+(context.fine-context.inizio)+"'}", null);
     
    }
     
      context.consumer.acknowledge (arguments.getToken ());
     
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> acknowledgeSucceededWGExtractionResultMessageCloudletContext context,  GenericCallbackCompletionArguments<Void> arguments ) {
      return ICallback.SUCCESS;
    }

    @Override
    public CallbackCompletion<Void> initializeSucceededWGExtractionResultMessageCloudletContext context,  CallbackArguments arguments ) {
      this.logger.info("Consumer:   initialized successfully");

      return ICallback.SUCCESS;
    }

    @Override
    public CallbackCompletion<Void> initializeFailedWGExtractionResultMessageCloudletContext context,  CallbackArguments arguments ) {
      this.logger.info("Consumer: initialization failed "+arguments.toString());

      return ICallback.SUCCESS;
    }

    @Override
    public CallbackCompletion<Void> destroySucceededWGExtractionResultMessageCloudletContext context,  CallbackArguments arguments ) {
      this.logger.info("Consumer:  unregistered successfully");

      return ICallback.SUCCESS;
    }
  }
 
 
  // Coda di ricezione dal Controller
 
  public static final class AmqpConsumerCallbackControl
  extends DefaultAmqpQueueConsumerConnectorCallback<WGExtractionResultMessageCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final WGExtractionResultMessageCloudletContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
  final String data = arguments.getMessage ();
  this.logger.info ("ConsumerCloudlet  received :", data);
 
  this.logger.info("\nDato ricevuto from Controller:"+data+"\n");

 
 
  context.object=context.gson.fromJson(data,JsonObjectfromString.class);
 
  if (context.object.getType().equals("Start")){
 
  this.logger.info("\nInvio segnale di start al Simulatore\n");
 
 
  context.wgb.WGStart(context);
 
  //context.consumerev.acknowledge (arguments.getToken ());
 
  }
 

 
  if (context.object.getType().equals("Stop")){
   
    this.logger.info("\n Invio segnale di stop al Simulatore\n");
   
    context.wgb.WGStop(context);
   
  //  context.consumerev.acknowledge (arguments.getToken ());
   
  }
 
  context.consumerev.acknowledge (arguments.getToken ());
 
  return ICallback.SUCCESS;
}

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

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

public CallbackCompletion<Void> acknowledgeSucceeded (final WGExtractionResultMessageCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//  context.cloudlet.destroy ();
  return ICallback.SUCCESS;
}

}
 
  //Push Verso Risorsa da testare
 
  public static final class AmqpPublisherCallback extends DefaultAmqpPublisherConnectorCallback<WGExtractionResultMessageCloudletContext, ExtractionResultMessage, Void>
  {
    @Override
    public CallbackCompletion<Void> initializeSucceeded( WGExtractionResultMessageCloudletContext context, CallbackArguments arguments )
    {
      this.logger.info( "Publisher:  initialized successfully" );
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded( WGExtractionResultMessageCloudletContext context, CallbackArguments arguments )
    {
      this.logger.info( "Publisher:  destroyed successfully" );
      return ICallback.SUCCESS;
    }
  }
 
  // Coda di trasmissione Stato e risultati al Controller
 
  public static final class AmqpPublisherCallbackStato
  extends DefaultAmqpPublisherConnectorCallback<WGExtractionResultMessageCloudletContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final WGExtractionResultMessageCloudletContext context, final CallbackArguments arguments)
{
  this.logger.info ("WGCloudlet publisher Stato destroyed successfully.");
  return ICallback.SUCCESS;
}

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

public CallbackCompletion<Void> publishSucceeded (final WGExtractionResultMessageCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
  this.logger.info ("PublisherStatoWGCloudlet push successfully.");
  context.wgb.WGPushController(context);
  //context.cloudlet.destroy ();
  return ICallback.SUCCESS;
}
}
 
 
 
 
 
 
 
  public static  class WGLifeCycleHandler
      extends DefaultCloudletCallback<WGExtractionResultMessageCloudletContext>
  {
   
    public void createWgBehaviour(WGExtractionResultMessageCloudletContext context) {
       context.wgb = new WGExtractionResultMessageBehaviour();
     
    }
   
    @Override
    public CallbackCompletion<Void> destroy ( WGExtractionResultMessageCloudletContext context,  CloudletCallbackArguments<WGExtractionResultMessageCloudletContext> arguments)
    {
      this.logger.info ("WGCloudlet destroying...");
      return CallbackCompletion.createAndChained (context.consumer.destroy (),context.consumerev.destroy(),context.publisher.destroy(),context.publisherev.destroy());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded ( WGExtractionResultMessageCloudletContext context,  CloudletCallbackCompletionArguments<WGExtractionResultMessageCloudletContext> arguments)
    {
      this.logger.info ("WGCloudlet destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize ( WGExtractionResultMessageCloudletContext context,  CloudletCallbackArguments<WGExtractionResultMessageCloudletContext> arguments)
    {
      this.logger.info ("WGCloudlet initializing...");
      context.cloudlet = arguments.getCloudlet ();
      final IConfiguration configuration = context.cloudlet.getConfiguration ();
      final IConfiguration queueConfiguration4 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuerequest.consumer"));
      context.consumerev = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration4, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallbackControl (), context);
      final IConfiguration queueConfiguration3 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuestate.publisher"));
      context.publisherev = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration3, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallbackStato (), context);
     
      ICloudletController<WGExtractionResultMessageCloudletContext> cloudlet = arguments.getCloudlet();
      context.cloudlet = cloudlet;
     
      IConfiguration cloudletConfiguration = cloudlet.getConfiguration ();
      IConfiguration configurationcommand = cloudlet.getConfiguration();
     
      String command = ConfigUtils.resolveParameter( configurationcommand, "source_file_processor.command", String.class, "default_command" );
      context.command = command;
     
      IConfiguration publisherConfiguration = cloudletConfiguration.spliceConfiguration( ConfigurationIdentifier.resolveAbsolute( "queue.publisher" ) );
      IConfiguration consumerConfiguration = cloudletConfiguration.spliceConfiguration(ConfigurationIdentifier.resolveAbsolute("queue.consumer"));
     
      context.publisher = cloudlet.getConnectorFactory( IAmqpQueuePublisherConnectorFactory.class ).create(
          publisherConfiguration,
          ExtractionResultMessage.class,
          JsonDataEncoder.create( ExtractionResultMessage.class ),
          new AmqpPublisherCallback(), context );
     
      context.consumer = cloudlet.getConnectorFactory(IAmqpQueueConsumerConnectorFactory.class).create(
          consumerConfiguration,
          ExtractionResultMessage.class,
          JsonDataEncoder.create( ExtractionResultMessage.class ),
          new AmqpConsumerCallback(), context);
     
      createWgBehaviour(context);
      return CallbackCompletion.createAndChained (context.consumer.initialize (),context.consumerev.initialize (),context.publisher.initialize (),context.publisherev.initialize ());
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded ( WGExtractionResultMessageCloudletContext context,  CloudletCallbackCompletionArguments<WGExtractionResultMessageCloudletContext> arguments)
    {
     
      this.logger.info ("WGCloudlet initialized successfully.");
      context.object=new JsonObjectfromString();
      context.gson=new Gson();
     
      return ICallback.SUCCESS;
    }
  }
 
  public static final class WGExtractionResultMessageCloudletContext extends ContextWithStatusStore
  {
    ICloudletController<WGExtractionResultMessageCloudletContext> cloudlet;
    IAmqpQueueConsumerConnector<ExtractionResultMessage, Void> consumer;
    IAmqpQueuePublisherConnector<ExtractionResultMessage, Void> publisher;
    IAmqpQueueConsumerConnector<String, Void> consumerev;
    IAmqpQueuePublisherConnector<String, Void> publisherev; 
    IKvStoreConnector<ListaResult, Void> kvStore;
   
    String command;
 
      public long fine;
    public long inizio;
    public  boolean verifica;
    public  WGExtractionResultMessageBehaviour wgb;
    public Gson gson;
    public JsonObjectfromString object;
  }
}
TOP

Related Classes of eu.mosaic_cloud.benchmarks.WGExtractionResultMessageCloudlet$AmqpConsumerCallbackControl

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.