/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simizer.utils;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import simizer.Node;
import simizer.processor.Processor;
import simizer.storage.ResourceFactory;
import simizer.storage.StorageElement;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import simizer.ServerNode;

/**
 *
 * @author isep
 */
public class SimizerUtils {
    static final JSONParser parser=new JSONParser();
    static  SimizerUtils sim;
    
    public static List<ServerNode> decodeNodes(String nodesJson) {
         List<ServerNode> nodes = new ArrayList<>();
         int id= 0;
        
        
         try {
            JSONArray array=(JSONArray)parser.parse(nodesJson);
            
            for(Object o: array) {
                JSONObject n = (JSONObject) o;
                
                Number nbNode = (Number) n.get("nb");
                Number cpu = (Number) n.get("cpuSlots");
                Number mem = (Number) n.get("memorySize");
                //System.out.println(mem.toString());
                Number queueSz = (Number) n.get("queueSize");
                Number cost = (Number) n.get("cost");
                Number diskSize = (Number) n.get("diskSize");
                Number nbMips = (Number) n.get("nbMips");
                String processorNom = (String) n.get("ProcessorName");
                Processor processor=null;
                System.out.println(processorNom);
                
                for(int i= 0; i< nbNode.intValue();i++) {
                    StorageElement disk = new StorageElement(diskSize.intValue(),4);
                    disk.setPerMBReadDelay(2.0);
                    
                    processor = createProcessor((int )cpu.longValue(), nbMips.doubleValue(), processorNom);
                    ServerNode current =new ServerNode(id,
                        mem.longValue()*1000,
                        queueSz.intValue(),
                        cost.doubleValue(), 
                        disk,
                        processor);
                    
                    processor.setNodeInstance(current);
                            
                    nodes.add(current);
                    id++;
                }                
            }
     
  
        } catch (ParseException ex) {
            Logger.getLogger(SimizerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }

        return nodes;
    }
    public static Processor createProcessor(int core, double nbMips, String pName) {
        Processor processor = null;
        try{
                    
                Constructor pConst = Class.forName(pName).getConstructor(int.class, double.class);
                processor =  (Processor) pConst.newInstance(core,nbMips);
                //System.out.println("Creating Class for :" + pName);
                }
                catch(Exception e){
                    System.out.println("Exception in creating processor instance"
                            + " at LBSimUtis class :"+e.getMessage());
                }
        return processor;
    }
    public static  String[] readRequestsFile(String fileName) {
        String tmp = readFile(fileName);
        return tmp.split("\\n");
        
    }
    public static String readFile(String filePath) {
        StringBuilder sb = new StringBuilder();
        String content=null;
        CharBuffer cbuf = CharBuffer.allocate(128);
        
        try {
            FileReader nfReader = new FileReader(filePath);
            int res = 0;
            do {
                res = nfReader.read(cbuf);
                if(res <= 0)
                    break;
                
                cbuf.rewind();
                //System.out.println("Read: " + res + " remaining:" + cbuf.remaining());
                sb.append(cbuf.subSequence(0,res));
                
                
              }while(res > -1);
            // Cleanup
            
            content = sb.toString();
            
            nfReader.close();
            
        } catch (IOException ex) {
            Logger.getLogger(SimizerUtils.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return content;
    }
    
    
    
     public static ResourceFactory getRessourceFactory(String resFile) {
         int nbInit=0, nbMax=0, size=0;
        try {
            JSONArray array=(JSONArray) parser.parse(readFile(resFile));
              for(Object o: array) {
                JSONObject n = (JSONObject) o;
                nbInit = ((Number) n.get("nbInit")).intValue();
                nbMax = ((Number) n.get("nbMax")).intValue();
                size = ((Number) n.get("size")).intValue();
              }
        } catch (ParseException ex) {
            Logger.getLogger(SimizerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
         return new ResourceFactory(nbInit,nbMax,size);
     }
     
     private static int DEFAULT_RES_NB = 1024;
     private static int DEFAULT_RES_SZ = 8096;
     
    public static ResourceFactory getDefaultResourceFactory() {
        return new ResourceFactory(DEFAULT_RES_NB,DEFAULT_RES_NB, DEFAULT_RES_SZ);
    }
     
  
}

@@ -188,312 +188,87 @@
 ava.
-lang.reflect.Constructor;%0Aimport java.nio.CharBuffer;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0Aimport simizer.Node;%0Aimport simizer.processor.Processor;%0Aimport simizer.storage.ResourceFactory;%0Aimport simizer.storage.StorageElement
+nio.CharBuffer;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger
 ;%0Aim
@@ -437,26 +437,39 @@
 simizer.
-ServerNode
+storage.ResourceFactory
 ;%0A%0A/**%0A 
@@ -610,2627 +610,8 @@
    %0A
-    public static List%3CServerNode%3E decodeNodes(String nodesJson) %7B%0A         List%3CServerNode%3E nodes = new ArrayList%3C%3E();%0A         int id= 0;%0A        %0A        %0A         try %7B%0A            JSONArray array=(JSONArray)parser.parse(nodesJson);%0A            %0A            for(Object o: array) %7B%0A                JSONObject n = (JSONObject) o;%0A                %0A                Number nbNode = (Number) n.get(%22nb%22);%0A                Number cpu = (Number) n.get(%22cpuSlots%22);%0A                Number mem = (Number) n.get(%22memorySize%22);%0A                //System.out.println(mem.toString());%0A                Number queueSz = (Number) n.get(%22queueSize%22);%0A                Number cost = (Number) n.get(%22cost%22);%0A                Number diskSize = (Number) n.get(%22diskSize%22);%0A                Number nbMips = (Number) n.get(%22nbMips%22);%0A                String processorNom = (String) n.get(%22ProcessorName%22);%0A                Processor processor=null;%0A                System.out.println(processorNom);%0A                %0A                for(int i= 0; i%3C nbNode.intValue();i++) %7B%0A                    StorageElement disk = new StorageElement(diskSize.intValue(),4);%0A                    disk.setPerMBReadDelay(2.0);%0A                    %0A                    processor = createProcessor((int )cpu.longValue(), nbMips.doubleValue(), processorNom);%0A                    ServerNode current =new ServerNode(id,%0A                        mem.longValue()*1000,%0A                        queueSz.intValue(),%0A                        cost.doubleValue(), %0A                        disk,%0A                        processor);%0A                    %0A                    processor.setNodeInstance(current);%0A                            %0A                    nodes.add(current);%0A                    id++;%0A                %7D                %0A            %7D%0A     %0A  %0A        %7D catch (ParseException ex) %7B%0A            Logger.getLogger(SimizerUtils.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D%0A%0A        return nodes;%0A    %7D%0A    public static Processor createProcessor(int core, double nbMips, String pName) %7B%0A        Processor processor = null;%0A        try%7B%0A                    %0A                Constructor pConst = Class.forName(pName).getConstructor(int.class, double.class);%0A                processor =  (Processor) pConst.newInstance(core,nbMips);%0A                //System.out.println(%22Creating Class for :%22 + pName);%0A                %7D%0A                catch(Exception e)%7B%0A                    System.out.println(%22Exception in creating processor instance%22%0A                            + %22 at LBSimUtis class :%22+e.getMessage());%0A                %7D%0A        return processor;%0A    %7D%0A
     

Remove utilities for loading ServerNode/Processor.