package eu.mosaic_cloud.sunutilities;
import java.io.UnsupportedEncodingException;
import com.google.gson.Gson;
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.sunutilities.ConsumerKeyByte.KeyValueCallback;
import eu.mosaic_cloud.sunutilities.XmlAnalyzerCloudlet.AmqpConsumerCallback;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
public class ClientXmlAnalyzer {
//Push Client
public static final class AmqpPublisherCallback
extends DefaultAmqpPublisherConnectorCallback<ClientXmlAnalyzerContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytePublisher destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytePublisher initialized successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> publishSucceeded (final ClientXmlAnalyzerContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//context.cloudlet.destroy ();
this.logger.info ("ClientXmlAnalyzer publisher successfully.");
return ICallback.SUCCESS;
}
}
// Consumer Mhttpgw
public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<ClientXmlAnalyzerContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> consume (final ClientXmlAnalyzerContext context, final AmqpQueueConsumeCallbackArguments<byte[]> arguments)
{
final byte[] val = arguments.getMessage ();
String data = new String(val);
this.logger.info ("\n\nConsumerByte received message String:"+data);
context.object=context.gson.fromJson(data,JsonObjectfromString.class);
if (context.count==0){
context.count++;
context.inizio = System.currentTimeMillis();
context.publisher.publish(context.object.getParameters(1), null);
String valinizio=context.inizio+"";
byte[] iniziobyte=null;
try {
iniziobyte = valinizio.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.kvStorexml.set(context.vectorListKey2, iniziobyte, null);
}else{
context.count++;
context.publisher.publish(context.object.getParameters(1), null);
}
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerByteConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerByteConsumer initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ClientXmlAnalyzerContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//Consumer XML
public static final class AmqpConsumerCallbackXml
extends DefaultAmqpQueueConsumerConnectorCallback<ClientXmlAnalyzerContext, String, Void>
{
@Override
public CallbackCompletion<Void> consume (final ClientXmlAnalyzerContext context, final AmqpQueueConsumeCallbackArguments<String> arguments)
{
final String val = arguments.getMessage ();
this.logger.info ("\n\nClientXmlAnalyzer received message For XmlANAlyzer:"+val);
context.finale = System.currentTimeMillis();
context.val=context.val+context.finale+" ";
byte[] finalebyte=null;
try {
finalebyte = context.val.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.kvStorexml.set(context.vectorListKey, finalebyte, null);
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerByteConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerByteConsumer initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ClientXmlAnalyzerContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//KV Store Tempi
public static final class KeyValueCallback
extends DefaultKvStoreConnectorCallback<ClientXmlAnalyzerContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final ClientXmlAnalyzerContext context, final KvStoreCallbackCompletionArguments<byte[],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");
byte[] valueb=(byte[])arguments.getValue();
String value=new String(valueb);
this.logger.info("\n\nResultati in String:"+value);
this.logger.info("\n\n");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult store delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final ClientXmlAnalyzerContext context,final KvStoreCallbackCompletionArguments<byte[],Void> arguments) {
this.logger.info("\n\nGet nel KV Store Contenente i Risultati memorizzati Succeeded\n\n");
String key =(String) arguments.getKey();
byte[] value=(byte[])arguments.getValue();
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final ClientXmlAnalyzerContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValueResult ungetSucceeded.");
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<ClientXmlAnalyzerContext>
{
@Override
public CallbackCompletion<Void> destroy (final ClientXmlAnalyzerContext context, final CloudletCallbackArguments<ClientXmlAnalyzerContext> arguments)
{
this.logger.info ("ClientXmlAnalyzer is being destroyed...");
return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ClientXmlAnalyzerContext context, final CloudletCallbackCompletionArguments<ClientXmlAnalyzerContext> arguments)
{
this.logger.info ("ClientXmlAnalyzer was destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final ClientXmlAnalyzerContext context, final CloudletCallbackArguments<ClientXmlAnalyzerContext> arguments)
{
this.logger.info ("ClientXmlAnalyzer 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 kvConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstore"));
context.kvStorexml = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfiguration, byte[].class, NullDataEncoder.create (), new KeyValueCallback (), context);
final IConfiguration queueConfiguration3 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuexml.consumer"));
context.consumerxml = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration3, String.class, PlainTextDataEncoder.DEFAULT_INSTANCE, new AmqpConsumerCallbackXml (), context);
return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize (),context.consumerxml.initialize(),context.kvStorexml.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ClientXmlAnalyzerContext context, final CloudletCallbackCompletionArguments<ClientXmlAnalyzerContext> arguments)
{
this.logger.info ("ClientXmlAnalyzer initialized successfully.");
context.object=new JsonObjectfromString();
context.gson=new Gson();
return ICallback.SUCCESS;
}
}
public static final class ClientXmlAnalyzerContext
{
ICloudletController<ClientXmlAnalyzerContext> cloudlet;
IAmqpQueuePublisherConnector<String, Void> publisher;
IAmqpQueueConsumerConnector<byte[], Void> consumer;
IAmqpQueueConsumerConnector<String, Void> consumerxml;
IKvStoreConnector<byte[], Void> kvStorexml;
public long inizio;
public long finale;
int count=0;
public Gson gson;
public JsonObjectfromString object;
String val=" ";
String vectorListKey="2";
String vectorListKey2="1";
}
}