Package eu.mosaic_cloud.sunutilities

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

//###
//# #%L
//# mosaic-mjades
//# %%
//# 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 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.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.sunutilities.ConsumerCountcharacters.KeyValueCallback;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import java.util.Vector;

import com.google.gson.Gson;


public class XmlAnalyzerCloudletKvWithTime
{
 
  // Consumer dal client
  public static final class AmqpConsumerCallback
      extends DefaultAmqpQueueConsumerConnectorCallback<XmlAnalyzerCloudletKvWithTimeContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> consume (final XmlAnalyzerCloudletKvWithTimeContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
    {
      context.countinizio++;
      this.logger.info("Count Inizio:"+context.countinizio);
      context.inizio = System.currentTimeMillis();
      context.tempi_iniziali.add(context.countinizio,context.inizio);
     
      final String data = arguments.getMessage ();
     
      context.object=context.gson.fromJson(data,JsonObjectfromString.class);
     
      this.logger.info("\nDato ricevuto da XmlAnalyzerCloudletKv:"+context.object.getParameters(1)+"\n");
     
      context.Count=CountElements.count(context.object.getParameters(1));
     
      context.da=new Dato(data,context.Count,context.iter);
     
      this.logger.info("\nSet nel Kv store oggetto Dato\n");
     
      context.kvStore.set(context.keybase, context.da, null);
     
      context.iter=Integer.toString((Integer.parseInt(context.iter))+1);
     
     
      context.consumer.acknowledge (arguments.getToken ());
     
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
    {
      this.logger.info ("XmlAnalyzerCloudletKv  destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
    {
      this.logger.info ("XmlAnalyzerCloudletKv ConsumerClient initialized successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> acknowledgeSucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final GenericCallbackCompletionArguments<Void> arguments)
    {
    //  context.cloudlet.destroy ();
      return ICallback.SUCCESS;
    }
  }
 
 
 
 
  //Push verso il WG
 
  public static final class AmqpPublisherCallback
  extends DefaultAmqpPublisherConnectorCallback<XmlAnalyzerCloudletKvWithTimeContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
{
  this.logger.info ("XmlAnalyzerCloudletKv publisher  destroyed successfully.");
  return ICallback.SUCCESS;
}

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

public CallbackCompletion<Void> publishSucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final GenericCallbackCompletionArguments<String> arguments)
{
  this.logger.info ("XmlAnalyzerCloudletKv  publisher push successfully");
  this.logger.info("\nPublisher Valutazione OK\n");
  //context.cloudlet.destroy ();
  return ICallback.SUCCESS;
}
}
 
  //KV Store
 
  public static final class KeyValueCallback
  extends DefaultKvStoreConnectorCallback<XmlAnalyzerCloudletKvWithTimeContext, Dato, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
{
  this.logger.info ("KeyValueResult destroyed successfully.");
  return ICallback.SUCCESS;
}

 

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

public CallbackCompletion<Void> setSucceeded(final XmlAnalyzerCloudletKvWithTimeContext context, final KvStoreCallbackCompletionArguments<Dato,Void> arguments) {
  context.countfine++;
  context.fine = System.currentTimeMillis();
  context.tempi_finali.add(context.countfine,context.fine);
  this.logger.info("Count Fine:"+context.countfine);
  this.logger.info("\n\nSet nel KV Store Contenente i Risultati(Xml,Ntag) memorizzati Succeeded\n\n");
  String k=arguments.getKey();
  this.logger.info("\n\nKEY:"+k);
  this.logger.info("\n\n");
  Dato dat=(Dato)arguments.getValue();
  this.logger.info("\n\nResultati Ntag:"+dat.getNtag());
  this.logger.info("\n\nResultati nml:"+dat.getxml());
  this.logger.info("\n\n")
  if (context.countfine==1167 && context.countinizio==1167){
   
    context.fine=0;
    context.inizio=0;
   
    ListaTempi value_inizio= new ListaTempi(context.tempi_iniziali);
    ListaTempi value_finale= new ListaTempi(context.tempi_finali);
   
    context.kvStoretempi.set(context.keyinizio, value_inizio, null);
    context.kvStoretempi.set(context.keyfine, value_finale, null);
    //Set dei due vettori nel kvStore;
   
  }
  //this.logger.info("\nPubblicazione Numero elementi contati:\n"+context.Count);
  //context.publisher.publish(context.Count, null);
 
  return ICallback.SUCCESS;
}

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

public CallbackCompletion<Void> getSucceeded(final XmlAnalyzerCloudletKvWithTimeContext context,final KvStoreCallbackCompletionArguments<Dato,Void> arguments) {
 
  this.logger.info("\n\nGet nel KV Store Contenente i Risultati(Xml,Ntag) memorizzati Succeeded\n\n");
  String key =(String) arguments.getKey();
  Dato dat1=(Dato)arguments.getValue();
  this.logger.info("\n\nResultati Ntag:"+dat1.getNtag());
  this.logger.info("\n\nResultati nml:"+dat1.getxml());
  this.logger.info("\n\n");
 
 
return ICallback.SUCCESS;
}

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

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

  public CallbackCompletion<Void> setSucceeded(final XmlAnalyzerCloudletKvWithTimeContext 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 XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueTempi store delete successfully.");
    return ICallback.SUCCESS;
  }

  public CallbackCompletion<Void> getSucceeded(final XmlAnalyzerCloudletKvWithTimeContext 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 XmlAnalyzerCloudletKvWithTimeContext context, final CallbackArguments arguments)
  {
    this.logger.info ("KeyValueTempi ungetSucceeded.");
    return ICallback.SUCCESS;
  }

  }
 
 
 
 
 
 
  public static final class LifeCycleHandler
      extends DefaultCloudletCallback<XmlAnalyzerCloudletKvWithTimeContext>
  {
    @Override
    public CallbackCompletion<Void> destroy (final XmlAnalyzerCloudletKvWithTimeContext context, final CloudletCallbackArguments<XmlAnalyzerCloudletKvWithTimeContext> arguments)
    {
      this.logger.info ("XmlAnalyzerCloudletKv destroying...");
      return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy(),context.kvStore.destroy());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CloudletCallbackCompletionArguments<XmlAnalyzerCloudletKvWithTimeContext> arguments)
    {
      this.logger.info ("XmlAnalyzerCloudletKv destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize (final XmlAnalyzerCloudletKvWithTimeContext context, final CloudletCallbackArguments<XmlAnalyzerCloudletKvWithTimeContext> arguments)
    {
      this.logger.info ("XmlAnalyzerCloudletKv initializing...");
      context.cloudlet = arguments.getCloudlet ();
      final IConfiguration configuration = context.cloudlet.getConfiguration ();
      final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue1.consumer"));
      context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration2, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallback (), context);
      final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue2.publisher"));
      context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpPublisherCallback (), context);
      final IConfiguration kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstore"));
      context.kvStore = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, Dato.class, JsonDataEncoder.create(Dato.class) , new KeyValueCallback (), 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.kvStore.initialize(),context.kvStoretempi.initialize());
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final XmlAnalyzerCloudletKvWithTimeContext context, final CloudletCallbackCompletionArguments<XmlAnalyzerCloudletKvWithTimeContext> arguments)
    {
     
      this.logger.info ("XmlAnalyzerCloudletKv initialized successfully.");
      context.object=new JsonObjectfromString();
      context.gson=new Gson();
      context.tempi_iniziali.setSize(3000);
      context.tempi_finali.setSize(3000);
      return ICallback.SUCCESS;
    }
  }
 
  public static final class XmlAnalyzerCloudletKvWithTimeContext
  {
    ICloudletController<XmlAnalyzerCloudletKvWithTimeContext> cloudlet;
    IAmqpQueueConsumerConnector<String, Void> consumer;
    IAmqpQueuePublisherConnector<String, Void> publisher;
    IKvStoreConnector<Dato, Void> kvStore;
    IKvStoreConnector<ListaTempi, Void> kvStoretempi;
    Dato da;
    String Count="0";
    String iter="1";
    String keybase="2";
    int countinizio=0;
    int countfine=0;
    long inizio=0;
    long fine=0;
    public Gson gson;
    public JsonObjectfromString object;
    Vector<Long> tempi_iniziali=new Vector<Long>();
    Vector<Long> tempi_finali=new Vector<Long>();
    String keyinizio="inizio";
    String keyfine="fine";
   
  }
}
TOP

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