src/java.base/share/classes/sun/nio/ch/MembershipRegistry.java
author igerasim
Wed, 21 Aug 2019 13:49:16 -0700
changeset 57826 bf4c808a4488
parent 47216 71c04702a3d5
child 59146 455612b3161a
permissions -rw-r--r--
8211360: Change #if DEF to #if defined(DEF) Reviewed-by: bpb, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     2
 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     4
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    10
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    15
 * accompanied this code).
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    16
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    24
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    25
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    26
package sun.nio.ch;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    27
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    28
import java.nio.channels.*;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    29
import java.net.InetAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    30
import java.net.NetworkInterface;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    31
import java.util.*;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    32
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    33
/**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    34
 * Simple registry of membership keys for a MulticastChannel.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    35
 *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    36
 * Instances of this object are not safe by multiple concurrent threads.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    37
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    38
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    39
class MembershipRegistry {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    40
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    41
    // map multicast group to keys
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    42
    private Map<InetAddress,List<MembershipKeyImpl>> groups = null;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    43
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    44
    MembershipRegistry() {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    45
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    46
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    47
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    48
     * Checks registry for membership of the group on the given
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    49
     * network interface.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    50
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    51
    MembershipKey checkMembership(InetAddress group, NetworkInterface interf,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    52
                                  InetAddress source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    53
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    54
        if (groups != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    55
            List<MembershipKeyImpl> keys = groups.get(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    56
            if (keys != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    57
                for (MembershipKeyImpl key: keys) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
    58
                    if (key.networkInterface().equals(interf)) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    59
                        // already a member to receive all packets so return
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    60
                        // existing key or detect conflict
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    61
                        if (source == null) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
    62
                            if (key.sourceAddress() == null)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    63
                                return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    64
                            throw new IllegalStateException("Already a member to receive all packets");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    65
                        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    66
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    67
                        // already have source-specific membership so return key
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    68
                        // or detect conflict
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
    69
                        if (key.sourceAddress() == null)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    70
                            throw new IllegalStateException("Already have source-specific membership");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
    71
                        if (source.equals(key.sourceAddress()))
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    72
                            return key;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    73
                    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    74
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    75
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    76
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    77
        return null;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    78
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    79
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    80
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    81
     * Add membership to the registry, returning a new membership key.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    82
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    83
    void add(MembershipKeyImpl key) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
    84
        InetAddress group = key.group();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    85
        List<MembershipKeyImpl> keys;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    86
        if (groups == null) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    87
            groups = new HashMap<>();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    88
            keys = null;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    89
        } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    90
            keys = groups.get(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    91
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    92
        if (keys == null) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    93
            keys = new LinkedList<>();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    94
            groups.put(group, keys);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    95
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    96
        keys.add(key);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    97
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    98
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
    99
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   100
     * Remove a key from the registry
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   101
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   102
    void remove(MembershipKeyImpl key) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   103
        InetAddress group = key.group();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   104
        List<MembershipKeyImpl> keys = groups.get(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   105
        if (keys != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   106
            Iterator<MembershipKeyImpl> i = keys.iterator();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   107
            while (i.hasNext()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   108
                if (i.next() == key) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   109
                    i.remove();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   110
                    break;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   111
                }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   112
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   113
            if (keys.isEmpty()) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   114
                groups.remove(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   115
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   116
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   117
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   118
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   119
    /**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   120
     * Invalidate all keys in the registry
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   121
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   122
    void invalidateAll() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   123
        if (groups != null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   124
            for (InetAddress group: groups.keySet()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   125
                for (MembershipKeyImpl key: groups.get(group)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   126
                    key.invalidate();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1152
diff changeset
   127
                }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   128
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   129
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   130
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents:
diff changeset
   131
}