Package eu.mosaic_cloud.benchmarks

Source Code of eu.mosaic_cloud.benchmarks.ResultCloudlet$KeyValueCallback

//###
//# #%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.examples.cloudlets.simple.ConsumerCloudlet.AmqpConsumerCallback;
//import eu.mosaic_cloud.examples.cloudlets.simple.ConsumerCloudlet.ConsumerCloudletContext;
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.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 ResultCloudlet
{
  public static final class AmqpConsumerCallback
      extends DefaultAmqpQueueConsumerConnectorCallback<ResultCloudletContext, String, Void>
  {
    @Override
    public CallbackCompletion<Void> consume (final ResultCloudletContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
    {
      final String data = arguments.getMessage ();
      this.logger.info ("ConsumerResult received message for Controller", data);
      this.logger.info("\n\nMessage Ricevuto:"+data);
      this.logger.info("\n\n");
     
      context.object=context.gson.fromJson(data,JsonObjectfromString.class);
     
      if (context.object.getType().equals("Stopping")) {
       
      context.typetemp= "Stopping"
     
      context.tmpSetResult.add(context.object.getResult());
     
      context.kvStore.get(context.object.getId(), null);
     
      context.consumer.acknowledge (arguments.getToken ());
     
      } else {
       
        context.typetemp= "Plot";
       
        context.kvStore.get(context.object.getId(), null);
       
        context.consumer.acknowledge (arguments.getToken ());
      }
     
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ResultCloudlet  destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ResultCloudlet initialized successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> acknowledgeSucceeded (final ResultCloudletContext context, final GenericCallbackCompletionArguments<Void> arguments)
    {
      //context.cloudlet.destroy ();
      return ICallback.SUCCESS;
    }
  }
 
 
  public static final class KeyValueCallback
      extends DefaultKvStoreConnectorCallback<ResultCloudletContext, ListaResult, Void>
  {
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ResultCloudlet store destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
     
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("KeyValueResult initialized successfully");
        return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> setSucceeded(final ResultCloudletContext context, final KvStoreCallbackCompletionArguments<ListaResult,Void> arguments) {
      this.logger.info("\n\nSet nel KV Store Contenente i Risultati memorizzati Succeeded\n\n");
      String k=arguments.getKey();
      this.logger.info("\n\nKEY:"+k);
      this.logger.info("\n\n");
      ListaResult value=(ListaResult)arguments.getValue();
      this.logger.info("\n\nResult:"+value.getRes());
      this.logger.info("\n\n");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> deleteSucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ResultCloudlet store delete successfully.");
      return ICallback.SUCCESS;
    }
   
    public CallbackCompletion<Void> getSucceeded(final ResultCloudletContext context,final KvStoreCallbackCompletionArguments<ListaResult,Void> arguments) {
     
      this.logger.info("\n\nGet nel KV Store Contenente i Risultati memorizzati Succeeded\n\n");
      String key =(String) arguments.getKey();
      ListaResult value=(ListaResult) arguments.getValue();
     
      if (context.typetemp.equals("Stopping"))
      {
      value.getRes().removeAllElements();
      value.getRes().addAll(0,context.tmpSetResult);
      context.kvStore.set(key, value, null);
   
     
      }else {
       
        this.logger.info("Risultati richiesti dal plot:"+value.getRes());
        // state.Output.print(value.getRes());
             
    }
  return ICallback.SUCCESS;
  }
   
    public CallbackCompletion<Void> ungetSucceeded (final ResultCloudletContext context, final CallbackArguments arguments)
    {
      this.logger.info ("ResultCloudlet ungetSucceeded.");
      return ICallback.SUCCESS;
    }
   
  }
 
  public static final class LifeCycleHandler
      extends DefaultCloudletCallback<ResultCloudletContext>
  {
    @Override
    public CallbackCompletion<Void> destroy (final ResultCloudletContext context, final CloudletCallbackArguments<ResultCloudletContext> arguments)
    {
      this.logger.info ("ResultCloudlet destroying...");
      return CallbackCompletion.createAndChained (context.kvStore.destroy (), context.consumer.destroy ());
    }
   
    @Override
    public CallbackCompletion<Void> destroySucceeded (final ResultCloudletContext context, final CloudletCallbackCompletionArguments<ResultCloudletContext> arguments)
    {
      this.logger.info ("ResultCloudlet destroyed successfully.");
      return ICallback.SUCCESS;
    }
   
    @Override
    public CallbackCompletion<Void> initialize (final ResultCloudletContext context, final CloudletCallbackArguments<ResultCloudletContext> arguments)
    {
      this.logger.info ("ResultCloudlet 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, ListaResult.class, JsonDataEncoder.create (ListaResult.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);
      return CallbackCompletion.createAndChained (context.kvStore.initialize (), context.consumer.initialize ());
    }
   
    @Override
    public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletContext context, final CloudletCallbackCompletionArguments<ResultCloudletContext> arguments)
    {
     
      this.logger.info ("ResultCloudlet initialized successfully.");
      context.list=new ListaResult();
      context.object=new JsonObjectfromString();
      context.gson=new Gson();
      context.kvStore.set(context.vectorListKey,context.list, null);
      return ICallback.SUCCESS;
    }
  }
 
  public static final class ResultCloudletContext
  {
    ICloudletController<ResultCloudletContext> cloudlet;
    IAmqpQueueConsumerConnector<String, Void> consumer;
    IKvStoreConnector<ListaResult, Void> kvStore;
    ListaResult list;
    String typetemp;
    Vector <String> tmpSetResult=new Vector <String>();
    Gson gson;
    JsonObjectfromString object;
    String vectorListKey="2";
  }
}
TOP

Related Classes of eu.mosaic_cloud.benchmarks.ResultCloudlet$KeyValueCallback

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.