1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.nio.ch.sctp;
  26 
  27 import java.net.SocketAddress;
  28 import com.sun.nio.sctp.Association;
  29 import com.sun.nio.sctp.PeerAddressChangeNotification;
  30 import javax.tools.annotation.GenerateNativeHeader;
  31 
  32 /**
  33  * An implementation of PeerAddressChangeNotification
  34  */
  35 /* No native methods here, but the constants are needed in the supporting JNI code */
  36 @GenerateNativeHeader
  37 public class PeerAddrChange extends PeerAddressChangeNotification
  38     implements SctpNotification
  39 {
  40     /* static final ints so that they can be referenced from native */
  41     private final static int SCTP_ADDR_AVAILABLE = 1;
  42     private final static int SCTP_ADDR_UNREACHABLE = 2;
  43     private final static int SCTP_ADDR_REMOVED = 3;
  44     private final static int SCTP_ADDR_ADDED = 4;
  45     private final static int SCTP_ADDR_MADE_PRIM = 5;
  46     private final static int SCTP_ADDR_CONFIRMED =6;
  47 
  48     private Association association;
  49 
  50     /* assocId is used to lookup the association before the notification is
  51      * returned to user code */
  52     private int assocId;
  53     private SocketAddress address;
  54     private AddressChangeEvent event;
  55 
  56     /* Invoked from native */
  57     private PeerAddrChange(int assocId, SocketAddress address, int intEvent) {
  58         switch (intEvent) {
  59             case SCTP_ADDR_AVAILABLE :
  60                 this.event = AddressChangeEvent.ADDR_AVAILABLE;
  61                 break;
  62             case SCTP_ADDR_UNREACHABLE :
  63                 this.event = AddressChangeEvent.ADDR_UNREACHABLE;
  64                 break;
  65             case SCTP_ADDR_REMOVED :
  66                 this.event = AddressChangeEvent.ADDR_REMOVED;
  67                 break;
  68             case SCTP_ADDR_ADDED :
  69                 this.event = AddressChangeEvent.ADDR_ADDED;
  70                 break;
  71             case SCTP_ADDR_MADE_PRIM :
  72                 this.event = AddressChangeEvent.ADDR_MADE_PRIMARY;
  73                 break;
  74             case SCTP_ADDR_CONFIRMED :
  75                 this.event = AddressChangeEvent.ADDR_CONFIRMED;
  76                 break;
  77             default:
  78                 throw new AssertionError("Unknown event type");
  79         }
  80         this.assocId = assocId;
  81         this.address = address;
  82     }
  83 
  84     @Override
  85     public int assocId() {
  86         return assocId;
  87     }
  88 
  89     @Override
  90     public void setAssociation(Association association) {
  91         this.association = association;
  92     }
  93 
  94     @Override
  95     public SocketAddress address() {
  96         assert address != null;
  97         return address;
  98     }
  99 
 100     @Override
 101     public Association association() {
 102         assert association != null;
 103         return association;
 104     }
 105 
 106     @Override
 107     public AddressChangeEvent event() {
 108         assert event != null;
 109         return event;
 110     }
 111 
 112     @Override
 113     public String toString() {
 114         StringBuilder sb = new StringBuilder();
 115         sb.append(super.toString()).append(" [");
 116         sb.append("Address: ").append(address);
 117         sb.append(", Association:").append(association);
 118         sb.append(", Event: ").append(event).append("]");
 119         return sb.toString();
 120     }
 121 }
 122