//###
//# #%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 ResultCloudletString
{
public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<ResultCloudletStringContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final ResultCloudletStringContext 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.val=context.val+context.object.getResult()+" ";
context.kvStore.set(context.object.getId(), context.val, null);
//context.consumer.acknowledge (arguments.getToken ());
} else {
// context.typetemp= "Plot";
context.kvStore.get(context.object.getId(), null);
// context.consumer.acknowledge (arguments.getToken ());
}
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudletString destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudletString initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ResultCloudletStringContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
public static final class KeyValueCallback
extends DefaultKvStoreConnectorCallback<ResultCloudletStringContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudletString store destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final ResultCloudletStringContext context, final KvStoreCallbackCompletionArguments<String,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");
String value=(String)arguments.getValue();
this.logger.info("\n\nResult:"+value);
this.logger.info("\n\n");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudletString store delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final ResultCloudletStringContext context,final KvStoreCallbackCompletionArguments<String,Void> arguments) {
this.logger.info("\n\nGet nel KV Store Contenente i Risultati memorizzati Succeeded\n\n");
String key =(String) arguments.getKey();
String value1=(String) arguments.getValue();
String value="The Benchmark Results in ms are:"+" "+value1;
byte[] vallbyte=null;
try {
vallbyte = value.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.kvStorebyte.set(key, vallbyte, null);
this.logger.info("Risultati richiesti dal plot:"+value);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("ResultCloudletString ungetSucceeded.");
return ICallback.SUCCESS;
}
}
//KvStoreByte
public static final class KeyValueCallbackbyte
extends DefaultKvStoreConnectorCallback<ResultCloudletStringContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResultByte destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResultByte initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final ResultCloudletStringContext context, final KvStoreCallbackCompletionArguments<byte[],Void> arguments) {
this.logger.info("\n\nSet nel KV Store Contenente i Risultati 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");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResultByte store delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final ResultCloudletStringContext context,final KvStoreCallbackCompletionArguments<byte[],Void> arguments) {
this.logger.info("\n\nGet nel KV Store Contenente i Risultati memorizzati in Byte Succeeded\n\n");
String key =(String) arguments.getKey();
byte[] value=(byte[])arguments.getValue();
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final ResultCloudletStringContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult ungetSucceeded.");
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<ResultCloudletStringContext>
{
@Override
public CallbackCompletion<Void> destroy (final ResultCloudletStringContext context, final CloudletCallbackArguments<ResultCloudletStringContext> arguments)
{
this.logger.info ("ResultCloudletString destroying...");
return CallbackCompletion.createAndChained (context.kvStore.destroy (), context.consumer.destroy ());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ResultCloudletStringContext context, final CloudletCallbackCompletionArguments<ResultCloudletStringContext> arguments)
{
this.logger.info ("ResultCloudletString destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final ResultCloudletStringContext context, final CloudletCallbackArguments<ResultCloudletStringContext> arguments)
{
this.logger.info ("ResultCloudletString 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 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.kvStorebyte.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ResultCloudletStringContext context, final CloudletCallbackCompletionArguments<ResultCloudletStringContext> arguments)
{
this.logger.info ("ResultCloudletString initialized successfully.");
context.list=new ListaResult();
context.object=new JsonObjectfromString();
context.gson=new Gson();
return ICallback.SUCCESS;
}
}
public static final class ResultCloudletStringContext
{
ICloudletController<ResultCloudletStringContext> cloudlet;
IAmqpQueueConsumerConnector<String, Void> consumer;
IKvStoreConnector<String, Void> kvStore;
IKvStoreConnector<byte[], Void> kvStorebyte;
ListaResult list;
String typetemp;
Vector <String> tmpSetResult=new Vector <String>();
Gson gson;
JsonObjectfromString object;
String vectorListKey="2";
String val=" ";
}
}