private void addCnxn(NIOServerCnxn cnxn) throws IOException { InetAddress addr = cnxn.getSocketAddress(); if (addr == null) { throw new IOException("Socket of " + cnxn + " has been closed"); } Set<NIOServerCnxn> set = ipMap.get(addr); if (set == null) { // in general we will see 1 connection from each // host, setting the initial cap to 2 allows us // to minimize mem usage in the common case // of 1 entry -- we need to set the initial cap // to 2 to avoid rehash when the first entry is added // Construct a ConcurrentHashSet using a ConcurrentHashMap set = Collections.newSetFromMap( new ConcurrentHashMap<NIOServerCnxn, Boolean>(2)); // Put the new set in the map, but only if another thread // hasn't beaten us to it Set<NIOServerCnxn> existingSet = ipMap.putIfAbsent(addr, set); if (existingSet != null) { set = existingSet; } } set.add(cnxn); cnxns.add(cnxn); touchCnxn(cnxn); }
/** * De-registers the connection from the various mappings maintained * by the factory. */ public boolean removeCnxn(NIOServerCnxn cnxn) { // If the connection is not in the master list it's already been closed if (!cnxns.remove(cnxn)) { return false; } cnxnExpiryQueue.remove(cnxn); removeCnxnFromSessionMap(cnxn); InetAddress addr = cnxn.getSocketAddress(); if (addr != null) { Set<NIOServerCnxn> set = ipMap.get(addr); if (set != null) { set.remove(cnxn); // Note that we make no effort here to remove empty mappings // from ipMap. } } // unregister from JMX unregisterConnection(cnxn); return true; }
public void removeCnxn(NIOServerCnxn cnxn) { synchronized(cnxns) { // Remove the related session from the sessionMap. long sessionId = cnxn.getSessionId(); if (sessionId != 0) { sessionMap.remove(sessionId); } // if this is not in cnxns then it's already closed if (!cnxns.remove(cnxn)) { return; } synchronized (ipMap) { Set<NIOServerCnxn> s = ipMap.get(cnxn.getSocketAddress()); s.remove(cnxn); } unregisterConnection(cnxn); } }