Package eu.mosaic_cloud.sunutilities

Source Code of eu.mosaic_cloud.sunutilities.CloudletLetturadaFile$AmqpConsumerCallback

package eu.mosaic_cloud.sunutilities;

import java.io.*;
import java.util.*;
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.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.tools.callbacks.core.CallbackCompletion;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Vector;

import com.google.gson.Gson;


public class CloudletLetturadaFile {

 
  public static final class AmqpPublisherCallback
  extends DefaultAmqpPublisherConnectorCallback<CloudletLetturadaFileContext, String, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final CloudletLetturadaFileContext context, final CallbackArguments arguments)
{
  this.logger.info ("CloudletLetturadaFilePublisher destroyed successfully.");
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> initializeSucceeded (final CloudletLetturadaFileContext context, final CallbackArguments arguments)
{
  this.logger.info ("CloudletLetturadaFilePublisher initialized successfully.");
 

 
  return ICallback.SUCCESS;
}

@Override
public CallbackCompletion<Void> publishSucceeded (final CloudletLetturadaFileContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
  //context.cloudlet.destroy ();
  this.logger.info ("CloudletLetturadaFile publisher successfully.");
  return ICallback.SUCCESS;
}
}


public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<CloudletLetturadaFileContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> consume (final CloudletLetturadaFileContext context, final AmqpQueueConsumeCallbackArguments<byte[]> arguments)
{
final byte[] val = arguments.getMessage ();

this.logger.info ("\n\nCloudletLetturadaFile received  message Byte:"+val);

String data = new String(val);

this.logger.info ("\n\nCloudletLetturadaFile received  message String:"+data);

context.object=context.gson.fromJson(data,JsonObjectfromString.class);



this.logger.info("Leggo da File");

BufferedReader br=null;
try {
  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(context.object.getParameters(0)))));
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
String line=null;
try {
  line = br.readLine();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
while(line !=null){
  StringTokenizer st = new StringTokenizer(line, "*");
  double time = Double.parseDouble(st.nextToken());
  int size = Integer.parseInt(st.nextToken());
  this.logger.info("Read "+ time + " and " + size);   
  try {
    line = br.readLine();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}
try {
  br.close();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}


context.publisher.publish(data, null);

context.consumer.acknowledge (arguments.getToken ());


return ICallback.SUCCESS;
}

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

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

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



public static final class LifeCycleHandler
  extends DefaultCloudletCallback<CloudletLetturadaFileContext>
{
@Override
public CallbackCompletion<Void> destroy (final CloudletLetturadaFileContext context, final CloudletCallbackArguments<CloudletLetturadaFileContext> arguments)
{
  this.logger.info ("CloudletLetturadaFile is being destroyed...");
  return CallbackCompletion.createAndChained (context.consumer.destroy (),context.publisher.destroy());
}

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

@Override
public CallbackCompletion<Void> initialize (final CloudletLetturadaFileContext context, final CloudletCallbackArguments<CloudletLetturadaFileContext> arguments)
{
  this.logger.info ("CloudletLetturadaFile 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);
  return CallbackCompletion.createAndChained (context.consumer.initialize (),context.publisher.initialize ());
}


@Override
public CallbackCompletion<Void> initializeSucceeded (final CloudletLetturadaFileContext context, final CloudletCallbackCompletionArguments<CloudletLetturadaFileContext> arguments)
{
  this.logger.info ("CloudletLetturadaFile initialized successfully.");
  context.gson=new Gson();
  context.object=new JsonObjectfromString();
  this.logger.info("Leggo da File");
  BufferedReader br=null;
  try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/root/times.txt"))));
  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  String line=null;
  try {
    line = br.readLine();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  while(line !=null){
    StringTokenizer st = new StringTokenizer(line, "*");
    double time = Double.parseDouble(st.nextToken());
    int size = Integer.parseInt(st.nextToken());
    this.logger.info("Read "+ time + " and " + size);   
    try {
      line = br.readLine();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  try {
    br.close();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
 
  return ICallback.SUCCESS;
}
}

public static final class CloudletLetturadaFileContext
{
ICloudletController<CloudletLetturadaFileContext> cloudlet;
IAmqpQueuePublisherConnector<String, Void> publisher;
IAmqpQueueConsumerConnector<byte[], Void> consumer;
Gson gson;
JsonObjectfromString object;
}
}
TOP

Related Classes of eu.mosaic_cloud.sunutilities.CloudletLetturadaFile$AmqpConsumerCallback

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.