Package eu.mosaic_cloud.benchmarks

Source Code of eu.mosaic_cloud.benchmarks.FixedCountElementFileTimes$FixedCountElementFileTimesLifeCycleHandler$TimeAndSize

//###
//# #%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 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.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Vector;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import eu.mosaic_cloud.benchmarks.JsonObjectfromString;
import eu.mosaic_cloud.benchmarks.WGBehaviour;
import eu.mosaic_cloud.benchmarks.WGCloudlet;



public class FixedCountElementFileTimes extends WGCloudlet {
 

  public static final class FixedCountElementFileTimesLifeCycleHandler extends
      WGLifeCycleHandler {
   
   

    public class NewBehaviour extends WGBehaviour {

      int dim;
      int count = 0;
      String mess;
      int j=1;
      boolean val = false;
        TimeAndSize[] timeAndSizes = null;
        TimeAndSize timeAndSize;
      String string4;
      String string8;
      String string12;
   
     
      public void WGRun(WGCloudletContext context)  {

        val = false;
        count = 0;
        Gson gson1 = new GsonBuilder().disableHtmlEscaping().create();
          try {
          JsonObjectfromString obj4 = gson1.fromJson(new BufferedReader(
              new InputStreamReader(new FileInputStream(context.object.getParameters(1)))),
              JsonObjectfromString.class);
          string4 = gson1.toJson(obj4);
        //  this.logger.info("String 4:"+string4);
        } catch (JsonSyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (JsonIOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
          try {
          JsonObjectfromString obj8 = gson1.fromJson(new BufferedReader(
              new InputStreamReader(new FileInputStream(context.object.getParameters(2)))),
              JsonObjectfromString.class);
          string8 = gson1.toJson(obj8);
        //  this.logger.info("String 8:"+string8);
        } catch (JsonSyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (JsonIOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
          try {
          JsonObjectfromString obj12 = gson1.fromJson(new BufferedReader(
              new InputStreamReader(new FileInputStream(context.object.getParameters(3)))),
              JsonObjectfromString.class);
          string12 = gson1.toJson(obj12);
        //  this.logger.info("String 12:"+string12);
        } catch (JsonSyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (JsonIOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
       
         
         
          //TimeAndSize[] timeAndSizes = null;
        try {
          timeAndSizes = readTimesFromFile(context.object.getParameters(0));
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
       
        this.logger.info("Lunghezza del Vettore e':"+ timeAndSizes.length );
       
         
           timeAndSize=timeAndSizes[0];
         
          double nextWaitTime = timeAndSize.getTime();
          int nextMsgSize = timeAndSize.getSize()
                long  millisWaitTime = (long) (nextWaitTime * 1000);
                try {
            Thread.sleep(millisWaitTime);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
              //  System.out.println("Sending msg of size " + nextMsgSize);
               
             
             
                switch (nextMsgSize) {
               
                case 4: context.publisher.publish(string4, null);
               
                break;
               
                case 8: context.publisher.publish(string8, null);
               
                break;
               
                case 12: context.publisher.publish(string12, null);
               
                break;
                }
      
              }
     
      public void WGPushWorkload(WGCloudletContext context)  {
       
        this.logger.info("Entro nel Push");
        this.logger.info("Length:"+timeAndSizes.length);
       
        if(j<timeAndSizes.length){
         
          this.logger.info("J:"+j);
            timeAndSize=timeAndSizes[j];
          
                  double nextWaitTime = timeAndSize.getTime();
                  int nextMsgSize = timeAndSize.getSize()
                  long  millisWaitTime = (long) (nextWaitTime * 1000);
                //  System.out.println("Sleeping " + millisWaitTime + "ms");
                  try {
              Thread.sleep(millisWaitTime);
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
                //  System.out.println("Sending msg of size " + nextMsgSize);
                 
                  j++;
               
                  switch (nextMsgSize) {
                 
                  case 4: context.publisher.publish(string4, null);
                 
                  break;
                 
                  case 8: context.publisher.publish(string8, null);
                 
                  break;
                 
                  case 12: context.publisher.publish(string12, null);
                 
                  break; }
                 
                 
                 
        
        }
               
       
      }
               

      public boolean WGCheck(WGCloudletContext context) {
        count++;
        this.logger.info("\nCount:" + count);

        if (count == timeAndSizes.length) {

          val = true;
                  }

        return val;

                                }

      private TimeAndSize[] readTimesFromFile(String fileName)
          throws IOException {
        Vector<TimeAndSize> timeAndSizes = new Vector<TimeAndSize>();
        BufferedReader br = new BufferedReader(new InputStreamReader(
            new FileInputStream(new File(fileName))));
        String line = br.readLine();
        while (line != null) {
          StringTokenizer st = new StringTokenizer(line, "*");
          double time = Double.parseDouble(st.nextToken());
          int size = Integer.parseInt(st.nextToken());
          //System.out.println("Read " + time + " and " + size);
          timeAndSizes.add(new TimeAndSize(time, size));
          line = br.readLine();
                  }
        TimeAndSize[] timeAndSizeArray = new TimeAndSize[timeAndSizes
            .size()];
        timeAndSizes.toArray(timeAndSizeArray);
        br.close();
        return timeAndSizeArray;
                    }

      public void ScriviFile(String fileName, String Val) {
        try {
          FileWriter w = new FileWriter(fileName);
          BufferedWriter b = new BufferedWriter(w);
          PrintWriter printout = new PrintWriter(b);
          printout.println(Val);
          printout.flush();
          } catch (Exception e) {
        //  System.out.println("ERRORE: " + e);
                      }

                                }
    }

    public void createWgBehaviour(WGCloudletContext context) {
      context.wgb = new NewBehaviour();

                                }

    class TimeAndSize {

      private double time;
      private int size;

      public TimeAndSize(double time, int size) {
        this.time = time;
        this.size = size;
                            }

      public int getSize() {
        return size;
                 }

      public double getTime() {
        return time;
                  }

              }

   
  }

}
TOP

Related Classes of eu.mosaic_cloud.benchmarks.FixedCountElementFileTimes$FixedCountElementFileTimesLifeCycleHandler$TimeAndSize

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.