Com configuracao
authorHumberto Silva Naves <[email protected]>
Fri, 21 Nov 2008 02:43:59 +0000 (21 00:43 -0200)
committerHumberto Silva Naves <[email protected]>
Fri, 21 Nov 2008 02:43:59 +0000 (21 00:43 -0200)
src/main/java/org/hnaves/dfs/Configurator.java
src/main/java/org/hnaves/dfs/http/HttpServer.java

index 974c211..775fffe 100644 (file)
@@ -22,6 +22,8 @@ public class Configurator {
        private static final int DEFAULT_MAIN_THREAD_WAIT = 3000;
        private static final int DEFAULT_HTTP_START = 10000;
        private static final int DEFAULT_HTTP_PERIOD = 20000;
+       private static final boolean DEFAULT_ACCEPT_TRANSFER = true;
+       private static final double DEFAULT_TRANSFER_PROBABILITY = 0.8;
        
        private static Properties props = null;
        
@@ -73,6 +75,14 @@ public class Configurator {
                return Integer.parseInt(getProperties().getProperty(key));
        }
        
+       public static boolean getAcceptTransfer() {
+               return Boolean.parseBoolean(getProperties().getProperty("ACCEPT_TRANSFER"));
+       }
+
+       public static double getTransferProbability() {
+               return Double.parseDouble(getProperties().getProperty("TRANSFER_PROBABILITY"));
+       }
+       
        private static void checkProperties(Properties props) {
                if (!props.containsKey("UDP_PORT")) {
                        props.setProperty("UDP_PORT", Integer.toString(DEFAULT_UDP_PORT));
@@ -107,7 +117,12 @@ public class Configurator {
                if (!props.containsKey("HTTP_PERIOD")) {
                        props.setProperty("HTTP_PERIOD", Integer.toString(DEFAULT_HTTP_PERIOD));
                }
-               
+               if (!props.containsKey("ACCEPT_TRANSFER")) {
+                       props.setProperty("ACCEPT_TRANSFER", Boolean.toString(DEFAULT_ACCEPT_TRANSFER));
+               }               
+               if (!props.containsKey("TRANSFER_PROBABILITY")) {
+                       props.setProperty("TRANSFER_PROBABILITY", Double.toString(DEFAULT_TRANSFER_PROBABILITY));
+               }               
        }
        
        private static synchronized Properties getProperties() {
index ce87b89..e7e927a 100644 (file)
@@ -81,7 +81,7 @@ public class HttpServer implements ElectionListener {
                                                out.close();
                                                is.close();
                                                clientSocket.close();
-                                       } else if (election.getId().equals(coordinator)) {
+                                       } else if (election.getId().equals(coordinator) && (Math.random() < Configurator.getTransferProbability())) {
                                                CopyOperation op = repository.randomCopyOperation();
                                                if (op != null) {
                                                        LOG.fine("Notification about copy to " + op.getDestination());
@@ -201,9 +201,13 @@ public class HttpServer implements ElectionListener {
                                        repository.updateRemoteFiles(c);
                                        output.write(("OK" + CRLF).getBytes());
                                } else if ("/transfer".equals(postRequest)) {
-                                       CopyOperation op = (CopyOperation) ObjectUtils.deserializeObject(baos.toByteArray());
-                                       new Thread(new HttpCopy(op)).start();
-                                       output.write(("OK" + CRLF).getBytes());
+                                       if (Configurator.getAcceptTransfer()) {
+                                               CopyOperation op = (CopyOperation) ObjectUtils.deserializeObject(baos.toByteArray());
+                                               new Thread(new HttpCopy(op)).start();
+                                               output.write(("OK" + CRLF).getBytes());
+                                       } else {
+                                               output.write(("ERROR" + CRLF).getBytes());
+                                       }
                                }
                        }