[vlma-devel] commit: Try to use less memory. (Adrien Grand )

git version control git at videolan.org
Wed Nov 26 21:28:09 CET 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Wed Nov 26 21:27:43 2008 +0100| [10d510bf2f99cc8de7595dd74ad033994ed0c85f] | committer: Adrien Grand 

Try to use less memory.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=10d510bf2f99cc8de7595dd74ad033994ed0c85f
---

 .../src/main/java/org/videolan/vlma/IpBank.java    |   46 +++++++++-----------
 1 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/vlma-core/src/main/java/org/videolan/vlma/IpBank.java b/vlma-core/src/main/java/org/videolan/vlma/IpBank.java
index b1703d1..952502f 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/IpBank.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/IpBank.java
@@ -23,8 +23,8 @@ package org.videolan.vlma;
 import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.log4j.Logger;
@@ -43,12 +43,14 @@ public class IpBank {
 
     private static final Logger logger = Logger.getLogger(IpBank.class);
 
-    private HashMap<InetAddress, Boolean> usedIps;
+    private Set<InetAddress> usedIps;
 
     private VLMaService vlmaService;
 
     private Configuration configuration;
 
+    private Inet4Address min, max;
+
     /**
      * Initializes IP addresses according to the data interface.
      *
@@ -56,15 +58,10 @@ public class IpBank {
      *             address is invalid
      */
     synchronized public void initIps() throws UnknownHostException {
-        Inet4Address min = (Inet4Address) InetAddress.getByName(configuration.getString("vlma.streaming.udp.ipbank.min"));
-        Inet4Address max = (Inet4Address) InetAddress.getByName(configuration.getString("vlma.streaming.udp.ipbank.max"));
-
-        usedIps = new HashMap<InetAddress, Boolean>();
-        usedIps.clear();
+        usedIps = new HashSet<InetAddress>();
 
-        for (int ip = ipv42int(min); ip < ipv42int(max); ip++) {
-            usedIps.put(int2ipv4(ip), Boolean.FALSE);
-        }
+        min = (Inet4Address) InetAddress.getByName(configuration.getString("vlma.streaming.udp.ipbank.min"));
+        max = (Inet4Address) InetAddress.getByName(configuration.getString("vlma.streaming.udp.ipbank.max"));
 
         for (Media m : vlmaService.getMedias()) {
             Program p = m.getProgram();
@@ -72,14 +69,12 @@ public class IpBank {
                 continue;
             }
             logger.debug("The media " + m.getName() + " has a program");
-            if (usedIps.containsKey(p.getIp())) {
-                usedIps.put(p.getIp(), Boolean.TRUE);
-            }
+            usedIps.add(p.getIp());
         }
     }
 
     synchronized public boolean isUsed(InetAddress ip) {
-        return usedIps.containsKey(ip) && usedIps.get(ip);
+        return usedIps.contains(ip);
     }
 
     /**
@@ -88,13 +83,18 @@ public class IpBank {
      * @return the IP address
      */
     synchronized public InetAddress getIp() {
-        for (Map.Entry<InetAddress, Boolean> entry : usedIps.entrySet()) {
-            if (!entry.getValue().booleanValue()) {
-                entry.setValue(Boolean.TRUE);
-                return entry.getKey();
+        for(int ip = ipv42int(min); ip < ipv42int(max); ip++) {
+            InetAddress i = null;
+            try {
+                i = int2ipv4(ip);
+            } catch (UnknownHostException e) {
+                // Can not happen since raw IPs are provided
+                assert(false);
             }
+            if(usedIps.add(i))
+                return i;
         }
-        throw new NotFoundException("No free IP found in the IP bank");
+        throw new NotFoundException("No free IP available");
     }
 
     /**
@@ -104,11 +104,7 @@ public class IpBank {
      */
     synchronized public void releaseIp(InetAddress ip) {
         if(ip == null) return;
-        if (usedIps.containsKey(ip)) {
-            usedIps.put(ip, Boolean.FALSE);
-        } else {
-            logger.warn("IP address " + ip.getHostAddress() + " is not in the pool");
-        }
+        usedIps.remove(ip);
     }
 
     /**



More information about the vlma-devel mailing list