Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | /** |
| 23 | * A class to represent Groups for membership query and reports. |
| 24 | */ |
| 25 | public abstract class IGMPGroup { |
| 26 | |
| 27 | protected int auxInfo; |
| 28 | protected IpAddress gaddr; |
| 29 | protected List<IpAddress> sources = new ArrayList<>(); |
| 30 | |
| 31 | public IGMPGroup() { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Initialize this object with a multicast group address and additional info. |
| 36 | * |
HIGUCHI Yuta | e48434a | 2016-03-01 23:48:52 -0800 | [diff] [blame] | 37 | * @param gaddr the multicast group address for this message type. |
| 38 | * @param auxInfo additional info potentially used by IGMPQuery |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 39 | */ |
| 40 | public IGMPGroup(IpAddress gaddr, int auxInfo) { |
| 41 | this.gaddr = gaddr; |
| 42 | this.auxInfo = auxInfo; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the multicast group address. |
| 47 | * |
| 48 | * @return the group address |
| 49 | */ |
| 50 | public IpAddress getGaddr() { |
| 51 | return this.gaddr; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the auxillary info. |
| 56 | * |
| 57 | * @return the auxillary info |
| 58 | */ |
| 59 | public int getAuxInfo() { |
| 60 | return this.auxInfo; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Add a unicast source address to this message. |
| 65 | * |
| 66 | * @param saddr IPv4 unicast source address |
| 67 | */ |
| 68 | public void addSource(IpAddress saddr) { |
| 69 | sources.add(saddr); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return the list of source addresses. |
| 74 | * |
| 75 | * @return list of source addresses |
| 76 | */ |
| 77 | public List<IpAddress> getSources() { |
| 78 | return sources; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Deserialize an IGMPQuery or IGMPMembership message. |
| 83 | * |
| 84 | * @param bb the ByteBuffer wrapping the serialized message. The position of the |
| 85 | * ByteBuffer should be pointing at the head of either message type. |
| 86 | * @return An object populated with the respective IGMPGroup subclass |
| 87 | * @throws DeserializationException in case deserialization goes wrong |
| 88 | */ |
| 89 | public abstract IGMPGroup deserialize(ByteBuffer bb) throws DeserializationException; |
| 90 | |
| 91 | /** |
| 92 | * Serialize the IGMPGroup subclass. |
| 93 | * |
| 94 | * @param bb the ByteBuffer to write into, positioned at the next spot to be written to. |
| 95 | * @return The serialized message |
| 96 | */ |
| 97 | public abstract byte[] serialize(ByteBuffer bb); |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | StringBuilder sb = new StringBuilder(); |
| 102 | sb.append("["); |
| 103 | sb.append("auxInfo= "); |
| 104 | sb.append(auxInfo); |
| 105 | sb.append("gaddr= "); |
| 106 | sb.append(gaddr); |
| 107 | sb.append("sources= "); |
| 108 | sb.append(sources.toString()); |
| 109 | sb.append("]"); |
| 110 | return sb.toString(); |
| 111 | } |
Rusty Eddy | 1da61a2 | 2015-09-01 00:48:58 +0000 | [diff] [blame] | 112 | } |