package net.yeputons.spbau.spring2016.torrent;

import java.net.InetSocketAddress;
import java.util.*;

public class SeedersTracker {
    private final int seederTimeoutMsecs;

    private final Map<Integer, Map<InetSocketAddress, Long>> seeders = new HashMap<>();

    public SeedersTracker(int seederTimeoutMsecs) {
        this.seederTimeoutMsecs = seederTimeoutMsecs;
    }
    // seeders[fileId][seederAddress] -> lastUpdated

    public List<InetSocketAddress> getSources(int fileId) {
        synchronized (seeders) {
            Map<InetSocketAddress, Long> fileSeeders = seeders.get(fileId);
            if (fileSeeders == null) {
                return Collections.emptyList();
            }
            List<InetSocketAddress> result = new ArrayList<>();
            for (Iterator<Map.Entry<InetSocketAddress, Long>> it
                    = fileSeeders.entrySet().iterator();
                 it.hasNext();
                 ) {
                Map.Entry<InetSocketAddress, Long> seeder = it.next();
                if (seeder.getValue() + seederTimeoutMsecs < System.currentTimeMillis()) {
                    it.remove();
                } else {
                    result.add(seeder.getKey());
                }
            }
            return result;
        }
    }

    public void updateSource(int fileId, InetSocketAddress address) {
        synchronized (seeders) {
            Map<InetSocketAddress, Long> fileSeeders = seeders.get(fileId);
            if (fileSeeders == null) {
                fileSeeders = new HashMap<>();
                seeders.put(fileId, fileSeeders);
            }
            fileSeeders.put(address, System.currentTimeMillis());
        }
    }
}

@@ -436,16 +436,29 @@
  public 
+synchronized 
 List%3CIne
@@ -502,45 +502,8 @@
 ) %7B%0A
-        synchronized (seeders) %7B%0A    
     
@@ -570,36 +570,32 @@
 ileId);%0A        
- 
-   
 if (fileSeeders 
@@ -597,36 +597,32 @@
 ders == null) %7B%0A
-    
             retu
@@ -657,32 +657,26 @@
         
-    %7D%0A  
+%7D%0A
         
   List%3CI
@@ -667,18 +667,16 @@
         
-  
 List%3CIne
@@ -715,28 +715,24 @@
 ayList%3C%3E();%0A
-    
         for 
@@ -796,20 +796,16 @@
         
- 
-   
 = fileSe
@@ -846,20 +846,16 @@
         
-   
- 
 it.hasNe
@@ -877,20 +877,12 @@
     
-    ) %7B%0A    
+) %7B%0A
     
@@ -940,28 +940,24 @@
  it.next();%0A
-    
             
@@ -1047,20 +1047,16 @@
         
-    
 it.remov
@@ -1072,20 +1072,16 @@
         
-    
 %7D else %7B
@@ -1081,20 +1081,16 @@
  else %7B%0A
-    
         
@@ -1138,33 +1138,26 @@
         
-    %7D%0A   
+%7D%0A
         
  %7D%0A     
@@ -1140,39 +1140,34 @@
       %7D%0A        
- 
 %7D%0A
-    
         return r
@@ -1173,26 +1173,16 @@
 result;%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1191,16 +1191,29 @@
  public 
+synchronized 
 void upd
@@ -1267,45 +1267,8 @@
 ) %7B%0A
-        synchronized (seeders) %7B%0A    
     
@@ -1339,28 +1339,24 @@
 d);%0A        
-    
 if (fileSeed
@@ -1374,36 +1374,32 @@
 ) %7B%0A            
- 
-   
 fileSeeders = ne
@@ -1409,28 +1409,24 @@
 ashMap%3C%3E();%0A
-    
             
@@ -1471,24 +1471,18 @@
     
-    %7D%0A  
+%7D%0A
         
   fi
@@ -1469,34 +1469,32 @@
       %7D%0A        
-  
 fileSeeders.put(
@@ -1531,26 +1531,16 @@
 lis());%0A
-        %7D%0A
     %7D%0A%7D%0A

make methods synchronized instead of explicit sync