Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
have a try at solving some SonarQube issues
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 27 May 2016 09:11:23 +0000 (11:11 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 27 May 2016 09:11:23 +0000 (11:11 +0200)
examples/java/app/bittorrent/Peer.java
examples/java/cloud/migration/TestHostOnOff.java
examples/java/energy/vm/EnergyVMRunner.java

index 7343686..b7ce0c3 100644 (file)
@@ -33,10 +33,10 @@ public class Peer extends Process {
   protected char[][] bitfieldBlocks = new char[Common.FILE_PIECES][Common.PIECES_BLOCKS];
   protected short[] piecesCount = new short[Common.FILE_PIECES];
   protected int piecesRequested = 0;
-  protected ArrayList<Integer> currentPieces = new ArrayList<Integer>();
+  protected ArrayList<Integer> currentPieces = new ArrayList<>();
   protected int currentPiece = -1;
-  protected HashMap<Integer, Connection> activePeers = new HashMap<Integer, Connection>();  
-  protected HashMap<Integer, Connection> peers = new HashMap<Integer, Connection>();
+  protected HashMap<Integer, Connection> activePeers = new HashMap<>();
+  protected HashMap<Integer, Connection> peers = new HashMap<>();
   protected Comm commReceived = null;
 
   public Peer(Host host, String name, String[]args) {
@@ -115,6 +115,7 @@ public class Peer extends Process {
         }
       }
       catch (MsgException e) {
+        e.printStackTrace();
         commReceived = null;
       }
     }
@@ -176,7 +177,8 @@ public class Peer extends Process {
   }
 
   private boolean getPeersData() {
-    boolean success = false, sendSuccess = false;
+    boolean success = false;
+    boolean sendSuccess = false;
     double timeout = Msg.getClock() + Common.GET_PEERS_TIMEOUT;
     //Build the task to send to the tracker
     TrackerTask taskSend = new TrackerTask(hostname, mailboxTracker, id);
@@ -188,6 +190,7 @@ public class Peer extends Process {
         sendSuccess = true;
       }
       catch (MsgException e) {
+        e.printStackTrace();
       }
     }
     while (!success && Msg.getClock() < timeout) {
@@ -204,7 +207,9 @@ public class Peer extends Process {
           success = true;
         }
       }
-      catch (MsgException e) {}
+      catch (MsgException e) {
+        e.printStackTrace();
+      }
       commReceived = null;
     }
     commReceived = null;
@@ -379,16 +384,14 @@ public class Peer extends Process {
     if (currentPieces.size() >= (Common.FILE_PIECES - pieces)) {
       return;
     }
-    if (true || pieces < 3) {
-      int peerPiece;
+//    if (pieces < 3) {
       do {
         currentPiece = stream.randInt(0,Common.FILE_PIECES - 1);
       } while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
-    }
-    else {
-      //trivial min algorithm.
-      //TODO
-    }
+//    }
+//    else {
+      //TODO trivial min algorithm.
+//    }
     currentPieces.add(currentPiece);
     Msg.debug("New interested piece: " + currentPiece);
     assert currentPiece >= 0 && currentPiece < Common.FILE_PIECES;
@@ -434,7 +437,7 @@ public class Peer extends Process {
             }
             i++;
           } //TODO: Not really the best way ever
-          if (!peerChoosed.interested) {
+          if (peerChoosed != null && !peerChoosed.interested) {
             peerChoosed = null;
           }
           j++;
index a564bb9..d5cc52b 100644 (file)
@@ -6,8 +6,6 @@
 
 package cloud.migration;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Random; 
 
 import org.simgrid.msg.*;
@@ -15,21 +13,18 @@ import org.simgrid.msg.Process;
 
 // This test aims at validating that the migration process is robust in face of host turning off either on the SRC 
 // node or on the DST node. 
-public class TestHostOnOff extends Process{    
+public class TestHostOnOff extends Process{
 
   public static Host host0 = null;
   public static Host host1 = null;
   public static Host host2 = null;
 
-
-  TestHostOnOff(Host host, String name, String[] args) throws HostNotFoundException, NativeException {
+  TestHostOnOff(Host host, String name, String[] args) throws NativeException {
     super(host, name, args);
   }
 
+  @Override
   public void main(String[] strings) throws MsgException {
-    double startTime = 0;
-    double endTime = 0;
-
     /* get hosts 1 and 2*/
     try {
       host0 = Host.getByName("host0");
@@ -41,18 +36,18 @@ public class TestHostOnOff extends Process{
 
     // Robustness on the SRC node
     //for (int i =0 ; i < 55000 ; i++)
-    //  test_vm_migrate(host1, i);
+    //  testVMMigrate(host1, i);
 
     // Robustness on the DST node
     //for (int i =0 ; i < 55000 ; i++)
-    //  test_vm_migrate(host2, i);
+    //  testVMMigrate(host2, i);
 
     /* End of Tests */
     Msg.info("Nor more tests, Bye Bye !");
     Main.setEndOfTest();
   }
 
-  public static void test_vm_migrate (Host hostToKill, long killAt) throws MsgException {
+  public static void testVMMigrate (Host hostToKill, long killAt) throws MsgException {
     Msg.info("**** **** **** ***** ***** Test Migrate with host shutdown ***** ***** **** **** ****");
     Msg.info("Turn on one host, assign a VM on this host, launch a process inside the VM, migrate the VM and "
              + "turn off either the SRC or DST");
@@ -121,15 +116,14 @@ public class TestHostOnOff extends Process{
     Process.sleep(20000);
   }
 
-  public static void test_vm_shutdown_destroy () throws HostFailureException {
+  public static void testVMShutdownDestroy () throws HostFailureException {
     Msg.info("**** **** **** ***** ***** Test shutdown a VM ***** ***** **** **** ****");
     Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
         "and check whether you can reallocate the same VM");
 
     // Create VM0
     int dpRate = 70;
-    XVM vm0 = null;
-    vm0 = new XVM(host1, "vm0",
+    XVM vm0 = new XVM(host1, "vm0",
         1, // Nb of vcpu
         2048, // Ramsize,
         125, // Net Bandwidth
index 32ac1fb..b7a2102 100644 (file)
@@ -5,8 +5,16 @@
 
 package energy.vm;
 
-import org.simgrid.msg.*;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.VM;
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Task;
 import org.simgrid.msg.Process;
+import org.simgrid.msg.MsgException;
+import org.simgrid.msg.HostFailureException;
+import org.simgrid.msg.HostNotFoundException;
+import org.simgrid.msg.TaskCancelledException;
+import org.simgrid.msg.NativeException;
 
 /* This class is a process in charge of running the test. It creates and starts the VMs, and fork processes within VMs */
 public class EnergyVMRunner extends Process {
@@ -16,11 +24,12 @@ public class EnergyVMRunner extends Process {
       super(host, name); 
     }
 
-    public void main(String[] args) {    
+    @Override
+    public void main(String[] strings) {
       Task  task = new Task(this.getHost().getName()+"-task", 300E6 , 0);
       try {
-        task.execute();   
-      } catch (Exception e) {
+        task.execute();
+      } catch (HostFailureException | TaskCancelledException e) {
         e.printStackTrace();
       } 
       Msg.info("This worker is done."); 
@@ -31,6 +40,7 @@ public class EnergyVMRunner extends Process {
     super(host, name, args);
   }
 
+  @Override
   public void main(String[] strings) throws MsgException, HostNotFoundException {
     double startTime = 0;
     double endTime = 0;